Cookies: read, set, and more than one Set-Cookie #113

Closed
opened 2026-09-25 14:14:48 +00:00 by chrnx · 2 comments
Owner

Part of the API freeze (#110). Decided: in before rc.1.

Blocker found while scoping: response headers are Record<string, string> everywhere: TransformerFn (src/metadata.ts:6), ErrorResponse (src/transformers.ts:9), HttpError.headers (src/signals.ts:38) and the buffered outcome in src/http/core.ts:104. Two Set-Cookie headers can't be joined with a comma the way other headers can (RFC 6265 §3), so today there's no way to set two cookies. Widening the value to string | string[] changes public types, which is why it lands before the freeze.

Shape:

  • Read: @cookie('session') and @cookies() argument decorators, next to @header. The parse follows RFC 6265: first value wins, %-decoding, malformed pairs skipped.
  • Write: setCookie(name, value, options), returning the header string, plus the widened header type so a handler, transformer or HttpError can send several. Options: maxAge, expires, path, domain, secure, httpOnly, sameSite, partitioned.
  • Safe defaults: httpOnly: true, sameSite: 'lax', path: '/', and secure on when the request is secure (already derived for HSTS). Names and values are validated, and CR/LF is refused (header injection).
  • Signing is out: that's sessions, which stay out of core (README, "Bring your own auth").

Tests: two cookies on one response on all four runtimes (Node needs an array for setHeader, fetch needs headers.append), the parse edge cases, and injection refused.

Part of the API freeze (#110). Decided: in before rc.1. **Blocker found while scoping:** response headers are `Record<string, string>` everywhere: `TransformerFn` (src/metadata.ts:6), `ErrorResponse` (src/transformers.ts:9), `HttpError.headers` (src/signals.ts:38) and the buffered outcome in src/http/core.ts:104. Two `Set-Cookie` headers can't be joined with a comma the way other headers can (RFC 6265 §3), so today there's no way to set two cookies. Widening the value to `string | string[]` changes public types, which is why it lands before the freeze. **Shape:** - Read: `@cookie('session')` and `@cookies()` argument decorators, next to `@header`. The parse follows RFC 6265: first value wins, `%`-decoding, malformed pairs skipped. - Write: `setCookie(name, value, options)`, returning the header string, plus the widened header type so a handler, transformer or `HttpError` can send several. Options: `maxAge`, `expires`, `path`, `domain`, `secure`, `httpOnly`, `sameSite`, `partitioned`. - Safe defaults: `httpOnly: true`, `sameSite: 'lax'`, `path: '/'`, and `secure` on when the request is secure (already derived for HSTS). Names and values are validated, and CR/LF is refused (header injection). - Signing is **out**: that's sessions, which stay out of core (README, "Bring your own auth"). **Tests:** two cookies on one response on all four runtimes (Node needs an array for `setHeader`, fetch needs `headers.append`), the parse edge cases, and injection refused.
chrnx added the breaking changeenhancement labels 2026-09-25 16:06:54 +00:00
Author
Owner

Merged into develop in #140 (CARD-14, FPRD-001c, 2026-10-23). Left open until the 1c acceptance gate (CARD-22), per the 1a/1b convention.

Merged into develop in #140 (CARD-14, FPRD-001c, 2026-10-23). Left open until the 1c acceptance gate (CARD-22), per the 1a/1b convention.
Author
Owner

Fixed in 26.11.0-rc.1 (7f9a0ee). From the changelog:

  • Response header values are string | string[] (#113, D11). TransformerFn, ErrorResponse.headers, HttpError.headers / HttpErrorLike.headers and the pipeline's ResponseShape.headers widen from Record<string, string>, so a response can carry several Set-Cookie lines. Code that produces headers is unaffected; code that reads headers[k] from one of these types as a string stops compiling and must handle the array. A set-cookie array is sent as one header line per element on Node, Deno, Bun and the edge — before, the fetch adapters stringified it into one broken a,b cookie. Any other array is sent as one line joined with , . Injected security and CORS headers stay authoritative, and a handler's vary array is merged with the injected one.

  • @cookie('name') / @cookies() argument decorators and setCookie() (#113). Cookies are parsed from the request's Cookie header on first use per request (RFC 6265: first value wins, %-sequences decoded, malformed pairs skipped rather than answered with a 400), on HTTP and WebSocket routes; @needs('cookies') gives the same map. OpenAPI lists a cookie argument as an in: cookie parameter. setCookie(name, value, options?) returns a Set-Cookie value with HttpOnly, SameSite=Lax, Path=/ and Secure by default (D10; secure: false opts out), and throws, naming the field, on a name outside the token set, a value with CR, LF, ;, ,, whitespace or a control character, an attribute with CR/LF, ; or a control character, and on sameSite: 'none' or partitioned without secure. parseCookies() and CookieOptions are exported too. No dependency: signing and sessions stay out of core.

Fixed in **26.11.0-rc.1** (`7f9a0ee`). From the changelog: - **Response header values are `string | string[]`** (#113, D11). `TransformerFn`, `ErrorResponse.headers`, `HttpError.headers` / `HttpErrorLike.headers` and the pipeline's `ResponseShape.headers` widen from `Record<string, string>`, so a response can carry several `Set-Cookie` lines. Code that *produces* headers is unaffected; code that *reads* `headers[k]` from one of these types as a `string` stops compiling and must handle the array. A `set-cookie` array is sent as one header line per element on Node, Deno, Bun and the edge — before, the fetch adapters stringified it into one broken `a,b` cookie. Any other array is sent as one line joined with `, `. Injected security and CORS headers stay authoritative, and a handler's `vary` array is merged with the injected one. - **`@cookie('name')` / `@cookies()` argument decorators and `setCookie()`** (#113). Cookies are parsed from the request's `Cookie` header on first use per request (RFC 6265: first value wins, `%`-sequences decoded, malformed pairs skipped rather than answered with a 400), on HTTP and WebSocket routes; `@needs('cookies')` gives the same map. OpenAPI lists a cookie argument as an `in: cookie` parameter. `setCookie(name, value, options?)` returns a `Set-Cookie` value with `HttpOnly`, `SameSite=Lax`, `Path=/` and `Secure` by default (D10; `secure: false` opts out), and throws, naming the field, on a name outside the token set, a value with CR, LF, `;`, `,`, whitespace or a control character, an attribute with CR/LF, `;` or a control character, and on `sameSite: 'none'` or `partitioned` without `secure`. `parseCookies()` and `CookieOptions` are exported too. No dependency: signing and sessions stay out of core.
chrnx closed this issue 2026-11-18 01:04:05 +00:00
Sign in to join this conversation.