Search Psychevus

12 results available.
↑ ↓ to navigate ↵ to open · esc to close

Stripe · Next.js · Supabase

Verify Stripe webhook signatures (Next.js and Supabase Edge Functions)

An unverified webhook lets anyone fake a payment and unlock your paid plan. Verifying the raw body in Next.js route handlers and Supabase Edge Functions, plus idempotency and what not to trust.

Short answerRead the raw request body as text, verify it with stripe.webhooks.constructEvent (constructEventAsync on Deno) using the Stripe-Signature header and your endpoint's signing secret, and return 400 if it fails. Never parse the JSON first, never trust prices or IDs from event metadata without re-reading the object from Stripe, and record event IDs so retries are processed once.

Why it matters

A webhook endpoint is a public URL. If it doesn't check Stripe's signature, one curl request with a made-up checkout.session.completed event upgrades any account. Supabase Edge Functions that receive webhooks must be deployed without JWT verification so Stripe can call them, which makes the signature the only gate.

Next.js App Router: app/api/stripe/route.ts
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, not req.json()
  let event: Stripe.Event;
  try {
    event = stripe.webhooks.constructEvent(body, req.headers.get("stripe-signature") ?? "", process.env.STRIPE_WEBHOOK_SECRET!);
  } catch {
    return new Response("Invalid signature", { status: 400 });
  }
  if (event.type === "checkout.session.completed") {
    const session = await stripe.checkout.sessions.retrieve(event.data.object.id);
    if (session.payment_status === "paid") { /* grant access, once per event.id */ }
  }
  return new Response("ok");
}
Supabase Edge Function (Deno)
import Stripe from "npm:stripe";
const stripe = new Stripe(Deno.env.get("STRIPE_SECRET_KEY")!);
const crypto = Stripe.createSubtleCryptoProvider();

Deno.serve(async (req) => {
  const body = await req.text();
  try {
    const event = await stripe.webhooks.constructEventAsync(
      body, req.headers.get("stripe-signature") ?? "",
      Deno.env.get("STRIPE_WEBHOOK_SECRET")!, undefined, crypto,
    );
    // handle event …
    return new Response("ok");
  } catch {
    return new Response("Invalid signature", { status: 400 });
  }
});
// deploy: supabase functions deploy stripe-webhook --no-verify-jwt

What not to trust, even after verification

  • Metadata you set from the browser when creating the session: a user can choose it. Look the customer up on your side.
  • Delivery once and in order: Stripe retries. Store event.id and skip duplicates.
  • The test-mode secret in production, or the other way round: each endpoint has its own signing secret.

Check it yourself

Send your endpoint an unsigned POST with a fake event body. It must answer 400 and change nothing. Then run a real test-mode checkout and confirm access is granted exactly once.

See this flaw in the sample security report

Questions

Why does my verification fail with a valid signature?

Almost always because the body was parsed or re-serialised before verifying. Verify the exact raw text Stripe sent.

Do I still need to verify if the URL is secret?

Yes. URLs leak through logs, source maps and client code. The signature is the only proof the event came from Stripe.

Updated 18 November 2026 · by

Reading about a flaw is the easy part. A Launch Check finds every one of these in your app, with evidence and a fix prompt for each, in two business days.

Get a fixed quote

Related

YOUR VISIT, YOUR CHOICE

Privacy, by intention.

You decide what stays on your device. You can change your choice at any time.

Essential

Saves your privacy choice. Administrator sign-in uses a secure session cookie.

Always on
Full privacy notice ↗