Skip to content

Commit 7abf24e

Browse files
authored
Merge pull request #5295 from modelcontextprotocol/v2/fix/5130-gzip-refuse-private-addresses
fix(everything): gzip-file-as-resource refuses non-public addresses on every hop
2 parents 5a5aa85 + 645a18d commit 7abf24e

9 files changed

Lines changed: 479 additions & 48 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-everything": minor
3+
---
4+
5+
`gzip-file-as-resource` now refuses to fetch from loopback, private, link-local (cloud metadata) and other non-public addresses, on the caller's URL and on every redirect hop, and applies `GZIP_ALLOWED_DOMAINS` to every hop too. Set `GZIP_ALLOW_PRIVATE_IPS=1` to fetch from a local or private origin as before.

‎src/everything/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ The server serves both protocol eras, over stdio and Streamable HTTP, from one p
2424

2525
Roots, sampling and logging are deprecated as of 2026-07-28 and stay available, labelled deprecated. [Server Features](docs/features.md) marks which era each feature belongs to.
2626

27+
## Configuring `gzip-file-as-resource`
28+
29+
The `gzip-file-as-resource` tool fetches the URL it is given, so these environment variables limit what it will fetch:
30+
31+
| Variable | Default | Effect |
32+
| --- | --- | --- |
33+
| `GZIP_MAX_FETCH_SIZE` | 10 MiB | The largest response, in bytes, it reads |
34+
| `GZIP_MAX_FETCH_TIME_MILLIS` | 30000 | How long a fetch may take, redirects included |
35+
| `GZIP_ALLOWED_DOMAINS` | empty (any name) | Comma-separated host names; a host must be one of them or a subdomain of one. Checked on the caller's URL and on every redirect hop |
36+
| `GZIP_ALLOW_PRIVATE_IPS` | off | Set to `1` or `true` to allow loopback, private (RFC 1918), CGNAT, link-local (cloud metadata) and other non-public addresses |
37+
38+
By default the tool refuses a URL whose host resolves to a non-public address, and follows at most five redirects, checking each hop the same way. `GZIP_ALLOWED_DOMAINS` filters names, not addresses: a listed name that resolves to a private address is still refused unless `GZIP_ALLOW_PRIVATE_IPS` is set. The address is checked before the request and resolved again when it is made, so a DNS answer that changes in between (DNS rebinding) is not caught. `data:` URLs are not fetched, so neither check applies to them.
39+
2740
## Requirements
2841

2942
Node.js 24 or later, when the server runs through `npx` or from source.

