|
| 1 | +import crypto from 'node:crypto' |
| 2 | +import { NextRequest } from 'next/server' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | +import { linearHandler } from '@/lib/webhooks/providers/linear' |
| 5 | + |
| 6 | +function signLinearBody(secret: string, rawBody: string): string { |
| 7 | + return crypto.createHmac('sha256', secret).update(rawBody, 'utf8').digest('hex') |
| 8 | +} |
| 9 | + |
| 10 | +function requestWithLinearSignature(secret: string, rawBody: string): NextRequest { |
| 11 | + const signature = signLinearBody(secret, rawBody) |
| 12 | + return new NextRequest('http://localhost/test', { |
| 13 | + headers: { |
| 14 | + 'Linear-Signature': signature, |
| 15 | + }, |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +describe('Linear webhook provider', () => { |
| 20 | + it('rejects signed requests when webhookTimestamp is missing', async () => { |
| 21 | + const secret = 'linear-secret' |
| 22 | + const rawBody = JSON.stringify({ |
| 23 | + action: 'create', |
| 24 | + type: 'Issue', |
| 25 | + }) |
| 26 | + |
| 27 | + const res = await linearHandler.verifyAuth!({ |
| 28 | + request: requestWithLinearSignature(secret, rawBody), |
| 29 | + rawBody, |
| 30 | + requestId: 'linear-t1', |
| 31 | + providerConfig: { webhookSecret: secret }, |
| 32 | + webhook: {}, |
| 33 | + workflow: {}, |
| 34 | + }) |
| 35 | + |
| 36 | + expect(res?.status).toBe(401) |
| 37 | + }) |
| 38 | + |
| 39 | + it('rejects signed requests when webhookTimestamp skew is too large', async () => { |
| 40 | + const secret = 'linear-secret' |
| 41 | + const rawBody = JSON.stringify({ |
| 42 | + action: 'update', |
| 43 | + type: 'Issue', |
| 44 | + webhookTimestamp: Date.now() - 120_000, |
| 45 | + }) |
| 46 | + |
| 47 | + const res = await linearHandler.verifyAuth!({ |
| 48 | + request: requestWithLinearSignature(secret, rawBody), |
| 49 | + rawBody, |
| 50 | + requestId: 'linear-t2', |
| 51 | + providerConfig: { webhookSecret: secret }, |
| 52 | + webhook: {}, |
| 53 | + workflow: {}, |
| 54 | + }) |
| 55 | + |
| 56 | + expect(res?.status).toBe(401) |
| 57 | + }) |
| 58 | + |
| 59 | + it('accepts signed requests within the allowed timestamp window', async () => { |
| 60 | + const secret = 'linear-secret' |
| 61 | + const rawBody = JSON.stringify({ |
| 62 | + action: 'update', |
| 63 | + type: 'Issue', |
| 64 | + webhookTimestamp: Date.now(), |
| 65 | + }) |
| 66 | + |
| 67 | + const res = await linearHandler.verifyAuth!({ |
| 68 | + request: requestWithLinearSignature(secret, rawBody), |
| 69 | + rawBody, |
| 70 | + requestId: 'linear-t3', |
| 71 | + providerConfig: { webhookSecret: secret }, |
| 72 | + webhook: {}, |
| 73 | + workflow: {}, |
| 74 | + }) |
| 75 | + |
| 76 | + expect(res).toBeNull() |
| 77 | + }) |
| 78 | +}) |
0 commit comments