-
Notifications
You must be signed in to change notification settings - Fork 60
[High] fix(stripe): enforce 5-minute timestamp tolerance to prevent webhook replay attacks #711
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,6 +184,21 @@ | |
| throw new Error('Stripe-Signature missing t or v1'); | ||
| } | ||
|
|
||
| // Reject replayed webhooks per Stripe's guidance: | ||
| // https://docs.stripe.com/webhooks/signatures#replay-prevention | ||
| const TOLERANCE_SECONDS = 300; // 5 minutes — matches Stripe's own SDK default | ||
| const ts = Number(timestamp); | ||
| if (!Number.isFinite(ts)) { | ||
| throw new Error('Stripe-Signature t is not numeric'); | ||
| } | ||
| const skew = Math.abs(Math.floor(Date.now() / 1000) - ts); | ||
| if (skew > TOLERANCE_SECONDS) { | ||
| throw new Error( | ||
|
Check failure on line 196 in packages/payments/stripe/src/index.ts
|
||
| `Stripe webhook timestamp ${ts} is outside the ${TOLERANCE_SECONDS}s tolerance (skew: ${skew}s). ` + | ||
| 'Reject to prevent replay attacks.', | ||
| ); | ||
| } | ||
|
Comment on lines
+187
to
+200
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The test file uses
Neither test is updated in this PR. Both need to use |
||
|
|
||
| const expected = createHmac('sha256', secret) | ||
| .update(`${timestamp}.${rawBody}`) | ||
| .digest('hex'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TOLERANCE_SECONDSis not configurable, making integration tests awkwardTOLERANCE_SECONDSis hardcoded inside the function with no way to override it. Stripe's ownstripe-nodeSDK exposes this as a parameter (defaulting to 300 s). Without an override path, test authors must either pinDate.nowvia fake timers or generate signatures at runtime — both add friction. Consider accepting an optionaltoleranceparameter inverifyStripeSignatureso callers can passInfinityor a wider window in test environments.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!