Skip to content

Commit 79ebc57

Browse files
authored
Handle Fetch Headers and subscriptable JS object separately (#300)
1 parent 0cf57c4 commit 79ebc57

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

lib/util.js

+39-19
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ async function validateWebhook(requestData, secret) {
3535
const signingSecret = secret || requestData.secret;
3636

3737
if (requestData && requestData.headers && requestData.body) {
38-
id =
39-
requestData.headers["webhook-id"] ||
40-
requestData.headers.get?.("webhook-id");
41-
timestamp =
42-
requestData.headers["webhook-timestamp"] ||
43-
requestData.headers.get?.("webhook-timestamp");
44-
signature =
45-
requestData.headers["webhook-signature"] ||
46-
requestData.headers.get?.("webhook-signature");
38+
if (typeof requestData.headers.get === "function") {
39+
// Headers object (e.g. Fetch API Headers)
40+
id = requestData.headers.get("webhook-id");
41+
timestamp = requestData.headers.get("webhook-timestamp");
42+
signature = requestData.headers.get("webhook-signature");
43+
} else {
44+
// Plain object with header key-value pairs
45+
id = requestData.headers["webhook-id"];
46+
timestamp = requestData.headers["webhook-timestamp"];
47+
signature = requestData.headers["webhook-signature"];
48+
}
4749
body = requestData.body;
4850
}
4951

@@ -75,11 +77,18 @@ async function validateWebhook(requestData, secret) {
7577

7678
const signedContent = `${id}.${timestamp}.${body}`;
7779

78-
const computedSignature = await createHMACSHA256(signingSecret.split("_").pop(), signedContent);
80+
const computedSignature = await createHMACSHA256(
81+
signingSecret.split("_").pop(),
82+
signedContent
83+
);
7984

80-
const expectedSignatures = signature.split(" ").map((sig) => sig.split(",")[1]);
85+
const expectedSignatures = signature
86+
.split(" ")
87+
.map((sig) => sig.split(",")[1]);
8188

82-
return expectedSignatures.some((expectedSignature) => expectedSignature === computedSignature);
89+
return expectedSignatures.some(
90+
(expectedSignature) => expectedSignature === computedSignature
91+
);
8392
}
8493

8594
/**
@@ -106,9 +115,13 @@ async function createHMACSHA256(secret, data) {
106115
crypto = require.call(null, "node:crypto").webcrypto;
107116
}
108117

109-
const key = await crypto.subtle.importKey("raw", base64ToBytes(secret), { name: "HMAC", hash: "SHA-256" }, false, [
110-
"sign",
111-
]);
118+
const key = await crypto.subtle.importKey(
119+
"raw",
120+
base64ToBytes(secret),
121+
{ name: "HMAC", hash: "SHA-256" },
122+
false,
123+
["sign"]
124+
);
112125

113126
const signature = await crypto.subtle.sign("HMAC", key, encoder.encode(data));
114127
return bytesToBase64(signature);
@@ -232,7 +245,11 @@ async function transformFileInputs(client, inputs, strategy) {
232245
try {
233246
return await transformFileInputsToReplicateFileURLs(client, inputs);
234247
} catch (error) {
235-
if (error instanceof ApiError && error.response.status >= 400 && error.response.status < 500) {
248+
if (
249+
error instanceof ApiError &&
250+
error.response.status >= 400 &&
251+
error.response.status < 500
252+
) {
236253
throw error;
237254
}
238255
return await transformFileInputsToBase64EncodedDataURIs(inputs);
@@ -296,7 +313,7 @@ async function transformFileInputsToBase64EncodedDataURIs(inputs) {
296313
totalBytes += buffer.byteLength;
297314
if (totalBytes > MAX_DATA_URI_SIZE) {
298315
throw new Error(
299-
`Combined filesize of prediction ${totalBytes} bytes exceeds 10mb limit for inline encoding, please provide URLs instead`,
316+
`Combined filesize of prediction ${totalBytes} bytes exceeds 10mb limit for inline encoding, please provide URLs instead`
300317
);
301318
}
302319

@@ -354,11 +371,14 @@ function isPlainObject(value) {
354371
if (proto === null) {
355372
return true;
356373
}
357-
const Ctor = Object.prototype.hasOwnProperty.call(proto, "constructor") && proto.constructor;
374+
const Ctor =
375+
Object.prototype.hasOwnProperty.call(proto, "constructor") &&
376+
proto.constructor;
358377
return (
359378
typeof Ctor === "function" &&
360379
Ctor instanceof Ctor &&
361-
Function.prototype.toString.call(Ctor) === Function.prototype.toString.call(Object)
380+
Function.prototype.toString.call(Ctor) ===
381+
Function.prototype.toString.call(Object)
362382
);
363383
}
364384

0 commit comments

Comments
 (0)