Skip to content

Commit 03f2689

Browse files
fix: prevent SSRF via /cors endpoint by blocking private/reserved IPs (#4084)
Resolve target hostname before proxying and reject any address that is not globally routable (loopback, RFC 1918, link-local, etc.) using ipaddr.js and dns.lookup().
1 parent dce2df6 commit 03f2689

2 files changed

Lines changed: 122 additions & 3 deletions

File tree

js/server_functions.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
1+
const dns = require("node:dns").promises;
12
const fs = require("node:fs");
23
const path = require("node:path");
4+
const ipaddr = require("ipaddr.js");
35
const Log = require("logger");
46

57
const startUp = new Date();
68

9+
/**
10+
* Checks whether a URL targets a private, reserved, or otherwise non-globally-routable address.
11+
* Used to prevent SSRF (Server-Side Request Forgery) via the /cors proxy endpoint.
12+
* @param {string} url - The URL to check.
13+
* @returns {Promise<boolean>} true if the target is private/reserved and should be blocked.
14+
*/
15+
async function isPrivateTarget (url) {
16+
let parsed;
17+
try {
18+
parsed = new URL(url);
19+
} catch {
20+
return true;
21+
}
22+
23+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") return true;
24+
25+
const hostname = parsed.hostname.replace(/^\[|\]$/g, "");
26+
27+
if (hostname.toLowerCase() === "localhost") return true;
28+
29+
try {
30+
const results = await dns.lookup(hostname, { all: true });
31+
for (const { address } of results) {
32+
if (ipaddr.process(address).range() !== "unicast") return true;
33+
}
34+
} catch {
35+
return true;
36+
}
37+
return false;
38+
}
39+
740
/**
841
* Gets the startup time.
942
* @param {Request} req - the request
@@ -52,6 +85,11 @@ async function cors (req, res) {
5285
}
5386
}
5487

88+
if (await isPrivateTarget(url)) {
89+
Log.warn(`SSRF blocked: ${url}`);
90+
return res.status(403).json({ error: "Forbidden: private or reserved addresses are not allowed" });
91+
}
92+
5593
const headersToSend = getHeadersToSend(req.url);
5694
const expectedReceivedHeaders = geExpectedReceivedHeaders(req.url);
5795
Log.log(`cors url: ${url}`);
@@ -202,4 +240,4 @@ function getConfigFilePath () {
202240
return path.resolve(global.configuration_file || `${global.root_path}/config/config.js`);
203241
}
204242

205-
module.exports = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getConfigFilePath, replaceSecretPlaceholder };
243+
module.exports = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getConfigFilePath, replaceSecretPlaceholder, isPrivateTarget };

tests/unit/functions/server_functions_spec.js

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
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+
}));
210

311
describe("server_functions tests", () => {
412
describe("The replaceSecretPlaceholder method", () => {
@@ -53,7 +61,7 @@ describe("server_functions tests", () => {
5361
};
5462

5563
request = {
56-
url: "/cors?url=www.test.com"
64+
url: "/cors?url=http://www.test.com"
5765
};
5866
});
5967

@@ -182,4 +190,77 @@ describe("server_functions tests", () => {
182190
global.config = previousConfig;
183191
});
184192
});
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+
});
185266
});

0 commit comments

Comments
 (0)