Merge pull request #51247 from Earlopain/fix-51228

Fix crash for invalid Content-Type in ShowExceptions middleware
This commit is contained in:
Rafael Mendonça França 2024-03-05 10:18:13 -05:00 committed by GitHub
commit 08b49f18eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

@ -67,9 +67,17 @@ def render_exception(request, wrapper)
def fallback_to_html_format_if_invalid_mime_type(request)
# If the MIME type for the request is invalid then the @exceptions_app may not
# be able to handle it. To make it easier to handle, we switch to HTML.
request.formats
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
request.set_header "HTTP_ACCEPT", "text/html"
begin
request.content_mime_type
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
request.set_header "CONTENT_TYPE", "text/html"
end
begin
request.formats
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
request.set_header "HTTP_ACCEPT", "text/html"
end
end
def pass_response(status)

@ -82,6 +82,7 @@ def not_acceptable
app_file "config/routes.rb", <<-RUBY
Rails.application.routes.draw do
get "/foo", to: "foo#index"
post "/foo", to: "foo#index"
match "/406", to: "foo#not_acceptable", via: :all
end
RUBY
@ -93,6 +94,18 @@ def not_acceptable
get "/foo", {}, { "HTTP_ACCEPT" => "invalid", "HTTPS" => "on" }
assert_equal 406, last_response.status
assert_not_equal "rendering index!", last_response.body
get "/foo", {}, { "CONTENT_TYPE" => "invalid", "HTTPS" => "on" }
assert_equal 406, last_response.status
assert_not_equal "rendering index!", last_response.body
get "/foo", {}, { "HTTP_ACCEPT" => "invalid", "CONTENT_TYPE" => "invalid", "HTTPS" => "on" }
assert_equal 406, last_response.status
assert_not_equal "rendering index!", last_response.body
post "/foo", {}, { "HTTP_ACCEPT" => "invalid", "CONTENT_TYPE" => "invalid", "HTTPS" => "on" }
assert_equal 406, last_response.status
assert_not_equal "rendering index!", last_response.body
end
test "uses custom exceptions app" do