Skip to content

fix(plugin-ecommerce): use constructEventAsync for Stripe webhook verification (Workers/edge runtimes) - #18125

Closed
LuigiNicaPRO wants to merge 1 commit into
payloadcms:mainfrom
LuigiNicaPRO:fix/plugin-ecommerce-stripe-webhook-constructeventasync
Closed

LuigiNicaPRO wants to merge 1 commit into
payloadcms:mainfrom
LuigiNicaPRO:fix/plugin-ecommerce-stripe-webhook-constructeventasync

Conversation

@LuigiNicaPRO

Copy link
Copy Markdown

What?

One line in the Stripe adapter's webhooks endpoint:

-        event = stripe.webhooks.constructEvent(body, stripeSignature, webhookSecret)
+        event = await stripe.webhooks.constructEventAsync(body, stripeSignature, webhookSecret)

Why?

webhooksEndpoint verifies the Stripe signature with the synchronous
stripe.webhooks.constructEvent(...). On Workers runtimes (workerd) the Stripe SDK selects
SubtleCryptoProvider, whose HMAC is async-only, so the call throws before any handler runs:

Error constructing Stripe event: SubtleCryptoProvider cannot be used in a synchronous context.
Use `await constructEventAsync(...)` instead of `constructEvent(...)`

The catch block sets returnStatus = 400, but the response body is still {"received": true}. So on
Workers every Stripe webhook is rejected while looking superficially healthy: Stripe retries, then
gives up, and no registered webhooks[event.type] handler ever fires. A paid order is never confirmed.

How?

The handler is already async and the call already sits inside the try, so the fix is one line and
a rejected promise still lands in the same 400 branch. No signature, type, or control-flow change.

Measured

Against @payloadcms/[email protected] on a built OpenNext/Cloudflare Worker
(opennextjs-cloudflare build + wrangler dev, local D1), with [email protected] resolved through its
workerd export condition, and a real Stripe test key. A payment_intent.succeeded body was
signed in Node with stripe.webhooks.generateTestHeaderString({ payload, secret }) and the identical
bytes POSTed to /api/payments/stripe/webhooks:

build request result
without this change valid signature HTTP 400 — log: SubtleCryptoProvider cannot be used in a synchronous context. Use await constructEventAsync(...) instead of constructEvent(...)
with this change valid signature HTTP 200
with this change corrupted signature HTTP 400 — log: No signatures found matching the expected signature for payload.

The third row is what makes the second mean something: it shows the 200 is a real verification rather
than a handler that stopped checking. A fourth control, with webhookSecret unset, returns a bare 200
for both a valid and a corrupted signature — that is the existing short-circuit at the top of the
handler, unchanged by this PR.

Not measured: behaviour on Node. Every run above was on workerd. constructEventAsync is
documented as the Workers-safe form of the same verification and the call is already inside the
existing try/catch, but this PR carries no Node before/after, so it should not be described as
verified-unchanged there.

Notes

…tEventAsync

The Stripe webhooks endpoint verified the signature with the synchronous
stripe.webhooks.constructEvent(). On Workers runtimes (workerd) the Stripe SDK
selects SubtleCryptoProvider, whose HMAC is async-only, so the call throws
"SubtleCryptoProvider cannot be used in a synchronous context" before any
registered handler runs. The catch sets returnStatus = 400 while the body still
reads {"received": true}, so every webhook is rejected while looking healthy.

The handler is already async and the call already sits inside the try, so the
awaited form lands a rejected promise in the same 400 branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant