Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions infra/github-backup/worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
Expand Down
94 changes: 93 additions & 1 deletion infra/github-backup/worker/test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading