fix(stripe): generic webhook error responses, detail logged server-side (E3, P2)

The webhook returned raw signature-lib / Postgres error text to the
caller (signature verify is reachable unauthenticated). Now returns
generic 'Invalid signature' / 'Webhook processing error' / 'Webhook
handler error'; the real detail is console.error'd server-side so
debuggability is kept. Stripe only acts on the HTTP status. 532 tests.
This commit is contained in:
Moheeb Zara
2026-05-19 02:13:59 -07:00
parent 9351969c96
commit 6b84b091e4
+8 -9
View File
@@ -55,10 +55,10 @@ export async function POST(
try {
event = stripe.webhooks.constructEvent(raw, sig, webhookSecret)
} catch (e) {
return NextResponse.json(
{ error: e instanceof Error ? e.message : 'Invalid signature' },
{ status: 400 },
)
// Do not echo the verification library's message to an unauthenticated
// caller; log server-side for debugging.
console.error('[stripe webhook] signature verify failed:', e instanceof Error ? e.message : e)
return NextResponse.json({ error: 'Invalid signature' }, { status: 400 })
}
// Idempotency: the unique PK rejects a replay; treat that as already-done.
@@ -69,7 +69,8 @@ export async function POST(
if (/duplicate key|already exists|unique/i.test(dedupe.error.message)) {
return NextResponse.json({ received: true, duplicate: true })
}
return NextResponse.json({ error: dedupe.error.message }, { status: 500 })
console.error('[stripe webhook] dedupe insert failed:', dedupe.error.message)
return NextResponse.json({ error: 'Webhook processing error' }, { status: 500 })
}
const graceDays = cfg?.grace_days ?? 7
@@ -331,10 +332,8 @@ export async function POST(
// remove it so Stripe's retry of this same event is reprocessed rather
// than silently short-circuited as a duplicate (no lost events).
await admin.from('stripe_webhook_events').delete().eq('event_id', event.id)
return NextResponse.json(
{ error: e instanceof Error ? e.message : 'handler error' },
{ status: 500 },
)
console.error('[stripe webhook] handler error:', e instanceof Error ? e.message : e)
return NextResponse.json({ error: 'Webhook handler error' }, { status: 500 })
}
return NextResponse.json({ received: true })