Repository navigation
fix: trim whitespace from cookie name and value (RFC 6265 5.2) - #88
Merged
nfriedly merged 1 commit intoOct 8, 2026
Merged
Conversation
parseNameValuePair split on the first "=" but never removed leading or trailing WSP from the name and value, so "session=abc ; Path=/" gave the value "abc " and "session = abc" gave the name "session " and the value " abc". nfriedly#80 only trimmed attribute names and values. Trim SP and HTAB (the WSP set from the RFC) after splitting and before decodeURIComponent. Nameless cookies, "=" in values, quotes and internal spaces are unchanged.
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.
Summary
parseNameValuePair(lib/set-cookie.js, called fromparseStringat ~line 27) splits the name-value pair on the first=but never removes leading/trailing whitespace from the name or the value. RFC 6265 section 5.2 says to "remove any leading or trailing WSP characters from the name string and the value string". #80 fixed this for attribute names and values only, so the name-value pair was missed.Before / after (
parseSetCookie(input)):session=abc ; Path=/"abc ""abc"session=abc; Path=/" session""session"session = abc; Path=/"session ", value" abc""session", value"abc"a=b ;Path=/"b ""b"a=1 ; Path=/; Expires=Wed, 21 Oct 2026 07:28:00 GMT, b=2 ; Secure"1 ","2 ""1","2"I compared these inputs with tough-cookie 6.0.2, which gives the "after" results.
Fix
Trim the name and value in
parseNameValuePair, after splitting on the first=. Because it happens there, it runs beforedecodeURIComponent, andparseString,parseSetCookieand themapoption all pick it up.Only WSP (SP and HTAB) is trimmed, as defined in the RFC, using a small linear-time loop rather than
String.prototype.trim(). Other Unicode whitespace such as NBSP is left alone, so this does not change behavior beyond what the RFC specifies. (.trim()would also work for ordinary input; I went with the exact spec set.)Unchanged: nameless cookies per 6265bis (
abc ; Path=/gives name"", value"abc";=abcgives name"", value"abc"),a=b=c, internal spaces (a=b c), and quoted values (quotes are kept, only whitespace is trimmed).Test plan
name-value-pair whitespace (rfc 6265 5.2)block totest/set-cookie-parser.jscovering the cases above, tab handling, NBSP not trimmed, trimming before decoding (and withdecodeValues: false),parseString,map: true, and the combined-header case throughsplitCookiesString. It also has guard tests for internal spaces,a=b=c, quoted values and nameless cookies.lib/set-cookie.jsreverted (anddist/rebuilt), 13 of the new tests fail with the untrimmed values, e.g. name" session"and value"abc ". With the fix they all pass.npm test(build, eslint, mocha, tsd) passes: 64 passing.=and value matched on name and value. I did not add tough-cookie as a devDependency.CHANGELOG.md(entries are written per release).