feat(http)!: cookie decorators, setCookie, and string | string[] response headers (CARD-14, #113) #140

Merged
chrnx merged 1 commits from feature/fprd-001c-cookies into develop 2026-10-23 06:09:52 +00:00
Owner

Implements CARD-14 (core #113) under decisions D10 (secure: true by default) and D11 (string | string[] for every response header). First card of the HTTP lane: CARD-15 and CARD-16 stack on this branch.

What changed and why

  • Read: @cookie('name') / @cookies() are a new 'cookies' arg source next to @header/@headers, with the same forms: none, key, key list, schema. The Cookie header is parsed on first use per request and cached on the context, so a route with no cookie argument never parses it. Parsing follows RFC 6265: the first value wins, %-sequences are decoded, malformed pairs are skipped (never a 400), and an undecodable value is kept raw. Several cookie lines are joined with ; . The map has no prototype, so __proto__=x is just a key. Works on HTTP and WS routes. 'cookies' joins the seed-key allow-list, and @needs('cookies') resolves the same map.
  • Write: setCookie(name, value, options?) (src/cookies.ts, no dependency) defaults to Path=/; Secure; HttpOnly; SameSite=Lax. It throws, naming the field, on a non-token name; on a value with CR/LF, ;, ,, whitespace or a control character; on an attribute with CR/LF; and on sameSite: 'none' or partitioned without secure.
  • Many cookies: TransformerFn, ErrorResponse.headers, HttpError/HttpErrorLike.headers, ResponseShape.headers and NeutralOutcome widen to Record<string, string | string[]>.
    • Node: joinListHeaders runs inside the existing writeHead patch, so every Node write path is covered. It keeps set-cookie arrays (Node writes one line per element) and joins every other array with , .
    • Fetch adapters (Deno, Bun, edge, app.fetch): toWebHeaders builds a Headers and appends each set-cookie element. The buffered/stream, shed-503 and preflight paths all use it.
    • mergeInjectedHeaders: injected headers still win. A handler vary array is joined before being merged with the injected value.
  • OpenAPI: a cookie argument becomes an in: 'cookie' parameter.
  • Plugins checked: metrics/src/prometheus.ts and rate-limit/src/errors.ts only produce headers, so they are unaffected.

Things I was unsure about / deviations

  • Stricter attribute check than the card. path/domain also refuse ; and other control characters, not only CR/LF: a ; in Path would add an attribute without breaking the header. I chose the conservative option; flag it if you want CR/LF only.
  • Card note not followed: cookies are parsed lazily instead of seeded. The card says to seed cookies into the context. In this codebase steps can only need req/params (topoSort seed keys), so seeding would only serve handlers. Lazy parsing in resolveArgs gives "parsed once, only when declared" without a per-request flag.
  • Quoted values are not unquoted. a="x" reads as "x"; RFC 6265 §5.2 doesn't unquote.
  • setCookie writes the value as given and does not encode it (T4 expects s=v). JSDoc tells callers to encodeURIComponent the value themselves.

Test evidence

New: test/cookies.test.ts (T1–T9, T11, WS route, OpenAPI, injected-header authority; raw socket rawHeaders on Node plus app.fetch), test/http-headers.test.ts (array merge, joinListHeaders, toWebHeaders), test/mesh/integration.test.ts (T10: teapot route with two cookies reaches the teacup client as two lines), test/deno/cookies.test.ts, test/bun/cookies.test.ts (real Deno.serve/Bun.serve), test/edge/run.ts + worker.ts (workerd).

== npm run lint
exit 0
✖ 50 problems (0 errors, 50 warnings)
== npm run format:check
exit 0
All matched files use Prettier code style!
== npm run typecheck
exit 0
== npm run test
exit 0
 Test Files  93 passed (93)
      Tests  941 passed (941)
== npm run build
exit 0
CJS ⚡️ Build success in 27ms
ESM ⚡️ Build success in 28ms
DTS ⚡️ Build success in 1145ms
== npm run complexity:check
exit 0
✓ cognitive complexity: every function ≤ 25
== npm run test:deno
exit 0
T3 serveDeno: an IPv6 peer is passed through as Deno reports it ... ok (822µs)
ok | 37 passed | 0 failed (4s)
== npm run test:bun
exit 0
 27 pass
 0 fail
== npm run test:edge
exit 0
edge (workerd via miniflare): HTTP 200 + cookies + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS
edge (workerd): router parity — 1158 matcher cases + 55 app.fetch cases — PASS

Lint warnings (50) are the pre-existing no-explicit-any baseline; this branch adds none. Frozen router-parity captures are untouched. npm audit --audit-level=moderate: 0 vulnerabilities after rebasing on develop (#134 merged).

Docs follow-up (CARD-12)

  • guides/arguments.md: @cookie/@cookies, parsing rules, @needs('cookies'); setCookie with options and defaults; several cookies via a set-cookie array; the secure: !isDev recipe for Safari, LAN IPs and plain-HTTP dev hosts (D10).
  • reference/decorators.md: the two decorators.
  • Migration note for the string | string[] header widening (D11), and how other arrays are joined.
Implements **CARD-14** (core #113) under decisions **D10** (`secure: true` by default) and **D11** (`string | string[]` for every response header). First card of the HTTP lane: CARD-15 and CARD-16 stack on this branch. ## What changed and why - **Read:** `@cookie('name')` / `@cookies()` are a new `'cookies'` arg source next to `@header`/`@headers`, with the same forms: none, key, key list, schema. The `Cookie` header is parsed on first use per request and cached on the context, so a route with no cookie argument never parses it. Parsing follows RFC 6265: the first value wins, `%`-sequences are decoded, malformed pairs are skipped (never a 400), and an undecodable value is kept raw. Several `cookie` lines are joined with `; `. The map has no prototype, so `__proto__=x` is just a key. Works on HTTP and WS routes. `'cookies'` joins the seed-key allow-list, and `@needs('cookies')` resolves the same map. - **Write:** `setCookie(name, value, options?)` (`src/cookies.ts`, no dependency) defaults to `Path=/; Secure; HttpOnly; SameSite=Lax`. It throws, naming the field, on a non-token name; on a value with CR/LF, `;`, `,`, whitespace or a control character; on an attribute with CR/LF; and on `sameSite: 'none'` or `partitioned` without `secure`. - **Many cookies:** `TransformerFn`, `ErrorResponse.headers`, `HttpError`/`HttpErrorLike.headers`, `ResponseShape.headers` and `NeutralOutcome` widen to `Record<string, string | string[]>`. - Node: `joinListHeaders` runs inside the existing `writeHead` patch, so every Node write path is covered. It keeps `set-cookie` arrays (Node writes one line per element) and joins every other array with `, `. - Fetch adapters (Deno, Bun, edge, `app.fetch`): `toWebHeaders` builds a `Headers` and appends each `set-cookie` element. The buffered/stream, shed-503 and preflight paths all use it. - `mergeInjectedHeaders`: injected headers still win. A handler `vary` array is joined before being merged with the injected value. - **OpenAPI:** a cookie argument becomes an `in: 'cookie'` parameter. - Plugins checked: `metrics/src/prometheus.ts` and `rate-limit/src/errors.ts` only *produce* headers, so they are unaffected. ## Things I was unsure about / deviations - **Stricter attribute check than the card.** `path`/`domain` also refuse `;` and other control characters, not only CR/LF: a `;` in `Path` would add an attribute without breaking the header. I chose the conservative option; flag it if you want CR/LF only. - **Card note not followed: cookies are parsed lazily instead of seeded.** The card says to seed `cookies` into the context. In this codebase steps can only `need` `req`/`params` (`topoSort` seed keys), so seeding would only serve handlers. Lazy parsing in `resolveArgs` gives "parsed once, only when declared" without a per-request flag. - **Quoted values are not unquoted.** `a="x"` reads as `"x"`; RFC 6265 §5.2 doesn't unquote. - `setCookie` writes the value as given and does not encode it (T4 expects `s=v`). JSDoc tells callers to `encodeURIComponent` the value themselves. ## Test evidence New: `test/cookies.test.ts` (T1–T9, T11, WS route, OpenAPI, injected-header authority; raw socket `rawHeaders` on Node plus `app.fetch`), `test/http-headers.test.ts` (array merge, `joinListHeaders`, `toWebHeaders`), `test/mesh/integration.test.ts` (T10: teapot route with two cookies reaches the teacup client as two lines), `test/deno/cookies.test.ts`, `test/bun/cookies.test.ts` (real `Deno.serve`/`Bun.serve`), `test/edge/run.ts` + `worker.ts` (workerd). ``` == npm run lint exit 0 ✖ 50 problems (0 errors, 50 warnings) == npm run format:check exit 0 All matched files use Prettier code style! == npm run typecheck exit 0 == npm run test exit 0 Test Files 93 passed (93) Tests 941 passed (941) == npm run build exit 0 CJS ⚡️ Build success in 27ms ESM ⚡️ Build success in 28ms DTS ⚡️ Build success in 1145ms == npm run complexity:check exit 0 ✓ cognitive complexity: every function ≤ 25 == npm run test:deno exit 0 T3 serveDeno: an IPv6 peer is passed through as Deno reports it ... ok (822µs) ok | 37 passed | 0 failed (4s) == npm run test:bun exit 0 27 pass 0 fail == npm run test:edge exit 0 edge (workerd via miniflare): HTTP 200 + cookies + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS edge (workerd): router parity — 1158 matcher cases + 55 app.fetch cases — PASS ``` Lint warnings (50) are the pre-existing `no-explicit-any` baseline; this branch adds none. Frozen router-parity captures are untouched. `npm audit --audit-level=moderate`: 0 vulnerabilities after rebasing on develop (#134 merged). ## Docs follow-up (CARD-12) - `guides/arguments.md`: `@cookie`/`@cookies`, parsing rules, `@needs('cookies')`; `setCookie` with options and defaults; several cookies via a `set-cookie` array; the `secure: !isDev` recipe for Safari, LAN IPs and plain-HTTP dev hosts (D10). - `reference/decorators.md`: the two decorators. - Migration note for the `string | string[]` header widening (D11), and how other arrays are joined.
chrnx force-pushed feature/fprd-001c-cookies from 82767eabd2 to 47743fdf1f 2026-10-23 05:03:08 +00:00 Compare
Author
Owner

Owner decisions (2026-10-23): lazy cookie parsing and the stricter path/domain validation are accepted. Unquoting and value encoding are out of 1c. Not merging yet: implementation review first.

Owner decisions (2026-10-23): lazy cookie parsing and the stricter `path`/`domain` validation are accepted. Unquoting and value encoding are out of 1c. Not merging yet: implementation review first.
Author
Owner

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.
chrnx force-pushed feature/fprd-001c-cookies from 47743fdf1f to 88945b0b1f 2026-10-23 06:04:15 +00:00 Compare
chrnx added 1 commit 2026-10-23 06:08:27 +00:00
feat(http)!: cookie decorators, setCookie, and string | string[] response headers (CARD-14, #113)
CI / runtimes (edge) (pull_request) Successful in 14s
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 27s
CI / runtimes (deno) (pull_request) Successful in 20s
7f9a0eef0f
Reading a cookie meant parsing the Cookie header by hand, and a response
could not carry more than one Set-Cookie: response header values were
Record<string, string>, and the fetch adapters would have stringified an
array into one broken "a,b" cookie anyway.

- @cookie('name') / @cookies() sit next to @header/@headers as a new
  'cookies' arg source. The header is parsed on first use per request and
  cached on the context, so a route without a cookie argument never parses
  it. RFC 6265: first value wins, %-sequences decoded, malformed pairs
  skipped (never a 400), undecodable values kept raw. Several cookie lines
  are joined with "; ". The map has no prototype. @needs('cookies') reads
  the same map, and 'cookies' joins the seed-key allow-list.
- setCookie(name, value, options) serialises one Set-Cookie value with
  HttpOnly, SameSite=Lax, Path=/ and Secure by default (D10), and throws,
  naming the field, on anything that could inject a header or that browsers
  reject (sameSite none / partitioned without secure).
- Response header values widen to string | string[] (D11) across
  TransformerFn, ErrorResponse, HttpError(Like).headers, ResponseShape and
  the neutral outcome. Node writes a set-cookie array as one line per
  element and joins every other array with ", " before writeHead; the
  fetch adapters build a Headers and append each set-cookie element.
  mergeInjectedHeaders keeps arrays by reference and joins a vary array
  before merging it.
- OpenAPI maps a cookie argument to an in: cookie parameter.

BREAKING CHANGE: code that reads a header value from TransformerFn,
ErrorResponse, HttpError or ResponseShape as a string must handle string[].

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
chrnx force-pushed feature/fprd-001c-cookies from 88945b0b1f to 7f9a0eef0f 2026-10-23 06:08:27 +00:00 Compare
chrnx merged commit 50010da88d into develop 2026-10-23 06:09:52 +00:00
chrnx deleted branch feature/fprd-001c-cookies 2026-10-23 06:09:53 +00:00
Sign in to join this conversation.