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