Skip to content

Commit e18b009

Browse files
cliffhallclaude
andcommitted
fix(everything): gzip-file-as-resource refuses non-public addresses on every hop
gzip-file-as-resource fetched any http(s) URL, loopback, RFC 1918 and link-local (cloud metadata) addresses included, and checked GZIP_ALLOWED_DOMAINS only on the caller's URL, never on a redirect hop. Before every request, the caller's URL and each redirect hop alike, the host is now checked against GZIP_ALLOWED_DOMAINS (when set) and resolved, and the request is refused if any address is not public, unless the new GZIP_ALLOW_PRIVATE_IPS opts in. Redirects are followed by hand, at most five. The loopback-bound gzip suites opt in before the server loads; a new suite covers the default refusal with a stubbed public origin. Closes #5130 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent b1a97f8 commit e18b009

9 files changed

Lines changed: 384 additions & 47 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
@@ -22,6 +22,19 @@ The server serves both protocol eras, over stdio and Streamable HTTP, from one p
2222

2323
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.
2424

25+
## Configuring `gzip-file-as-resource`
26+
27+
The `gzip-file-as-resource` tool fetches the URL it is given, so these environment variables limit what it will fetch:
28+
29+
| Variable | Default | Effect |
30+
| --- | --- | --- |
31+
| `GZIP_MAX_FETCH_SIZE` | 10 MiB | The largest response, in bytes, it reads |
32+
| `GZIP_MAX_FETCH_TIME_MILLIS` | 30000 | How long a fetch may take, redirects included |
33+
| `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 |
34+
| `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 |
35+
36+
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.
37+
2538
## Requirements
2639

