Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MIME::Type.parse handling of single media with a q value #4918

Merged
merged 2 commits into from Feb 7, 2012

Conversation

scottwb
Copy link
Contributor

@scottwb scottwb commented Feb 7, 2012

An HTTP request header like this:

Accept: text/html;q=0.9

Is valid according to the HTTP spec, but is not handled well by Mime::Type.parse. It is not uncommon to receive requests like this from bots and, according to one source, the PSP's browser. The failed parsing typically manifests itself as a MissingTemplate error.

This change fixes issue #736, which is not currently open, but should be.

The fix should be easy to also apply to 3.x branches. I'd be happy to help with that if necessary.

@masterkain
Copy link
Contributor

Related: #4127 #701

@@ -69,6 +69,12 @@ class MimeTypeTest < ActiveSupport::TestCase
assert_equal expect, Mime::Type.parse(accept)
end

test "parse single media range with q" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also add a test that handles multiple types with q=? Your changes also updated this part of the code, but you haven't added any tests for it. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim Just above that line (line 66) there is a pre-existing test that handles multiple types with q=:

  test "parse with q" do
    accept = "text/xml,application/xhtml+xml,text/yaml; q=0.3,application/xml,text/html; q=0.8,image/png,text/plain; q=0.5,application/pdf,*/*; q=0.2"
    expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PDF, Mime::TEXT, Mime::YAML, Mime::ALL]
    assert_equal expect, Mime::Type.parse(accept)
  end

This test passes before and after my change. Or am I misunderstanding your request?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was it, thanks! Merging.

@bittersweet
Copy link

Just wanted to say thanks for taking the time and doing the effort scottwb! I hope your work fixes it once and for all :)

josevalim pushed a commit that referenced this pull request Feb 7, 2012
Fix MIME::Type.parse handling of single media with a q value
@josevalim josevalim merged commit dd9b428 into rails:master Feb 7, 2012
@pheld
Copy link

pheld commented Feb 9, 2012

Here's a freedom patch that works for this for now:

  class << Mime::Type
    Q_SEPARATOR_REGEXP = /;\s*q=/

    def parse_with_q_fix(accept_header)
      if accept_header !~ /,/
        accept_header = accept_header.split(Q_SEPARATOR_REGEXP).first
      end

      parse_without_q_fix accept_header
    end

    alias_method_chain :parse, :q_fix
  end

@jeremywadsack
Copy link
Contributor

@scottwb Thanks for this fix and getting it merged. GoogleBot hits us on this every day now. @pheld Thanks for the monkey patch for the meantime.

@asanghi
Copy link
Contributor

asanghi commented Feb 17, 2012

This fix is in Rails master (4.0) or Rails stable (3.2.x)? If not, can this please be merged into 3.2.x?

@scottwb
Copy link
Contributor Author

scottwb commented Feb 17, 2012

As far as I know, it only got merged into edge (4.0). I was gonna work up some additional pull requests for the various 3.x branches one of these days. It shouldn't be too hard if you wanna take a stab at it. The changes are pretty small and should be straightforward to apply to Rails 3.

On Feb 16, 2012, at 23:47, Aditya Sanghi reply@reply.github.com wrote:

This fix is in Rails master (4.0) or Rails stable (3.2.x)? If not, can this please be merged into 3.2.x?


Reply to this email directly or view it on GitHub:
#4918 (comment)

@asanghi
Copy link
Contributor

asanghi commented Feb 17, 2012

Fine. In the mood for taking on some stabbing.

josevalim pushed a commit that referenced this pull request Feb 17, 2012
Backporting #4918 with one added test for googlebot accept header as I saw it
steveklabnik added a commit to steveklabnik/rails that referenced this pull request Mar 5, 2012
@bokor
Copy link

bokor commented Apr 24, 2012

what version was this merged into. I'm still on 3.0.9

@scottwb
Copy link
Contributor Author

scottwb commented Apr 24, 2012

@designwaves I am pretty sure it was merged into 3.2.x. Not sure what 'x' is, but when I discussed it with some of the maintainers they said they did not plan any functional updates to 3.0 and 3.1 at the time so it would only be relevant to apply it to 3.2 and later.

@bokor
Copy link

bokor commented Apr 24, 2012

Scott,

Is there a monkey patch that I can cut and paste into 3.0.9?

@steveklabnik
Copy link
Member

@scottwb @designwaves Above, you can see this was merged into 3.2 stable.

@scottwb
Copy link
Contributor Author

scottwb commented Apr 24, 2012

@designwaves I posted a while back about some monkey-patches I was using on 3.0.7, which should probably work for you on 3.0.9: https://gist.github.com/1754727, however I think I like the one pasted above by @pheld better.

@bokor
Copy link

bokor commented Apr 24, 2012

sweet thank you so much for helping out with this. We are getting enough traffic now that this is becoming an issue and wanted to patch it. Awesome work man!

@bokor
Copy link

bokor commented Apr 25, 2012

I used the Gist and it fixed all my issues. The monkey patch above only fixed one but your code fixed all. thank you for that. It really helped out.

@mladeny
Copy link

mladeny commented Jun 13, 2014

Has this been fixed for 3.2?

I saw that it was merged into master, but I'm using 3.2.13 and I'm having the same issue.

ActionView::MissingTemplate: Missing template homes/show, application/show with {:locale=>[:en], :formats=>["text/html; chars...lder, :slim, :coffee]}. Searched in: * "/app/app/views" * "/app/vendor/bundle/ruby/1.9.1/gems/kaminari-0.14.1/app/views"
HTTP_ACCEPT
text/html; charset=utf-8; q=0.1

seems to be happening with bots (as mentioned above). Here's the user agent that triggered it

HTTP_USER_AGENT
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36 HubSpot Marketing Grader

replicated with

curl -H"Accept: text/html; charset=utf-8; q=0.1" http://localhost:3000/

@rafaelfranca
Copy link
Member

It seems to be included in 3-2-stable so maybe it is in one 3.2.x release.

@mladeny
Copy link

mladeny commented Jun 13, 2014

@rafaelfranca doesn't seem so.

I just tried 3.2.18, which is the latest version as shown in the RAILS_VERSION file under the 3.2-stable branch and I still get the same error...

lukaszx0 added a commit to square/rails that referenced this pull request Jun 13, 2014
@rafaelfranca
Copy link
Member

Maybe it was reverted because caused some regression.

tamird pushed a commit to square/rails that referenced this pull request Jun 22, 2014
tamird pushed a commit to square/rails that referenced this pull request Jun 22, 2014
Based on rails#4918.

Related to rails#4127.

Conflicts:
	actionpack/test/dispatch/mime_type_test.rb
tamird pushed a commit to square/rails that referenced this pull request Jun 22, 2014
Based on rails#4918.

Related to rails#4127.

Conflicts:
	actionpack/test/dispatch/mime_type_test.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet