Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(ssrf): block special-use IPv4 ranges in isPrivateAddress

SeashoreShi599450a9c2ed
diff --git a/src/utils/ssrf.test.ts b/src/utils/ssrf.test.ts
new file mode 100644
index 00000000..eeec94ed
--- /dev/null
+++ b/src/utils/ssrf.test.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it } from 'vitest';
+import { isPrivateAddress } from './ssrf.js';
+
+describe('isPrivateAddress', () => {
+ it('flags loopback, private, link-local and CGNAT blocks', () => {
+ for (const ip of [
+ '0.0.0.0',
+ '10.1.2.3',
+ '127.0.0.1',
+ '169.254.169.254',
+ '172.16.0.1',
+ '172.31.255.255',
+ '192.168.1.1',
+ '100.64.0.1',
+ '100.127.255.255',
+ '::1',
+ '::',
+ 'fe80::1',
+ 'fd00::1',
+ ]) {
+ expect(isPrivateAddress(ip), ip).toBe(true);
+ }
+ });
+
+ it('flags special-use / non-global IPv4 blocks', () => {
+ for (const ip of [
+ '198.18.0.1', // benchmark 198.18.0.0/15
+ '198.19.255.255',
+ '192.0.0.1', // IETF protocol assignments
+ '192.0.2.1', // TEST-NET-1
+ '198.51.100.1', // TEST-NET-2
+ '203.0.113.1', // TEST-NET-3
+ '224.0.0.1', // multicast
+ '240.0.0.1', // reserved
+ ]) {
+ expect(isPrivateAddress(ip), ip).toBe(true);
+ }
+ });
+
+ it('allows ordinary public addresses', () => {
+ for (const ip of [
+ '8.8.8.8',
+ '1.1.1.1',
+ '93.184.216.34',
+ '172.32.0.1',
+ '198.20.0.1',
+ '203.0.114.1',
+ ]) {
+ expect(isPrivateAddress(ip), ip).toBe(false);
+ }
+ });
+});
diff --git a/src/utils/ssrf.ts b/src/utils/ssrf.ts
index 4f928bd9..08a7ea5b 100644
--- a/src/utils/ssrf.ts
+++ b/src/utils/ssrf.ts
@@ -16,12 +16,19 @@ export function isPrivateAddress(ip: string): boolean {
if (ip.startsWith('fe80:') || ip.startsWith('fc') || ip.startsWith('fd')) return true;
const v4 = ip.split('.').map((p) => parseInt(p, 10));
if (v4.length !== 4 || v4.some((p) => Number.isNaN(p))) return Boolean(ip.startsWith('::ffff:'));
- const [a, b] = v4;
+ const [a, b, c] = v4;
if (a === 0 || a === 10 || a === 127) return true;
if (a === 169 && b === 254) return true; // link-local incl. cloud metadata
if (a === 172 && b >= 16 && b <= 31) return true;
if (a === 192 && b === 168) return true;
if (a === 100 && b >= 64 && b <= 127) return true; // CGNAT
+ // Special-use / non-global blocks (RFC 6890): benchmarking, IETF protocol
+ // assignments and the TEST-NET ranges. Not globally routable, so not valid
+ // fetch targets.
+ if (a === 198 && (b === 18 || b === 19)) return true; // 198.18.0.0/15 benchmark
+ if (a === 192 && b === 0 && (c === 0 || c === 2)) return true; // 192.0.0.0/24, 192.0.2.0/24
+ if (a === 198 && b === 51 && c === 100) return true; // 198.51.100.0/24 TEST-NET-2
+ if (a === 203 && b === 0 && c === 113) return true; // 203.0.113.0/24 TEST-NET-3
if (a >= 224) return true; // multicast + reserved
return false;
}