‎src/everything/__tests__/gzip-file-as-resource.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
* registering it for later reads, and the errors for bad protocols, oversized
66
* or empty responses and failed fetches. It also guards #4808: two sessions
77
* that use the same file name each keep their own resource. The configurable
8-
* limits are in `gzip-limits.test.ts`.
8+
* limits are in `gzip-limits.test.ts`. Its fixture listens on 127.0.0.1, so it
9+
* opts in to private addresses (#5130) before the server loads; the default
10+
* refusal is in `gzip-private-addresses.test.ts`.
911
*/
12+
import "./private-ips-opted-in.js";
1013
import { createServer as createHttpServer, type Server } from "node:http";
1114
import { once } from "node:events";
1215
import { gunzipSync } from "node:zlib";

‎src/everything/__tests__/gzip-limits.test.ts‎

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/**
22
* Characterizes the environment variables that configure
33
* `gzip-file-as-resource` (#4854): `GZIP_MAX_FETCH_SIZE`,
4-
* `GZIP_MAX_FETCH_TIME_MILLIS` and `GZIP_ALLOWED_DOMAINS`. The tool reads them
5-
* once, when its module loads, so this file sets them before it imports the
6-
* server (vitest gives each test file its own module graph).
4+
* `GZIP_MAX_FETCH_TIME_MILLIS` and `GZIP_ALLOWED_DOMAINS`, which is applied to
5+
* every redirect hop (#5130). The tool reads them once, when its module loads,
6+
* so this file sets them before it imports the server (vitest gives each test
7+
* file its own module graph). Its fixture is on loopback, so it also sets
8+
* `GZIP_ALLOW_PRIVATE_IPS`.
79
*/
810
import { createServer as createHttpServer, type Server } from "node:http";
911
import { once } from "node:events";
@@ -25,11 +27,32 @@ let port: number;
2527
beforeAll(async () => {
2628
vi.stubEnv("GZIP_MAX_FETCH_SIZE", "16");
2729
vi.stubEnv("GZIP_MAX_FETCH_TIME_MILLIS", "150");
28-
vi.stubEnv("GZIP_ALLOWED_DOMAINS", " Example.COM, localhost ,,");
30+
vi.stubEnv("GZIP_ALLOWED_DOMAINS", " Example.COM, localhost ,,[::1]");
31+
vi.stubEnv("GZIP_ALLOW_PRIVATE_IPS", "1");
2932
harness = await import("./harness.js");
3033

3134
http = createHttpServer((req, res) => {
3235
if (req.url === "/hang") return; // never answers
36+
if (req.url === "/redirect") {
37+
res.writeHead(302, { location: "/file" }).end("moved");
38+
return;
39+
}
40+
if (req.url === "/off-list") {
41+
res.writeHead(307, { location: `http://127.0.0.1:${port}/file` }).end();
42+
return;
43+
}
44+
if (req.url === "/to-missing") {
45+
res.writeHead(302, { location: "/missing" }).end();
46+
return;
47+
}
48+
if (req.url === "/missing") {
49+
res.writeHead(404).end("not here");
50+
return;
51+
}
52+
if (req.url === "/loop") {
53+
res.writeHead(301, { location: "/loop" }).end();
54+
return;
55+
}
3356
res.end("small");
3457
});
3558
http.listen(0); // every interface, so both localhost and 127.0.0.1 reach it
@@ -81,6 +104,12 @@ describe("gzip-file-as-resource limits", () => {
81104
});
82105
});
83106

107+
it("matches a listed IPv6 literal with its brackets", async () => {
108+
expect(await gzip(`http://[::1]:${port}/file`)).toMatchObject({
109+
type: "resource_link",
110+
});
111+
});
112+
84113
it("refuses a host that only ends with a listed domain's name", async () => {
85114
const lookalike = "https://notexample.com/x";
86115
expect(await gzipText(lookalike)).toBe(
@@ -99,4 +128,31 @@ describe("gzip-file-as-resource limits", () => {
99128
`Fetching ${url} took more than 150 ms and was aborted.`,
100129
);
101130
});
131+
132+
it("follows a redirect to a listed host", async () => {
133+
expect(await gzip(`http://localhost:${port}/redirect`)).toMatchObject({
134+
type: "resource_link",
135+
uri: "demo://resource/session/limited.gz",
136+
});
137+
});
138+
139+
it("reports the status of a redirect's final response, not the redirect's", async () => {
140+
const url = `http://localhost:${port}/to-missing`;
141+
expect(await gzipText(url)).toBe(
142+
`Fetching ${url} failed with HTTP 404 Not Found`,
143+
);
144+
});
145+
146+
it("applies GZIP_ALLOWED_DOMAINS to a redirect hop (#5130)", async () => {
147+
expect(await gzipText(`http://localhost:${port}/off-list`)).toBe(
148+
`Error processing file http://127.0.0.1:${port}/file: Domain 127.0.0.1 is not in the allowed domains list.`,
149+
);
150+
});
151+
152+
it("abandons a fetch after five redirects", async () => {
153+
const url = `http://localhost:${port}/loop`;
154+
expect(await gzipText(url)).toBe(
155+
`Fetching ${url} followed more than 5 redirects and was abandoned.`,
156+
);
157+
});
102158
});
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/**
2+
* Characterizes how `gzip-file-as-resource` treats non-public destinations
3+
* with its default configuration (#5130): a URL whose host is, or resolves
4+
* to, a loopback, private, link-local or other non-public address is refused
5+
* before anything is requested, and so is a redirect from a public origin to
6+
* one. A public origin cannot be reached from a test, so `node:dns/promises`
7+
* and `fetch` are stubbed for one name, `public.example`; every other name
8+
* and URL goes to the real resolver and the real `fetch`. The opt-in
9+
* (`GZIP_ALLOW_PRIVATE_IPS`) is exercised by the other gzip suites, whose
10+
* fixtures are on loopback. The fetch deadline is shortened, before the
11+
* server loads, to show that it bounds a lookup that never answers.
12+
*/
13+
import { createServer as createHttpServer, type Server } from "node:http";
14+
import { once } from "node:events";
15+
import {
16+
afterAll,
17+
afterEach,
18+
beforeAll,
19+
describe,
20+
expect,
21+
it,
22+
vi,
23+
} from "vitest";
24+
import { connect, contentOf, textOf, type Session } from "./harness.js";
25+
26+
vi.hoisted(() => {
27+
vi.stubEnv("GZIP_MAX_FETCH_TIME_MILLIS", "500");
28+
});
29+
30+
vi.mock("node:dns/promises", async (importOriginal) => {
31+
const actual = await importOriginal<typeof import("node:dns/promises")>();
32+
const stubbed: Record<string, { address: string; family: number }> = {
33+
"public.example": { address: "93.184.215.14", family: 4 },
34+
"anycast.example": { address: "192.0.0.9", family: 4 }, // inside 192.0.0.0/24
35+
"anycast6.example": { address: "2001:3::1", family: 6 }, // inside 2001::/23
36+
};
37+
return {
38+
...actual,
39+
lookup: (hostname: string, options: { all: true }) => {
40+
if (hostname === "stalled.example") return new Promise(() => {});
41+
const answer = stubbed[hostname];
42+
return answer
43+
? Promise.resolve([answer])
44+
: actual.lookup(hostname, options);
45+
},
46+
};
47+
});
48+
49+
let http: Server;
50+
let port: number;
51+
let hits = 0;
52+
53+
beforeAll(async () => {
54+
http = createHttpServer((_req, res) => {
55+
hits++;
56+
res.end("internal only");
57+
});
58+
http.listen(0); // every interface, so 127.0.0.1, ::1 and localhost reach it
59+
await once(http, "listening");
60+
const address = http.address();
61+
if (address === null || typeof address === "string")
62+
throw new Error("no port");
63+
port = address.port;
64+
65+
const realFetch = globalThis.fetch;
66+
vi.stubGlobal("fetch", (input: URL, init?: RequestInit) => {
67+
if (!input.hostname.endsWith(".example")) return realFetch(input, init);
68+
if (input.pathname === "/to-loopback") {
69+
return Promise.resolve(
70+
new Response(null, {
71+
status: 302,
72+
headers: { location: `http://127.0.0.1:${port}/secret` },
73+
}),
74+
);
75+
}
76+
return Promise.resolve(new Response("public content"));
77+
});
78+
});
79+
80+
afterAll(async () => {
81+
vi.unstubAllGlobals();
82+
vi.unstubAllEnvs();
83+
http.closeAllConnections();
84+
await new Promise((resolve) => http.close(resolve));
85+
});
86+
87+
let session: Session | undefined;
88+
89+
afterEach(async () => {
90+
await session?.close();
91+
session = undefined;
92+
hits = 0;
93+
});
94+
95+
async function gzip(data: string) {
96+
session ??= await connect();
97+
const result = await session.client.callTool({
98+
name: "gzip-file-as-resource",
99+
arguments: { name: "private.gz", data },
100+
});
101+
return contentOf(result)[0];
102+
}
103+
104+
const refusal = (url: string, host: string, address: string) =>
105+
`Error processing file ${url}: ${host} resolves to ${address}, which is not a public address. Set GZIP_ALLOW_PRIVATE_IPS=1 to allow private, loopback and link-local addresses.`;
106+
107+
describe("gzip-file-as-resource private addresses (#5130)", () => {
108+
it("refuses a loopback address by default, without requesting it", async () => {
109+
const url = `http://127.0.0.1:${port}/secret`;
110+
expect(textOf(await gzip(url))).toBe(
111+
refusal(url, "127.0.0.1", "127.0.0.1"),
112+
);
113+
expect(hits).toBe(0);
114+
});
115+
116+
it("refuses an IPv6 loopback address, and an IPv4-mapped one", async () => {
117+
const v6 = `http://[::1]:${port}/secret`;
118+
expect(textOf(await gzip(v6))).toBe(refusal(v6, "::1", "::1"));
119+
const mapped = `http://[::ffff:7f00:1]:${port}/secret`;
120+
expect(textOf(await gzip(`http://[::ffff:127.0.0.1]:${port}/secret`))).toBe(
121+
refusal(mapped, "::ffff:7f00:1", "::ffff:7f00:1"),
122+
);
123+
expect(hits).toBe(0);
124+
});
125+
126+
it("refuses a name that resolves to a loopback address", async () => {
127+
const url = `http://localhost:${port}/secret`;
128+
expect(textOf(await gzip(url))).toMatch(
129+
/^Error processing file http:\/\/localhost:\d+\/secret: localhost resolves to (::1|127\.0\.0\.1), which is not a public address\./,
130+
);
131+
expect(hits).toBe(0);
132+
});
133+
134+
it.each([
135+
["link-local (cloud metadata)", "169.254.169.254"],
136+
["RFC 1918", "10.1.2.3"],
137+
["CGNAT", "100.100.100.200"],
138+
["unspecified", "0.0.0.0"],
139+
["multicast", "224.0.0.1"],
140+
])("refuses a %s address", async (_kind, address) => {
141+
const url = `http://${address}/latest/meta-data/`;
142+
expect(textOf(await gzip(url))).toBe(refusal(url, address, address));
143+
});
144+
145+
it.each([
146+
["unique local", "fd00:ec2::254"],
147+
["link-local", "fe80::1"],
148+
])("refuses an IPv6 %s address", async (_kind, address) => {
149+
const url = `http://[${address}]/latest/meta-data/`;
150+
expect(textOf(await gzip(url))).toBe(refusal(url, address, address));
151+
});
152+
153+
it("fetches from a public address", async () => {
154+
expect(await gzip("http://public.example/file")).toMatchObject({
155+
type: "resource_link",
156+
uri: "demo://resource/session/private.gz",
157+
});
158+
});
159+
160+
it.each([
161+
["IPv4", "anycast.example"],
162+
["IPv6", "anycast6.example"],
163+
])(
164+
"fetches from a globally reachable %s exception inside a refused range",
165+
async (_family, host) => {
166+
expect(await gzip(`http://${host}/file`)).toMatchObject({
167+
type: "resource_link",
168+
});
169+
},
170+
);
171+
172+
it("bounds the address lookup by GZIP_MAX_FETCH_TIME_MILLIS", async () => {
173+
const url = "http://stalled.example/file";
174+
expect(textOf(await gzip(url))).toBe(
175+
`Fetching ${url} took more than 500 ms and was aborted.`,
176+
);
177+
});
178+
179+
it("refuses a redirect from a public origin to a loopback address", async () => {
180+
const hop = `http://127.0.0.1:${port}/secret`;
181+
expect(textOf(await gzip("http://public.example/to-loopback"))).toBe(
182+
refusal(hop, "127.0.0.1", "127.0.0.1"),
183+
);
184+
expect(hits).toBe(0);
185+
});
186+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Opts a test file in to fetching from loopback and other private addresses
3+
* (#5130), for suites whose fixtures listen on 127.0.0.1. Import it first,
4+
* before anything that loads the server: `gzip-file-as-resource` reads
5+
* `GZIP_ALLOW_PRIVATE_IPS` once, when its module is evaluated, and imports are
6+
* evaluated in order (vitest gives each test file its own module graph). The
7+
* variable is unset again when the file's tests are done.
8+
*/
9+
import { afterAll, vi } from "vitest";
10+
11+
vi.stubEnv("GZIP_ALLOW_PRIVATE_IPS", "1");
12+
afterAll(() => {
13+
vi.unstubAllEnvs();
14+
});

‎src/everything/docs/instructions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The server serves two protocol eras. A client on protocol version 2026-07-28 con
1919

2020
## Constraints & Limitations
2121

22-
- `gzip-file-as-resource`: Max fetch size controlled by `GZIP_MAX_FETCH_SIZE` (default 10MB), timeout by `GZIP_MAX_FETCH_TIME_MILLIS` (default 30s), allowed domains by `GZIP_ALLOWED_DOMAINS`
22+
- `gzip-file-as-resource`: Max fetch size controlled by `GZIP_MAX_FETCH_SIZE` (default 10MB), timeout by `GZIP_MAX_FETCH_TIME_MILLIS` (default 30s), allowed domains by `GZIP_ALLOWED_DOMAINS` (checked on every redirect hop). It refuses URLs whose host resolves to a loopback, private or link-local address, unless the server was started with `GZIP_ALLOW_PRIVATE_IPS=1`
2323
- `get-env`: Values of credential-like variable names (`*_KEY`, `*_TOKEN`, `*_SECRET`, `*_PASSWORD` and the like) come back as `"<redacted>"`; the server operator can set `GET_ENV_INCLUDE_SECRETS=1` to return them unmasked
2424
- Session resources are ephemeral and lost when the session ends; minted resources expire after ten minutes
2525
- Roots, sampling and logging are deprecated as of protocol version 2026-07-28, as is the HTTP+SSE transport

‎src/everything/docs/structure.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ src/everything
185185
- Environment controls:
186186
- `GZIP_MAX_FETCH_SIZE` (bytes, default 10 MiB)
187187
- `GZIP_MAX_FETCH_TIME_MILLIS` (ms, default 30000)
188-
- `GZIP_ALLOWED_DOMAINS` (comma-separated allowlist; empty means all domains allowed)
188+
- `GZIP_ALLOWED_DOMAINS` (comma-separated name allowlist, checked on the caller's URL and on every redirect hop; empty means any name is allowed)
189+
- `GZIP_ALLOW_PRIVATE_IPS` (`1`/`true` to allow loopback, private, link-local and other non-public addresses; by default a host that resolves to one is refused, on every hop)
189190
- `simulate-research-query.ts`
190191
- 2026-07-28: registers a `simulate-research-query` tool that runs as a task on the tasks extension, through four timed stages, pausing at `input_required` for a clarification on an ambiguous topic.
191192
- `trigger-elicitation-request.ts`

0 commit comments

Comments
 (0)