|
| 1 | +import { Request, Response } from 'express'; |
| 2 | +import crypto from 'crypto'; |
| 3 | +import { createWebhookSignatureMiddleware } from '../webhookSignatureMiddleware'; |
| 4 | +import { WebhookSecret } from '../../../../src/types/webhook'; |
| 5 | + |
| 6 | +describe('createWebhookSignatureMiddleware', () => { |
| 7 | + let req: Partial<Request>; |
| 8 | + let res: Partial<Response>; |
| 9 | + let next: jest.Mock; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + req = { |
| 13 | + headers: {}, |
| 14 | + }; |
| 15 | + res = { |
| 16 | + status: jest.fn().mockReturnThis(), |
| 17 | + json: jest.fn(), |
| 18 | + }; |
| 19 | + next = jest.fn(); |
| 20 | + }); |
| 21 | + |
| 22 | + const signPayload = (payload: string, secret: string, includePrefix = false) => { |
| 23 | + const hmac = crypto.createHmac('sha256', secret); |
| 24 | + hmac.update(Buffer.from(payload, 'utf8')); |
| 25 | + const hex = hmac.digest('hex'); |
| 26 | + return includePrefix ? `sha256=${hex}` : hex; |
| 27 | + }; |
| 28 | + |
| 29 | + it('should return 401 if signature header is missing', async () => { |
| 30 | + const middleware = createWebhookSignatureMiddleware({ |
| 31 | + secrets: [{ key: 'secret', validFrom: 0, createdAt: 0 }], |
| 32 | + }); |
| 33 | + |
| 34 | + await middleware(req as Request, res as Response, next); |
| 35 | + |
| 36 | + expect(res.status).toHaveBeenCalledWith(401); |
| 37 | + expect(res.json).toHaveBeenCalledWith({ error: 'Missing X-SubTrackr-Signature header' }); |
| 38 | + expect(next).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return 500 if raw body is missing', async () => { |
| 42 | + req.headers!['x-subtrackr-signature'] = 'dummy-signature'; |
| 43 | + |
| 44 | + const middleware = createWebhookSignatureMiddleware({ |
| 45 | + secrets: [{ key: 'secret', validFrom: 0, createdAt: 0 }], |
| 46 | + }); |
| 47 | + |
| 48 | + await middleware(req as Request, res as Response, next); |
| 49 | + |
| 50 | + expect(res.status).toHaveBeenCalledWith(500); |
| 51 | + expect(res.json).toHaveBeenCalledWith({ error: 'Raw request body not available for signature verification' }); |
| 52 | + expect(next).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should verify valid signature correctly without prefix', async () => { |
| 56 | + const payload = JSON.stringify({ event: 'test' }); |
| 57 | + const secret = 'my-secret-key'; |
| 58 | + const signature = signPayload(payload, secret); |
| 59 | + |
| 60 | + req.headers!['x-subtrackr-signature'] = signature; |
| 61 | + (req as any).rawBody = payload; |
| 62 | + |
| 63 | + const middleware = createWebhookSignatureMiddleware({ |
| 64 | + secrets: [{ key: secret, validFrom: 0, createdAt: 0 }], |
| 65 | + }); |
| 66 | + |
| 67 | + await middleware(req as Request, res as Response, next); |
| 68 | + |
| 69 | + expect(next).toHaveBeenCalledWith(); // success |
| 70 | + expect(res.status).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should verify valid signature correctly with sha256= prefix', async () => { |
| 74 | + const payload = JSON.stringify({ event: 'test' }); |
| 75 | + const secret = 'my-secret-key'; |
| 76 | + const signature = signPayload(payload, secret, true); // sha256=... |
| 77 | + |
| 78 | + req.headers!['x-subtrackr-signature'] = signature; |
| 79 | + (req as any).rawBody = payload; |
| 80 | + |
| 81 | + const middleware = createWebhookSignatureMiddleware({ |
| 82 | + secrets: [{ key: secret, validFrom: 0, createdAt: 0 }], |
| 83 | + }); |
| 84 | + |
| 85 | + await middleware(req as Request, res as Response, next); |
| 86 | + |
| 87 | + expect(next).toHaveBeenCalledWith(); // success |
| 88 | + expect(res.status).not.toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return 401 for an invalid signature', async () => { |
| 92 | + const payload = JSON.stringify({ event: 'test' }); |
| 93 | + const signature = signPayload(payload, 'wrong-secret'); |
| 94 | + |
| 95 | + req.headers!['x-subtrackr-signature'] = signature; |
| 96 | + (req as any).rawBody = payload; |
| 97 | + |
| 98 | + const middleware = createWebhookSignatureMiddleware({ |
| 99 | + secrets: [{ key: 'my-secret-key', validFrom: 0, createdAt: 0 }], |
| 100 | + }); |
| 101 | + |
| 102 | + await middleware(req as Request, res as Response, next); |
| 103 | + |
| 104 | + expect(res.status).toHaveBeenCalledWith(401); |
| 105 | + expect(res.json).toHaveBeenCalledWith({ error: 'Invalid webhook signature' }); |
| 106 | + expect(next).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should support dynamic retrieval of secrets for key rotation', async () => { |
| 110 | + const payload = JSON.stringify({ event: 'test' }); |
| 111 | + const secret1 = 'old-secret-key'; |
| 112 | + const secret2 = 'new-secret-key'; |
| 113 | + const signatureForOld = signPayload(payload, secret1); |
| 114 | + |
| 115 | + req.headers!['x-subtrackr-signature'] = signatureForOld; |
| 116 | + (req as any).rawBody = payload; |
| 117 | + |
| 118 | + // Both secrets valid (during rotation overlap) |
| 119 | + const getSecrets = jest.fn().mockResolvedValue([ |
| 120 | + { key: secret1, validFrom: 0, createdAt: 0 }, |
| 121 | + { key: secret2, validFrom: 0, createdAt: 0 } |
| 122 | + ]); |
| 123 | + |
| 124 | + const middleware = createWebhookSignatureMiddleware({ secrets: getSecrets }); |
| 125 | + |
| 126 | + await middleware(req as Request, res as Response, next); |
| 127 | + |
| 128 | + expect(getSecrets).toHaveBeenCalled(); |
| 129 | + expect(next).toHaveBeenCalledWith(); // Should succeed with old secret |
| 130 | + }); |
| 131 | + |
| 132 | + it('should ignore secrets that are expired or not yet valid', async () => { |
| 133 | + const payload = JSON.stringify({ event: 'test' }); |
| 134 | + const secretExpired = 'expired-key'; |
| 135 | + const signature = signPayload(payload, secretExpired); |
| 136 | + |
| 137 | + req.headers!['x-subtrackr-signature'] = signature; |
| 138 | + (req as any).rawBody = payload; |
| 139 | + |
| 140 | + const now = Date.now(); |
| 141 | + const secrets: WebhookSecret[] = [ |
| 142 | + { key: secretExpired, validFrom: 0, validUntil: now - 10000, createdAt: 0 }, // expired |
| 143 | + { key: 'future-key', validFrom: now + 10000, createdAt: 0 }, // not valid yet |
| 144 | + ]; |
| 145 | + |
| 146 | + const middleware = createWebhookSignatureMiddleware({ secrets }); |
| 147 | + |
| 148 | + await middleware(req as Request, res as Response, next); |
| 149 | + |
| 150 | + expect(res.status).toHaveBeenCalledWith(401); |
| 151 | + expect(res.json).toHaveBeenCalledWith({ error: 'No valid webhook secrets configured' }); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should allow custom header name and raw body extractor', async () => { |
| 155 | + const payload = Buffer.from(JSON.stringify({ event: 'test' }), 'utf8'); |
| 156 | + const secret = 'secret-key'; |
| 157 | + const signature = signPayload(payload.toString('utf8'), secret); |
| 158 | + |
| 159 | + req.headers!['x-custom-signature'] = signature; |
| 160 | + (req as any).customRawBody = payload; // Custom location |
| 161 | + |
| 162 | + const middleware = createWebhookSignatureMiddleware({ |
| 163 | + secrets: [{ key: secret, validFrom: 0, createdAt: 0 }], |
| 164 | + headerName: 'X-Custom-Signature', |
| 165 | + getRawBody: (req) => (req as any).customRawBody, |
| 166 | + }); |
| 167 | + |
| 168 | + await middleware(req as Request, res as Response, next); |
| 169 | + |
| 170 | + expect(next).toHaveBeenCalledWith(); |
| 171 | + }); |
| 172 | +}); |
0 commit comments