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:
@@ -55,10 +55,10 @@ export async function POST(
|
|||||||
try {
|
try {
|
||||||
event = stripe.webhooks.constructEvent(raw, sig, webhookSecret)
|
event = stripe.webhooks.constructEvent(raw, sig, webhookSecret)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return NextResponse.json(
|
// Do not echo the verification library's message to an unauthenticated
|
||||||
{ error: e instanceof Error ? e.message : 'Invalid signature' },
|
// caller; log server-side for debugging.
|
||||||
{ status: 400 },
|
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.
|
// 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)) {
|
if (/duplicate key|already exists|unique/i.test(dedupe.error.message)) {
|
||||||
return NextResponse.json({ received: true, duplicate: true })
|
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
|
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
|
// remove it so Stripe's retry of this same event is reprocessed rather
|
||||||
// than silently short-circuited as a duplicate (no lost events).
|
// than silently short-circuited as a duplicate (no lost events).
|
||||||
await admin.from('stripe_webhook_events').delete().eq('event_id', event.id)
|
await admin.from('stripe_webhook_events').delete().eq('event_id', event.id)
|
||||||
return NextResponse.json(
|
console.error('[stripe webhook] handler error:', e instanceof Error ? e.message : e)
|
||||||
{ error: e instanceof Error ? e.message : 'handler error' },
|
return NextResponse.json({ error: 'Webhook handler error' }, { status: 500 })
|
||||||
{ status: 500 },
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({ received: true })
|
return NextResponse.json({ received: true })
|
||||||
|
|||||||
Reference in New Issue
Block a user