|
| 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 | +}); |
0 commit comments