Unverified Stripe Webhooks: Anyone Can Tell Your App a Payment Succeeded
Your Stripe webhook URL is public, and anyone who finds it can send it JSON. If the handler trusts the body, a forged checkout.session.completed event unlocks a paid plan for free. Stripe signs every event. Verify that signature against the raw request body with your endpoint's signing secret, reject anything that fails, and make the handler safe to run twice, because Stripe retries.
On this page
Generated webhook handlers tend to parse the JSON and act on it. They work in testing, since Stripe's real events have the right shape. So do fake ones.
The vulnerable handler
// app/api/webhooks/stripe/route.ts
export async function POST(req: Request) {
const event = await req.json(); // trusts the caller
if (event.type === 'checkout.session.completed') {
await db.user.update({
where: { id: event.data.object.client_reference_id },
data: { plan: 'pro' },
});
}
return Response.json({ received: true });
}
The attack:
curl -X POST https://yourapp.com/api/webhooks/stripe \
-H 'Content-Type: application/json' \
-d '{"type":"checkout.session.completed","data":{"object":{"client_reference_id":"<my-user-id>"}}}'
Webhook paths are easy to guess (/api/webhooks/stripe, /api/stripe/webhook), and the event format is documented.
Verify the signature
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: Request) {
const body = await req.text(); // raw body, exactly as sent
const signature = req.headers.get('stripe-signature');
if (!signature) return new Response('Missing signature', { status: 400 });
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(body, signature, process.env.STRIPE_WEBHOOK_SECRET!);
} catch {
return new Response('Invalid signature', { status: 400 });
}
// event is now trustworthy
// ...
return Response.json({ received: true });
}
Three details cause most of the trouble:
The raw body. The signature is computed over the exact bytes Stripe sent. If a framework parses the JSON first and you re-serialise it, verification fails. In the Next.js App Router, use req.text(). In Express, mount express.raw({ type: 'application/json' }) on the webhook route before any JSON middleware. When verification fails for this reason, the tempting fix is to remove the check. Fix the body handling instead.
The right secret. The signing secret starts with whsec_ and belongs to one endpoint. It is not your API key. Test mode, live mode and the Stripe CLI's local forwarding each have their own.
No bypass flags. A line like if (process.env.NODE_ENV !== 'production') skipVerification() tends to reach production through a preview or staging deployment.
constructEvent also rejects events with an old timestamp, which blocks replaying a captured request later.
Handle duplicates and ordering
Stripe retries when your endpoint is slow or returns an error, and it does not guarantee order. A handler that adds credits on each call will add them twice.
try {
await db.processedEvent.create({ data: { id: event.id } }); // unique primary key
} catch {
return Response.json({ received: true, duplicate: true }); // seen it already
}
Better still, make the effect itself idempotent: "set plan to pro" can run ten times safely. "Add 100 credits" cannot, so tie the grant to the event or payment ID.
For anything important, do not rely on the event payload's snapshot. Fetch the current object from Stripe and act on that:
const session = await stripe.checkout.sessions.retrieve(event.data.object.id);
if (session.payment_status !== 'paid') return Response.json({ received: true });
Respond quickly with a 2xx and move slow work to a queue. Long handlers time out, and timeouts cause retries.
Do not trust the client side of the payment
Two related mistakes sit next to this one:
- Granting access on the success page.
/success?session_id=...is a redirect the user controls. Grant access from the verified webhook, or by retrieving the session from Stripe on the server. - Taking the amount from the browser. Covered in the client sets the price, role or plan.
Check what you are granting, too. Compare the price or product ID in the event against what your app sells, so a cheap product cannot unlock an expensive plan.
Other providers
The same rule applies to every webhook: GitHub (X-Hub-Signature-256), Clerk and Resend (Svix signatures), Paddle, Lemon Squeezy, Twilio. Each signs its requests and documents how to verify them. A webhook with no verification option should at least carry a long random secret in the URL and be treated with suspicion.
Test it
# forged event: expect 400
curl -i -X POST https://yourapp.com/api/webhooks/stripe \
-H 'Content-Type: application/json' -d '{"type":"checkout.session.completed"}'
# real signed event to your local server
stripe listen --forward-to localhost:3000/api/webhooks/stripe
stripe trigger checkout.session.completed
Then use "Resend" on an event in the Stripe dashboard and confirm nothing is granted twice.
Webhooks are check 8 in our security checklist for vibe-coded apps. If you want your payment flow tested end to end and fixed, that is part of our app rescue and security audit.
