From 32908bf3cc531f7b1461822ca7c1057397519fd5 Mon Sep 17 00:00:00 2001 From: yinbowen-dc <65101280+yinbowen-dc@users.noreply.github.com> Date: Thu, 19 Nov 2026 13:11:02 +0800 Subject: [PATCH] [SDK] Fix webhook timestamp validation and HMAC verification --- .changeset/tidy-webhook-verification.md | 5 ++ packages/thirdweb/src/bridge/Webhook.test.ts | 48 ++++++++++++++++++++ packages/thirdweb/src/bridge/Webhook.ts | 34 +++++++++----- 3 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 .changeset/tidy-webhook-verification.md diff --git a/.changeset/tidy-webhook-verification.md b/.changeset/tidy-webhook-verification.md new file mode 100644 index 00000000000..8bd354b6b94 --- /dev/null +++ b/.changeset/tidy-webhook-verification.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Reject malformed webhook timestamps and signatures, and verify webhook HMACs with Web Crypto instead of comparing signature strings. diff --git a/packages/thirdweb/src/bridge/Webhook.test.ts b/packages/thirdweb/src/bridge/Webhook.test.ts index ef1349444ec..db85c3b9613 100644 --- a/packages/thirdweb/src/bridge/Webhook.test.ts +++ b/packages/thirdweb/src/bridge/Webhook.test.ts @@ -90,6 +90,54 @@ describe("parseIncomingWebhook", () => { expect(result).toEqual(validWebhook); }); + it.each([ + "NaN", + "abc", + "Infinity", + `${testTimestamp}trailing`, + `${testTimestamp}\n`, + `${testTimestamp}.5`, + `+${testTimestamp}`, + ` ${testTimestamp}`, + "9007199254740992", + ])( + "rejects malformed timestamp %s even with a valid HMAC", + async (timestamp) => { + const payload = JSON.stringify(validPayload); + const signature = await generateSignature(timestamp, payload); + + await expect( + parse( + payload, + { + "x-payload-signature": signature, + "x-timestamp": timestamp, + }, + secret, + ), + ).rejects.toThrow("Invalid webhook timestamp"); + }, + ); + + it.each([ + "0".repeat(64), + "ff".repeat(32), + "g0".repeat(32), + "0".repeat(63), + "0".repeat(65), + ])("rejects invalid or malformed signature %s", async (signature) => { + await expect( + parse( + JSON.stringify(validPayload), + { + "x-payload-signature": signature, + "x-timestamp": testTimestamp, + }, + secret, + ), + ).rejects.toThrow("Invalid webhook signature"); + }); + it("should accept alternative header names", async () => { const signature = await generateSignature( testTimestamp, diff --git a/packages/thirdweb/src/bridge/Webhook.ts b/packages/thirdweb/src/bridge/Webhook.ts index 224762008aa..45f3f8f1053 100644 --- a/packages/thirdweb/src/bridge/Webhook.ts +++ b/packages/thirdweb/src/bridge/Webhook.ts @@ -105,7 +105,8 @@ export async function parse( payload: string, /** - * The webhook headers received from thirdweb. + * The webhook headers received from thirdweb. The timestamp must contain + * decimal Unix seconds and the HMAC signature must be lowercase hex. */ headers: Record, @@ -153,7 +154,10 @@ export async function parse( // Verify timestamp const now = Math.floor(Date.now() / 1000); - const timestamp = Number.parseInt(receivedTimestamp, 10); + const timestamp = Number(receivedTimestamp); + if (/[^0-9]/.test(receivedTimestamp) || !Number.isSafeInteger(timestamp)) { + throw new Error("Invalid webhook timestamp"); + } const diff = Math.abs(now - timestamp); if (diff > tolerance) { @@ -162,29 +166,35 @@ export async function parse( ); } - // Generate signature using the same method as the sender + if (receivedSignature.length !== 64 || /[^0-9a-f]/.test(receivedSignature)) { + throw new Error("Invalid webhook signature"); + } + const encoder = new TextEncoder(); const key = await crypto.subtle.importKey( "raw", encoder.encode(secret), { hash: "SHA-256", name: "HMAC" }, false, - ["sign"], + ["verify"], ); - const signature = await crypto.subtle.sign( + const signature = new Uint8Array(32); + for (let i = 0; i < signature.length; i++) { + signature[i] = Number.parseInt( + receivedSignature.slice(i * 2, i * 2 + 2), + 16, + ); + } + + const validSignature = await crypto.subtle.verify( "HMAC", key, + signature, encoder.encode(`${receivedTimestamp}.${payload}`), ); - // Convert the signature to hex string - const computedSignature = Array.from(new Uint8Array(signature)) - .map((b) => b.toString(16).padStart(2, "0")) - .join(""); - - // Compare signatures - if (computedSignature !== receivedSignature) { + if (!validSignature) { throw new Error("Invalid webhook signature"); }