mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
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) <noreply@anthropic.com>
This commit is contained in:
1 parent
e903d9a2c2
commit
c7b600575e
2 files changed
+52
-4
No files matched your search
@@ -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.
|
||||
|
||||
+11
-4
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user