fix: do not acknowledge unverified Stripe webhooks
masterplan-lock / check (push) Successful in 10s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / integration (pull_request) Successful in 27s
CI (SIAX Cloud) / quality (push) Successful in 39s
CI (SIAX Cloud) / quality (pull_request) Successful in 32s
CI (SIAX Cloud) / security (push) Successful in 43s
masterplan-lock / check (pull_request) Successful in 41s
CI (SIAX Cloud) / security (pull_request) Successful in 1m12s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 1m14s
CI (SIAX Cloud) / integration (push) Successful in 1m36s
masterplan-lock / check (push) Successful in 10s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / integration (pull_request) Successful in 27s
CI (SIAX Cloud) / quality (push) Successful in 39s
CI (SIAX Cloud) / quality (pull_request) Successful in 32s
CI (SIAX Cloud) / security (push) Successful in 43s
masterplan-lock / check (pull_request) Successful in 41s
CI (SIAX Cloud) / security (pull_request) Successful in 1m12s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 1m14s
CI (SIAX Cloud) / integration (push) Successful in 1m36s
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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" });
|
||||
});
|
||||
Reference in New Issue
Block a user