|
| 1 | +// A rotated root key keeps authenticating during its grace window so callers |
| 2 | +// can switch over without downtime. These tests lock in that the old key can't |
| 3 | +// use that window to recover its replacement: it can't read whoami, ship a |
| 4 | +// deployment, or touch the environment's variables, while the current key can. |
| 5 | + |
| 6 | +import { describe, expect, it } from "vitest"; |
| 7 | +import { getTestServer } from "./helpers/sharedTestServer"; |
| 8 | +import { seedTestEnvironment } from "./helpers/seedTestEnvironment"; |
| 9 | + |
| 10 | +async function seedRotatedEnvironment() { |
| 11 | + const server = getTestServer(); |
| 12 | + const seeded = await seedTestEnvironment(server.prisma); |
| 13 | + const currentKey = `tr_dev_rotated_${Math.random().toString(36).slice(2)}`; |
| 14 | + |
| 15 | + // Rotate exactly as regenerate-api-key does: new value on the env, old value |
| 16 | + // parked in RevokedApiKey with a future grace expiry. |
| 17 | + await server.prisma.$transaction([ |
| 18 | + server.prisma.revokedApiKey.create({ |
| 19 | + data: { |
| 20 | + apiKey: seeded.apiKey, |
| 21 | + runtimeEnvironmentId: seeded.environment.id, |
| 22 | + expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000), |
| 23 | + }, |
| 24 | + }), |
| 25 | + server.prisma.runtimeEnvironment.update({ |
| 26 | + where: { id: seeded.environment.id }, |
| 27 | + data: { apiKey: currentKey }, |
| 28 | + }), |
| 29 | + ]); |
| 30 | + |
| 31 | + return { ...seeded, rotatedKey: seeded.apiKey, currentKey }; |
| 32 | +} |
| 33 | + |
| 34 | +function request(path: string, authorization: string, init: RequestInit = {}) { |
| 35 | + return getTestServer().webapp.fetch(path, { |
| 36 | + ...init, |
| 37 | + headers: { |
| 38 | + Authorization: authorization, |
| 39 | + "Content-Type": "application/json", |
| 40 | + ...init.headers, |
| 41 | + }, |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +describe("rotated root keys during the grace window", () => { |
| 46 | + // Catches: whoami handing a rotated key the environment's replacement root key. |
| 47 | + it("whoami rejects the rotated key and never returns key material", async () => { |
| 48 | + const { rotatedKey, currentKey } = await seedRotatedEnvironment(); |
| 49 | + |
| 50 | + const rotated = await request("/api/v1/whoami", `Bearer ${rotatedKey}`); |
| 51 | + expect(rotated.status).toBe(401); |
| 52 | + |
| 53 | + const current = await request("/api/v1/whoami", `Bearer ${currentKey}`); |
| 54 | + expect(current.status).toBe(200); |
| 55 | + const body = JSON.stringify(await current.json()); |
| 56 | + expect(body).not.toContain(currentKey); |
| 57 | + expect(body).not.toContain("apiKey"); |
| 58 | + }); |
| 59 | + |
| 60 | + // Catches: a rotated key reading env vars, which can hold secrets updated after the rotation. |
| 61 | + it("rejects env var reads with the rotated key", async () => { |
| 62 | + const { project, rotatedKey, currentKey } = await seedRotatedEnvironment(); |
| 63 | + const path = `/api/v1/projects/${project.externalRef}/envvars/dev`; |
| 64 | + |
| 65 | + expect((await request(path, `Bearer ${rotatedKey}`)).status).toBe(401); |
| 66 | + expect((await request(path, `Bearer ${currentKey}`)).status).toBe(200); |
| 67 | + }); |
| 68 | + |
| 69 | + // Catches: a rotated key setting a variable such as NODE_OPTIONS that runs code with the new key. |
| 70 | + it("rejects env var writes with the rotated key", async () => { |
| 71 | + const { project, rotatedKey, currentKey } = await seedRotatedEnvironment(); |
| 72 | + const path = `/api/v1/projects/${project.externalRef}/envvars/dev`; |
| 73 | + const init = { method: "POST", body: JSON.stringify({ name: "E2E_VAR", value: "x" }) }; |
| 74 | + |
| 75 | + expect((await request(path, `Bearer ${rotatedKey}`, init)).status).toBe(401); |
| 76 | + expect((await request(path, `Bearer ${currentKey}`, init)).status).toBe(200); |
| 77 | + }); |
| 78 | + |
| 79 | + // Catches: a rotated key driving a deployment whose runs receive the new key. |
| 80 | + it("rejects deployment writes with the rotated key", async () => { |
| 81 | + const { rotatedKey, currentKey } = await seedRotatedEnvironment(); |
| 82 | + const path = "/api/v1/deployments/deployment_missing/progress"; |
| 83 | + const init = { method: "POST", body: JSON.stringify({}) }; |
| 84 | + |
| 85 | + expect((await request(path, `Bearer ${rotatedKey}`, init)).status).toBe(401); |
| 86 | + // Past auth, the current key reaches the handler, which finds no deployment. |
| 87 | + expect((await request(path, `Bearer ${currentKey}`, init)).status).toBe(404); |
| 88 | + }); |
| 89 | + |
| 90 | + // Catches: extra whitespace after "Bearer" authenticating the rotated key while the grace-window lookup misses it. |
| 91 | + it("rejects the rotated key with extra whitespace in the header", async () => { |
| 92 | + const { project, rotatedKey } = await seedRotatedEnvironment(); |
| 93 | + |
| 94 | + const res = await request( |
| 95 | + `/api/v1/projects/${project.externalRef}/envvars/dev`, |
| 96 | + `Bearer ${rotatedKey}` |
| 97 | + ); |
| 98 | + expect(res.status).toBe(401); |
| 99 | + }); |
| 100 | + |
| 101 | + // Catches: the restriction spilling over and breaking the zero-downtime switchover for reads. |
| 102 | + it("still lets the rotated key read deployments", async () => { |
| 103 | + const { rotatedKey } = await seedRotatedEnvironment(); |
| 104 | + |
| 105 | + // Past auth, the handler finds no deployment for the fresh environment. |
| 106 | + const res = await request("/api/v1/deployments/latest", `Bearer ${rotatedKey}`); |
| 107 | + expect(res.status).toBe(404); |
| 108 | + }); |
| 109 | +}); |
0 commit comments