From 8911709931794e47b1bca7588ebdab2916a18e51 Mon Sep 17 00:00:00 2001 From: Simon Axelsson Date: Tue, 15 Sep 2026 18:08:13 +0200 Subject: [PATCH] fix: do not acknowledge unverified Stripe webhooks --- app/api/webhooks/stripe/route.ts | 9 ++++-- docs/architecture/ARCHITECTURE.md | 2 ++ tests/api/stripe-webhook-config.test.ts | 40 +++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/api/stripe-webhook-config.test.ts diff --git a/app/api/webhooks/stripe/route.ts b/app/api/webhooks/stripe/route.ts index 21808c7..784f705 100644 --- a/app/api/webhooks/stripe/route.ts +++ b/app/api/webhooks/stripe/route.ts @@ -7,8 +7,13 @@ export async function POST(req: NextRequest) { const stripe = getStripe(); const whSecret = process.env.STRIPE_WEBHOOK_SECRET; const sig = req.headers.get("stripe-signature"); - if (!stripe || !whSecret || !sig) { - return NextResponse.json({ received: false, reason: "not_configured" }, { status: 200 }); + if (!stripe || !whSecret) { + // Never acknowledge an event that was not signature-verified and processed. + // A non-2xx response keeps Stripe's delivery retry/reconciliation path open. + return NextResponse.json({ received: false, reason: "not_configured" }, { status: 503 }); + } + if (!sig) { + return NextResponse.json({ error: "missing_signature" }, { status: 400 }); } const body = await req.text(); let event; diff --git a/docs/architecture/ARCHITECTURE.md b/docs/architecture/ARCHITECTURE.md index ba2c256..6ff8aeb 100644 --- a/docs/architecture/ARCHITECTURE.md +++ b/docs/architecture/ARCHITECTURE.md @@ -343,6 +343,8 @@ Om/när Postgres provisioneras måste detta lösas separat — jfr estate-blocke utom literalen `"false"`, och `assertAuthConfigSafe()` kastar vid `NODE_ENV=production` + auth av. Designen är korrekt. - **Stripe-webhooken** verifierar signatur mot `STRIPE_WEBHOOK_SECRET`. + Om Stripe-konfiguration saknas svarar den nu `503` i stället för att + kvittera ett obearbetat event med `200`; saknad signatur ger `400`. - `SECURITY.md` finns med rapporteringsväg (`security@siax.io`). ### KÄNDA LUCKOR diff --git a/tests/api/stripe-webhook-config.test.ts b/tests/api/stripe-webhook-config.test.ts new file mode 100644 index 0000000..467c114 --- /dev/null +++ b/tests/api/stripe-webhook-config.test.ts @@ -0,0 +1,40 @@ +import { afterEach, expect, it } from "vitest"; +import { NextRequest } from "next/server"; +import { POST } from "@/app/api/webhooks/stripe/route"; + +const savedSecretKey = process.env.STRIPE_SECRET_KEY; +const savedWebhookSecret = process.env.STRIPE_WEBHOOK_SECRET; + +afterEach(() => { + if (savedSecretKey === undefined) delete process.env.STRIPE_SECRET_KEY; + else process.env.STRIPE_SECRET_KEY = savedSecretKey; + if (savedWebhookSecret === undefined) delete process.env.STRIPE_WEBHOOK_SECRET; + else process.env.STRIPE_WEBHOOK_SECRET = savedWebhookSecret; +}); + +it("does not acknowledge an unverified Stripe event when credentials are absent", async () => { + delete process.env.STRIPE_SECRET_KEY; + delete process.env.STRIPE_WEBHOOK_SECRET; + const req = new NextRequest("https://0pt.app/api/webhooks/stripe", { + method: "POST", + headers: { "stripe-signature": "unverified" }, + body: "{}", + }); + + const res = await POST(req); + expect(res.status).toBe(503); + expect(await res.json()).toEqual({ received: false, reason: "not_configured" }); +}); + +it("rejects a delivery with no Stripe signature even when configured", async () => { + process.env.STRIPE_SECRET_KEY = "sk_test_fake_local_regression"; + process.env.STRIPE_WEBHOOK_SECRET = "whsec_fake_local_regression"; + const req = new NextRequest("https://0pt.app/api/webhooks/stripe", { + method: "POST", + body: "{}", + }); + + const res = await POST(req); + expect(res.status).toBe(400); + expect(await res.json()).toEqual({ error: "missing_signature" }); +});