-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#162105] Handle mime type errors (#4819)
* Add middleware to fix mime type errors * Refactor middleware
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class SanitizeHeadersMiddleware | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
request = ActionDispatch::Request.new(env) | ||
|
||
fallback_to_html_format_if_invalid_mime_type(request) | ||
|
||
@app.call(env) | ||
end | ||
|
||
private | ||
def fallback_to_html_format_if_invalid_mime_type(request) | ||
request.formats | ||
rescue ActionDispatch::Http::MimeNegotiation::InvalidType | ||
request.set_header "CONTENT_TYPE", "text/html" | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "rack/mock" | ||
require "rails_helper" | ||
|
||
RSpec.describe SanitizeHeadersMiddleware do | ||
let(:app) { ->(env) { [200, env, "OK"] } } | ||
let(:middleware) { described_class.new(app) } | ||
|
||
context "when HTTP_ACCEPT header is valid" do | ||
it "does not alter the headers" do | ||
env = Rack::MockRequest.env_for("/", "HTTP_ACCEPT" => "text/html,application/json") | ||
status, headers, _body = middleware.call(env) | ||
|
||
expect(env["HTTP_ACCEPT"]).to eq("text/html,application/json") | ||
expect(status).to eq(200) | ||
end | ||
end | ||
|
||
context "when HTTP_ACCEPT header is invalid" do | ||
it "sets CONTENT_TYPE to text/html" do | ||
env = Rack::MockRequest.env_for("/", "HTTP_ACCEPT" => "../../../../../etc/passwd{{") | ||
status, headers, _body = middleware.call(env) | ||
|
||
expect(env["CONTENT_TYPE"]).to eq("text/html") | ||
expect(status).to eq(200) | ||
end | ||
end | ||
|
||
context "when HTTP_ACCEPT header is missing" do | ||
it "does not alter the headers" do | ||
env = Rack::MockRequest.env_for("/") | ||
status, headers, _body = middleware.call(env) | ||
|
||
expect(env["CONTENT_TYPE"]).to be_nil | ||
expect(status).to eq(200) | ||
end | ||
end | ||
end |