Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 281 additions & 0 deletions packages/engine/src/utils/urlDownloader.restrictedNetwork.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { EventEmitter } from "node:events";
import { createServer, type IncomingHttpHeaders, type Server } from "node:http";
import { connect, isIP, type AddressInfo, type LookupFunction } from "node:net";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { gzipSync } from "node:zlib";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => ({
lookup: vi.fn(),
resolve4: vi.fn(),
execFile: vi.fn(),
serverPort: 0,
dialFailure: undefined as string | undefined,
}));

vi.mock("node:dns", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:dns")>();
class Resolver {
resolve4 = mocks.resolve4;
cancel() {}
}
const promises = { ...actual.promises, Resolver };
return {
...actual,
default: { ...actual, lookup: mocks.lookup, promises },
lookup: mocks.lookup,
promises,
};
});
vi.mock("node:child_process", async (importOriginal) => ({
...(await importOriginal<typeof import("node:child_process")>()),
execFile: mocks.execFile,
}));
// The downloader's own lookup decides; an approved dial then lands on the local test server in clear text.
vi.mock("undici", async (importOriginal) => {
const actual = await importOriginal<typeof import("undici")>();
class Agent extends actual.Agent {
constructor(options: { connect: { lookup: LookupFunction } }) {
super({
connect: (target, callback) => {
const dial = (error: Error | null) => {
if (error) return callback(error, null);
if (mocks.dialFailure) {
const failure = Object.assign(new Error(`connect ${mocks.dialFailure}`), {
code: mocks.dialFailure,
});
return callback(failure, null);
}
const socket = connect(mocks.serverPort, "127.0.0.1");
socket.once("connect", () => callback(null, socket));
socket.once("error", (socketError) => callback(socketError, null));
};
const host = target.hostname.replace(/^\[|\]$/g, "");
if (isIP(host)) return dial(null);
options.connect.lookup(host, {}, (error) => dial(error));
},
});
}
}
return { ...actual, Agent };
});

import { downloadToTemp, fetchPublicHttpsText, UrlDownloadError } from "./urlDownloader.js";

type Route = { status: number; headers?: Record<string, string>; body?: string | Buffer };
let routes: Record<string, Route | "drop"> = {};
let received: Array<{ url: string; headers: IncomingHttpHeaders }> = [];
const BODY = "\u0000\u0001asset-bytes";
let server: Server;

beforeAll(async () => {
server = createServer((req, res) => {
const url = `https://${req.headers.host}${req.url}`;
received.push({ url, headers: req.headers });
const found = routes[url];
if (found === "drop") return req.socket.destroy();
if (!found) return res.writeHead(599).end();
res.writeHead(found.status, found.headers).end(found.body ?? "");
});
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
mocks.serverPort = (server.address() as AddressInfo).port;
});
afterAll(() => new Promise<void>((resolve) => server.close(() => resolve())));

function answers(ipv4: string[], ipv6: string[] = []): void {
const all = [
...ipv4.map((address) => ({ address, family: 4 })),
...ipv6.map((address) => ({ address, family: 6 })),
];
mocks.lookup.mockImplementation((_host, _options, callback) => callback(null, all));
if (ipv4.length) mocks.resolve4.mockResolvedValue(ipv4);
else
mocks.resolve4.mockRejectedValue(
Object.assign(new Error("queryA ENODATA"), { code: "ENODATA" }),
);
}

const dirs: string[] = [];
function tempDir(): string {
const dir = mkdtempSync(join(tmpdir(), "hf-restricted-download-"));
dirs.push(dir);
return dir;
}

beforeEach(() => {
vi.resetAllMocks();
routes = {};
received = [];
mocks.dialFailure = undefined;
mocks.execFile.mockImplementation((_file, _args, _options, callback) => {
callback(new Error("Command failed"), "", "ENOTFOUND");
return Object.assign(new EventEmitter(), { kill: vi.fn() });
});
});

