Stripe Webhook Signature Verification: The 11-Line Fix
AI tools ship Stripe webhook handlers with no signature check. How the forgery works, why constructEvent needs the raw body, and the 11-line fix.
Prompt Lovable, Bolt, Cursor, or any current model for a Stripe webhook handler and you get one that reads the request body, trusts whatever it says, and writes to your database. There is no Stripe webhook signature verification. We call it the unsigned-webhook default, and it shows up in nearly every handler we’ve seen an AI tool produce. We’ve reproduced it across reference apps built with each. Anyone who can reach /api/stripe/webhook marks an order paid or upgrades an account with one HTTP request and no secret. Stripe’s docs tell you to always verify signatures; this is what the model ships when the prompt doesn’t ask. The fix is constructEvent, the raw body, and 11 lines in the handler.
The generated handler trusts the body
The handler calls request.json() and updates your database on the strength of it. Nothing verifies the request came from Stripe. Here’s what Lovable generates for “handle Stripe webhook”. Bolt and Cursor produce the same thing on the same prompt, give or take a comment:
// app/api/stripe/webhook/route.ts
import { NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'
export async function POST(request: Request) {
const event = await request.json()
const supabase = createClient()
if (event.type === 'checkout.session.completed') {
await supabase
.from('orders')
.update({ status: 'paid', paid_at: new Date().toISOString() })
.eq('stripe_session_id', event.data.object.id)
}
if (event.type === 'customer.subscription.created') {
await supabase
.from('users')
.update({ plan: 'pro', subscription_active: true })
.eq('stripe_customer_id', event.data.object.customer)
}
return NextResponse.json({ received: true })
}
The handler:
- Accepts any POST request.
- Parses the body as JSON.
- Updates your orders/users tables based on what the JSON says.
No signature check, no shared secret, no origin verification. Anyone with the URL can flip an order to paid or upgrade a user to your pro plan.
Exploiting it takes one curl command
Sign up for a free account and note your stripe_customer_id (often visible on your own profile page). That gets you an account to upgrade. Any other customer ID you have works the same way:
curl -X POST https://yourapp.com/api/stripe/webhook \
-H "Content-Type: application/json" \
-d '{
"type": "customer.subscription.created",
"data": {
"object": {
"customer": "cus_THE_VICTIMS_ID"
}
}
}'
The handler returns 200 OK. The customer named in the payload now has a pro subscription nobody paid for. Swap in checkout.session.completed with a session ID in data.object.id and that pending order gets marked paid without money changing hands.
We’ve reproduced this on reference apps built with each of these tools. Same handler, same flaw, every time.
The prompt-boundary problem is why the check goes missing
Stripe’s docs explicitly tell you to verify signatures, so this isn’t a documentation gap. It’s a prompt-boundary problem in how AI tools generate code.
Prompt “build a Stripe webhook handler that marks orders as paid” and the model builds exactly that. The prompt never says verify the request actually came from Stripe, because no founder thinks to write that, and the model optimizes for making the prompt work rather than making the result safe in production.
You see the same root cause across AI-generated code. The happy path gets built and the adversarial path gets ignored: no negative tests, no “what if an attacker sends this,” no question of whether the request can be trusted at all.
You can sometimes catch it by adding “and verify the webhook signature” to the prompt. Even then, in our audits, the verification it produces often looks right and isn’t. Raw body versus parsed body is the usual mistake, and that one rejects every real event.
Stripe webhook signature verification takes 11 lines
Stripe’s own Next.js example has the same shape: read the body with request.text(), pass it to constructEvent, return 400 if that throws.
// app/api/stripe/webhook/route.ts
import { NextResponse } from 'next/server'
import Stripe from 'stripe'
import { createClient } from '@/lib/supabase/server'
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!)
const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET!
export async function POST(request: Request) {
const body = await request.text() // raw body, not JSON
const signature = request.headers.get('stripe-signature')
if (!signature) {
return NextResponse.json({ error: 'Missing signature' }, { status: 400 })
}
let event: Stripe.Event
try {
event = stripe.webhooks.constructEvent(body, signature, webhookSecret)
} catch (err) {
return NextResponse.json({ error: 'Invalid signature' }, { status: 400 })
}
const supabase = createClient()
if (event.type === 'checkout.session.completed') {
const session = event.data.object as Stripe.Checkout.Session
await supabase
.from('orders')
.update({ status: 'paid', paid_at: new Date().toISOString() })
.eq('stripe_session_id', session.id)
}
if (event.type === 'customer.subscription.created') {
const sub = event.data.object as Stripe.Subscription
await supabase
.from('users')
.update({ plan: 'pro', subscription_active: true })
.eq('stripe_customer_id', sub.customer as string)
}
return NextResponse.json({ received: true })
}
Three things changed:
- Read the raw body, not parsed JSON.
stripe.webhooks.constructEventhashes the body bytes to verify the signature, and re-serialized JSON doesn’t reproduce those bytes. Feed it the output ofrequest.json()and verification fails on every real event. - Check the
stripe-signatureheader and reject if missing. - Call
constructEventwith the webhook secret. This is the actual verification. It throws if the signature is wrong, the timestamp is too old (replay protection), or the body doesn’t match.
You’ll also need STRIPE_WEBHOOK_SECRET in your env. In the Stripe Dashboard, open the endpoint under Workbench > Webhooks and click Reveal secret. It starts with whsec_.
constructEvent checks an HMAC over the raw body, plus a timestamp
The Stripe-Signature header isn’t opaque. It carries a timestamp and one or more signatures:
Stripe-Signature: t=1737936000,v1=5257a869e7...3b5f
constructEvent pulls out t and v1, recomputes the signature itself as HMAC-SHA256(secret = whsec_..., message = "{t}.{rawBody}"), and compares it against v1 with a constant-time equality check, so a wrong guess leaks nothing through timing. Two conditions have to hold or it throws:
- The recomputed HMAC matches
v1. This is why the raw body matters. The HMAC is over the exact bytes Stripe signed. Parsing and re-serializing drops whitespace and can reorder keys, so the bytes stop matching even when the JSON is equal. tis inside the tolerance window, 300 seconds by default in stripe-node. That’s the replay defense: a captured request that’s resent later fails once it ages past five minutes, valid signature or not.
When verification starts failing for legitimate events in production, check the secret before anything else. A mismatched whsec_ fails every event identically, and Stripe names the wrong secret as the most common cause. The secret stripe listen prints is not the one on your Dashboard endpoint, which is the easy way to end up with the wrong one.
Two negative tests AI doesn’t write prove the fix landed
Two tests. Both negative:
# 1. Unsigned request should return 400
curl -X POST https://yourapp.com/api/stripe/webhook \
-H "Content-Type: application/json" \
-d '{"type":"customer.subscription.created"}'
# Expect: 400 Missing signature
# 2. Forged signature should return 400
curl -X POST https://yourapp.com/api/stripe/webhook \
-H "Content-Type: application/json" \
-H "stripe-signature: t=1234567890,v1=deadbeef" \
-d '{"type":"customer.subscription.created"}'
# Expect: 400 Invalid signature
If either returns 200, your fix isn’t deployed correctly.
For positive testing, use stripe trigger from the Stripe CLI. It generates properly signed test events, as long as your local STRIPE_WEBHOOK_SECRET is the one stripe listen prints:
stripe listen --forward-to localhost:3000/api/stripe/webhook
stripe trigger customer.subscription.created
Signature verification doesn’t protect you from legitimate events
Signature verification stops forged Stripe events. It doesn’t stop legitimate events from breaking your app. Stripe says endpoints can receive the same event more than once and doesn’t guarantee delivery order, so you can get a real customer.subscription.deleted twice, or a checkout.session.completed for a session your database has no record of. Those failure modes need idempotency, retry-safety, and an event-ordering strategy, none of which this fix touches. We name those as separate findings in the audit.
Two things sit outside this post. Stripe also recommends accepting webhook requests only from its published IP addresses. And it now steers new integrations to thin events, verified with parseEventNotification(). constructEvent is the call for the snapshot events shown here.
AI tools write the handler, not the threat model
Every external-facing endpoint in your app needs to answer two questions:
- Who is allowed to call this? (auth)
- Can a caller fake their identity? (signature or origin verification)
If the answers aren’t somewhere in your code, an attacker is the one answering them.