Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,44 @@ describe("app webhook_url delivery", () => {
expect(body.installation.id).toBe(55);
});

it("gives each app webhook delivery its own GUID, even within the same millisecond", async () => {
vi.spyOn(Date, "now").mockReturnValue(1_700_000_000_000);
try {
const { app } = createTestApp({
users: [{ login: "octocat" }],
repos: [{ owner: "octocat", name: "hello-world" }],
apps: [
{
app_id: 550,
slug: "delivery-app",
name: "Delivery App",
private_key: "fake-key",
events: ["issues"],
webhook_url: "https://delivery.example/webhook",
installations: [{ installation_id: 57, account: "octocat", repository_selection: "all" }],
},
],
});

for (const title of ["First delivery", "Second delivery"]) {
await app.request(`${base}/repos/octocat/hello-world/issues`, {
method: "POST",
headers: { ...authHeaders(), "Content-Type": "application/json" },
body: JSON.stringify({ title }),
});
}

const ids = mockFetch.mock.calls
.filter((c) => c[0] === "https://delivery.example/webhook")
.map((c) => ((c[1] as RequestInit).headers as Record<string, string>)["X-GitHub-Delivery"]);
expect(ids).toHaveLength(2);
for (const id of ids) expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
expect(new Set(ids).size).toBe(2);
} finally {
vi.restoreAllMocks();
}
});

it("signs app webhook delivery with webhook_secret", async () => {
const secret = "app-webhook-secret";
const { app } = createTestApp({
Expand Down
2 changes: 1 addition & 1 deletion packages/@emulators/github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ async function deliverToAppWebhookUrls(
const headers: Record<string, string> = {
"Content-Type": "application/json",
"X-GitHub-Event": event,
"X-GitHub-Delivery": String(Date.now()),
"X-GitHub-Delivery": crypto.randomUUID(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Bare crypto global; import randomUUID instead.

--- a/packages/@emulators/github/src/index.ts
+++ b/packages/@emulators/github/src/index.ts
@@
-import { createHmac, generateKeyPair } from "crypto";
+import { createHmac, generateKeyPair, randomUUID } from "crypto";
@@
-      "X-GitHub-Delivery": crypto.randomUUID(),
+      "X-GitHub-Delivery": randomUUID(),
🤖 Fix with your agent
Fix this code review finding (aictrl-dev/github-emulator PR #13, packages/@emulators/github/src/index.ts:634):

Problem: Bare crypto global; import randomUUID instead
Detail: The new code calls `crypto.randomUUID()` via the ambient WebCrypto global, but this file already imports its crypto primitives by name (`import { createHmac, generateKeyPair } from "crypto"`, line 1). The bare `crypto` identifier is never imported, so the module only compiles/runs where the global exists: the WebCrypto global is only available by default on Node >= 19 (Node 18 needs --experimental-global-webcrypto) and `tsc` only resolves it with recent @types/node. The surrounding code establishes at most a Node 18+ baseline (it uses global `fetch` / `AbortSignal.timeout`), so on Node 18 runtimes every app-webhook dispatch in `deliverToAppWebhookUrls` would throw a ReferenceError, and even where it works the implicit global is inconsistent with the file's own named-import convention. Use the named export from the module already imported.
Suggested fix: Extend the existing import on line 1 and call it directly: `import { createHmac, generateKeyPair, randomUUID } from "crypto";` then `"X-GitHub-Delivery": randomUUID(),`.

Suggested patch:
--- a/packages/@emulators/github/src/index.ts
+++ b/packages/@emulators/github/src/index.ts
@@
-import { createHmac, generateKeyPair } from "crypto";
+import { createHmac, generateKeyPair, randomUUID } from "crypto";
@@
-      "X-GitHub-Delivery": crypto.randomUUID(),
+      "X-GitHub-Delivery": randomUUID(),

Implement the fix on the PR head branch and add a regression test that fails before the fix and passes after.
Why this matters

The new code calls crypto.randomUUID() via the ambient WebCrypto global, but this file already imports its crypto primitives by name (import { createHmac, generateKeyPair } from "crypto", line 1). The bare crypto identifier is never imported, so the module only compiles/runs where the global exists: the WebCrypto global is only available by default on Node >= 19 (Node 18 needs --experimental-global-webcrypto) and tsc only resolves it with recent @types/node. The surrounding code establishes at most a Node 18+ baseline (it uses global fetch / AbortSignal.timeout), so on Node 18 runtimes every app-webhook dispatch in deliverToAppWebhookUrls would throw a ReferenceError, and even where it works the implicit global is inconsistent with the file's own named-import convention. Use the named export from the module already imported.

    const headers: Record<string, string> = {
      "Content-Type": "application/json",
      "X-GitHub-Event": event,
      "X-GitHub-Delivery": crypto.randomUUID(),
    };
    if (ghApp.webhook_secret) {
      const hmac = createHmac("sha256", ghApp.webhook_secret).update(body).digest("hex");

};
if (ghApp.webhook_secret) {
const hmac = createHmac("sha256", ghApp.webhook_secret).update(body).digest("hex");
Expand Down
Loading