2740
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: 53 additions & 3 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";
@@ -26,10 +28,31 @@ beforeAll(async () => {
2628
vi.stubEnv("GZIP_MAX_FETCH_SIZE", "16");
2729
vi.stubEnv("GZIP_MAX_FETCH_TIME_MILLIS", "150");
2830
vi.stubEnv("GZIP_ALLOWED_DOMAINS", " Example.COM, localhost ,,");
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
@@ -99,4 +122,31 @@ describe("gzip-file-as-resource limits", () => {
99122
`Fetching ${url} took more than 150 ms and was aborted.`,
100123
);
101124
});
125+
126+
it("follows a redirect to a listed host", async () => {
127+
expect(await gzip(`http://localhost:${port}/redirect`)).toMatchObject({
128+
type: "resource_link",
129+
uri: "demo://resource/session/limited.gz",
130+
});
131+
});
132+
133+
it("reports the status of a redirect's final response, not the redirect's", async () => {
134+
const url = `http://localhost:${port}/to-missing`;
135+
expect(await gzipText(url)).toBe(
136+
`Fetching ${url} failed with HTTP 404 Not Found`,
137+
);
138+
});
139+
140+
it("applies GZIP_ALLOWED_DOMAINS to a redirect hop (#5130)", async () => {
141+
expect(await gzipText(`http://localhost:${port}/off-list`)).toBe(
142+
`Error processing file http://127.0.0.1:${port}/file: Domain 127.0.0.1 is not in the allowed domains list.`,
143+
);
144+
});
145+
146+
it("abandons a fetch after five redirects", async () => {
147+
const url = `http://localhost:${port}/loop`;
148+
expect(await gzipText(url)).toBe(
149+
`Fetching ${url} followed more than 5 redirects and was abandoned.`,
150+
);
151+
});
102152
});
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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.
11+
*/
12+
import { createServer as createHttpServer, type Server } from "node:http";
13+
import { once } from "node:events";
14+
import {
15+
afterAll,
16+
afterEach,
17+
beforeAll,
18+
describe,
19+
expect,
20+
it,
21+
vi,
22+
} from "vitest";
23+
import { connect, contentOf, textOf, type Session } from "./harness.js";
24+
25+
vi.mock("node:dns/promises", async (importOriginal) => {
26+
const actual = await importOriginal<typeof import("node:dns/promises")>();
27+
return {
28+
...actual,
29+
lookup: (hostname: string, options: { all: true }) =>
30+
hostname === "public.example"
31+
? Promise.resolve([{ address: "93.184.215.14", family: 4 }])
32+
: actual.lookup(hostname, options),
33+
};
34+
});
35+
36+
let http: Server;
37+
let port: number;
38+
let hits = 0;
39+
40+
beforeAll(async () => {
41+
http = createHttpServer((_req, res) => {
42+
hits++;
43+
res.end("internal only");
44+
});
45+
http.listen(0); // every interface, so 127.0.0.1, ::1 and localhost reach it
46+
await once(http, "listening");
47+
const address = http.address();
48+
if (address === null || typeof address === "string")
49+
throw new Error("no port");
50+
port = address.port;
51+
52+
const realFetch = globalThis.fetch;
53+
vi.stubGlobal("fetch", (input: URL, init?: RequestInit) => {
54+
if (input.hostname !== "public.example") return realFetch(input, init);
55+
if (input.pathname === "/to-loopback") {
56+
return Promise.resolve(
57+
new Response(null, {
58+
status: 302,
59+
headers: { location: `http://127.0.0.1:${port}/secret` },
60+
}),
61+
);
62+
}
63+
return Promise.resolve(new Response("public content"));
64+
});
65+
});
66+
67+
afterAll(async () => {
68+
vi.unstubAllGlobals();
69+
http.closeAllConnections();
70+
await new Promise((resolve) => http.close(resolve));
71+
});
72+
73+
let session: Session | undefined;
74+
75+
afterEach(async () => {
76+
await session?.close();
77+
session = undefined;
78+
hits = 0;
79+
});
80+
81+
async function gzip(data: string) {
82+
session ??= await connect();
83+
const result = await session.client.callTool({
84+
name: "gzip-file-as-resource",
85+
arguments: { name: "private.gz", data },
86+
});
87+
return contentOf(result)[0];
88+
}
89+
90+
const refusal = (url: string, host: string, address: string) =>
91+
`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.`;
92+
93+
describe("gzip-file-as-resource private addresses (#5130)", () => {
94+
it("refuses a loopback address by default, without requesting it", async () => {
95+
const url = `http://127.0.0.1:${port}/secret`;
96+
expect(textOf(await gzip(url))).toBe(
97+
refusal(url, "127.0.0.1", "127.0.0.1"),
98+
);
99+
expect(hits).toBe(0);
100+
});
101+
102+
it("refuses an IPv6 loopback address, and an IPv4-mapped one", async () => {
103+
const v6 = `http://[::1]:${port}/secret`;
104+
expect(textOf(await gzip(v6))).toBe(refusal(v6, "::1", "::1"));
105+
const mapped = `http://[::ffff:7f00:1]:${port}/secret`;
106+
expect(textOf(await gzip(`http://[::ffff:127.0.0.1]:${port}/secret`))).toBe(
107+
refusal(mapped, "::ffff:7f00:1", "::ffff:7f00:1"),
108+
);
109+
expect(hits).toBe(0);
110+
});
111+
112+
it("refuses a name that resolves to a loopback address", async () => {
113+
const url = `http://localhost:${port}/secret`;
114+
expect(textOf(await gzip(url))).toMatch(
115+
/^Error processing file http:\/\/localhost:\d+\/secret: localhost resolves to (::1|127\.0\.0\.1), which is not a public address\./,
116+
);
117+
expect(hits).toBe(0);
118+
});
119+
120+
it.each([
121+
["link-local (cloud metadata)", "169.254.169.254"],
122+
["RFC 1918", "10.1.2.3"],
123+
["CGNAT", "100.100.100.200"],
124+
["unspecified", "0.0.0.0"],
125+
["multicast", "224.0.0.1"],
126+
])("refuses a %s address", async (_kind, address) => {
127+
const url = `http://${address}/latest/meta-data/`;
128+
expect(textOf(await gzip(url))).toBe(refusal(url, address, address));
129+
});
130+
131+
it.each([
132+
["unique local", "fd00:ec2::254"],
133+
["link-local", "fe80::1"],
134+
])("refuses an IPv6 %s address", async (_kind, address) => {
135+
const url = `http://[${address}]/latest/meta-data/`;
136+
expect(textOf(await gzip(url))).toBe(refusal(url, address, address));
137+
});
138+
139+
it("fetches from a public address", async () => {
140+
expect(await gzip("http://public.example/file")).toMatchObject({
141+
type: "resource_link",
142+
uri: "demo://resource/session/private.gz",
143+
});
144+
});
145+
146+
it("refuses a redirect from a public origin to a loopback address", async () => {
147+
const hop = `http://127.0.0.1:${port}/secret`;
148+
expect(textOf(await gzip("http://public.example/to-loopback"))).toBe(
149+
refusal(hop, "127.0.0.1", "127.0.0.1"),
150+
);
151+
expect(hits).toBe(0);
152+
});
153+
});
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
- Session resources are ephemeral and lost when the session ends; minted resources expire after ten minutes
2424
- Roots, sampling and logging are deprecated as of protocol version 2026-07-28, as is the HTTP+SSE transport
2525

‎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)