|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createDefaultNetworkAdapter } from "../../../src/index.js"; |
| 3 | +import { isPrivateIp } from "../../../src/node/driver.js"; |
| 4 | + |
| 5 | +describe("SSRF protection", () => { |
| 6 | + // --------------------------------------------------------------- |
| 7 | + // isPrivateIp — unit coverage for all reserved ranges |
| 8 | + // --------------------------------------------------------------- |
| 9 | + |
| 10 | + describe("isPrivateIp", () => { |
| 11 | + it.each([ |
| 12 | + ["10.0.0.1", true], // 10.0.0.0/8 |
| 13 | + ["10.255.255.255", true], |
| 14 | + ["172.16.0.1", true], // 172.16.0.0/12 |
| 15 | + ["172.31.255.255", true], |
| 16 | + ["172.15.0.1", false], // just below range |
| 17 | + ["172.32.0.1", false], // just above range |
| 18 | + ["192.168.0.1", true], // 192.168.0.0/16 |
| 19 | + ["192.168.255.255", true], |
| 20 | + ["127.0.0.1", true], // 127.0.0.0/8 |
| 21 | + ["127.255.255.255", true], |
| 22 | + ["169.254.169.254", true], // 169.254.0.0/16 (link-local / metadata) |
| 23 | + ["169.254.0.1", true], |
| 24 | + ["0.0.0.0", true], // 0.0.0.0/8 |
| 25 | + ["224.0.0.1", true], // multicast |
| 26 | + ["239.255.255.255", true], |
| 27 | + ["240.0.0.1", true], // reserved |
| 28 | + ["255.255.255.255", true], |
| 29 | + ["8.8.8.8", false], // public |
| 30 | + ["1.1.1.1", false], |
| 31 | + ["142.250.80.46", false], // google |
| 32 | + ])("IPv4 %s → %s", (ip, expected) => { |
| 33 | + expect(isPrivateIp(ip)).toBe(expected); |
| 34 | + }); |
| 35 | + |
| 36 | + it.each([ |
| 37 | + ["::1", true], // loopback |
| 38 | + ["::", true], // unspecified |
| 39 | + ["fc00::1", true], // ULA fc00::/7 |
| 40 | + ["fd12:3456::1", true], // ULA fd |
| 41 | + ["fe80::1", true], // link-local |
| 42 | + ["ff02::1", true], // multicast |
| 43 | + ["2607:f8b0:4004::1", false], // public (google) |
| 44 | + ])("IPv6 %s → %s", (ip, expected) => { |
| 45 | + expect(isPrivateIp(ip)).toBe(expected); |
| 46 | + }); |
| 47 | + |
| 48 | + it("detects IPv4-mapped IPv6 addresses", () => { |
| 49 | + expect(isPrivateIp("::ffff:10.0.0.1")).toBe(true); |
| 50 | + expect(isPrivateIp("::ffff:169.254.169.254")).toBe(true); |
| 51 | + expect(isPrivateIp("::ffff:8.8.8.8")).toBe(false); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + // --------------------------------------------------------------- |
| 56 | + // Network adapter SSRF blocking |
| 57 | + // --------------------------------------------------------------- |
| 58 | + |
| 59 | + describe("network adapter blocks private IPs", () => { |
| 60 | + const adapter = createDefaultNetworkAdapter(); |
| 61 | + |
| 62 | + it("fetch blocks metadata endpoint 169.254.169.254", async () => { |
| 63 | + await expect( |
| 64 | + adapter.fetch("http://169.254.169.254/latest/meta-data/", {}), |
| 65 | + ).rejects.toThrow(/SSRF blocked/); |
| 66 | + }); |
| 67 | + |
| 68 | + it("fetch blocks 10.x private range", async () => { |
| 69 | + await expect( |
| 70 | + adapter.fetch("http://10.0.0.1/internal", {}), |
| 71 | + ).rejects.toThrow(/SSRF blocked/); |
| 72 | + }); |
| 73 | + |
| 74 | + it("fetch blocks 192.168.x private range", async () => { |
| 75 | + await expect( |
| 76 | + adapter.fetch("http://192.168.1.1/admin", {}), |
| 77 | + ).rejects.toThrow(/SSRF blocked/); |
| 78 | + }); |
| 79 | + |
| 80 | + it("httpRequest blocks metadata endpoint 169.254.169.254", async () => { |
| 81 | + await expect( |
| 82 | + adapter.httpRequest("http://169.254.169.254/latest/meta-data/", {}), |
| 83 | + ).rejects.toThrow(/SSRF blocked/); |
| 84 | + }); |
| 85 | + |
| 86 | + it("httpRequest blocks localhost", async () => { |
| 87 | + await expect( |
| 88 | + adapter.httpRequest("http://127.0.0.1:9999/", {}), |
| 89 | + ).rejects.toThrow(/SSRF blocked/); |
| 90 | + }); |
| 91 | + |
| 92 | + it("fetch allows data: URLs (no network)", async () => { |
| 93 | + const result = await adapter.fetch("data:text/plain,ssrf-test-ok", {}); |
| 94 | + expect(result.ok).toBe(true); |
| 95 | + expect(result.body).toContain("ssrf-test-ok"); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + // --------------------------------------------------------------- |
| 100 | + // Redirect-to-private-IP blocking |
| 101 | + // --------------------------------------------------------------- |
| 102 | + |
| 103 | + describe("redirect to private IP is blocked", () => { |
| 104 | + afterEach(() => { |
| 105 | + vi.restoreAllMocks(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("fetch blocks 302 redirect to private IP", async () => { |
| 109 | + // Mock global fetch to simulate a 302 redirect to a private IP |
| 110 | + const originalFetch = globalThis.fetch; |
| 111 | + const mockFetch = vi.fn().mockResolvedValueOnce( |
| 112 | + new Response(null, { |
| 113 | + status: 302, |
| 114 | + headers: { location: "http://169.254.169.254/latest/meta-data/" }, |
| 115 | + }), |
| 116 | + ); |
| 117 | + vi.stubGlobal("fetch", mockFetch); |
| 118 | + |
| 119 | + const adapter = createDefaultNetworkAdapter(); |
| 120 | + // Use a public-looking IP so the initial check passes |
| 121 | + await expect( |
| 122 | + adapter.fetch("http://8.8.8.8/redirect", {}), |
| 123 | + ).rejects.toThrow(/SSRF blocked/); |
| 124 | + |
| 125 | + vi.stubGlobal("fetch", originalFetch); |
| 126 | + }); |
| 127 | + |
| 128 | + it("fetch blocks 307 redirect to 10.x range", async () => { |
| 129 | + const originalFetch = globalThis.fetch; |
| 130 | + const mockFetch = vi.fn().mockResolvedValueOnce( |
| 131 | + new Response(null, { |
| 132 | + status: 307, |
| 133 | + headers: { location: "http://10.0.0.1/internal-api" }, |
| 134 | + }), |
| 135 | + ); |
| 136 | + vi.stubGlobal("fetch", mockFetch); |
| 137 | + |
| 138 | + const adapter = createDefaultNetworkAdapter(); |
| 139 | + await expect( |
| 140 | + adapter.fetch("http://8.8.8.8/redirect", {}), |
| 141 | + ).rejects.toThrow(/SSRF blocked/); |
| 142 | + |
| 143 | + vi.stubGlobal("fetch", originalFetch); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + // --------------------------------------------------------------- |
| 148 | + // DNS rebinding — documented as known limitation |
| 149 | + // --------------------------------------------------------------- |
| 150 | + |
| 151 | + describe("DNS rebinding", () => { |
| 152 | + it("known limitation: DNS rebinding after initial check is not blocked at the adapter level", () => { |
| 153 | + // DNS rebinding attacks involve a hostname that resolves to a safe public IP |
| 154 | + // on the first lookup (passing the SSRF check) but resolves to a private IP on |
| 155 | + // the subsequent connection. Fully mitigating this requires either: |
| 156 | + // - Pinning the resolved IP for the connection (not possible with native fetch) |
| 157 | + // - Using a custom DNS resolver with caching and TTL enforcement |
| 158 | + // |
| 159 | + // This is documented as a known limitation. The pre-flight DNS check still |
| 160 | + // provides defense in depth against most SSRF vectors including direct IP |
| 161 | + // access, redirect-based attacks, and static DNS entries. |
| 162 | + expect(true).toBe(true); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments