Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions .changeset/hungry-mangos-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@clerk/backend': minor
'@clerk/astro': minor
'@clerk/express': minor
'@clerk/fastify': minor
'@clerk/nextjs': minor
'@clerk/nuxt': minor
'@clerk/react-router': minor
'@clerk/tanstack-react-start': minor
---

- Refactored the `verifyWebhook()` function to manually verify a incoming Clerk webhook request. This change removes the need to install the `svix` package. There are no changes to the usage of the function.

If you installed `svix` for the previous versions of `verifyWebhook()` you can uninstall it.
Comment thread
wobsoriano marked this conversation as resolved.
Outdated

```shell
npm uninstall svix
```
4 changes: 0 additions & 4 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,8 @@
"@edge-runtime/vm": "5.0.0",
"msw": "2.8.7",
"npm-run-all": "^4.1.5",
"svix": "^1.66.0",
"vitest-environment-miniflare": "2.14.4"
},
"peerDependencies": {
"svix": "^1.62.0"
},
"peerDependenciesMeta": {
"svix": {
"optional": true
Comment thread
royanger marked this conversation as resolved.
Outdated
Expand Down
26 changes: 19 additions & 7 deletions packages/backend/src/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getEnvVariable } from '@clerk/shared/getEnvVariable';
import crypto from 'crypto';
import { errorThrower } from 'src/util/shared';
import { Webhook } from 'svix';

import type { WebhookEvent } from './api/resources/Webhooks';

Expand Down Expand Up @@ -69,12 +69,24 @@ export async function verifyWebhook(request: Request, options: VerifyWebhookOpti
return errorThrower.throw(`Missing required Svix headers: ${missingHeaders.join(', ')}`);
}

const sivx = new Webhook(secret);
const body = await request.text();

return sivx.verify(body, {
[SVIX_ID_HEADER]: svixId,
[SVIX_TIMESTAMP_HEADER]: svixTimestamp,
[SVIX_SIGNATURE_HEADER]: svixSignature,
}) as WebhookEvent;
const signedContent = `${svixId}.${svixTimestamp}.${body}`;

const secretBytes = Buffer.from(secret.split('_')[1], 'base64');

const constructedSignature = crypto.createHmac('sha256', secretBytes).update(signedContent).digest('base64');

// svixSignature can be a string with one or more space separated signatures
if (svixSignature.split(' ').includes(constructedSignature)) {
return errorThrower.throw('Incoming webhook does not have a valid signature');
}

const payload = JSON.parse(body);

return {
type: payload.type,
object: 'event',
data: payload.data,
} as WebhookEvent;
}
44 changes: 1 addition & 43 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading