Repository navigation
Fix NoMethodError when Accept-Language has an empty comma-separated entry - #200
Open
hikmetba-bit wants to merge 1 commit into
Open
hikmetba-bit wants to merge 1 commit into
hikmetba-bit wants to merge 1 commit into
Conversation
…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>
Member
|
Should we accept this or reject it as a bad request? |
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A header like
",en;q=0.8"(leading comma) or"ja,,ko;q=0.8"(double comma) splits into an entry whoselanguage_tagis the empty string."".split(/;q=/i)returns[], so the destructuring assignment giveslocale = nil, and thatnilsurvives the existingrejectfilter (which only excludes'*'andq=0) into the parsed locales list.When no locale in that list exactly matches an available locale,
user_preferred_localefalls through to the variant-match fallback, which callslocale[0,2]on each candidate in turn — including thenilentry — raisingNoMethodError: undefined method '[]' for nilinstead of falling back to the default locale.Fix
Reject empty locale strings (and the
nilthey produce) up front, alongside the existing'*'andq=0filters — this is exactly the fix @mpalmer said in the issue thread they were inclined to go with.Testing
test/spec_rack_locale.rb.NoMethodError: undefined method '[]' for nilatlocale.rb:91invariant_match?), then restored the fix and the fullspec_rack_locale.rbsuite passed clean (20 runs, 20 assertions, 0 failures).bundle exec rake test): unrelated pre-existing failures only (Ruby-version Hash#inspect formatting differences inspec_rack_json_body_parser_spec.rb, and aGarbageCollectorspec issue), same before and after this change.Fixes #199
🤖 Generated with Claude Code