fix(cookie): relax name validation when parsing Cookie header (#5164)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 26d8e42 · Browse files · 2 files·+34 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+34 added−1 removed
- tests:+26 added−0 removed
- other:+8 added−1 removed
Provenance
- Commit
09cf01c- Tree
2213750- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
26d8e42
2 files changed+34 added−1 removed
modified:src/utils/cookie.test.ts+26 added
78 unmodified lines79 expect(cookie['best_cookie\\']).toBeUndefined()80 expect(cookie['']).toBeUndefined()81 })82 83+ it('Should parse cookies whose names are not RFC 6265 tokens', () => {84+ const cookieString =85+ 'paraglide:lang=en; CognitoIdentityServiceProvider.abc.user@example.com.idToken=token; round(brackets)=ok; square[brackets]=ok; curly{brackets}=ok'86+ const cookie: Cookie = parse(cookieString)87+ expect(cookie['paraglide:lang']).toBe('en')88+ expect(cookie['CognitoIdentityServiceProvider.abc.user@example.com.idToken']).toBe('token')89+ expect(cookie['round(brackets)']).toBe('ok')90+ expect(cookie['square[brackets]']).toBe('ok')91+ expect(cookie['curly{brackets}']).toBe('ok')92+ })93+94+ it('Should parse one cookie specified by a name that is not an RFC 6265 token', () => {95+ const cookieString = 'paraglide:lang=en; test=ok'96+ const cookie: Cookie = parse(cookieString, 'paraglide:lang')97+ expect(cookie['paraglide:lang']).toBe('en')98+ expect(cookie['test']).toBeUndefined()99+ })100+101+ it('Should ignore cookie names containing non-ASCII or control characters', () => {102+ const cookieString = 'yummy_cookie=choco; クッキー=strawberry; bad\x01name=sugar'103+ const cookie: Cookie = parse(cookieString)104+ expect(cookie['yummy_cookie']).toBe('choco')105+ expect(cookie['クッキー']).toBeUndefined()106+ expect(cookie['bad\x01name']).toBeUndefined()107+ })108+109 it('Should ignore invalid cookie values', () => {110 const cookieString = 'yummy_cookie=choco\\nchip; tasty_cookie=strawberry; best_cookie="sugar'111 const cookie: Cookie = parse(cookieString)112 expect(cookie['yummy_cookie']).toBeUndefined()341 unmodified linesmodified:src/utils/cookie.ts+8 added-1 removed
65 unmodified lines66 }67 68 // all alphanumeric chars and all of _!#$%&'*.^`|~+-69 // (see: https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1)70+// used only when producing cookies via serialize(); parsing uses the relaxed regex below71 const validCookieNameRegEx = /^[\w!#$%&'*.^`|~+-]+$/72 73+// all ASCII chars 33-126 except 34, 59, 61, and 92 (i.e. "!" to "~" but not double quote, semicolon, equals, or backslash)74+// the strict RFC 6265 token rule above is a requirement for cookie producers; when consuming75+// the Cookie header we accept the names other producers emit in the wild, e.g. "paraglide:lang"76+// (see: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.6 and https://github.com/honojs/hono/issues/3189)77+const relaxedCookieNameRegEx = /^[!#-:<>-[\]-~]+$/78+79 // all ASCII chars 32-126 except 34, 59, and 92 (i.e. space to tilde but not double quote, semicolon, or backslash)80 // (see: https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1)81 //82 // note: the spec also prohibits comma and space, but we allow both since they are very common in the real world38 unmodified lines121 122 const cookieName = trimCookieWhitespace(pairStr.substring(0, valueStartPos))123 if (124 (name && name !== cookieName) ||118- !validCookieNameRegEx.test(cookieName) ||125+ !relaxedCookieNameRegEx.test(cookieName) ||126 cookieName in parsedCookie127 ) {128 continue129 }152 unmodified lines