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

[7.1] MIME parsing raising errors on valid parameters #51594

Closed
glennsarti opened this issue Apr 18, 2024 · 1 comment
Closed

[7.1] MIME parsing raising errors on valid parameters #51594

glennsarti opened this issue Apr 18, 2024 · 1 comment
Milestone

Comments

@glennsarti
Copy link

Steps to reproduce

It's very easy to demonstrate in the test files:

in actionpack/test/dispatch/mime_type_test.rb add the following tests;

test "works1" do
    accept = "*/*;q=0.2"
    expect = ["*/*"]
    assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
  end

  test "works2" do
    accept = "foo/*;q=0.2 abc=123"
    expect = ["foo/*"]
    assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
  end

  test "works3" do
    accept = "*/*;q=1.0 abc=123"
    expect = ["*/*"]
    assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
  end

 # The test below should pass but it does not ------------

  test "doesnt_work2" do
    accept = "*/*;abc=123"
    expect = ["*/*;abc=123"]
    assert_equal expect.map(&:to_s), Mime::Type.parse(accept).map(&:to_s)
  end

Expected behavior

The test cases should all pass because */*;abc=123 is a valid accept header value

Relevant RFC section

The RFC doesn't make any distinction between */* and a full type / subtype, the parameter should be allowed.

Each media-range might be followed by zero or more applicable media
type parameters (e.g., charset), an optional "q" parameter for
indicating a relative weight (Section 5.3.1), and then zero or more
extension parameters.

Actual behavior

The test case fails.

System configuration

Rails version:
Rails 7.2.0.alpha

However I've noticed the behaviour from Rails 7.1.0 onwards. (Rails 7.0.8.1 does not have the same problem)

Ruby version:
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin22]

@rafaelfranca rafaelfranca added this to the 7.1.4 milestone Apr 18, 2024
@glennsarti
Copy link
Author

We currently have a monkey patch for this line;

PARAMETER_SEPARATOR_REGEXP = /;\s*q="?/

From

PARAMETER_SEPARATOR_REGEXP = /;\s*q="?/

To

PARAMETER_SEPARATOR_REGEXP = /(?>\s*#{Mime::Type::MIME_PARAMETER}\s*)*\z/

This may not be a preferred solution but it works for the moment.

notchairmk added a commit to notchairmk/rails that referenced this issue Apr 20, 2024
Fixes MIME parsing raising errors on valid parameters rails#51594.

Mime type lookups were updated to handle custom registered types as part of rails#48397.

This fix the strips out custom media range parameters before falling back to the default type creation.
notchairmk added a commit to notchairmk/rails that referenced this issue Apr 22, 2024
Fixes MIME parsing raising errors on valid parameters rails#51594.

Mime type lookups were updated to handle custom registered types as part of rails#48397.

This fix the strips out custom media range parameters before falling back to the default type creation.
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

No branches or pull requests

2 participants