Skip to content

Trim optional whitespace in the Accept-Language q-parameter - #106

Open
gaoflow wants to merge 1 commit into
jshttp:masterfrom
gaoflow:fix-language-qvalue-ows
Open

Trim optional whitespace in the Accept-Language q-parameter#106
gaoflow wants to merge 1 commit into
jshttp:masterfrom
gaoflow:fix-language-qvalue-ows

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 29, 2026

Copy link
Copy Markdown

parseLanguage() splits the q parameter without trimming it, unlike the charset, encoding and mediaType parsers. RFC 7231 §5.3.1 allows optional whitespace after the ;, so en; q=0.1 leaves the key as " q" and the q value is silently dropped (defaulting to 1).

As a result en;q=0.9, fr; q=0.1 sorts as ['fr', 'en'], the reverse of the byte-identical en;q=0.9, fr;q=0.1. Worse, fr; q=0 returns ['fr'] instead of [] — an explicit q=0 rejection bypassed by a single space. charset/encoding return [] for that same input.

The fix is the .trim() the three siblings already apply. It was left out of language.js in 0484c74, the 2019 commit that fixed this same loop in all three parsers. Added a whitespace matrix to the language tests; full suite passes (255).

parseLanguage() split the q-parameter without trimming, so a space
after the ';' (legal OWS per RFC 7231 sec 5.3.1) left the key as ' q'
and the q-value was silently dropped and defaulted to 1. charset,
encoding and mediaType already trim the parameter; language did not.

'fr; q=0' now yields [] instead of ['fr'], matching 'fr;q=0'.
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.

1 participant