Skip to content

Remove attempt to require "node:crypto" on Node 18 #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2025
Merged
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ const requestData = {
const webhookIsValid = await validateWebhook(requestData);
```

> [!NOTE]
> The `validateWebhook` function uses the global `crypto` API available in most JavaScript runtimes. Node <= 18 does not provide this global so in this case you need to either call node with the `--no-experimental-global-webcrypto` or provide the `webcrypto` module manually.
> ```js
> const crypto = require("node:crypto").webcrypto;
> const webhookIsValid = await valdiateWebhook(requestData, crypto);
> ```

## TypeScript

The `Replicate` constructor and all `replicate.*` methods are fully typed.
Expand Down
24 changes: 14 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,16 +340,20 @@ declare module "replicate" {
}

export function validateWebhook(
requestData:
| Request
| {
id?: string;
timestamp?: string;
body: string;
secret?: string;
signature?: string;
},
secret: string
request: Request,
secret: string,
crypto?: Crypto
): Promise<boolean>;

export function validateWebhook(
requestData: {
id: string;
timestamp: string;
signature: string;
body: string;
secret: string;
},
crypto?: Crypto
): Promise<boolean>;

export function parseProgressFromLogs(logs: Prediction | string): {
Expand Down
36 changes: 34 additions & 2 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Replicate, {
} from "replicate";
import nock from "nock";
import { createReadableStream } from "./lib/stream";
import { webcrypto } from "node:crypto";

let client: Replicate;
const BASE_URL = "https://api.replicate.com/v1";
Expand Down Expand Up @@ -1779,9 +1780,40 @@ describe("Replicate client", () => {

// This is a test secret and should not be used in production
const secret = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw";
if (globalThis.crypto) {
const isValid = await validateWebhook(request, secret);
expect(isValid).toBe(true);
} else {
const isValid = await validateWebhook(
request,
secret,
webcrypto as Crypto // Node 18 requires this to be passed manually
);
expect(isValid).toBe(true);
}
});

const isValid = await validateWebhook(request, secret);
expect(isValid).toBe(true);
test("Can be used to validate webhook", async () => {
// Test case from https://github.com/svix/svix-webhooks/blob/b41728cd98a7e7004a6407a623f43977b82fcba4/javascript/src/webhook.test.ts#L190-L200
const requestData = {
id: "msg_p5jXN8AQM9LWM0D4loKWxJek",
timestamp: "1614265330",
signature: "v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=",
body: `{"test": 2432232314}`,
secret: "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw",
};

// This is a test secret and should not be used in production
if (globalThis.crypto) {
const isValid = await validateWebhook(requestData);
expect(isValid).toBe(true);
} else {
const isValid = await validateWebhook(
requestData,
webcrypto as Crypto // Node 18 requires this to be passed manually
);
expect(isValid).toBe(true);
}
});

// Add more tests for error handling, edge cases, etc.
Expand Down
65 changes: 40 additions & 25 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { create: createFile } = require("./files");
* @param {string} requestData.body - The raw body of the incoming webhook request.
* @param {string} requestData.secret - The webhook secret, obtained from `replicate.webhooks.defaul.secret` method.
* @param {string} requestData.signature - The webhook signature header from the incoming request, comprising one or more space-delimited signatures.
* @param {Crypto} [crypto] - An optional `Crypto` implementation that conforms to the [browser Crypto interface](https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto)
*/

/**
Expand All @@ -22,6 +23,7 @@ const { create: createFile } = require("./files");
* @param {string} requestData.headers["webhook-signature"] - The webhook signature header from the incoming request, comprising one or more space-delimited signatures
* @param {string} requestData.body - The raw body of the incoming webhook request
* @param {string} secret - The webhook secret, obtained from `replicate.webhooks.defaul.secret` method
* @param {Crypto} [crypto] - An optional `Crypto` implementation that conforms to the [browser Crypto interface](https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto)
*/

/**
Expand All @@ -30,9 +32,13 @@ const { create: createFile } = require("./files");
* @returns {Promise<boolean>} - True if the signature is valid
* @throws {Error} - If the request is missing required headers, body, or secret
*/
async function validateWebhook(requestData, secret) {
let { id, timestamp, body, signature } = requestData;
const signingSecret = secret || requestData.secret;
async function validateWebhook(requestData, secretOrCrypto, customCrypto) {
let id;
let body;
let timestamp;
let signature;
let secret;
let crypto = globalThis.crypto;

if (requestData && requestData.headers && requestData.body) {
if (typeof requestData.headers.get === "function") {
Expand All @@ -47,6 +53,25 @@ async function validateWebhook(requestData, secret) {
signature = requestData.headers["webhook-signature"];
}
body = requestData.body;
if (typeof secretOrCrypto !== "string") {
throw new Error(
"Unexpected value for secret passed to validateWebhook, expected a string"
);
}

secret = secretOrCrypto;
if (customCrypto) {
crypto = customCrypto;
}
} else {
id = requestData.id;
body = requestData.body;
timestamp = requestData.timestamp;
signature = requestData.signature;
secret = requestData.secret;
if (secretOrCrypto) {
crypto = secretOrCrypto;
}
}

if (body instanceof ReadableStream || body.readable) {
Expand All @@ -71,15 +96,22 @@ async function validateWebhook(requestData, secret) {
throw new Error("Missing required body");
}

if (!signingSecret) {
if (!secret) {
throw new Error("Missing required secret");
}

if (!crypto) {
throw new Error(
'Missing `crypto` implementation. If using Node 18 pass in require("node:crypto").webcrypto'
);
}

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

const computedSignature = await createHMACSHA256(
signingSecret.split("_").pop(),
signedContent
secret.split("_").pop(),
signedContent,
crypto
);

const expectedSignatures = signature
Expand All @@ -94,27 +126,10 @@ async function validateWebhook(requestData, secret) {
/**
* @param {string} secret - base64 encoded string
* @param {string} data - text body of request
* @param {Crypto} crypto - an implementation of the web Crypto api
*/
async function createHMACSHA256(secret, data) {
async function createHMACSHA256(secret, data, crypto) {
const encoder = new TextEncoder();
let crypto = globalThis.crypto;

// In Node 18 the `crypto` global is behind a --no-experimental-global-webcrypto flag
if (typeof crypto === "undefined" && typeof require === "function") {
// NOTE: Webpack (primarily as it's used by Next.js) and perhaps some
// other bundlers do not currently support the `node` protocol and will
// error if it's found in the source. Other platforms like CloudFlare
// will only support requires when using the node protocol.
//
// As this line is purely to support Node 18.x we make an indirect request
// to the require function which fools Webpack...
//
// We may be able to remove this in future as it looks like Webpack is getting
// support for requiring using the `node:` protocol.
// See: https://github.com/webpack/webpack/issues/18277
crypto = require.call(null, "node:crypto").webcrypto;
}

const key = await crypto.subtle.importKey(
"raw",
base64ToBytes(secret),
Expand Down