|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import { strict as assert } from 'node:assert'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { |
| 6 | + assertUrlAllowed, |
| 7 | + isPrivateIPv6, |
| 8 | + isPrivateIpv4, |
| 9 | +} from '../../src/url-security.js'; |
| 10 | +import { testFunction, createTestResults, printTestSummary } from '../helpers/test-utils.js'; |
| 11 | +import { EnvManager } from '../helpers/env-utils.js'; |
| 12 | + |
| 13 | +const results = createTestResults(); |
| 14 | +const envManager = new EnvManager(); |
| 15 | + |
| 16 | +async function runTests() { |
| 17 | + console.log('🧪 Testing: url-security.ts\n'); |
| 18 | + |
| 19 | + await testFunction('isPrivateIpv4 blocks CGNAT boundaries', () => { |
| 20 | + assert.equal(isPrivateIpv4('100.64.0.1'), true); |
| 21 | + assert.equal(isPrivateIpv4('100.127.255.255'), true); |
| 22 | + assert.equal(isPrivateIpv4('100.63.255.255'), false); |
| 23 | + assert.equal(isPrivateIpv4('100.128.0.0'), false); |
| 24 | + }, results); |
| 25 | + |
| 26 | + await testFunction('isPrivateIpv4 blocks benchmarking boundaries', () => { |
| 27 | + assert.equal(isPrivateIpv4('198.18.0.1'), true); |
| 28 | + assert.equal(isPrivateIpv4('198.19.255.255'), true); |
| 29 | + assert.equal(isPrivateIpv4('198.20.0.0'), false); |
| 30 | + }, results); |
| 31 | + |
| 32 | + await testFunction('isPrivateIpv4 blocks multicast and reserved ranges', () => { |
| 33 | + assert.equal(isPrivateIpv4('224.0.0.1'), true); |
| 34 | + assert.equal(isPrivateIpv4('239.255.255.255'), true); |
| 35 | + assert.equal(isPrivateIpv4('240.0.0.1'), true); |
| 36 | + assert.equal(isPrivateIpv4('255.255.255.255'), true); |
| 37 | + }, results); |
| 38 | + |
| 39 | + await testFunction('isPrivateIpv4 blocks IANA special-purpose documentation ranges', () => { |
| 40 | + assert.equal(isPrivateIpv4('192.0.0.1'), true); |
| 41 | + assert.equal(isPrivateIpv4('192.0.2.5'), true); |
| 42 | + assert.equal(isPrivateIpv4('198.51.100.5'), true); |
| 43 | + assert.equal(isPrivateIpv4('203.0.113.5'), true); |
| 44 | + }, results); |
| 45 | + |
| 46 | + await testFunction('isPrivateIpv4 allows public control addresses', () => { |
| 47 | + assert.equal(isPrivateIpv4('8.8.8.8'), false); |
| 48 | + assert.equal(isPrivateIpv4('1.1.1.1'), false); |
| 49 | + assert.equal(isPrivateIpv4('100.128.0.5'), false); |
| 50 | + }, results); |
| 51 | + |
| 52 | + await testFunction('isPrivateIPv6 delegates IPv4-mapped CGNAT addresses to IPv4 check', () => { |
| 53 | + assert.equal(isPrivateIPv6('::ffff:100.64.0.1'), true); |
| 54 | + }, results); |
| 55 | + |
| 56 | + await testFunction('assertUrlAllowed blocks CGNAT by default and honors private URL override', () => { |
| 57 | + envManager.delete('MCP_HTTP_HARDEN'); |
| 58 | + envManager.delete('MCP_HTTP_ALLOW_PRIVATE_URLS'); |
| 59 | + |
| 60 | + try { |
| 61 | + assert.throws( |
| 62 | + () => assertUrlAllowed(new URL('http://100.64.0.1/')), |
| 63 | + /blocked by security policy/, |
| 64 | + ); |
| 65 | + |
| 66 | + envManager.set('MCP_HTTP_ALLOW_PRIVATE_URLS', 'true'); |
| 67 | + assert.doesNotThrow(() => assertUrlAllowed(new URL('http://100.64.0.1/'))); |
| 68 | + } finally { |
| 69 | + envManager.restore(); |
| 70 | + } |
| 71 | + }, results); |
| 72 | + |
| 73 | + printTestSummary(results, 'URL Security Module'); |
| 74 | + return results; |
| 75 | +} |
| 76 | + |
| 77 | +if (process.argv[1] !== undefined && fileURLToPath(import.meta.url) === process.argv[1]) { |
| 78 | + runTests().then(results => { |
| 79 | + process.exit(results.failed > 0 ? 1 : 0); |
| 80 | + }).catch(console.error); |
| 81 | +} |
| 82 | + |
| 83 | +export { runTests }; |
0 commit comments