Skip to content

Commit

Permalink
Only accept formats from registered mime types
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn authored and tenderlove committed Mar 10, 2019
1 parent 94b5cd3 commit d7fac9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions actionpack/lib/action_dispatch/http/mime_negotiation.rb
Expand Up @@ -74,6 +74,11 @@ def formats
else
[Mime[:html]]
end

v = v.select do |format|
format.symbol || format.ref == "*/*"
end

set_header k, v
end
end
Expand Down
10 changes: 6 additions & 4 deletions actionpack/test/controller/mime/respond_to_test.rb
Expand Up @@ -105,7 +105,7 @@ def made_for_content_type
def custom_type_handling
respond_to do |type|
type.html { render body: "HTML" }
type.custom("application/crazy-xml") { render body: "Crazy XML" }
type.custom("application/fancy-xml") { render body: "Fancy XML" }
type.all { render body: "Nothing" }
end
end
Expand Down Expand Up @@ -294,12 +294,14 @@ def setup
@request.host = "www.example.com"
Mime::Type.register_alias("text/html", :iphone)
Mime::Type.register("text/x-mobile", :mobile)
Mime::Type.register("application/fancy-xml", :fancy_xml)
end

def teardown
super
Mime::Type.unregister(:iphone)
Mime::Type.unregister(:mobile)
Mime::Type.unregister(:fancy_xml)
end

def test_html
Expand Down Expand Up @@ -455,10 +457,10 @@ def test_synonyms
end

def test_custom_types
@request.accept = "application/crazy-xml"
@request.accept = "application/fancy-xml"
get :custom_type_handling
assert_equal "application/crazy-xml", @response.content_type
assert_equal "Crazy XML", @response.body
assert_equal "application/fancy-xml", @response.content_type
assert_equal "Fancy XML", @response.body

@request.accept = "text/html"
get :custom_type_handling
Expand Down
14 changes: 12 additions & 2 deletions actionpack/test/controller/new_base/content_negotiation_test.rb
Expand Up @@ -20,9 +20,19 @@ class TestContentNegotiation < Rack::TestCase
assert_body "Hello world */*!"
end

test "Not all mimes are converted to symbol" do
test "A js or */* Accept header will return HTML" do
get "/content_negotiation/basic/hello", headers: { "HTTP_ACCEPT" => "text/javascript, */*" }
assert_body "Hello world text/html!"
end

test "A js or */* Accept header on xhr will return HTML" do
get "/content_negotiation/basic/hello", headers: { "HTTP_ACCEPT" => "text/javascript, */*" }, xhr: true
assert_body "Hello world text/javascript!"
end

test "Unregistered mimes are ignored" do
get "/content_negotiation/basic/all", headers: { "HTTP_ACCEPT" => "text/plain, mime/another" }
assert_body '[:text, "mime/another"]'
assert_body '[:text]'
end
end
end

0 comments on commit d7fac9c

Please sign in to comment.