VibeSecCheck
Home/Security Guides/webhook-signature-verification
Critical RiskHeaders, Auth & APIsStripeWebhooksNext.jsLemonSqueezy

Verifying Stripe & Payment Webhook Signatures

Your `/api/webhooks/stripe` endpoint is publicly reachable without a user login cookie. If you parse `req.json()` without cryptographically verifying the webhook signature on the raw request body, anyone can forge a payment event in 5 seconds.

Why AI Builders Introduce This

AI tools frequently skip `stripe.webhooks.constructEvent()` during prototyping because verifying signatures requires reading `await req.text()` raw bytes and configuring `STRIPE_WEBHOOK_SECRET`.

Real-World Exploit Impact
  • Anyone can send a forged `checkout.session.completed` POST request and grant themselves lifetime Pro access
  • Attackers can trigger fake refund, credit top-up, or seat-addition workflows
  • Parsing JSON before verification breaks signature validation and tempts developers to comment out the check

Vulnerable Pattern vs. Production Fix

CRITICAL: Forgery-Prone Stripe Webhook Routetypescript
// app/api/webhooks/stripe/route.ts — VULNERABLE!
export async function POST(req: Request) {
  const event = await req.json(); // Zero cryptographic verification!
  if (event.type === "checkout.session.completed") {
    await upgradeUserToPro(event.data.object.metadata.userId);
  }
  return Response.json({ received: true });
}
Cryptographically Verified Stripe Webhook (Raw Body Bytes)
// app/api/webhooks/stripe/route.ts — SECURE
import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST(req: Request) {
  const signature = req.headers.get("stripe-signature");
  if (!signature) {
    return Response.json({ error: "Missing stripe-signature" }, { status: 400 });
  }

  // CRITICAL: Read raw text bytes BEFORE JSON parsing!
  const rawBody = await req.text();
  let event: Stripe.Event;
  try {
    event = stripe.webhooks.constructEvent(
      rawBody,
      signature,
      process.env.STRIPE_WEBHOOK_SECRET!
    );
  } catch {
    return Response.json({ error: "Invalid webhook signature" }, { status: 400 });
  }

  if (event.type === "checkout.session.completed") {
    const session = event.data.object as Stripe.Checkout.Session;
    const userId = session.metadata?.userId;
    if (userId) await upgradeUserToPro(userId, session.id);
  }

  return Response.json({ received: true });
}

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Audit every payment and third-party webhook endpoint (Stripe, LemonSqueezy, Paddle, Clerk, GitHub) in this repository:
1. Read raw body bytes via await req.text() before any JSON parsing.
2. Verify the cryptographic signature header using the provider's official verifier (e.g. stripe.webhooks.constructEvent or HMAC-SHA256 with crypto.timingSafeEqual) and a server-only webhook secret.
3. Reject missing or invalid signatures with HTTP 400 immediately.
4. Make webhook fulfillment idempotent using event.id so retried deliveries never double-credit users.

How to Verify Your Fix Worked

  1. 1Send a fake POST with `curl -X POST -d '{"type":"checkout.session.completed"}' https://yourapp.com/api/webhooks/stripe` — confirm it returns HTTP 400.
  2. 2Test real signed deliveries using `stripe listen --forward-to localhost:3000/api/webhooks/stripe`.
  3. 3Verify `subscriptions` table RLS denies client-side updates so ONLY this verified webhook upgrades plans.

Frequently Asked Questions

Why does Stripe signature verification fail in Next.js App Router?

Almost always because `req.json()` was called instead of `req.text()`, or a proxy/middleware altered the body whitespace, or the test vs live `whsec_` secret was mixed up.

How do I verify LemonSqueezy or GitHub webhooks without an SDK?

Compute an HMAC-SHA256 hex digest of the raw body string using your webhook secret, and compare it against the signature header using `crypto.timingSafeEqual` (never `===`).

Why does idempotency matter on webhooks?

Payment providers retry webhooks if your server is slow. Storing processed `event.id` values prevents granting duplicate AI credits or duplicate onboarding emails.

Related Security Playbooks