Skip to content
Merged
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
14 changes: 11 additions & 3 deletions src/lib/server/utils/hmac.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { createHmac, timingSafeEqual } from "node:crypto";

export function verifySignature(secret: string, body: string, signature: string): boolean {
const expected = createHmac("sha256", secret).update(body).digest("hex");
// Try HMAC verification first (GitHub-style: sha256=<hex>)
const cleaned = signature.replace(/^sha256=/, "");
if (cleaned.length !== expected.length) return false;
return timingSafeEqual(Buffer.from(cleaned, "hex"), Buffer.from(expected, "hex"));
if (/^[0-9a-f]{64}$/i.test(cleaned)) {
const expected = createHmac("sha256", secret).update(body).digest("hex");
return timingSafeEqual(Buffer.from(cleaned, "hex"), Buffer.from(expected, "hex"));
}

// Fall back to direct token comparison (GitLab-style: X-Gitlab-Token)
const sigBuf = Buffer.from(signature);
const secretBuf = Buffer.from(secret);
if (sigBuf.length !== secretBuf.length) return false;
return timingSafeEqual(sigBuf, secretBuf);
}
Loading