From c7b600575e386bf88179aa736f469b7896469662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sat, 9 May 2026 02:00:49 +0200 Subject: [PATCH] fix(idempotency): refuse to cache bodies containing third-party AI keys P12. The body-content guard already rejected `hlk_` / `hlr_` (our access and refresh tokens). Extend it to also reject `sk-` so any OpenAI or Anthropic key that might be echoed by a misrouted handler never lands in the IdempotencyKey table. The substring match also covers `sk-ant-` so we don't need a second branch. Adds a parametrised vitest case asserting all four prefixes (hlk_, hlr_, sk-, sk-ant-) suppress caching, plus a positive case to prove a normal payload still caches. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/lib/__tests__/idempotency.test.ts | 41 +++++++++++++++++++++++++++ src/lib/idempotency.ts | 15 +++++++--- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/lib/__tests__/idempotency.test.ts b/src/lib/__tests__/idempotency.test.ts index 9b1606949..866e8762b 100644 --- a/src/lib/__tests__/idempotency.test.ts +++ b/src/lib/__tests__/idempotency.test.ts @@ -270,6 +270,47 @@ describe("defaultUserIdResolver (audit C-4)", () => { }); }); +// P12: bodies that contain a freshly-issued bearer token, refresh token, +// or third-party AI provider key must NEVER be persisted to the +// idempotency cache. Even if a future caller forgets and wraps an +// auth/settings route in withIdempotency, the body-content guard refuses +// to write the secret into the DB. +describe("withIdempotency body-content exclusion (P12)", () => { + beforeEach(() => { + vi.resetAllMocks(); + vi.mocked(prisma.idempotencyKey.findUnique).mockResolvedValue(null); + vi.mocked(prisma.idempotencyKey.create).mockResolvedValue({} as never); + }); + + it.each([ + ["hlk_ access token", '{"data":{"token":"hlk_abc123"},"error":null}'], + ["hlr_ refresh token", '{"data":{"refresh":"hlr_xyz789"},"error":null}'], + ["sk- OpenAI key", '{"data":{"echoed":"sk-1234567890"},"error":null}'], + [ + "sk-ant- Anthropic key", + '{"data":{"echoed":"sk-ant-api03-xyz"},"error":null}', + ], + ])("does NOT cache responses containing %s", async (_label, body) => { + const handler = vi.fn( + async () => new NextResponse(body, { status: 201 }), + ); + const wrapped = withIdempotency<[NextRequest]>(handler, async () => "u-1"); + await wrapped(makeRequest("POST", { "idempotency-key": "abc-12345678" })); + expect(handler).toHaveBeenCalledTimes(1); + expect(prisma.idempotencyKey.create).not.toHaveBeenCalled(); + }); + + it("still caches a normal payload that does not carry a secret", async () => { + const handler = vi.fn( + async () => + new NextResponse('{"data":{"id":"m-1"},"error":null}', { status: 201 }), + ); + const wrapped = withIdempotency<[NextRequest]>(handler, async () => "u-1"); + await wrapped(makeRequest("POST", { "idempotency-key": "abc-12345678" })); + expect(prisma.idempotencyKey.create).toHaveBeenCalledTimes(1); + }); +}); + // V3 audit STILL-V2-NEW: the cachable-status filter (do-not-cache for // 401/403/408/429/5xx) had zero tests, so a regression that re-cached an // expired bearer token's 401 would have been silent. diff --git a/src/lib/idempotency.ts b/src/lib/idempotency.ts index 49d0473cf..8c6131675 100644 --- a/src/lib/idempotency.ts +++ b/src/lib/idempotency.ts @@ -219,12 +219,19 @@ export function withIdempotency< if (isCachableStatus(response.status)) { // Defence-in-depth: never persist a body that carries a freshly-issued - // bearer or refresh token. Auth routes shouldn't be wrapped in - // withIdempotency to begin with, but if a future caller forgets we - // refuse to leak. `hlk_` = access tokens, `hlr_` = refresh tokens. + // bearer / refresh token or a third-party AI provider key. Auth and + // settings routes shouldn't be wrapped in withIdempotency to begin + // with, but if a future caller forgets we refuse to leak. + // `hlk_` = our access tokens + // `hlr_` = our refresh tokens + // `sk-` = OpenAI keys (also matches `sk-ant-` Anthropic keys) const cloned = response.clone(); const text = await cloned.text(); - if (!text.includes("hlk_") && !text.includes("hlr_")) { + if ( + !text.includes("hlk_") && + !text.includes("hlr_") && + !text.includes("sk-") + ) { await persistCached(ctx, response, text); } }