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
5 changes: 4 additions & 1 deletion pkgs/github-ci/worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ during the dual-run diff period.
## Shape

- `src/index.ts` — HMAC-SHA256 gate (Web Crypto, same semantics as
the Edge Function), routing, workflow_run ingest.
the Edge Function), routing, workflow_run ingest. The gate is
unconditional (#6936): with `GITHUB_WEBHOOK_SECRET` unset or empty,
every POST is rejected 503 "webhook secret not configured" before
any write — the Worker fails closed, never open.
- `src/policy.ts` — the v4 ingest policy as pure functions:
completed runs only, conclusion != skipped, raw payload kept
only for failures, dedupe_key `github:<repo>:<run>:<attempt>:<action>`.
Expand Down
17 changes: 10 additions & 7 deletions pkgs/github-ci/worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ export default {
if (req.method === "GET") return new Response("github-ci worker ok", { status: 200 });
if (req.method !== "POST") return new Response("method not allowed", { status: 405 });

const raw = await req.text();
const secret = env.GITHUB_WEBHOOK_SECRET ?? "";
if (secret) {
const given = (req.headers.get("x-hub-signature-256") ?? "").replace("sha256=", "");
const expected = await hmacHex(secret, raw);
if (!timingSafeEqual(given, expected)) {
return new Response("bad signature", { status: 401 });
}
if (!secret.trim()) {
console.error("github-ci worker rejected webhook: GITHUB_WEBHOOK_SECRET is not configured");
return new Response("webhook secret not configured", { status: 503 });
}

const raw = await req.text();
const given = (req.headers.get("x-hub-signature-256") ?? "").replace("sha256=", "");
const expected = await hmacHex(secret, raw);
if (!timingSafeEqual(given, expected)) {
return new Response("bad signature", { status: 401 });
}

let payload: Record<string, unknown>;
Expand Down
55 changes: 49 additions & 6 deletions pkgs/github-ci/worker/test/worker.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { createExecutionContext, env, fetchMock, waitOnExecutionContext } from "cloudflare:test";
import { describe, expect, it, beforeAll, afterEach } from "vitest";
import { describe, expect, it, beforeAll, afterEach, vi } from "vitest";
import worker from "../src/index";

const SECRET = "test-webhook-secret";
const PLATFORM = "https://fleet-platform-dezren39.aws-us-east-1.turso.io";
const QUEUE = "https://fleet-queue-dezren39.aws-us-east-1.turso.io";

async function sign(body: string): Promise<string> {
async function sign(body: string, secret = SECRET): Promise<string> {
const key = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(SECRET),
new TextEncoder().encode(secret),
{ name: "HMAC", hash: "SHA-256" },
false,
["sign"],
Expand All @@ -24,7 +24,12 @@ function tursoOk() {
});
}

async function post(body: string, event: string, sig?: string): Promise<Response> {
async function post(
body: string,
event: string,
sig?: string,
webhookSecret: string | null = SECRET,
): Promise<Response> {
const req = new Request("https://github-ci.test/", {
method: "POST",
headers: {
Expand All @@ -34,7 +39,13 @@ async function post(body: string, event: string, sig?: string): Promise<Response
body,
});
const ctx = createExecutionContext();
const res = await worker.fetch(req, env as unknown as Parameters<typeof worker.fetch>[1]);
const workerEnv = { ...env } as unknown as Record<string, unknown>;
if (webhookSecret === null) {
delete workerEnv.GITHUB_WEBHOOK_SECRET;
} else {
workerEnv.GITHUB_WEBHOOK_SECRET = webhookSecret;
}
const res = await worker.fetch(req, workerEnv as unknown as Parameters<typeof worker.fetch>[1]);
await waitOnExecutionContext(ctx);
return res;
}
Expand Down Expand Up @@ -62,9 +73,41 @@ beforeAll(() => {
fetchMock.activate();
fetchMock.disableNetConnect();
});
afterEach(() => fetchMock.assertNoPendingInterceptors());
afterEach(() => {
vi.restoreAllMocks();
fetchMock.assertNoPendingInterceptors();
});

describe("github-ci worker", () => {
it.each([
["unset", null],
["empty", ""],
])("rejects unsigned webhooks when the secret is %s and logs the configuration error", async (_name, secret) => {
const log = vi.spyOn(console, "error").mockImplementation(() => {});
const res = await post(runPayload, "workflow_run", undefined, secret);

expect(res.status).toBe(503);
expect(await res.text()).toBe("webhook secret not configured");
expect(log).toHaveBeenCalledWith(
"github-ci worker rejected webhook: GITHUB_WEBHOOK_SECRET is not configured",
);
log.mockRestore();
});

it.each([
["unset", null],
["empty", ""],
])("rejects ping when the secret is %s", async (_name, secret) => {
const res = await post("{}", "ping", undefined, secret);
expect(res.status).toBe(503);
expect(await res.text()).toBe("webhook secret not configured");
});

it("rejects a signature made with the wrong secret", async () => {
const res = await post(runPayload, "workflow_run", await sign(runPayload, "wrong-secret"));
expect(res.status).toBe(401);
});

it("rejects a bad signature with 401", async () => {
const res = await post(runPayload, "workflow_run", "sha256=00");
expect(res.status).toBe(401);
Expand Down