Public: anyone can read this; the crew can change it.
Every cookie the forge sets is HttpOnly and SameSite, and Secure over HTTPS, proven by one test #13150
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13124-every-cookie-the-forge-sets-is-httponly → main 5 commits fork 1 thread · 1 unresolved
2 files
changed +55
−10
src/auth/cookies.ts +1 −2
| @@ -7,8 +7,7 @@ | ||
| 7 | 7 | * walks src/ for any line that spells a cookie anywhere else. |
| 8 | 8 | * |
| 9 | 9 | * Readable by script: none. The theme is painted by the server from its |
| 10 | * cookie on the first byte, so no page script reads any cookie; were one ever | |
| 11 | * to need it, it is named here, with its reason, and nowhere else. | |
| 10 | * cookie on the first byte, so no page script reads any cookie. | |
| 12 | 11 | * |
| 13 | 12 | * Lax, not Strict: a person sent back from a parent forge's sign-in or an |
| 14 | 13 | * app's sign-in handoff (#12177) arrives by a top-level cross-site |
test/cookie-flags.test.ts +54 −8
| @@ -38,10 +38,10 @@ setDefaultTimeout(60_000); | ||
| 38 | 38 | /** |
| 39 | 39 | * Every cookie set in src/ through src/auth/cookies.ts (`setCookie(name, …)` |
| 40 | 40 | * or `expireCookie(name, …)`), keyed by its file and its name as the call |
| 41 | * spells it, with how many calls set it there. `name` is what a browser sees (a RegExp when the name carries | |
| 42 | * the thing it is for); `door` is the walk above that drives it, or `why` says | |
| 43 | * why no walk does. `path` is set only where a cookie is narrower than `/`, | |
| 44 | * with its reason: a narrower path is stricter, never weaker. | |
| 41 | * spells it, with how many calls set it there. `name` is what a browser sees (a RegExp when the | |
| 42 | * name carries the thing it is for); `door` is the walk above that drives it, or `why` says why no | |
| 43 | * walk does. `path` is set only where a cookie is narrower than `/`, with its reason: a narrower | |
| 44 | * path is stricter, never weaker. | |
| 45 | 45 | */ |
| 46 | 46 | interface Spelled { |
| 47 | 47 | name: string | RegExp; |
| @@ -141,10 +141,24 @@ const tmp = async () => { | ||
| 141 | 141 | return dir; |
| 142 | 142 | }; |
| 143 | 143 | |
| 144 | /** Each door's label and the cookie names it set, over http and https: what the coverage test reads. */ | |
| 144 | /** | |
| 145 | * Each door's label and the cookie names it set, over http and https: what the coverage test reads. | |
| 146 | */ | |
| 145 | 147 | const driven = new Map<string, Set<string>>(); |
| 146 | 148 | |
| 147 | function check(label: string, cookies: string[], https: boolean) { | |
| 149 | /** | |
| 150 | * The one origin a cookie may go without Secure (#13124): the test server's own | |
| 151 | * plain-http loopback address, where no browser could keep a Secure cookie. | |
| 152 | * Every other origin, https or a plain-http host off this machine, demands it. | |
| 153 | */ | |
| 154 | const PLAIN_HTTP_LOOPBACK = "a plain-http loopback test origin (127.0.0.1, ::1 or localhost)"; | |
| 155 | const exemptFromSecure = (origin: string): boolean => { | |
| 156 | const u = new URL(origin); | |
| 157 | return u.protocol === "http:" && ["127.0.0.1", "[::1]", "localhost"].includes(u.hostname); | |
| 158 | }; | |
| 159 | ||
| 160 | function check(label: string, cookies: string[], origin: string) { | |
| 161 | const https = !exemptFromSecure(origin); | |
| 148 | 162 | expect(cookies.length).toBeGreaterThan(0); |
| 149 | 163 | for (const cookie of cookies) { |
| 150 | 164 | const flags = cookie.split(";").slice(1).map((f) => f.trim().toLowerCase()); |
| @@ -152,9 +166,10 @@ function check(label: string, cookies: string[], https: boolean) { | ||
| 152 | 166 | const where = `${label}: ${name}`; |
| 153 | 167 | const entry = entryFor(name); |
| 154 | 168 | expect([where, entry === undefined ? "a cookie COOKIES does not name" : "named"]).toEqual([where, "named"]); |
| 155 | // No cookie is readable by script: src/auth/cookies.ts has no way to drop HttpOnly. | |
| 169 | // No cookie is readable by script. | |
| 156 | 170 | expect([where, flags.includes("httponly")]).toEqual([where, true]); |
| 157 | 171 | expect([where, flags.includes("samesite=lax") || flags.includes("samesite=strict")]).toEqual([where, true]); |
| 172 | // Secure everywhere but PLAIN_HTTP_LOOPBACK. | |
| 158 | 173 | expect([where, flags.includes("secure")]).toEqual([where, https]); |
| 159 | 174 | // Exactly one Path, and it is `/` unless the entry scopes it narrower. |
| 160 | 175 | expect([where, flags.filter((f) => f.startsWith("path="))]).toEqual([where, [`path=${(entry?.path?.path ?? "/").toLowerCase()}`]]); |
| @@ -169,7 +184,7 @@ for (const https of [false, true]) { | ||
| 169 | 184 | const take = (label: string, res: Response, ...want: string[]) => { |
| 170 | 185 | const set = res.headers.getSetCookie(); |
| 171 | 186 | for (const name of want) expect([label, set.some((c) => c.startsWith(`${name}=`))]).toEqual([label, true]); |
| 172 | check(label, set, https); | |
| 187 | check(label, set, origin); | |
| 173 | 188 | seen.push(...set); |
| 174 | 189 | return set; |
| 175 | 190 | }; |
| @@ -182,6 +197,8 @@ for (const https of [false, true]) { | ||
| 182 | 197 | const cs: Servers = await serve(child); |
| 183 | 198 | stops.push(() => { ps.stop(); cs.stop(); }); |
| 184 | 199 | const origin = public_ ?? cs.url; |
| 200 | // The http run's only exemption from Secure is that its origin is PLAIN_HTTP_LOOPBACK. | |
| 201 | expect([PLAIN_HTTP_LOOPBACK, exemptFromSecure(origin)]).toEqual([PLAIN_HTTP_LOOPBACK, !https]); | |
| 185 | 202 | |
| 186 | 203 | const mk = async (app: App, username: string, adminIfFirst = false) => { |
| 187 | 204 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@x.local`, password: "a-long-family-passphrase", adminIfFirst }); |
| @@ -198,6 +215,16 @@ for (const https of [false, true]) { | ||
| 198 | 215 | const pw = await post("/api/v1/sessions", { headers: { "content-type": "application/json" }, body: JSON.stringify({ username: "carol", password: "a-long-family-passphrase" }) }); |
| 199 | 216 | expect(pw.status).toBe(201); |
| 200 | 217 | take("password sign-in", pw, "forge_session"); |
| 218 | ||
| 219 | // The sign-in and sign-up forms, the doors a person without script uses. | |
| 220 | const formPost = (path: string, fields: Record<string, string>) => | |
| 221 | post(path, { headers: { "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(fields) }); | |
| 222 | const signedInForm = await formPost("/signin", { username: "carol", password: "a-long-family-passphrase" }); | |
| 223 | expect(signedInForm.status).toBe(303); | |
| 224 | take("form sign-in", signedInForm, "forge_session"); | |
| 225 | const signedUpForm = await formPost("/signup", { username: "finch", email: "", password: "a-long-family-passphrase" }); | |
| 226 | expect(signedUpForm.status).toBe(303); | |
| 227 | take("form sign-up", signedUpForm, "forge_session"); | |
| 201 | 228 | const out = await post("/logout", { cookie: `forge_session=${createSession(child.db, carol.id).token}` }); |
| 202 | 229 | take("sign-out", out, "forge_session"); |
| 203 | 230 | |
| @@ -462,6 +489,25 @@ function argsAt(text: string, open: number): string { | ||
| 462 | 489 | return text.slice(open); |
| 463 | 490 | } |
| 464 | 491 | |
| 492 | describe("the exceptions are named, and only those", () => { | |
| 493 | test(`Secure is waived only on ${PLAIN_HTTP_LOOPBACK}`, () => { | |
| 494 | for (const o of ["http://127.0.0.1:4100", "http://localhost:3000", "http://[::1]:8080"]) expect([o, exemptFromSecure(o)]).toEqual([o, true]); | |
| 495 | for (const o of ["https://127.0.0.1:4100", "https://forge.cookies.test", "http://forge.cookies.test", "http://10.0.0.2", "http://localhost.evil.test"]) | |
| 496 | expect([o, exemptFromSecure(o)]).toEqual([o, false]); | |
| 497 | }); | |
| 498 | ||
| 499 | test("a cookie without HttpOnly fails", () => { | |
| 500 | expect(() => check("no HttpOnly", ["forge_session=x; Path=/; SameSite=Lax; Secure"], "https://forge.cookies.test")).toThrow(); | |
| 501 | }); | |
| 502 | ||
| 503 | test("a cookie without SameSite, or without Secure off loopback, fails", () => { | |
| 504 | expect(() => check("no SameSite", ["forge_session=x; Path=/; HttpOnly; Secure"], "https://forge.cookies.test")).toThrow(); | |
| 505 | expect(() => check("no Secure over https", ["forge_session=x; Path=/; HttpOnly; SameSite=Lax"], "https://forge.cookies.test")).toThrow(); | |
| 506 | expect(() => check("no Secure on a plain-http host", ["forge_session=x; Path=/; HttpOnly; SameSite=Lax"], "http://forge.cookies.test")).toThrow(); | |
| 507 | expect(() => check("loopback", ["forge_session=x; Path=/; HttpOnly; SameSite=Lax"], "http://127.0.0.1:4100")).not.toThrow(); | |
| 508 | }); | |
| 509 | }); | |
| 510 | ||
| 465 | 511 | describe("cookie flags, walked in the source", () => { |
| 466 | 512 | const files = [...new Bun.Glob("src/**/*.ts").scanSync(".")].sort().map((file) => ({ file, text: readFileSync(file, "utf8") })); |
| 467 | 513 | const CALL = /\b(?:setCookie|expireCookie)\(/g; |
new above
offline