Skip to content

fix(appsec): wrap shared Stripe webhooks methods only once - #10932

Closed
israelALF wants to merge 3 commits into
DataDog:masterfrom
israelALF:israel.lopez/fix-stripe-webhooks-rewrap
Closed

israelALF wants to merge 3 commits into
DataDog:masterfrom
israelALF:israel.lopez/fix-stripe-webhooks-rewrap

Conversation

@israelALF

@israelALF israelALF commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Makes the Stripe instrumentation wrap each method once. instrumentStripeInstance runs on every new Stripe() and used to call shimmer.wrap on webhooks.constructEvent and webhooks.constructEventAsync each time. In stripe 9, 10, early 11.x and 18+, webhooks is a single object shared by every client:

  • stripe 9: this.webhooks = require('./Webhooks')
  • stripe 21: Stripe.webhooks = createWebhooks(...) once, then this.webhooks = Stripe.webhooks per instance
  • stripe 23: assigned in Stripe.initialize(), then this.webhooks = Stripe.webhooks per instance

So every client added one more wrapper around the same function. The fix keeps a WeakSet of the wrappers we install and skips a method that is already one of them. It marks the functions rather than the webhooks object, so a method the user replaces later (for example a stub) still gets wrapped exactly once.

Motivation

A process that creates a Stripe client per request (per account or per API key) eventually fails webhook signature verification:

RangeError: Maximum call stack size exceeded
    at wrappedConstructEvent (packages/datadog-instrumentations/src/stripe.js:38)
    at wrappedConstructEvent (packages/datadog-instrumentations/src/stripe.js:38)
    ... one frame per client created

Reproduced with the default config (--require dd-trace/init, AppSec off) on dd-trace 5.105.0, 5.132.0 and 6.21.0 with stripe 9.16.0, 21.0.1 and 23.0.1. It fails at about 8,870 clients on Node 24, for both constructEvent and constructEventAsync. At that depth constructEventAsync throws synchronously instead of returning a rejected promise.

The bug costs something before the overflow too. With AppSec enabled, each webhook published datadog:stripe:constructEvent:finish once per client ever created, so the WAF ran N times for the same payment event. Verification latency also grows linearly with the number of clients.

checkout.sessions and paymentIntents are per-instance objects, so they never stacked. They now go through the same helper for consistency. Later 11.x releases and stripe 12–17 create a per-instance webhooks and were not affected.

Additional Notes

🤖 Generated with Claude Code

`stripe.webhooks` is a single object shared by every Stripe client (stripe
9, 10, early 11.x and 18+), but the instrumentation wrapped `constructEvent` and
`constructEventAsync` again on every `new Stripe()`. Each client added one
wrapper layer, so processes that create clients per request eventually threw
`RangeError: Maximum call stack size exceeded` on webhook verification, and
with AppSec enabled every webhook published once per client created.

Track the wrappers we install and skip methods that are already wrapped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@datadog-prod-us1-6

datadog-prod-us1-6 Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Pipelines

❌ Errors

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 4 Pipeline jobs failed

All Green | all-green

View more details · View in GitHub Actions

CodeQL | Analyze (javascript)

View more details · View in GitHub Actions

Project | package-size-report

View more details · View in GitHub Actions

View all 4 failed jobs.

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 3fd7d41 | Docs | View more details | Give us feedback!

@BridgeAR
BridgeAR marked this pull request as ready for review October 27, 2026 18:32
@BridgeAR
BridgeAR requested a review from a team as a code owner October 27, 2026 18:32
@BridgeAR
BridgeAR requested review from tlhunter and removed request for a team October 27, 2026 18:32
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T18:49:31.348351Z db5b8d5 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@BridgeAR BridgeAR left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change LGTM (the test should be more resilient, while that can land afterwards)

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Isolated checks using the actual instrumentation and Shimmer support retaining one wrapper per shared Stripe method across repeated client creation. The full Stripe suite remains unverified because dependencies are absent.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit ebec24d

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: db5b8d5eb3

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


// `webhooks` is shared by every Stripe instance, so wrapping it on each
// construction would stack one wrapper per client until the stack overflows
function wrapOnce (target, name, wrapper) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Type the new wrapOnce parameters

wrapOnce is a newly introduced production helper with three parameters but no TypeScript-compatible JSDoc types. Add concrete parameter types for the target object, property name, and wrapper so the helper's contract remains statically checkable (without adding a primitive @returns tag).

AGENTS.md reference: AGENTS.md:L105-L108

Useful? React with 👍 / 👎.

@israelALF

Copy link
Copy Markdown
Contributor Author

@BridgeAR thanks for the approval! I added the JSDoc types Codex asked for on wrapOnce (3fd7d41). The remaining CI failures (all-green, Analyze, package-size-report) come from dd-octo-sts-action not getting id-token: write because this PR is from a fork, and I don't have push access to the main repo. Could you push the branch to DataDog/dd-trace-js (or re-run CI from there) and merge if it's green? Thanks!

@ratomirjovanovic ratomirjovanovic left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked this against stripe-node across the hooked range. Sharing of webhooks differs by major: 9 to 11 assign the module singleton (this.webhooks = require('./Webhooks')), 12 to 17 build a fresh object per client (this.webhooks = createWebhooks(platformFunctions), and 17 uses createWebhooksDefault()), and 18+ reuse the static Stripe.webhooks created in initialize(). Keying the guard on the function value rather than on the target is the right call because of that: per-client objects in 12 to 17 still get their own wrapper, and the shared ones only get one. Two small points inline, neither blocking.


const wrappedMethods = new WeakSet()

// `webhooks` is shared by every Stripe instance, so wrapping it on each

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this is true for stripe 9 to 11 and 18+, but 12 to 17 create a new webhooks object per client, so nothing stacked there. Maybe "webhooks is shared by every Stripe instance in most versions" so nobody later moves this to a one-time wrap on Stripe.webhooks at hook time, which would miss 12 to 17 (there Stripe.webhooks is the createWebhooks factory, not the object).


it('does not stack wrappers when many clients are created', async () => {
let stripe
for (let i = 0; i < 20_000; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The publishes assertions already catch the regression with two clients: two stacked wrappers publish twice. 20k constructions only reproduce the stack overflow, and each one instantiates every resource class, for each version withVersions runs. Something like new Stripe(...) twice plus assert.equal(publishes, 1) keeps the coverage and avoids the timing risk. On the shared-object versions you could also assert a.webhooks.constructEvent === b.webhooks.constructEvent.

@BridgeAR

BridgeAR commented Nov 1, 2026

Copy link
Copy Markdown
Member

Closing as superseded

@BridgeAR BridgeAR closed this Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants