Skip to content

Commit 8e6a975

Browse files
committed
fix: db webhook signature check working properly hopefully
1 parent 42198b6 commit 8e6a975

4 files changed

Lines changed: 27 additions & 23 deletions

File tree

src/lib/utils.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ export const scriptStages: Record<TScriptStages, NameValueIcon> = {
175175
archived: { name: "Archived", value: "archived", icon: "💀" }
176176
}
177177

178-
export function hexToBytes(hex: string) {
179-
const bytes = new Uint8Array(hex.length / 2)
180-
for (let i = 0; i < bytes.length; i++) {
181-
bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16)
182-
}
183-
return bytes as Uint8Array<ArrayBuffer>
178+
export function base64ToBytes(base64: string) {
179+
const binString = atob(base64)
180+
return Uint8Array.from(binString, (m) => m.codePointAt(0)!)
184181
}

src/routes/api/supabase/scripts/simba/+server.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { error, json } from "@sveltejs/kit"
22
import { SUPABASE_WEBHOOK_SECRET } from "$env/static/private"
3-
import { hexToBytes } from "$lib/utils"
3+
import { base64ToBytes } from "$lib/utils"
44
import { getSimbaVersions, resetSimbaVersions } from "$lib/server/versions.server"
55

66
export 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

1211
const encoder = new TextEncoder()
@@ -18,9 +17,13 @@ export const POST = async ({ request }) => {
1817
["verify"]
1918
)
2019

21-
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))
2222

23-
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+
}
2427

2528
const old = await resetSimbaVersions()
2629
if (old.length > 0) error(500, "Failed to reset old versions.")

src/routes/api/supabase/scripts/versions/+server.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { error, json } from "@sveltejs/kit"
22
import { SUPABASE_WEBHOOK_SECRET } from "$env/static/private"
3-
import { hexToBytes } from "$lib/utils"
43
import { getScriptVersion, resetScriptVersions } from "$lib/server/versions.server"
4+
import { base64ToBytes } from "$lib/utils"
55

66
export 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

src/routes/api/supabase/scripts/wasplib/+server.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { error, json } from "@sveltejs/kit"
22
import { SUPABASE_WEBHOOK_SECRET } from "$env/static/private"
3-
import { hexToBytes } from "$lib/utils"
3+
import { base64ToBytes } from "$lib/utils"
44
import { getWaspLibVersions, resetWaspLibVersions } from "$lib/server/versions.server"
55

66
export 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

1211
const encoder = new TextEncoder()
@@ -18,9 +17,13 @@ export const POST = async ({ request }) => {
1817
["verify"]
1918
)
2019

21-
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))
2222

23-
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+
}
2427

2528
const old = await resetWaspLibVersions()
2629
if (old.length > 0) error(500, "Failed to reset old versions.")

0 commit comments

Comments
 (0)