11import { error , json } from "@sveltejs/kit"
22import { SUPABASE_WEBHOOK_SECRET } from "$env/static/private"
3- import { hexToBytes } from "$lib/utils"
43import { getScriptVersion , resetScriptVersions } from "$lib/server/versions.server"
4+ import { base64ToBytes } from "$lib/utils"
55
66export const POST = async ( { request } ) => {
77 const signature = request . headers . get ( "x-supabase-signature" )
8- const body = await request . text ( )
9-
8+ const bodyPromise = request . text ( )
109 if ( ! signature ) error ( 401 , "Webhook signature is missing" )
1110
12- console . log ( "Signature: " , signature )
13-
1411 const encoder = new TextEncoder ( )
1512 const key = await crypto . subtle . importKey (
1613 "raw" ,
@@ -20,9 +17,13 @@ export const POST = async ({ request }) => {
2017 [ "verify" ]
2118 )
2219
23- const isValid = await crypto . subtle . verify ( "HMAC" , key , hexToBytes ( signature ) , encoder . encode ( body ) )
20+ const body = await bodyPromise
21+ const isValid = await crypto . subtle . verify ( "HMAC" , key , base64ToBytes ( signature ) , encoder . encode ( body ) )
2422
25- if ( ! isValid ) error ( 403 , "Webhook signature is not valid" )
23+ if ( ! isValid ) {
24+ console . log ( "Signature is invalid!\n" , "Signature: " , signature , "\nBody: " , body )
25+ error ( 403 , "Webhook signature is not valid" )
26+ }
2627
2728 const payload = JSON . parse ( body )
2829
0 commit comments