generated from knso/webklar-preview-template
Webhook abgesichert: fehlendes Secret und kaputtes JSON fuehren nicht mehr zum 500er
Ohne gesetztes STRIPE_WEBHOOK_SECRET starb der Handler in hmacHex mit einer Worker-Exception (error 1101 / HTTP 500). Jetzt: sauberes 503 mit Log-Hinweis. JSON.parse ist zusaetzlich abgefangen -> 400 statt Absturz. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
464e9b109f
commit
fd97a76787
1 file changed
+13
-1
+13
-1
@@ -535,13 +535,25 @@ async function webhookHandler(request: Request, env: Env): Promise<Response> {
|
|||||||
const t = teile['t'];
|
const t = teile['t'];
|
||||||
const v1 = teile['v1'];
|
const v1 = teile['v1'];
|
||||||
if (!t || !v1) return new Response('Signatur fehlt', { status: 400 });
|
if (!t || !v1) return new Response('Signatur fehlt', { status: 400 });
|
||||||
|
// Ohne gesetztes Secret koennen wir nichts pruefen. Sauber ablehnen statt
|
||||||
|
// im hmacHex mit einem 500er (Worker-Exception) zu sterben.
|
||||||
|
if (!env.STRIPE_WEBHOOK_SECRET) {
|
||||||
|
console.log('Webhook ohne STRIPE_WEBHOOK_SECRET aufgerufen — Secret fehlt!');
|
||||||
|
return new Response('Webhook nicht konfiguriert', { status: 503 });
|
||||||
|
}
|
||||||
const erwartet = await hmacHex(env.STRIPE_WEBHOOK_SECRET, `${t}.${payload}`);
|
const erwartet = await hmacHex(env.STRIPE_WEBHOOK_SECRET, `${t}.${payload}`);
|
||||||
if (!gleich(v1, erwartet)) return new Response('Signatur ungültig', { status: 400 });
|
if (!gleich(v1, erwartet)) return new Response('Signatur ungültig', { status: 400 });
|
||||||
|
|
||||||
const event = JSON.parse(payload) as {
|
let event: {
|
||||||
type?: string;
|
type?: string;
|
||||||
data?: { object?: { id?: string; metadata?: Record<string, string>; customer_details?: { email?: string } } };
|
data?: { object?: { id?: string; metadata?: Record<string, string>; customer_details?: { email?: string } } };
|
||||||
};
|
};
|
||||||
|
try {
|
||||||
|
event = JSON.parse(payload);
|
||||||
|
} catch {
|
||||||
|
return new Response('Kein gültiges JSON', { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
if (event.type === 'checkout.session.completed') {
|
if (event.type === 'checkout.session.completed') {
|
||||||
const o = event.data?.object;
|
const o = event.data?.object;
|
||||||
const walletId = o?.metadata?.walletId;
|
const walletId = o?.metadata?.walletId;
|
||||||
|
|||||||
Reference in new issue
Block a user