Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/four-stingrays-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/express": patch
---

Make webhook verification work with both raw and parsed request bodies
20 changes: 17 additions & 3 deletions packages/express/src/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ export * from '@clerk/backend/webhooks';
* @example
* ```typescript
* import { verifyWebhook } from '@clerk/express/webhooks';
* import express from 'express';
*
* app.post('/api/webhooks', express.raw({ type: 'application/json' }), async (req, res) => {
* app.post('/api/webhooks', async (req, res) => {
* try {
* const evt = await verifyWebhook(req);
* // handle event
Expand All @@ -40,8 +39,23 @@ export async function verifyWebhook(req: ExpressRequest, options?: VerifyWebhook
const webRequest = incomingMessageToRequest(req);
// Cloning instead of implementing the body inside incomingMessageToRequest
// to make it more predictable
// we must pass in body as string not as an Object or Buffer
let serializedBody: string;
if (typeof req.body === 'string') {
serializedBody = req.body;
} else if (Buffer.isBuffer(req.body)) {
serializedBody = req.body.toString('utf8');
} else if (req.body === undefined || req.body === null) {
serializedBody = '';
} else {
try {
serializedBody = JSON.stringify(req.body);
} catch (error) {
throw new Error(`Failed to serialize request body: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
const clonedRequest = new Request(webRequest, {
body: req.body,
body: serializedBody,
});
return verifyWebhookBase(clonedRequest, options);
}