From 9e33dfa4167a2520756d502ecce55f47bc42b101 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sat, 7 Nov 2026 20:06:57 -0600 Subject: [PATCH] fix(github-backup): validate queue ack and KV values --- infra/github-backup/worker/src/index.js | 24 ++++- .../github-backup/worker/test/index.test.mjs | 94 ++++++++++++++++++- 2 files changed, 114 insertions(+), 4 deletions(-) diff --git a/infra/github-backup/worker/src/index.js b/infra/github-backup/worker/src/index.js index 9d9e1ad4fd..8e5fab7c65 100644 --- a/infra/github-backup/worker/src/index.js +++ b/infra/github-backup/worker/src/index.js @@ -81,7 +81,13 @@ export default { const values = await env.GHB_EVENTS.get(chunk); for (const name of chunk) { const v = values.get(name); - if (v) events.push(JSON.parse(v)); + if (v !== null && v !== undefined) { + try { + events.push(JSON.parse(v)); + } catch { + return new Response(`invalid JSON stored for queue key ${name}`, { status: 500 }); + } + } } } return Response.json({ @@ -92,8 +98,20 @@ export default { } if (url.pathname === "/queue/ack" && req.method === "POST") { if (!authed(req, env)) return new Response("unauthorized", { status: 401 }); - const { ids } = await req.json(); - const keys = (ids || []).map((id) => "ev:" + id); + let body; + try { + body = await req.json(); + } catch { + return Response.json({ error: "request body must be valid JSON" }, { status: 400 }); + } + if (!body || typeof body !== "object" || Array.isArray(body)) { + return Response.json({ error: "request body must be an object with an ids array" }, { status: 400 }); + } + const { ids } = body; + if (!Array.isArray(ids) || ids.some((id) => typeof id !== "string" || id.trim().length === 0)) { + return Response.json({ error: "ids must be an array of non-empty strings" }, { status: 400 }); + } + const keys = ids.map((id) => "ev:" + id); // The binding has no bulk delete, so fan the per-key deletes // out in parallel chunks (#5496): sequential awaits needed // ~50s for the drainer's 500-id ack batches and timed it out diff --git a/infra/github-backup/worker/test/index.test.mjs b/infra/github-backup/worker/test/index.test.mjs index 2a0d2a47f6..621cdb6898 100644 --- a/infra/github-backup/worker/test/index.test.mjs +++ b/infra/github-backup/worker/test/index.test.mjs @@ -129,10 +129,102 @@ test("POST /queue/ack deletes every id and reports the count", async () => { env, ); assert.equal(response.status, 200); - assert.deepEqual(await response.json(), { acked: 3 }); + const result = await response.json(); + assert.deepEqual(result, { acked: 3 }); + assert.equal(result.acked, deleted.length); assert.deepEqual([...deleted].sort(), ["ev:a", "ev:b", "ev:c"]); }); +test("POST /queue/ack rejects malformed JSON and non-object bodies without deleting", async () => { + const deleted = []; + const env = { + DRAIN_BEARER: "test-bearer", + GHB_EVENTS: { async delete(name) { deleted.push(name); } }, + }; + const cases = [ + ["non-JSON", "{"], + ["empty", ""], + ["JSON null", "null"], + ["array", "[]"], + ["string", JSON.stringify("ids")], + ["number", "1"], + ]; + + for (const [label, body] of cases) { + const response = await worker.fetch( + new Request("https://worker.test/queue/ack", { + method: "POST", + headers: { Authorization: "Bearer test-bearer", "Content-Type": "application/json" }, + body, + }), + env, + ); + assert.equal(response.status, 400, label); + assert.ok((await response.json()).error, label); + } + assert.deepEqual(deleted, []); +}); + +test("POST /queue/ack rejects missing or malformed ids without deleting", async () => { + const deleted = []; + const env = { + DRAIN_BEARER: "test-bearer", + GHB_EVENTS: { async delete(name) { deleted.push(name); } }, + }; + const cases = [ + ["missing ids", {}], + ["string ids", { ids: "abc" }], + ["numeric ids", { ids: 123 }], + ["numeric entry", { ids: [123] }], + ["null entry", { ids: [null] }], + ["empty entry", { ids: [""] }], + ["whitespace entry", { ids: [" "] }], + ]; + + for (const [label, body] of cases) { + const response = await worker.fetch( + new Request("https://worker.test/queue/ack", { + method: "POST", + headers: { Authorization: "Bearer test-bearer", "Content-Type": "application/json" }, + body: JSON.stringify(body), + }), + env, + ); + assert.equal(response.status, 400, label); + assert.match((await response.json()).error, /ids/, label); + } + assert.deepEqual(deleted, []); +}); + +test("GET /queue names a corrupt KV key instead of throwing a bare parse error", async () => { + const env = { + DRAIN_BEARER: "test-bearer", + GHB_EVENTS: { + async list() { + return { + keys: [{ name: "ev:good" }, { name: "ev:corrupt" }], + cursor: null, + list_complete: true, + }; + }, + async get(names) { + return new Map(names.map((name) => [ + name, + name === "ev:corrupt" ? "" : JSON.stringify({ id: "good" }), + ])); + }, + }, + }; + const response = await worker.fetch( + new Request("https://worker.test/queue", { + headers: { Authorization: "Bearer test-bearer" }, + }), + env, + ); + assert.equal(response.status, 500); + assert.match(await response.text(), /ev:corrupt/); +}); + test("POST /queue/ack requires auth", async () => { let deleted = false; const response = await worker.fetch(