|
1 | 1 | import { describe, test, expect, mock, afterEach } from 'bun:test' |
2 | 2 |
|
| 3 | +/** Helper: generate a valid Svix signature for testing */ |
| 4 | +async function signWebhook(secret: string, svixId: string, timestamp: string, body: string) { |
| 5 | + const secretBytes = Uint8Array.from(atob(secret.replace(/^whsec_/, '')), c => c.charCodeAt(0)) |
| 6 | + const toSign = `${svixId}.${timestamp}.${body}` |
| 7 | + const key = await crypto.subtle.importKey( |
| 8 | + 'raw', secretBytes, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign'], |
| 9 | + ) |
| 10 | + const sig = await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(toSign)) |
| 11 | + return `v1,${btoa(String.fromCharCode(...new Uint8Array(sig)))}` |
| 12 | +} |
| 13 | + |
| 14 | +const TEST_SECRET = 'whsec_' + btoa('test-secret-key-for-unit-tests!') |
| 15 | + |
3 | 16 | describe('Delivery Status Handler', () => { |
4 | 17 | afterEach(() => { mock.restore() }) |
5 | 18 |
|
@@ -65,4 +78,109 @@ describe('Delivery Status Handler', () => { |
65 | 78 | const body = await res.json() as { ok: boolean } |
66 | 79 | expect(body.ok).toBe(true) |
67 | 80 | }) |
| 81 | + |
| 82 | + test('rejects webhook with invalid signature when secret is configured', async () => { |
| 83 | + const { handleResendWebhook } = await import('../../worker/src/handlers/delivery-status') |
| 84 | + |
| 85 | + const bodyStr = JSON.stringify({ type: 'email.sent', created_at: '2026-04-04', data: { email_id: 'abc' } }) |
| 86 | + const request = new Request('http://localhost/api/resend-webhook', { |
| 87 | + method: 'POST', |
| 88 | + headers: { |
| 89 | + 'Content-Type': 'application/json', |
| 90 | + 'svix-id': 'msg_test123', |
| 91 | + 'svix-timestamp': String(Math.floor(Date.now() / 1000)), |
| 92 | + 'svix-signature': 'v1,invalidsignature', |
| 93 | + }, |
| 94 | + body: bodyStr, |
| 95 | + }) |
| 96 | + const env = { RESEND_WEBHOOK_SECRET: TEST_SECRET, DB: {} } as any |
| 97 | + const ctx = { waitUntil: () => {} } as any |
| 98 | + |
| 99 | + const res = await handleResendWebhook(request, env, ctx) |
| 100 | + expect(res.status).toBe(401) |
| 101 | + }) |
| 102 | + |
| 103 | + test('rejects webhook with missing signature headers when secret is configured', async () => { |
| 104 | + const { handleResendWebhook } = await import('../../worker/src/handlers/delivery-status') |
| 105 | + |
| 106 | + const request = new Request('http://localhost/api/resend-webhook', { |
| 107 | + method: 'POST', |
| 108 | + headers: { 'Content-Type': 'application/json' }, |
| 109 | + body: JSON.stringify({ type: 'email.sent', created_at: '2026-04-04', data: { email_id: 'abc' } }), |
| 110 | + }) |
| 111 | + const env = { RESEND_WEBHOOK_SECRET: TEST_SECRET, DB: {} } as any |
| 112 | + const ctx = { waitUntil: () => {} } as any |
| 113 | + |
| 114 | + const res = await handleResendWebhook(request, env, ctx) |
| 115 | + expect(res.status).toBe(401) |
| 116 | + }) |
| 117 | + |
| 118 | + test('rejects webhook with expired timestamp', async () => { |
| 119 | + const { handleResendWebhook } = await import('../../worker/src/handlers/delivery-status') |
| 120 | + |
| 121 | + const bodyStr = JSON.stringify({ type: 'email.sent', created_at: '2026-04-04', data: { email_id: 'abc' } }) |
| 122 | + const expiredTs = String(Math.floor(Date.now() / 1000) - 600) // 10 min ago |
| 123 | + const sig = await signWebhook(TEST_SECRET, 'msg_test123', expiredTs, bodyStr) |
| 124 | + |
| 125 | + const request = new Request('http://localhost/api/resend-webhook', { |
| 126 | + method: 'POST', |
| 127 | + headers: { |
| 128 | + 'Content-Type': 'application/json', |
| 129 | + 'svix-id': 'msg_test123', |
| 130 | + 'svix-timestamp': expiredTs, |
| 131 | + 'svix-signature': sig, |
| 132 | + }, |
| 133 | + body: bodyStr, |
| 134 | + }) |
| 135 | + const env = { RESEND_WEBHOOK_SECRET: TEST_SECRET, DB: {} } as any |
| 136 | + const ctx = { waitUntil: () => {} } as any |
| 137 | + |
| 138 | + const res = await handleResendWebhook(request, env, ctx) |
| 139 | + expect(res.status).toBe(401) |
| 140 | + }) |
| 141 | + |
| 142 | + test('accepts webhook with valid signature', async () => { |
| 143 | + const { handleResendWebhook } = await import('../../worker/src/handlers/delivery-status') |
| 144 | + |
| 145 | + const bodyStr = JSON.stringify({ type: 'email.unknown_event', created_at: '2026-04-04', data: { email_id: 'abc' } }) |
| 146 | + const ts = String(Math.floor(Date.now() / 1000)) |
| 147 | + const svixId = 'msg_valid123' |
| 148 | + const sig = await signWebhook(TEST_SECRET, svixId, ts, bodyStr) |
| 149 | + |
| 150 | + const request = new Request('http://localhost/api/resend-webhook', { |
| 151 | + method: 'POST', |
| 152 | + headers: { |
| 153 | + 'Content-Type': 'application/json', |
| 154 | + 'svix-id': svixId, |
| 155 | + 'svix-timestamp': ts, |
| 156 | + 'svix-signature': sig, |
| 157 | + }, |
| 158 | + body: bodyStr, |
| 159 | + }) |
| 160 | + const env = { |
| 161 | + RESEND_WEBHOOK_SECRET: TEST_SECRET, |
| 162 | + DB: { prepare: () => ({ bind: () => ({ first: async () => null, run: async () => ({}) }) }) }, |
| 163 | + } as any |
| 164 | + const ctx = { waitUntil: () => {} } as any |
| 165 | + |
| 166 | + const res = await handleResendWebhook(request, env, ctx) |
| 167 | + // Unknown event type → acknowledged with 200 |
| 168 | + expect(res.status).toBe(200) |
| 169 | + }) |
| 170 | + |
| 171 | + test('skips signature check when RESEND_WEBHOOK_SECRET is not set', async () => { |
| 172 | + const { handleResendWebhook } = await import('../../worker/src/handlers/delivery-status') |
| 173 | + |
| 174 | + const request = new Request('http://localhost/api/resend-webhook', { |
| 175 | + method: 'POST', |
| 176 | + headers: { 'Content-Type': 'application/json' }, |
| 177 | + body: JSON.stringify({ type: 'email.unknown', created_at: '2026-04-04', data: { email_id: 'abc' } }), |
| 178 | + }) |
| 179 | + // No RESEND_WEBHOOK_SECRET in env |
| 180 | + const env = { DB: { prepare: () => ({ bind: () => ({ first: async () => null, run: async () => ({}) }) }) } } as any |
| 181 | + const ctx = { waitUntil: () => {} } as any |
| 182 | + |
| 183 | + const res = await handleResendWebhook(request, env, ctx) |
| 184 | + expect(res.status).toBe(200) |
| 185 | + }) |
68 | 186 | }) |
0 commit comments