afterEach(() => {
vi.unstubAllEnvs();
vi.unstubAllGlobals();
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});

describe("downloads on a render service follow the capture proxy's address rule", () => {
beforeEach(() => vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "true"));

it("refuses an IPv6 literal host before any request", async () => {
routes["https://[2606:4700:4700::1111]/a.png"] = { status: 200, body: BODY };
await expect(downloadToTemp("https://[2606:4700:4700::1111]/a.png", tempDir())).rejects.toThrow(
/not permitted/,
);
expect(received).toEqual([]);
});

it("refuses a name that has only IPv6 answers", async () => {
answers([], ["2606:4700::6810:84e5"]);
routes["https://v6only.assets-cdn.com/a.png"] = { status: 200, body: BODY };
await expect(
downloadToTemp("https://v6only.assets-cdn.com/a.png", tempDir()),
).rejects.toBeInstanceOf(UrlDownloadError);
expect(received).toEqual([]);
});

it("refuses a redirect hop to an IPv6 literal", async () => {
answers(["93.184.216.34"]);
routes["https://cdn.assets-public.com/a.png"] = {
status: 302,
headers: { location: "https://[2606:4700:4700::1111]/b.png" },
};
routes["https://[2606:4700:4700::1111]/b.png"] = { status: 200, body: BODY };
await expect(
downloadToTemp("https://cdn.assets-public.com/a.png", tempDir()),
).rejects.toMatchObject({
kind: "http_rejected",
message: "Download redirect target is not permitted",
});
expect(received.map((request) => request.url)).toEqual(["https://cdn.assets-public.com/a.png"]);
});

it.each([
["not matched by NO_PROXY", ""],
["matched by NO_PROXY", "assets.private-cdn.com"],
])(
"behind an env proxy, refuses a name that resolves to a private address %s",
async (_, noProxy) => {
vi.stubEnv("HTTPS_PROXY", "http://proxy.corp-gateway.com:3128");
vi.stubEnv("NODE_USE_ENV_PROXY", "1");
vi.stubEnv("NO_PROXY", noProxy);
const viaEnvProxy = vi.fn(async () => new Response(BODY));
vi.stubGlobal("fetch", viaEnvProxy);
answers(["10.1.2.3"]);
routes["https://assets.private-cdn.com/a.png"] = { status: 200, body: BODY };
await expect(
downloadToTemp("https://assets.private-cdn.com/a.png", tempDir()),
).rejects.toMatchObject({
kind: "http_rejected",
retryable: false,
message: "Download URL is not permitted",
});
expect(viaEnvProxy).not.toHaveBeenCalled();
expect(received).toEqual([]);
},
);

it("downloads from a public IPv4 host after checking its answers", async () => {
answers(["93.184.216.34"], ["2606:2800:220:1:248:1893:25c8:1946"]);
routes["https://cdn.assets-public.com/a.png"] = { status: 200, body: BODY };
const path = await downloadToTemp("https://cdn.assets-public.com/a.png", tempDir());
expect(readFileSync(path, "utf8")).toBe(BODY);
expect(mocks.resolve4).toHaveBeenCalledWith("cdn.assets-public.com");
expect(mocks.lookup).not.toHaveBeenCalled();
});

it("sends a User-Agent and Accept header", async () => {
answers(["93.184.216.34"]);
routes["https://upload.media-wiki-host.org/a.png"] = { status: 200, body: BODY };
await downloadToTemp("https://upload.media-wiki-host.org/a.png", tempDir());
expect(received[0]?.headers["user-agent"]).toBeTruthy();
expect(received[0]?.headers.accept).toBe("*/*");
});

it("unpacks a gzip text response", async () => {
answers(["93.184.216.34"]);
routes["https://cdn.assets-public.com/app.js"] = {
status: 200,
headers: { "content-encoding": "gzip" },
body: gzipSync("console.log('hello');"),
};
await expect(
fetchPublicHttpsText("https://cdn.assets-public.com/app.js", { maxBytes: 1024 }),
).resolves.toBe("console.log('hello');");
});

it("stops a gzip text response whose unpacked size passes the cap", async () => {
answers(["93.184.216.34"]);
routes["https://cdn.assets-public.com/bomb.js"] = {
status: 200,
headers: { "content-encoding": "gzip" },
body: gzipSync(Buffer.alloc(8 * 1024 * 1024)),
};
await expect(
fetchPublicHttpsText("https://cdn.assets-public.com/bomb.js", { maxBytes: 64 * 1024 }),
).rejects.toMatchObject({ kind: "length_mismatch", retryable: false });
});

it("refuses a compressed asset download, as the default transport does", async () => {
answers(["93.184.216.34"]);
routes["https://cdn.assets-public.com/a.png"] = {
status: 200,
headers: { "content-encoding": "gzip" },
body: gzipSync(BODY),
};
await expect(
downloadToTemp("https://cdn.assets-public.com/a.png", tempDir()),
).rejects.toMatchObject({ kind: "http_rejected", retryable: false });
});

it("retries a gzip-encoded 5xx like any 5xx", async () => {
answers(["93.184.216.34"]);
routes["https://cdn.assets-public.com/a.png"] = {
status: 503,
headers: { "content-encoding": "gzip" },
body: gzipSync("busy"),
};
await expect(
downloadToTemp("https://cdn.assets-public.com/a.png", tempDir()),
).rejects.toMatchObject({ kind: "http_transient", retryable: true });
expect(received).toHaveLength(2);
});

it.each(["EHOSTUNREACH", "ENETUNREACH"])(
"retries an unreachable address (%s) as a network failure",
async (code) => {
answers(["93.184.216.34"]);
mocks.dialFailure = code;
await expect(
downloadToTemp(`https://${code.toLowerCase()}.assets-public.com/a.png`, tempDir()),
).rejects.toMatchObject({ kind: "network", retryable: true });
expect(mocks.resolve4).toHaveBeenCalledTimes(2);
},
);

it("retries a connection dropped mid-response as a network failure", async () => {
answers(["93.184.216.34"]);
routes["https://cdn.assets-public.com/a.png"] = "drop";
await expect(
downloadToTemp("https://cdn.assets-public.com/a.png", tempDir()),
).rejects.toMatchObject({ kind: "network", retryable: true });
expect(received).toHaveLength(2);
});
});

