Files
t3code/apps/server/src/scheduledTasks/webhookVerification.test.ts
T
78552a8c61 chore(deps): upgrade @effect/tsgo to 0.46.1 (#16360)
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-06 10:52:08 -07:00

61 lines
1.9 KiB
TypeScript

// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto has no createHmac.
import * as NodeCrypto from "node:crypto";
import { assert, describe, it } from "@effect/vitest";
import { verifyWebhookSignature } from "./webhookVerification.ts";
const body = new TextEncoder().encode('{"action":"opened"}');
const secret = "shared-secret";
const hmac = () => NodeCrypto.createHmac("sha256", secret).update(body);
describe("verifyWebhookSignature", () => {
const github = { header: "x-hub-signature-256", encoding: "hex", prefix: "sha256=" } as const;
it("accepts a GitHub-style hex signature with prefix", () => {
const headers = { "x-hub-signature-256": `sha256=${hmac().digest("hex")}` };
assert.isTrue(verifyWebhookSignature({ signature: github, secret, headers, body }));
});
it("rejects a tampered body, a wrong secret, a missing header and a missing prefix", () => {
const valid = `sha256=${hmac().digest("hex")}`;
const tampered = new TextEncoder().encode('{"action":"closed"}');
assert.isFalse(
verifyWebhookSignature({
signature: github,
secret,
headers: { "x-hub-signature-256": valid },
body: tampered,
}),
);
assert.isFalse(
verifyWebhookSignature({
signature: github,
secret: "other",
headers: { "x-hub-signature-256": valid },
body,
}),
);
assert.isFalse(verifyWebhookSignature({ signature: github, secret, headers: {}, body }));
assert.isFalse(
verifyWebhookSignature({
signature: github,
secret,
headers: { "x-hub-signature-256": hmac().digest("hex") },
body,
}),
);
});
it("accepts a base64 signature without prefix", () => {
assert.isTrue(
verifyWebhookSignature({
signature: { header: "X-Signature", encoding: "base64", prefix: "" },
secret,
headers: { "x-signature": hmac().digest("base64") },
body,
}),
);
});
});