feat(core): add webhook signature verification helpers (resolves #5101)#5106
Open
eslam-reda-div wants to merge 4 commits into
Open
feat(core): add webhook signature verification helpers (resolves #5101)#5106eslam-reda-div wants to merge 4 commits into
eslam-reda-div wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Adds first-class webhook signature verification utilities to @adonisjs/core/helpers, covering both the Standard Webhooks spec (v1 HMAC + v1a ed25519) and a configurable HMAC verifier for provider-specific schemes.
Changes:
- Introduces
createStandardWebhookVerifier(Standard Webhooks: required headers + tolerance checks + v1/v1a verification). - Introduces
createWebhookVerifierfor configurable/custom HMAC verification (custom header parsing + signed payload builder). - Adds tests for Standard Webhooks (v1 HMAC) + missing headers + tolerance, and a basic custom HMAC verifier test.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/helpers/webhooks.ts |
Implements Standard Webhooks + configurable webhook signature verification helpers and error type. |
src/helpers/main.ts |
Re-exports the new webhook helpers from the public helpers entrypoint. |
tests/webhooks.spec.ts |
Adds tests for Standard Webhooks HMAC flow, tolerance/missing-header failures, and a custom HMAC verifier. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+15
to
+37
| test('verify Standard Webhooks signatures', ({ assert }) => { | ||
| const rawSecret = 'super-secret' | ||
| const secret = `whsec_${Buffer.from(rawSecret).toString('base64')}` | ||
| const webhookId = 'msg_2KWPBgLlAfxdpx2AI54pPJ85f4W' | ||
| const timestamp = 1700000000 | ||
| const payload = JSON.stringify({ type: 'user.created' }) | ||
|
|
||
| const signedPayload = `${webhookId}.${timestamp}.${payload}` | ||
| const signature = createHmac('sha256', Buffer.from(rawSecret)) | ||
| .update(signedPayload) | ||
| .digest('base64') | ||
|
|
||
| const verifier = createStandardWebhookVerifier(secret, { now: () => timestamp }) | ||
| const result = verifier.verify(payload, { | ||
| 'webhook-id': webhookId, | ||
| 'webhook-timestamp': String(timestamp), | ||
| 'webhook-signature': `v1,${signature}`, | ||
| }) | ||
|
|
||
| assert.isTrue(result.isValid) | ||
| assert.equal(result.webhookId, webhookId) | ||
| assert.equal(result.timestamp, timestamp) | ||
| }) |
Comment on lines
+440
to
+445
| const rawPublicKey = Buffer.from(secretValue.slice(STANDARD_PUBLIC_PREFIX.length), 'base64') | ||
| const spkiKey = Buffer.concat([ED25519_SPKI_PREFIX, rawPublicKey]) | ||
| return { | ||
| type: 'ed25519', | ||
| key: createPublicKey({ key: spkiKey, format: 'der', type: 'spki' }), | ||
| } |
Comment on lines
+439
to
+452
| if (format !== 'raw' && secretValue.startsWith(STANDARD_PUBLIC_PREFIX)) { | ||
| const rawPublicKey = Buffer.from(secretValue.slice(STANDARD_PUBLIC_PREFIX.length), 'base64') | ||
| try { | ||
| const spkiKey = Buffer.concat([ED25519_SPKI_PREFIX, rawPublicKey]) | ||
| return { | ||
| type: 'ed25519', | ||
| key: createPublicKey({ key: spkiKey, format: 'der', type: 'spki' }), | ||
| } | ||
| } catch { | ||
| throw new Error( | ||
| 'Invalid Standard Webhooks public key. Expected whpk_ base64 encoded ed25519 key.' | ||
| ) | ||
| } | ||
| } |
Comment on lines
+228
to
+238
| const verifyOrThrow = ( | ||
| payload: WebhookPayload, | ||
| headers: WebhookHeaders | ||
| ): WebhookVerificationSuccess => { | ||
| const result = verify(payload, headers) | ||
| if (!result.isValid) { | ||
| throw new WebhookVerificationError(result.reason!, WEBHOOK_ERROR_MESSAGES[result.reason!]) | ||
| } | ||
|
|
||
| return result as WebhookVerificationSuccess | ||
| } |
| ): WebhookVerificationSuccess => { | ||
| const result = verify(payload, headers) | ||
| if (!result.isValid) { | ||
| throw new WebhookVerificationError(result.reason!, WEBHOOK_ERROR_MESSAGES[result.reason!]) |
Comment on lines
+212
to
+217
| const signedPayload = options.buildSignedPayload({ | ||
| payload, | ||
| headers: normalizedHeaders, | ||
| webhookId, | ||
| timestamp, | ||
| }) |
Comment on lines
+445
to
+449
| const stripped = secretValue.startsWith(STANDARD_SECRET_PREFIX) | ||
| ? secretValue.slice(STANDARD_SECRET_PREFIX.length) | ||
| : secretValue | ||
| return { type: 'hmac', key: Buffer.from(stripped, 'base64') } | ||
| }) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Resolves #5101
What
Why
How
createStandardWebhookVerifierimplements the Standard Webhooks signature rules and tolerance checks.createWebhookVerifierallows custom header parsing and payload signing for non-standard providers.WebhookVerificationErrorfor ergonomic error handling.Testing
npm testNotes
whsec_andwhpk_prefixes;format: 'raw'is supported for raw secrets.