describe("downloads with the render-service setting off", () => {
it("keep the default lookup, and accept a public IPv6 literal", async () => {
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "");
routes["https://[2606:4700:4700::1111]/a.png"] = { status: 200, body: BODY };
const path = await downloadToTemp("https://[2606:4700:4700::1111]/a.png", tempDir());
expect(readFileSync(path, "utf8")).toBe(BODY);
expect(mocks.resolve4).not.toHaveBeenCalled();
});
});
67 changes: 55 additions & 12 deletions packages/engine/src/utils/urlDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import dns from "node:dns";
import { fetchGoesThroughEnvProxy } from "@hyperframes/core/env-proxy";
import { type LookupFunction } from "node:net";
import { isBlockedNetworkHost } from "./networkPolicy.js";
import { resolveRestrictPageNetwork } from "../config.js";
import {
assertPublicHostname,
NonPublicHostError,
resolvePublicAddress,
} from "../services/publicNetwork/publicNetwork.js";
import { dirname, extname, join } from "path";
import { Readable, Transform } from "stream";
import { pipeline } from "stream/promises";
Expand Down Expand Up @@ -240,11 +246,21 @@ export function assertPublicHttpsUrl(url: string): void {
if (parsed.protocol !== "https:") {
throw new Error(`[URLDownloader] Only HTTPS URLs are permitted in compositions`);
}
if (isBlockedNetworkHost(parsed.hostname)) {
if (isBlockedNetworkHost(parsed.hostname) || refusedByRestrictedNetwork(parsed.hostname)) {
throw new Error("[URLDownloader] URL targets a private/reserved address and is not permitted");
}
}

function refusedByRestrictedNetwork(hostname: string): boolean {
if (!resolveRestrictPageNetwork()) return false;
try {
assertPublicHostname(hostname);
return false;
} catch {
return true;
}
}

function getFilenameFromUrl(url: string, validationScope: string): string {
const physicalIdentity = validationScope === "" ? url : `${url}\0${validationScope}`;
const hash = createHash("md5").update(physicalIdentity).digest("hex").slice(0, 12);
Expand Down Expand Up @@ -438,6 +454,12 @@ const MAX_REDIRECTS = 5;

const NON_PUBLIC_ADDRESS_CODE = "HF_NON_PUBLIC_ADDRESS";

function nonPublicAddressError(): Error {
return Object.assign(new Error("host resolves to a non-public address"), {
code: NON_PUBLIC_ADDRESS_CODE,
});
}

/** Resolves a host and refuses to connect when any answer is a non-public address. */
const publicOnlyLookup: LookupFunction = (hostname, options, callback) => {
dns.lookup(hostname, { ...options, all: true }, (error, answers) => {
Expand All @@ -446,28 +468,49 @@ const publicOnlyLookup: LookupFunction = (hostname, options, callback) => {
answers.length === 0 ||
answers.some((a) => isBlockedNetworkHost(a.address.split("%")[0]!))
) {
const refused = Object.assign(new Error("host resolves to a non-public address"), {
code: NON_PUBLIC_ADDRESS_CODE,
});
return callback(refused, "", 0);
return callback(nonPublicAddressError(), "", 0);
}
if (options.all) return callback(null, answers as never);
return callback(null, answers[0]!.address, answers[0]!.family);
});
};

/** The capture proxy's resolver: IPv4 answers only, every one public, and the checked address is dialled. */
const capturePolicyLookup: LookupFunction = (hostname, options, callback) => {
resolvePublicAddress(hostname).then(
(address) =>
options.all ? callback(null, [{ address, family: 4 }]) : callback(null, address, 4),
(error: unknown) =>
callback(
error instanceof NonPublicHostError ? nonPublicAddressError() : (error as Error),
"",
0,
),
);
};

type DownloadFetch = (url: string, init: RequestInit) => Promise<Response>;

let undiciTransport: Promise<DownloadFetch> | undefined;
const undiciTransports = new Map<LookupFunction, Promise<DownloadFetch>>();

function undiciTransport(lookup: LookupFunction): Promise<DownloadFetch> {
let transport = undiciTransports.get(lookup);
if (!transport) {
transport = import("undici").then(({ Agent, fetch: undiciFetch }) => {
const dispatcher = new Agent({ connect: { lookup } });
return (target, options) =>
undiciFetch(target, { ...(options as object), dispatcher }) as unknown as Promise<Response>;
});
undiciTransports.set(lookup, transport);
}
return transport;
}

const fetchPublic: DownloadFetch = async (url, init) => {
if (resolveRestrictPageNetwork()) return (await undiciTransport(capturePolicyLookup))(url, init);
// Behind an env proxy the proxy resolves the name; the URL and redirect checks are the guard then.
if (fetchGoesThroughEnvProxy()) return fetch(url, init);
undiciTransport ??= import("undici").then(({ Agent, fetch: undiciFetch }) => {
const dispatcher = new Agent({ connect: { lookup: publicOnlyLookup } });
return (target, options) =>
undiciFetch(target, { ...(options as object), dispatcher }) as unknown as Promise<Response>;
});
return (await undiciTransport)(url, init);
return (await undiciTransport(publicOnlyLookup))(url, init);
};

let downloadFetch: DownloadFetch = fetchPublic;
Expand Down
Loading