Learn / Payment security
criticalStripe · Next.js · Node

Your Stripe webhook is unverified, and someone can unlock your product for free

Most apps unlock a paid feature when Stripe sends a webhook saying a payment succeeded. The webhook endpoint is a public URL, so the only thing separating a real Stripe event from a forged one is the signature Stripe attaches to every request.

If your handler does not verify that signature, anyone who knows (or guesses) the URL can POST their own "checkout.session.completed" event and flip themselves to paid, for free.

Why it matters

An attacker sends a single crafted request to /api/webhooks/stripe with a body that looks like a successful payment. Your app trusts it, marks the account as subscribed, and grants access. No card, no charge. At scale this is straight revenue loss, and it is trivial to automate.

How do I know if my webhook is safe?

Open your webhook handler and look for one thing: does it verify the signature before trusting the event?

The fix

Verify every webhook against your endpoint signing secret before you trust it, and read the raw body so the signature check works. Reject anything that does not verify.

  1. Get the endpoint’s signing secret from the Stripe dashboard (starts with whsec_).
  2. Read the raw request body (disable body parsing for this route).
  3. Call constructEvent with the raw body, the stripe-signature header, and the secret.
  4. Only act on the event if verification passes; return 400 otherwise.
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

export async function POST(req) {
  const raw = await req.text();               // raw body, not parsed JSON
  const sig = req.headers.get('stripe-signature');
  let event;
  try {
    event = stripe.webhooks.constructEvent(raw, sig, process.env.STRIPE_WEBHOOK_SECRET);
  } catch (err) {
    return new Response('invalid signature', { status: 400 });
  }
  if (event.type === 'checkout.session.completed') {
    // safe to grant access now
  }
  return new Response('ok');
}

FAQ

Where do I find the webhook signing secret?

In the Stripe dashboard under Developers, Webhooks, select your endpoint. The signing secret starts with whsec_. It is different from your API secret key.

Why does verification keep failing?

Almost always because the framework parsed the body into JSON before you verified it. Stripe signs the raw bytes, so you must read the raw body (in Next.js App Router use req.text(); in Express use express.raw for that route).