-
Notifications
You must be signed in to change notification settings - Fork 422
chore(backend): Refactor verifyWebhook() to use standardwebhooks library
#6252
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/backend': patch | ||
| --- | ||
|
|
||
| Refactor webhook verification to use verification from the `standardwebhooks` package, which is what our underlying provider relies on. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,45 @@ | ||
| import { Webhook } from 'standardwebhooks'; | ||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| import { verifyWebhook } from '../webhooks'; | ||
|
|
||
| describe('verifyWebhook', () => { | ||
| const mockSecret = 'test_signing_secret'; | ||
| const mockSecret = 'whsec_' + Buffer.from('test_signing_secret_32_chars_long').toString('base64'); | ||
| const mockBody = JSON.stringify({ type: 'user.created', data: { id: 'user_123' } }); | ||
|
|
||
| beforeEach(() => { | ||
| process.env.CLERK_WEBHOOK_SIGNING_SECRET = mockSecret; | ||
| }); | ||
|
|
||
| // Helper function to create a valid signature with Standard Webhooks | ||
| const createValidSignature = (id: string, timestamp: string, body: string) => { | ||
| const webhook = new Webhook(mockSecret); | ||
| // Create a signature using the Standard Webhooks library | ||
| return webhook.sign(id, new Date(parseInt(timestamp) * 1000), body); | ||
| }; | ||
|
|
||
| it('throws when required headers are missing', async () => { | ||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: mockBody, | ||
| headers: new Headers({ | ||
| // Missing svix-signature but with valid format for others | ||
| 'svix-id': 'msg_123', | ||
| 'svix-timestamp': '1614265330', | ||
| // Missing all required headers | ||
| }), | ||
| }); | ||
|
|
||
| await expect(verifyWebhook(mockRequest)).rejects.toThrow('Missing required Svix headers: svix-signature'); | ||
| await expect(verifyWebhook(mockRequest)).rejects.toThrow('Missing required webhook headers'); | ||
| }); | ||
|
|
||
| it('throws with all missing headers in error message', async () => { | ||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: mockBody, | ||
| headers: new Headers({}), | ||
| headers: new Headers({ | ||
| // Missing all required headers | ||
| }), | ||
| }); | ||
|
|
||
| await expect(verifyWebhook(mockRequest)).rejects.toThrow( | ||
| 'Missing required Svix headers: svix-id, svix-timestamp, svix-signature', | ||
| ); | ||
| await expect(verifyWebhook(mockRequest)).rejects.toThrow('svix-id, svix-timestamp, svix-signature'); | ||
| }); | ||
|
|
||
| it('throws when signing secret is missing', async () => { | ||
|
|
@@ -44,24 +50,26 @@ describe('verifyWebhook', () => { | |
| body: mockBody, | ||
| headers: new Headers({ | ||
| 'svix-id': 'msg_123', | ||
| 'svix-timestamp': '1614265330', | ||
| 'svix-timestamp': (Date.now() / 1000).toString(), | ||
| 'svix-signature': 'v1,test_signature', | ||
| }), | ||
| }); | ||
|
|
||
| await expect(verifyWebhook(mockRequest)).rejects.toThrow( | ||
| 'Missing webhook signing secret. Set the CLERK_WEBHOOK_SIGNING_SECRET environment variable with the webhook secret from the Clerk Dashboard.', | ||
| ); | ||
| await expect(verifyWebhook(mockRequest)).rejects.toThrow('Missing webhook signing secret'); | ||
| }); | ||
|
|
||
| it('validates webhook request requirements', async () => { | ||
| const svixId = 'msg_123'; | ||
| const svixTimestamp = (Date.now() / 1000).toString(); | ||
| const validSignature = createValidSignature(svixId, svixTimestamp, mockBody); | ||
|
|
||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: mockBody, | ||
| headers: new Headers({ | ||
| 'svix-id': 'msg_123', | ||
| 'svix-timestamp': '1614265330', | ||
| 'svix-signature': 'v1,test_signature', | ||
| 'svix-id': svixId, | ||
| 'svix-timestamp': svixTimestamp, | ||
| 'svix-signature': validSignature, | ||
| }), | ||
| }); | ||
|
|
||
|
|
@@ -72,4 +80,111 @@ describe('verifyWebhook', () => { | |
| expect(result).toHaveProperty('type', 'user.created'); | ||
| expect(result).toHaveProperty('data.id', 'user_123'); | ||
| }); | ||
|
|
||
| it('should accept valid signatures', async () => { | ||
| const svixId = 'msg_123'; | ||
| const svixTimestamp = (Date.now() / 1000).toString(); | ||
| const validSignature = createValidSignature(svixId, svixTimestamp, mockBody); | ||
|
|
||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: mockBody, | ||
| headers: new Headers({ | ||
| 'svix-id': svixId, | ||
| 'svix-timestamp': svixTimestamp, | ||
| 'svix-signature': validSignature, | ||
| }), | ||
| }); | ||
|
|
||
| // Should accept and return parsed data | ||
| const result = await verifyWebhook(mockRequest); | ||
| expect(result).toHaveProperty('type', 'user.created'); | ||
| expect(result).toHaveProperty('data.id', 'user_123'); | ||
| }); | ||
|
|
||
| it('should reject invalid signatures', async () => { | ||
| const svixId = 'msg_123'; | ||
| const svixTimestamp = (Date.now() / 1000).toString(); | ||
| const invalidSignature = 'v1,invalid_signature_here'; | ||
|
|
||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: mockBody, | ||
| headers: new Headers({ | ||
| 'svix-id': svixId, | ||
| 'svix-timestamp': svixTimestamp, | ||
| 'svix-signature': invalidSignature, | ||
| }), | ||
| }); | ||
|
|
||
| // Should reject invalid signatures | ||
| await expect(verifyWebhook(mockRequest)).rejects.toThrow('No matching signature found'); | ||
| }); | ||
|
|
||
| it('should handle multiple signatures in header', async () => { | ||
| const svixId = 'msg_123'; | ||
| const svixTimestamp = (Date.now() / 1000).toString(); | ||
| const validSignature = createValidSignature(svixId, svixTimestamp, mockBody); | ||
| const invalidSignature = 'v1,invalid_signature'; | ||
|
|
||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: mockBody, | ||
| headers: new Headers({ | ||
| 'svix-id': svixId, | ||
| 'svix-timestamp': svixTimestamp, | ||
| 'svix-signature': `${invalidSignature} ${validSignature}`, | ||
| }), | ||
| }); | ||
|
|
||
| // Should accept if any signature in the list is valid | ||
| const result = await verifyWebhook(mockRequest); | ||
| expect(result).toHaveProperty('type', 'user.created'); | ||
| expect(result).toHaveProperty('data.id', 'user_123'); | ||
| }); | ||
|
|
||
| it('should handle signatures without version prefixes for backward compatibility', async () => { | ||
| const svixId = 'msg_123'; | ||
| const svixTimestamp = (Date.now() / 1000).toString(); | ||
| // Test with Standard Webhooks generated signature without custom prefix | ||
| const validSignature = createValidSignature(svixId, svixTimestamp, mockBody); | ||
|
|
||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: mockBody, | ||
| headers: new Headers({ | ||
| 'svix-id': svixId, | ||
| 'svix-timestamp': svixTimestamp, | ||
| 'svix-signature': validSignature, | ||
| }), | ||
| }); | ||
|
|
||
| // Should accept signatures without version prefixes | ||
| const result = await verifyWebhook(mockRequest); | ||
| expect(result).toHaveProperty('type', 'user.created'); | ||
| expect(result).toHaveProperty('data.id', 'user_123'); | ||
| }); | ||
|
|
||
| it('should verify against Standard Webhooks specification', async () => { | ||
| // Test with proper Clerk webhook format | ||
| const clerkPayload = '{"type":"user.created","data":{"id":"user_123","email":"[email protected]"}}'; | ||
| const msgId = 'msg_test123'; | ||
| const timestamp = (Date.now() / 1000).toString(); | ||
|
|
||
| const validSignature = createValidSignature(msgId, timestamp, clerkPayload); | ||
|
|
||
| const mockRequest = new Request('https://clerk.com/webhooks', { | ||
| method: 'POST', | ||
| body: clerkPayload, | ||
| headers: new Headers({ | ||
| 'svix-id': msgId, | ||
| 'svix-timestamp': timestamp, | ||
| 'svix-signature': validSignature, | ||
| }), | ||
| }); | ||
|
|
||
| const result = await verifyWebhook(mockRequest, { signingSecret: mockSecret }); | ||
| expect(result).toHaveProperty('type', 'user.created'); | ||
| expect(result).toHaveProperty('data.id', 'user_123'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.