|
| 1 | +import crypto from 'crypto'; |
| 2 | +import { verifyWebhookSignature } from './webhook-signature.utils'; |
| 3 | + |
| 4 | +// ── Helpers ─────────────────────────────────────────────────────────────────── |
| 5 | + |
| 6 | +const secret = 'test-webhook-signing-secret'; |
| 7 | +const payload = Buffer.from(JSON.stringify({ event_type: 'buy', amount: '10' })); |
| 8 | + |
| 9 | +function computeValidHeader(): string { |
| 10 | + const hex = crypto.createHmac('sha256', secret).update(payload).digest('hex'); |
| 11 | + return `sha256=${hex}`; |
| 12 | +} |
| 13 | + |
| 14 | +function flipHexChar(char: string): string { |
| 15 | + return char === '0' ? '1' : '0'; |
| 16 | +} |
| 17 | + |
| 18 | +// ── Tests ───────────────────────────────────────────────────────────────────── |
| 19 | + |
| 20 | +describe('verifyWebhookSignature', () => { |
| 21 | + it('returns true for a valid signature', () => { |
| 22 | + expect(verifyWebhookSignature(payload, computeValidHeader(), secret)).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns false when the signature differs in the last character', () => { |
| 26 | + const header = computeValidHeader(); |
| 27 | + const tampered = |
| 28 | + header.slice(0, -1) + flipHexChar(header[header.length - 1]); |
| 29 | + |
| 30 | + expect(verifyWebhookSignature(payload, tampered, secret)).toBe(false); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns false when the signature differs in the first character', () => { |
| 34 | + const header = computeValidHeader(); |
| 35 | + const prefix = 'sha256='; |
| 36 | + const firstHexChar = header[prefix.length]; |
| 37 | + const tampered = |
| 38 | + prefix + flipHexChar(firstHexChar) + header.slice(prefix.length + 1); |
| 39 | + |
| 40 | + expect(verifyWebhookSignature(payload, tampered, secret)).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns false without throwing when the signature is one character shorter than expected', () => { |
| 44 | + const header = computeValidHeader(); |
| 45 | + const shortened = header.slice(0, -1); |
| 46 | + |
| 47 | + expect(() => verifyWebhookSignature(payload, shortened, secret)).not.toThrow(); |
| 48 | + expect(verifyWebhookSignature(payload, shortened, secret)).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns false without throwing when the signature is one character longer than expected', () => { |
| 52 | + const header = computeValidHeader(); |
| 53 | + const lengthened = `${header}a`; |
| 54 | + |
| 55 | + expect(() => verifyWebhookSignature(payload, lengthened, secret)).not.toThrow(); |
| 56 | + expect(verifyWebhookSignature(payload, lengthened, secret)).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns false without throwing for a malformed header', () => { |
| 60 | + expect(() => |
| 61 | + verifyWebhookSignature(payload, 'not-a-valid-header', secret) |
| 62 | + ).not.toThrow(); |
| 63 | + expect(verifyWebhookSignature(payload, 'not-a-valid-header', secret)).toBe( |
| 64 | + false |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns false for an empty header', () => { |
| 69 | + expect(verifyWebhookSignature(payload, '', secret)).toBe(false); |
| 70 | + }); |
| 71 | +}); |
0 commit comments