Public: anyone can read this; the crew can change it.
The JWKS answers with Cache-Control and keeps the previous signing key listed while a rotation settles #16062
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16054-the-jwks-answers-with-cache-control-and → main 0 commits fork
1 file
changed +74
−0
test/jwks-rotation.test.ts added +74
| @@ -0,0 +1,74 @@ | ||
| 1 | /** | |
| 2 | * The JWKS over HTTP: cacheable, public members only, and the previous key | |
| 3 | * stays listed after a rotation so a token it signed still verifies. | |
| 4 | * (These already held; this pins them.) | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { ID_TOKEN_TTL_S, rotateKeys, signIdToken, verifyIdToken } from "../src/core/oidc.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | ||
| 15 | describe("jwks rotation over http", () => { | |
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | ||
| 20 | beforeAll(async () => { | |
| 21 | dataDir = await mkdtemp(join(tmpdir(), "forge-jwks-")); | |
| 22 | app = createApp({ dataDir, port: 0 }); | |
| 23 | servers = await serve(app); | |
| 24 | }); | |
| 25 | afterAll(async () => { | |
| 26 | servers.stop(); | |
| 27 | await rm(dataDir, { recursive: true, force: true }); | |
| 28 | }); | |
| 29 | ||
| 30 | const fetchSet = async () => { | |
| 31 | const res = await fetch(`${servers.url}/oauth/jwks`); | |
| 32 | return { res, doc: (await res.json()) as { keys: Record<string, unknown>[] } }; | |
| 33 | }; | |
| 34 | ||
| 35 | test("answers with public Cache-Control, both keys after a rotation, public members only", async () => { | |
| 36 | const made = await createUser(app.db, { | |
| 37 | origin: "seed", | |
| 38 | trusted: true, | |
| 39 | username: "bob", | |
| 40 | email: "[email protected]", | |
| 41 | password: "a-good-passphrase", | |
| 42 | adminIfFirst: true, | |
| 43 | }); | |
| 44 | if (!made.ok) throw new Error(made.error); | |
| 45 | const expectFor = { issuer: "https://forge.test", clientId: "c1" }; | |
| 46 | const t0 = Date.now(); | |
| 47 | ||
| 48 | const before = await fetchSet(); | |
| 49 | expect(before.res.status).toBe(200); | |
| 50 | expect(before.res.headers.get("cache-control")).toMatch(/^public, max-age=\d+$/); | |
| 51 | expect(before.doc.keys.length).toBe(1); | |
| 52 | const oldKid = before.doc.keys[0]!["kid"]; | |
| 53 | ||
| 54 | const token = await signIdToken(app.db, dataDir, { ...expectFor, user: made.value, scopes: ["openid"], nonce: "n" }, t0); | |
| 55 | const fresh = await rotateKeys(app.db, dataDir, t0 + 1000); | |
| 56 | ||
| 57 | const after = await fetchSet(); | |
| 58 | expect(after.res.headers.get("cache-control")).toMatch(/^public, max-age=\d+$/); | |
| 59 | expect(after.doc.keys.map((k) => k["kid"])).toEqual([fresh.kid, oldKid]); | |
| 60 | for (const k of after.doc.keys) { | |
| 61 | const allowed = ["alg", "crv", "ext", "key_ops", "kid", "kty", "use", "x", "y"]; | |
| 62 | expect(Object.keys(k).filter((m) => !allowed.includes(m))).toEqual([]); | |
| 63 | expect(k["d"]).toBeUndefined(); | |
| 64 | } | |
| 65 | ||
| 66 | // The token the previous key signed still verifies while it is listed. | |
| 67 | expect((await verifyIdToken(app.db, dataDir, token, expectFor, t0 + 2000)).ok).toBe(true); | |
| 68 | expect((await verifyIdToken(app.db, dataDir, token, expectFor, t0 + (ID_TOKEN_TTL_S + 1) * 1000)).ok).toBe(false); | |
| 69 | ||
| 70 | // A second rotation retires the first key: only two are ever listed. | |
| 71 | await rotateKeys(app.db, dataDir, t0 + 3000); | |
| 72 | expect((await fetchSet()).doc.keys.map((k) => k["kid"])).not.toContain(oldKid); | |
| 73 | }); | |
| 74 | }); |
new above
offline