Skip to content

Commit 3824d8d

Browse files
authored
Apply suggestions from code review
1 parent bbe5dfc commit 3824d8d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/thirdweb/src/bridge/Webhook.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,31 @@ export async function parse<T extends Record<string, unknown>>(
2727
tolerance = 300, // Default to 5 minutes if not specified
2828
) {
2929
// Get the signature and timestamp from headers
30+
// Normalize header keys to lowercase for broader compatibility
31+
const lower = Object.fromEntries(
32+
Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v]),
33+
);
3034
const receivedSignature =
31-
headers["x-payload-signature"] || headers["x-pay-signature"];
35+
lower["x-payload-signature"] || lower["x-pay-signature"];
3236
const receivedTimestamp =
33-
headers["x-timestamp"] || headers["x-pay-timestamp"];
34-
37+
lower["x-timestamp"] || lower["x-pay-timestamp"];
3538
if (!receivedSignature || !receivedTimestamp) {
3639
throw new Error("Missing required webhook headers: signature or timestamp");
3740
}
3841

3942
// Verify timestamp
4043
const now = Math.floor(Date.now() / 1000);
4144
const timestamp = Number.parseInt(receivedTimestamp, 10);
45+
if (Number.isNaN(timestamp)) {
46+
throw new Error("Invalid webhook timestamp: must be a Unix epoch (seconds)");
47+
}
4248
const diff = Math.abs(now - timestamp);
4349

4450
if (diff > tolerance) {
4551
throw new Error(
52+
// …
53+
);
54+
}
4655
`Webhook timestamp is too old. Difference: ${diff}s, tolerance: ${tolerance}s`,
4756
);
4857
}

0 commit comments

Comments
 (0)