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
5 changes: 5 additions & 0 deletions .changeset/tidy-webhook-verification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Reject malformed webhook timestamps and signatures, and verify webhook HMACs with Web Crypto instead of comparing signature strings.
48 changes: 48 additions & 0 deletions packages/thirdweb/src/bridge/Webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 22 additions & 12 deletions packages/thirdweb/src/bridge/Webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>,

Expand Down Expand Up @@ -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) {
Expand All @@ -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");
}

Expand Down
Loading