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
10 changes: 9 additions & 1 deletion infra/github-backup/worker/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export default {
const url = new URL(req.url);
if (url.pathname === "/health") return new Response("ok");
if (url.pathname === "/github" && req.method === "POST") {
// Fail closed on a misconfigured deployment (#6951): without a
// secret the HMAC key is zero-length and importKey throws for
// every delivery, leaving an opaque 500 that names nothing.
if (!env.GITHUB_WEBHOOK_SECRET) {
return new Response("webhook secret not configured", { status: 500 });
}
const body = await req.text();
const given = (req.headers.get("X-Hub-Signature-256") || "").replace("sha256=", "");
const expect = await hmacHex(env.GITHUB_WEBHOOK_SECRET, body);
Expand All @@ -46,7 +52,9 @@ export default {
try { payload = JSON.parse(body); } catch { /* ping has form encoding sometimes */ }
const event = req.headers.get("X-GitHub-Event") || "unknown";
if (event === "ping") return new Response("pong");
const repo = payload.repository && payload.repository.full_name;
// A JSON null body parses to null (#6951): every other
// non-object shape already lands on the 202 below.
const repo = payload?.repository && payload.repository.full_name;
if (!repo) return new Response("no repository in payload", { status: 202 });
const id = `${Date.now()}-${crypto.randomUUID()}`;
await env.GHB_EVENTS.put("ev:" + id, JSON.stringify({
Expand Down
54 changes: 54 additions & 0 deletions infra/github-backup/worker/test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,57 @@ test("POST /queue/ack requires auth", async () => {
assert.equal(response.status, 401);
assert.equal(deleted, false);
});

import { createHmac } from "node:crypto";

const sign = (secret, body) =>
"sha256=" + createHmac("sha256", secret).update(body).digest("hex");

function githubRequest(secret, body, event = "push") {
const headers = { "X-GitHub-Event": event };
if (secret !== null) headers["X-Hub-Signature-256"] = sign(secret, body);
return new Request("https://worker.test/github", { method: "POST", headers, body });
}

test("POST /github stores a correctly signed repository event", async () => {
const puts = [];
const env = {
GHB_EVENTS: { async put(k, v) { puts.push([k, v]); } },
GITHUB_WEBHOOK_SECRET: "whsec",
};
const body = JSON.stringify({ repository: { full_name: "owner/repo" }, action: "created" });
const response = await worker.fetch(githubRequest("whsec", body), env);
assert.equal(response.status, 202);
assert.equal(puts.length, 1);
assert.equal(JSON.parse(puts[0][1]).repo, "owner/repo");
const bad = await worker.fetch(githubRequest("wrong", body), env);
assert.equal(bad.status, 401);
assert.equal(puts.length, 1);
});

test("POST /github returns 202 (not a crash) for a signed JSON null body (#6951)", async () => {
let puts = 0;
const env = {
GHB_EVENTS: { async put() { puts++; } },
GITHUB_WEBHOOK_SECRET: "whsec",
};
const response = await worker.fetch(githubRequest("whsec", "null"), env);
assert.equal(response.status, 202);
assert.equal(await response.text(), "no repository in payload");
assert.equal(puts, 0);
});

test("POST /github fails closed naming the config when the webhook secret is unset or empty (#6951)", async () => {
let puts = 0;
const kv = { async put() { puts++; } };
const body = JSON.stringify({ repository: { full_name: "owner/repo" } });
for (const secret of [undefined, ""]) {
const env = { GHB_EVENTS: kv };
if (secret !== undefined) env.GITHUB_WEBHOOK_SECRET = secret;
// Even a signature computed with the empty key must not be accepted.
const response = await worker.fetch(githubRequest("", body), env);
assert.equal(response.status, 500);
assert.match(await response.text(), /webhook secret not configured/);
}
assert.equal(puts, 0);
});
Loading