import { describe, it, expect } from "vitest"; import { verifyWebhookSignature, signWebhookPayload, DEFAULT_REPLAY_WINDOW_MS } from "./webhooks.js"; const SECRET = "whsec_test_super_secret"; const PAYLOAD = `{"event":"contact.created","data":{"id":"c_42"}}`; const TIMESTAMP = 1_700_000_000; describe("signWebhookPayload", () => { it("produces a deterministic hex digest", () => { const sigA = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); const sigB = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); expect(sigA).toBe(sigB); expect(sigA).toMatch(/^[0-9a-f]{64}$/); }); it("differs when the timestamp changes", () => { const sig1 = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); const sig2 = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP + 1 }); expect(sig1).not.toBe(sig2); }); it("rejects empty secrets", () => { expect(() => signWebhookPayload({ secret: "", payload: PAYLOAD, timestamp: TIMESTAMP })).toThrow(/secret/); }); }); describe("verifyWebhookSignature", () => { const now = () => TIMESTAMP * 1000 + 30_000; // 30s after issue it("accepts a valid signature within the replay window", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature, timestamp: TIMESTAMP, now })).toBe(true); }); it("rejects a tampered payload", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); const tampered = PAYLOAD.replace("c_42", "c_99"); expect(verifyWebhookSignature({ secret: SECRET, payload: tampered, signature, timestamp: TIMESTAMP, now })).toBe(false); }); it("rejects a tampered signature", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); const broken = signature.replace(/.$/, signature.slice(-1) === "a" ? "b" : "a"); expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature: broken, timestamp: TIMESTAMP, now })).toBe(false); }); it("rejects a wrong secret", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); expect(verifyWebhookSignature({ secret: "other_secret", payload: PAYLOAD, signature, timestamp: TIMESTAMP, now })).toBe(false); }); it("rejects a signature outside the replay window", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); const farFuture = () => TIMESTAMP * 1000 + DEFAULT_REPLAY_WINDOW_MS + 1000; expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature, timestamp: TIMESTAMP, now: farFuture })).toBe(false); }); it("accepts a clock-skewed signature in the past within the window", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); const slightlyEarly = () => TIMESTAMP * 1000 - 30_000; expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature, timestamp: TIMESTAMP, now: slightlyEarly })).toBe(true); }); it("accepts signatures with `sha256=` prefix", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature: `sha256=${signature}`, timestamp: TIMESTAMP, now })).toBe(true); }); it("returns false on malformed input rather than throwing", () => { expect(verifyWebhookSignature({ secret: "", payload: PAYLOAD, signature: "x", timestamp: TIMESTAMP, now })).toBe(false); expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature: "", timestamp: TIMESTAMP, now })).toBe(false); expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature: "not-hex", timestamp: TIMESTAMP, now })).toBe(false); expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature: "ff", timestamp: "not-a-number", now })).toBe(false); }); it("verifies Buffer payloads identically", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); expect( verifyWebhookSignature({ secret: SECRET, payload: Buffer.from(PAYLOAD, "utf8"), signature, timestamp: TIMESTAMP, now, }), ).toBe(true); }); it("respects a custom replay window override", () => { const signature = signWebhookPayload({ secret: SECRET, payload: PAYLOAD, timestamp: TIMESTAMP }); const past = () => TIMESTAMP * 1000 + 90_000; // Default window (5min) accepts it. expect(verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature, timestamp: TIMESTAMP, now: past })).toBe(true); // 30-second window rejects it. expect( verifyWebhookSignature({ secret: SECRET, payload: PAYLOAD, signature, timestamp: TIMESTAMP, now: past, replayWindowMs: 30_000, }), ).toBe(false); }); });