Skip to content

Commit b9359ed

Browse files
committed
fix(types): handle undefined cases
1 parent aea8a63 commit b9359ed

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/runtime/server/lib/validators/mailchannels.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const validateContentDigest = async (header: string, body: string) => {
1212
if (!match) return false
1313

1414
const [, algorithm, hash] = match
15+
if (!algorithm || !hash) return false
16+
1517
const normalizedAlgorithm = algorithm.replace('-', '').toLowerCase()
1618

1719
if (!['sha256'].includes(normalizedAlgorithm)) return false
@@ -20,7 +22,7 @@ const validateContentDigest = async (header: string, body: string) => {
2022

2123
const extractSignature = (signatureHeader: string): string | null => {
2224
const signatureMatch = signatureHeader.match(/sig_\d+=:([^:]+):/)
23-
return signatureMatch ? signatureMatch[1] : null
25+
return signatureMatch && signatureMatch[1] ? signatureMatch[1] : null
2426
}
2527

2628
const extractInputValues = (header: string) => {
@@ -31,7 +33,7 @@ const extractInputValues = (header: string) => {
3133

3234
return {
3335
name: match[1],
34-
timestamp: Number.parseInt(match[3], 10),
36+
timestamp: Number.parseInt(match[3] || '', 10),
3537
algorithm: match[4],
3638
keyId: match[5],
3739
}

src/runtime/server/lib/validators/svix.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export const isValidSvixWebhook = async (event: H3Event): Promise<boolean> => {
2525

2626
const payload = `${webhookId}.${webhookTimestamp}.${body}`
2727
const secretKey = config.secretKey.split('_')[1]
28+
29+
if (!secretKey) return false
2830
const signatureEntries = webhookSignature.split(' ')
2931

3032
for (const signatureEntry of signatureEntries) {

test/simulations/svix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const secretKey = nuxtConfig.runtimeConfig?.webhook?.svix?.secretKey
1111
export const simulateSvixEvent = async (key: string = secretKey!) => {
1212
const timestamp = Math.floor(Date.now() / 1000).toString()
1313
const payload = `${webhookId}.${timestamp}.${body}`
14-
const secretKeyBase64 = key.split('_')[1]
14+
const secretKeyBase64 = key.split('_')[1]!
1515
const signatureBuffer = Buffer.from(secretKeyBase64, 'base64')
1616
const signature = await subtle.importKey('raw', signatureBuffer, HMAC_SHA256, false, ['sign'])
1717
const hmac = await subtle.sign(HMAC_SHA256.name, signature, encoder.encode(payload))

0 commit comments

Comments
 (0)