Skip to content

Fix NoMethodError when Accept-Language has an empty comma-separated entry - #200

Open
hikmetba-bit wants to merge 1 commit into
rack:mainfrom
hikmetba-bit:fix/locale-empty-accept-language-entries
Open

hikmetba-bit wants to merge 1 commit into
rack:mainfrom
hikmetba-bit:fix/locale-empty-accept-language-entries

Conversation

@hikmetba-bit

Copy link
Copy Markdown

A header like ",en;q=0.8" (leading comma) or "ja,,ko;q=0.8" (double comma) splits into an entry whose language_tag is the empty string. "".split(/;q=/i) returns [], so the destructuring assignment gives locale = nil, and that nil survives the existing reject filter (which only excludes '*' and q=0) into the parsed locales list.

When no locale in that list exactly matches an available locale, user_preferred_locale falls through to the variant-match fallback, which calls locale[0,2] on each candidate in turn — including the nil entry — raising NoMethodError: undefined method '[]' for nil instead of falling back to the default locale.

Fix

Reject empty locale strings (and the nil they produce) up front, alongside the existing '*' and q=0 filters — this is exactly the fix @mpalmer said in the issue thread they were inclined to go with.

Testing

  • Added two regression tests (leading comma, double comma) to test/spec_rack_locale.rb.
  • Mutation-tested: reverted just the one-line source fix and reran — both new tests failed with the exact reported error (NoMethodError: undefined method '[]' for nil at locale.rb:91 in variant_match?), then restored the fix and the full spec_rack_locale.rb suite passed clean (20 runs, 20 assertions, 0 failures).
  • Full test suite (bundle exec rake test): unrelated pre-existing failures only (Ruby-version Hash#inspect formatting differences in spec_rack_json_body_parser_spec.rb, and a GarbageCollector spec issue), same before and after this change.

Fixes #199

🤖 Generated with Claude Code

…ntry

A header like ",en;q=0.8" (leading comma) or "ja,,ko;q=0.8" (double
comma) splits into an entry whose language_tag is the empty string.
""..split(/;q=/i) returns [], so destructuring assigns locale = nil,
and that nil survives the existing reject filter (which only excludes
'*' and q=0) into the parsed locales list.

When no locale in that list exactly matches an available locale,
user_preferred_locale falls through to the variant-match fallback,
which calls locale[0,2] on each candidate in turn -- including the nil
entry, raising NoMethodError: undefined method '[]' for nil instead of
falling back to the default locale.

Reject empty locale strings (and the nil they produce) up front,
alongside the existing '*' and q=0 filters.

Fixes rack#199

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@ioquatix

Copy link
Copy Markdown
Member

Should we accept this or reject it as a bad request?

@mpalmer

mpalmer commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Yeah, I'm still not able to convince myself whether it's better to reject than accept. I can't think of a way to do any damage with this kind of bad request, but my comfort level with allowing it is very low. That's why I haven't pulled the trigger on fixing it one way or the other, I just can't make up my mind.

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.

Bug in Rack::Locale handling of Accept Language empty values

3 participants