|
1 | 1 | import test from 'ava' |
| 2 | +import { Webhook } from 'svix' |
2 | 3 |
|
| 4 | +import { |
| 5 | + isSeamInvalidWebhookPayloadError, |
| 6 | + SeamInvalidWebhookPayloadError, |
| 7 | +} from './seam-invalid-webhook-payload-error.js' |
3 | 8 | import { SeamWebhook } from './seam-webhook.js' |
4 | 9 |
|
| 10 | +const secret = 'MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw' |
| 11 | + |
| 12 | +const eventPayload = JSON.stringify({ |
| 13 | + event_id: '11111111-1111-1111-1111-111111111111', |
| 14 | + event_type: 'device.connected', |
| 15 | + workspace_id: '22222222-2222-2222-2222-222222222222', |
| 16 | + device_id: '33333333-3333-3333-3333-333333333333', |
| 17 | + created_at: '2026-08-27T00:00:00.000Z', |
| 18 | + occurred_at: '2026-08-27T00:00:00.000Z', |
| 19 | +}) |
| 20 | + |
| 21 | +const signHeaders = ( |
| 22 | + payload: string, |
| 23 | + { msgId = 'msg_1', timestamp = new Date(), signingSecret = secret } = {}, |
| 24 | +): Record<string, string> => ({ |
| 25 | + 'svix-id': msgId, |
| 26 | + 'svix-timestamp': Math.floor(timestamp.getTime() / 1000).toString(), |
| 27 | + 'svix-signature': new Webhook(signingSecret).sign(msgId, timestamp, payload), |
| 28 | +}) |
| 29 | + |
5 | 30 | test('SeamWebhook: constructor', (t) => { |
6 | 31 | t.truthy(new SeamWebhook('1234')) |
7 | 32 | }) |
| 33 | + |
| 34 | +test('SeamWebhook: verifies and parses a signed event', (t) => { |
| 35 | + const webhook = new SeamWebhook(secret) |
| 36 | + |
| 37 | + const event = webhook.verify(eventPayload, signHeaders(eventPayload)) |
| 38 | + |
| 39 | + t.is(event.event_type, 'device.connected') |
| 40 | + t.is(event.event_id, '11111111-1111-1111-1111-111111111111') |
| 41 | +}) |
| 42 | + |
| 43 | +test('SeamWebhook: accepts mixed case headers', (t) => { |
| 44 | + const webhook = new SeamWebhook(secret) |
| 45 | + const headers = Object.fromEntries( |
| 46 | + Object.entries(signHeaders(eventPayload)).map(([key, value]) => [ |
| 47 | + key.toUpperCase(), |
| 48 | + value, |
| 49 | + ]), |
| 50 | + ) |
| 51 | + |
| 52 | + t.is(webhook.verify(eventPayload, headers).event_type, 'device.connected') |
| 53 | +}) |
| 54 | + |
| 55 | +test('SeamWebhook: a tampered payload fails verification', (t) => { |
| 56 | + const webhook = new SeamWebhook(secret) |
| 57 | + const headers = signHeaders(eventPayload) |
| 58 | + const tampered = eventPayload.replace( |
| 59 | + 'device.connected', |
| 60 | + 'device.disconnected', |
| 61 | + ) |
| 62 | + |
| 63 | + const error = t.throws(() => webhook.verify(tampered, headers)) |
| 64 | + |
| 65 | + t.false(isSeamInvalidWebhookPayloadError(error)) |
| 66 | + t.regex(error?.message ?? '', /No matching signature/) |
| 67 | +}) |
| 68 | + |
| 69 | +test('SeamWebhook: a wrong secret fails verification', (t) => { |
| 70 | + const webhook = new SeamWebhook(secret) |
| 71 | + const headers = signHeaders(eventPayload, { |
| 72 | + signingSecret: 'WrongQ9r8GKYqrTwjUPD8ILPZIo2LaLa', |
| 73 | + }) |
| 74 | + |
| 75 | + const error = t.throws(() => webhook.verify(eventPayload, headers)) |
| 76 | + |
| 77 | + t.regex(error?.message ?? '', /No matching signature/) |
| 78 | +}) |
| 79 | + |
| 80 | +test('SeamWebhook: an expired timestamp fails verification', (t) => { |
| 81 | + const webhook = new SeamWebhook(secret) |
| 82 | + const headers = signHeaders(eventPayload, { |
| 83 | + timestamp: new Date(Date.now() - 60 * 60 * 1000), |
| 84 | + }) |
| 85 | + |
| 86 | + const error = t.throws(() => webhook.verify(eventPayload, headers)) |
| 87 | + |
| 88 | + t.regex(error?.message ?? '', /too old/) |
| 89 | +}) |
| 90 | + |
| 91 | +for (const missing of ['svix-id', 'svix-timestamp', 'svix-signature']) { |
| 92 | + test(`SeamWebhook: a missing ${missing} header fails verification`, (t) => { |
| 93 | + const webhook = new SeamWebhook(secret) |
| 94 | + const { [missing]: _, ...headers } = signHeaders(eventPayload) |
| 95 | + |
| 96 | + const error = t.throws(() => webhook.verify(eventPayload, headers)) |
| 97 | + |
| 98 | + t.false(isSeamInvalidWebhookPayloadError(error)) |
| 99 | + t.regex(error?.message ?? '', /Missing required headers/) |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +test('SeamWebhook: a signed but unparseable payload is not forgery', (t) => { |
| 104 | + const webhook = new SeamWebhook(secret) |
| 105 | + const payload = '{"event_id": "trailing-comma",}' |
| 106 | + |
| 107 | + const error = t.throws(() => webhook.verify(payload, signHeaders(payload)), { |
| 108 | + instanceOf: SeamInvalidWebhookPayloadError, |
| 109 | + }) |
| 110 | + |
| 111 | + t.regex(error?.message ?? '', /not valid JSON/) |
| 112 | + t.truthy(error?.cause) |
| 113 | +}) |
| 114 | + |
| 115 | +for (const payload of ['null', '[1]', '42', '"event"', '{}']) { |
| 116 | + test(`SeamWebhook: a signed non-event payload ${payload} is not forgery`, (t) => { |
| 117 | + const webhook = new SeamWebhook(secret) |
| 118 | + |
| 119 | + const error = t.throws( |
| 120 | + () => webhook.verify(payload, signHeaders(payload)), |
| 121 | + { |
| 122 | + instanceOf: SeamInvalidWebhookPayloadError, |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + t.regex(error?.message ?? '', /did not contain a Seam event/) |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +test('SeamWebhook: an unknown event type still parses', (t) => { |
| 131 | + const webhook = new SeamWebhook(secret) |
| 132 | + const payload = JSON.stringify({ |
| 133 | + event_id: '11111111-1111-1111-1111-111111111111', |
| 134 | + event_type: 'future.event_type', |
| 135 | + }) |
| 136 | + |
| 137 | + // event_type is a closed union of known types, so widen it to assert |
| 138 | + // that an unrecognized type still round-trips. |
| 139 | + const event: { event_type?: string | undefined } = webhook.verify( |
| 140 | + payload, |
| 141 | + signHeaders(payload), |
| 142 | + ) |
| 143 | + |
| 144 | + t.is(event.event_type, 'future.event_type') |
| 145 | +}) |
0 commit comments