|
| 1 | +import { isPrivateIP, validateWebhookEndpoint } from '../../src/utils/webhookEndpointValidator'; |
| 2 | + |
| 3 | +describe('isPrivateIP', () => { |
| 4 | + describe('should block private/reserved IPv4', () => { |
| 5 | + it.each([ |
| 6 | + ['127.0.0.1'], |
| 7 | + ['127.255.255.255'], |
| 8 | + ['10.0.0.1'], |
| 9 | + ['10.255.255.255'], |
| 10 | + ['0.0.0.0'], |
| 11 | + ['172.16.0.1'], |
| 12 | + ['172.31.255.255'], |
| 13 | + ['192.168.0.1'], |
| 14 | + ['192.168.255.255'], |
| 15 | + ['169.254.1.1'], |
| 16 | + ['169.254.169.254'], |
| 17 | + ['100.64.0.1'], |
| 18 | + ['100.127.255.255'], |
| 19 | + ])('%s', (ip) => { |
| 20 | + expect(isPrivateIP(ip)).toBe(true); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('should block broadcast and multicast IPv4', () => { |
| 25 | + it.each([ |
| 26 | + ['255.255.255.255'], |
| 27 | + ['224.0.0.1'], |
| 28 | + ['239.255.255.255'], |
| 29 | + ['230.1.2.3'], |
| 30 | + ])('%s', (ip) => { |
| 31 | + expect(isPrivateIP(ip)).toBe(true); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('should block documentation and benchmarking IPv4', () => { |
| 36 | + it.each([ |
| 37 | + ['192.0.2.1'], |
| 38 | + ['198.51.100.1'], |
| 39 | + ['203.0.113.1'], |
| 40 | + ['198.18.0.1'], |
| 41 | + ['198.19.255.255'], |
| 42 | + ])('%s', (ip) => { |
| 43 | + expect(isPrivateIP(ip)).toBe(true); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('should block private/reserved IPv6', () => { |
| 48 | + it.each([ |
| 49 | + ['::1'], |
| 50 | + ['::'], |
| 51 | + ['fe80::1'], |
| 52 | + ['FE80::abc'], |
| 53 | + ['fc00::1'], |
| 54 | + ['fd12:3456::1'], |
| 55 | + ])('%s', (ip) => { |
| 56 | + expect(isPrivateIP(ip)).toBe(true); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('should block IPv6 multicast', () => { |
| 61 | + it.each([ |
| 62 | + ['ff02::1'], |
| 63 | + ['ff05::2'], |
| 64 | + ['FF0E::1'], |
| 65 | + ])('%s', (ip) => { |
| 66 | + expect(isPrivateIP(ip)).toBe(true); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('should block IPv6 with zone ID', () => { |
| 71 | + it.each([ |
| 72 | + ['fe80::1%lo0'], |
| 73 | + ['fe80::1%eth0'], |
| 74 | + ['::1%lo0'], |
| 75 | + ])('%s', (ip) => { |
| 76 | + expect(isPrivateIP(ip)).toBe(true); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('should block IPv4-mapped IPv6', () => { |
| 81 | + it.each([ |
| 82 | + ['::ffff:127.0.0.1'], |
| 83 | + ['::ffff:10.0.0.1'], |
| 84 | + ['::ffff:192.168.1.1'], |
| 85 | + ['::ffff:172.16.0.1'], |
| 86 | + ['::ffff:169.254.169.254'], |
| 87 | + ['::ffff:100.64.0.1'], |
| 88 | + ['::ffff:0.0.0.0'], |
| 89 | + ['::FFFF:127.0.0.1'], |
| 90 | + ])('%s', (ip) => { |
| 91 | + expect(isPrivateIP(ip)).toBe(true); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('should allow public IPv4', () => { |
| 96 | + it.each([ |
| 97 | + ['8.8.8.8'], |
| 98 | + ['1.1.1.1'], |
| 99 | + ['93.184.216.34'], |
| 100 | + ['172.32.0.1'], |
| 101 | + ['172.15.255.255'], |
| 102 | + ['192.169.0.1'], |
| 103 | + ['100.128.0.1'], |
| 104 | + ['100.63.255.255'], |
| 105 | + ['169.255.0.1'], |
| 106 | + ['223.255.255.255'], |
| 107 | + ])('%s', (ip) => { |
| 108 | + expect(isPrivateIP(ip)).toBe(false); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('should allow public IPv6', () => { |
| 113 | + it.each([ |
| 114 | + ['2001:db8::1'], |
| 115 | + ['2606:4700::1'], |
| 116 | + ])('%s', (ip) => { |
| 117 | + expect(isPrivateIP(ip)).toBe(false); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('should allow public IPv4-mapped IPv6', () => { |
| 122 | + it.each([ |
| 123 | + ['::ffff:8.8.8.8'], |
| 124 | + ['::ffff:93.184.216.34'], |
| 125 | + ])('%s', (ip) => { |
| 126 | + expect(isPrivateIP(ip)).toBe(false); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('validateWebhookEndpoint', () => { |
| 132 | + it('should reject invalid URL', async () => { |
| 133 | + expect(await validateWebhookEndpoint('not-a-url')).toBe('Invalid webhook URL'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should reject ftp protocol', async () => { |
| 137 | + expect(await validateWebhookEndpoint('ftp://example.com/hook')).toBe('Webhook URL must use http or https protocol'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should reject non-standard ports', async () => { |
| 141 | + const result = await validateWebhookEndpoint('https://example.com:8080/hook'); |
| 142 | + |
| 143 | + expect(result).toMatch(/port.*not allowed/); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should reject localhost', async () => { |
| 147 | + const result = await validateWebhookEndpoint('http://localhost/hook'); |
| 148 | + |
| 149 | + expect(result).toMatch(/not allowed/); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should reject .local hostnames', async () => { |
| 153 | + const result = await validateWebhookEndpoint('http://myapp.local/hook'); |
| 154 | + |
| 155 | + expect(result).toMatch(/not allowed/); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should reject private IP in URL', async () => { |
| 159 | + const result = await validateWebhookEndpoint('http://127.0.0.1/hook'); |
| 160 | + |
| 161 | + expect(result).toMatch(/private/i); |
| 162 | + }); |
| 163 | + |
| 164 | + it('should reject 169.254.169.254 (metadata)', async () => { |
| 165 | + const result = await validateWebhookEndpoint('http://169.254.169.254/latest/meta-data'); |
| 166 | + |
| 167 | + expect(result).toMatch(/private/i); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should accept valid public https URL', async () => { |
| 171 | + const result = await validateWebhookEndpoint('https://example.com/hawk-webhook'); |
| 172 | + |
| 173 | + expect(result).toBeNull(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should accept valid public http URL on port 80', async () => { |
| 177 | + const result = await validateWebhookEndpoint('http://example.com/hawk-webhook'); |
| 178 | + |
| 179 | + expect(result).toBeNull(); |
| 180 | + }); |
| 181 | +}); |
0 commit comments