|
1 | | -const { cors, getUserAgent, replaceSecretPlaceholder } = require("#server_functions"); |
| 1 | +const { cors, getUserAgent, replaceSecretPlaceholder, isPrivateTarget } = require("#server_functions"); |
| 2 | + |
| 3 | +const mockLookup = vi.fn(() => Promise.resolve([{ address: "93.184.216.34", family: 4 }])); |
| 4 | + |
| 5 | +vi.mock("node:dns", () => ({ |
| 6 | + promises: { |
| 7 | + lookup: mockLookup |
| 8 | + } |
| 9 | +})); |
2 | 10 |
|
3 | 11 | describe("server_functions tests", () => { |
4 | 12 | describe("The replaceSecretPlaceholder method", () => { |
@@ -53,7 +61,7 @@ describe("server_functions tests", () => { |
53 | 61 | }; |
54 | 62 |
|
55 | 63 | request = { |
56 | | - url: "/cors?url=www.test.com" |
| 64 | + url: "/cors?url=http://www.test.com" |
57 | 65 | }; |
58 | 66 | }); |
59 | 67 |
|
@@ -182,4 +190,77 @@ describe("server_functions tests", () => { |
182 | 190 | global.config = previousConfig; |
183 | 191 | }); |
184 | 192 | }); |
| 193 | + |
| 194 | + describe("The isPrivateTarget method", () => { |
| 195 | + beforeEach(() => { |
| 196 | + mockLookup.mockReset(); |
| 197 | + }); |
| 198 | + |
| 199 | + it("Blocks unparseable URLs", async () => { |
| 200 | + expect(await isPrivateTarget("not a url")).toBe(true); |
| 201 | + }); |
| 202 | + |
| 203 | + it("Blocks non-http protocols", async () => { |
| 204 | + expect(await isPrivateTarget("file:///etc/passwd")).toBe(true); |
| 205 | + expect(await isPrivateTarget("ftp://internal/file")).toBe(true); |
| 206 | + }); |
| 207 | + |
| 208 | + it("Blocks localhost", async () => { |
| 209 | + expect(await isPrivateTarget("http://localhost/path")).toBe(true); |
| 210 | + expect(await isPrivateTarget("http://LOCALHOST:8080/")).toBe(true); |
| 211 | + }); |
| 212 | + |
| 213 | + it("Blocks private IPs (loopback)", async () => { |
| 214 | + mockLookup.mockResolvedValue([{ address: "127.0.0.1", family: 4 }]); |
| 215 | + expect(await isPrivateTarget("http://loopback.example.com/")).toBe(true); |
| 216 | + }); |
| 217 | + |
| 218 | + it("Blocks private IPs (RFC 1918)", async () => { |
| 219 | + mockLookup.mockResolvedValue([{ address: "192.168.1.1", family: 4 }]); |
| 220 | + expect(await isPrivateTarget("http://internal.example.com/")).toBe(true); |
| 221 | + }); |
| 222 | + |
| 223 | + it("Blocks link-local addresses", async () => { |
| 224 | + mockLookup.mockResolvedValue([{ address: "169.254.169.254", family: 4 }]); |
| 225 | + expect(await isPrivateTarget("http://metadata.example.com/")).toBe(true); |
| 226 | + }); |
| 227 | + |
| 228 | + it("Blocks when DNS lookup fails", async () => { |
| 229 | + mockLookup.mockRejectedValue(new Error("ENOTFOUND")); |
| 230 | + expect(await isPrivateTarget("http://nonexistent.invalid/")).toBe(true); |
| 231 | + }); |
| 232 | + |
| 233 | + it("Allows public unicast IPs", async () => { |
| 234 | + mockLookup.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]); |
| 235 | + expect(await isPrivateTarget("http://example.com/api")).toBe(false); |
| 236 | + }); |
| 237 | + |
| 238 | + it("Blocks if any resolved address is private", async () => { |
| 239 | + mockLookup.mockResolvedValue([ |
| 240 | + { address: "93.184.216.34", family: 4 }, |
| 241 | + { address: "127.0.0.1", family: 4 } |
| 242 | + ]); |
| 243 | + expect(await isPrivateTarget("http://dual.example.com/")).toBe(true); |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
| 247 | + describe("The cors method blocks SSRF", () => { |
| 248 | + it("Returns 403 for private target URLs", async () => { |
| 249 | + mockLookup.mockReset(); |
| 250 | + mockLookup.mockResolvedValue([{ address: "127.0.0.1", family: 4 }]); |
| 251 | + |
| 252 | + const request = { url: "/cors?url=http://127.0.0.1:8080/config" }; |
| 253 | + const response = { |
| 254 | + set: vi.fn(), |
| 255 | + send: vi.fn(), |
| 256 | + status: vi.fn(function () { return this; }), |
| 257 | + json: vi.fn() |
| 258 | + }; |
| 259 | + |
| 260 | + await cors(request, response); |
| 261 | + |
| 262 | + expect(response.status).toHaveBeenCalledWith(403); |
| 263 | + expect(response.json).toHaveBeenCalledWith({ error: "Forbidden: private or reserved addresses are not allowed" }); |
| 264 | + }); |
| 265 | + }); |
185 | 266 | }); |
0 commit comments