Repository navigation
Commit 833c240
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
125 | 126 | | |
126 | 127 | | |
127 | 128 | | |
| |||
132 | 133 | | |
133 | 134 | | |
134 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
135 | 156 | | |
136 | 157 | | |
137 | 158 | | |
| |||
463 | 484 | | |
464 | 485 | | |
465 | 486 | | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
466 | 505 | | |
467 | 506 | | |
468 | 507 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| 151 | + | |
| 152 | + | |
151 | 153 | | |
152 | 154 | | |
153 | 155 | | |
| |||
219 | 221 | | |
220 | 222 | | |
221 | 223 | | |
| 224 | + | |
222 | 225 | | |
223 | 226 | | |
224 | 227 | | |
| |||
232 | 235 | | |
233 | 236 | | |
234 | 237 | | |
235 | | - | |
236 | 238 | | |
| 239 | + | |
237 | 240 | | |
238 | 241 | | |
239 | 242 | | |
| |||
0 commit comments