Guides · Free check · $9 fix pack

Guides

Your Stripe webhook verifies the signature, then accepts the event anyway

The code calls constructEvent, the tests pass, and the endpoint still accepts a fake checkout.session.completed. The verification runs, but when it fails the handler keeps going. Stripe's warning applies in full: "Without verification, an attacker could send fake webhook events to your endpoint to trigger actions like fulfilling orders, granting account access, or modifying records" (Stripe docs: Verify events).

What "fails open" looks like

Stripe's libraries tell you when verification fails: "If verification fails, you get an error" (Stripe docs: Receive events). Fail-open code catches that error and doesn't stop. These are the patterns to search for:

AI builders produce these when asked to "fix the signature error". The real fix for that error is the raw body or the right whsec_ secret (signature verification failed), not catching it.

The fix: fail closed

Verify first, and return 400 from the catch block before anything else runs. Stripe's own Express example does exactly this: on error it logs and sends res.status(400), then returns (stripe-node example: Express webhook). The Ruby and Python examples in Stripe's docs return 400 the same way (source).

// Next.js App Router (route.ts). Same shape works in Express or a Supabase Edge Function.
const secret = process.env.STRIPE_WEBHOOK_SECRET;

export async function POST(req: Request) {
  if (!secret) return new Response("webhook secret not configured", { status: 500 }); // never skip
  const sig = req.headers.get("stripe-signature");
  if (!sig) return new Response("missing signature", { status: 400 });

  const body = await req.text();                 // raw body, byte for byte
  let event;
  try {
    event = stripe.webhooks.constructEvent(body, sig, secret);
  } catch (err) {
    console.error("stripe signature failed", (err as Error).message);
    return new Response("bad signature", { status: 400 });   // stop here
  }

  // Only verified events reach this line.
  await handleEvent(event);                       // your helper: idempotent on event.id
  return new Response("ok", { status: 200 });
}

Helper names are placeholders. In Python, return 400 from the except and never fall through to json.loads.

Two rules make the pattern hard to break later: the only event your handler ever sees comes out of constructEvent, and a missing secret is a server error (500), not a reason to skip.

Write the test that would have caught it

Green tests that only send good events can't catch a fail-open handler. Add negative tests against your webhook route:

  1. No Stripe-Signature header: expect 400 and no database change.
  2. A signature made with the wrong secret (or a valid signature with one byte of the body changed): expect 400.
  3. A correctly signed test event (for example from stripe listen + stripe trigger, Stripe docs: test webhooks): expect 200 and exactly one fulfillment.

Run tests 1 and 2 against production too after each deploy, so a missing env var shows up as a 500 instead of open doors: curl -i -X POST -H 'content-type: application/json' -d '{}' https://yourdomain.com/api/webhooks/stripe should return 400, not 200.

Prompt for your AI builder

Our Stripe webhook must fail closed. 1) Read the raw request body and the Stripe-Signature header. If STRIPE_WEBHOOK_SECRET is not set, return 500; never skip verification. If the header is missing, return 400. 2) Call stripe.webhooks.constructEvent(rawBody, signature, secret) inside try/catch; in the catch, log the message and return 400 immediately. Do not parse the body any other way and do not continue after a failed check. 3) Remove any SKIP/dev flags that bypass verification. 4) Add tests: no signature returns 400, wrong-secret signature returns 400, a valid signed event returns 200 and fulfills exactly once (dedupe on event.id).

Re-test

  1. Send the unsigned curl above to your live URL: 400 is correct, 200 means it is still open (why that matters).
  2. Run one sandbox Checkout with card 4242 4242 4242 4242 and confirm the delivery returned 2xx and the user was unlocked.
  3. Before launch, run the full webhook security checklist.
Check yours in 10 seconds (free).

Paste your webhook URL. WebhookProof sends 3 harmless test requests and tells you whether Stripe can reach it and whether it rejects unsigned events. If it finds a problem, the $9 fix pack gives you the exact copy-paste fix for your stack (refundable for 14 days).

Run the free check

FAQ

Why does my Stripe webhook accept fake events even though I call constructEvent?

Usually the error from constructEvent is caught and the handler keeps going, or verification only runs when the Stripe-Signature header or the webhook secret is present. Return 400 from the catch block and never process an event that did not come out of constructEvent.

What status should a Stripe webhook return when the signature is invalid?

Stripe's own examples return 400 when signature verification fails. Return it before any fulfillment code runs.

What if STRIPE_WEBHOOK_SECRET is missing in production?

Treat it as a server error and return 500 so the failure is visible and Stripe retries. Never skip verification because the secret is missing.

How do I test that my webhook fails closed?

Send a request with no Stripe-Signature header and one signed with the wrong secret. Both should return 400 with no change in your database. A correctly signed test event should return 200.

Sources

More guides

Stripe webhook not working? Check these 7 things in order · A customer paid but didn't get access. What went wrong? · No signatures found matching the expected signature for payload · Stripe webhook failing with a 307 or 308 redirect

Last reviewed 2026-11-13. WebhookProof is an independent tool, not affiliated with Stripe, Inc.. Quotes are from the linked documentation.