Skip to content

Commit 833c240

Browse files
authored
fix(client): reject nameless cookies whose value looks like a __Secure- or __Host- cookie (#2507)
## Problem Following RFC 6265bis, `parseSetCookie` gives `Set-Cookie: =X` and a bare `Set-Cookie: X` an empty name and the value `X`. `CookieJar.setCookie` checked the `__Secure-`/`__Host-` prefix rules only on the name, which is empty here, and `mergeCookieHeader` sends a nameless cookie as its bare value. So a nameless cookie can pass for a prefixed one: ```http Set-Cookie: =__Host-session=evil; Path=/ # stored as { name: '', value: '__Host-session=evil' } Cookie: __Host-session=evil # what the next request sends ``` With one `CookieJar` shared between links, as the docs' Custom Jar section shows, a response from `http://api.example.com`, or from `https://cdn.example.com` with `Domain=example.com`, could plant a `__Host-`/`__Secure-` cookie that the `https://api.example.com` link then sent. The same cookie written with a name (`__Host-session=evil; Path=/`) was already rejected. Servers trust these prefixes for session and CSRF cookies, so this opens the door to session fixation and CSRF-token bypass. A related gap: the "explicit cookie header wins" check compared jar cookies by their stored name. A nameless cookie with value `session=jar` has the name `''`, so it went out next to an explicit `session=explicit` (`Cookie: session=explicit; session=jar`). ## Fix - `setCookie` ignores a cookie whose name is empty and whose value starts with `__Secure-` or `__Host-` (case-insensitive). This is the RFC 6265bis storage rule, which browsers also apply. - `mergeCookieHeader` matches jar cookies against the explicit header by the name in the pair that is sent, the way a server reads it, so a nameless `a=b` counts as `a`. ## Verification - **New tests:** 12 cases in `packages/client/src/plugins/cookie-jar.test.ts` cover the shared-jar attack from an HTTP origin and from a sibling subdomain, the prefix rule on nameless values (letter case, the bare form, non-prefixed controls), and the explicit-header de-duplication. The 9 attack cases fail without the fix; all 104 tests in the file pass with it. - **Repo checks:** `pnpm format` and `pnpm check` pass. - **Full `vitest run`:** the only failure is the openapi-inference tsconfig test, which already fails on `main`.
1 parent 89cd1e6 commit 833c240

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎packages/client/src/plugins/cookie-jar.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ describe('cookieJarLinkPlugin', () => {
122122
'session=explicit; lang=en; theme=dark',
123123
],
124124
[['a=1', ' ', 'b=2'], ['a=jar', 'c=jar'], 'a=1; b=2; c=jar'],
125+
['session=explicit', ['=session=jar', 'theme=dark'], 'session=explicit; theme=dark'],
125126
])('sends cookie header %j with jar cookies %j as %j', async (cookie, jarCookies, expected) => {
126127
const jar = new CookieJar()
127128
jarCookies.forEach((jarCookie) => jar.setCookie(`${jarCookie}; Path=/`, origin))
@@ -132,6 +133,26 @@ describe('cookieJarLinkPlugin', () => {
132133
expect(sentCookie(transport, 0)).toEqual(expected)
133134
})
134135

136+
it.each([
137+
['http://api.example.com', '=__Host-session=evil; Path=/'],
138+
['https://cdn.example.com', '=__Secure-session=evil; Secure; Domain=example.com; Path=/'],
139+
['http://api.example.com', '__Host-session=evil; Path=/'],
140+
])(
141+
'does not let %s pass a nameless cookie off as a prefixed one with %s',
142+
async (attackerOrigin, setCookie) => {
143+
const jar = new CookieJar()
144+
const attackerTransport = createTransport({ headers: { 'set-cookie': setCookie } })
145+
const transport = createTransport()
146+
147+
await createLink({ jar, origin: attackerOrigin }, attackerTransport).call(['ping'], 'input', {
148+
context: {},
149+
})
150+
await createLink({ jar }, transport).call(['ping'], 'input', { context: {} })
151+
152+
expect(sentCookie(transport, 0)).toBeUndefined()
153+
},
154+
)
155+
135156
it('picks the jar per request', async () => {
136157
const jars = { alice: new CookieJar(), bob: new CookieJar() }
137158
jars.alice.setCookie('session=alice; Path=/', origin)
@@ -463,6 +484,24 @@ describe('cookieJar', () => {
463484
expect(jar.getCookies()).toHaveLength(stored ? 1 : 0)
464485
})
465486

487+
it.each([
488+
['=__Host-session=evil; Path=/', false],
489+
['=__Host-session=evil; Secure; Path=/', false],
490+
['=__host-session=evil; Secure; Path=/', false],
491+
['= __Secure-session=evil; Secure', false],
492+
['=__SECURE-session=evil; Secure', false],
493+
['__Host-session; Secure; Path=/', false],
494+
['=__Hostsession=1; Path=/', true],
495+
['=session=1; Path=/', true],
496+
])(
497+
'applies the cookie prefix rules to the value of nameless %s: stored %s',
498+
(setCookie, stored) => {
499+
jar.setCookie(setCookie, site)
500+
501+
expect(jar.getCookies()).toHaveLength(stored ? 1 : 0)
502+
},
503+
)
504+
466505
describe('expiration', () => {
467506
it('expires a cookie after Max-Age seconds', () => {
468507
jar.setCookie('a=1; Max-Age=60', site)

‎packages/client/src/plugins/cookie-jar.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ export class CookieJar {
148148
!domainMatch(target.hostname, domain) ||
149149
(/^__(?:secure|host)-/i.test(parsed.name) && !parsed.secure) ||
150150
(/^__host-/i.test(parsed.name) && (!hostOnly || parsed.path !== '/')) ||
151+
// A nameless cookie is sent as its bare value, so it must not pass for a prefixed one, see RFC 6265bis
152+
(!parsed.name && /^__(?:secure|host)-/i.test(parsed.value)) ||
151153
// Insecure origins can neither set nor overwrite secure cookies, see "leave secure cookies alone" in RFC 6265bis
152154
(!secureUrl &&
153155
(parsed.secure ||
@@ -219,6 +221,7 @@ export class CookieJar {
219221

220222
/**
221223
* Appends jar cookies to an existing `cookie` header, skipping names it already has.
224+
* Names are compared as a server reads the header, so a nameless cookie whose value is `a=b` counts as `a`.
222225
* Returns `undefined` when the jar adds nothing.
223226
*/
224227
function mergeCookieHeader(
@@ -232,8 +235,8 @@ function mergeCookieHeader(
232235
const explicitNames = new Set(explicit.map((pair) => splitCookiePair(pair)[0]))
233236

234237
const pairs = cookies
235-
.filter((cookie) => !explicitNames.has(cookie.name))
236238
.map((cookie) => (cookie.name ? `${cookie.name}=${cookie.value}` : cookie.value))
239+
.filter((pair) => !explicitNames.has(splitCookiePair(pair)[0]))
237240

238241
return pairs.length ? [...explicit, ...pairs].join('; ') : undefined
239242
}

0 commit comments

Comments
 (0)