Skip to content

Commit 4b81f78

Browse files
committed
fix(engine): close downloader trust-boundary gaps
1 parent 5ce2eb8 commit 4b81f78

2 files changed

Lines changed: 113 additions & 32 deletions

File tree

‎packages/engine/src/utils/urlDownloader.test.ts‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// fallow-ignore-file code-duplication
22
import { afterEach, describe, expect, it, vi } from "vitest";
3-
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
3+
import {
4+
existsSync,
5+
mkdtempSync,
6+
readFileSync,
7+
readdirSync,
8+
rmSync,
9+
symlinkSync,
10+
writeFileSync,
11+
} from "node:fs";
412
import { tmpdir } from "node:os";
513
import { join } from "node:path";
614
import { assertPublicHttpsUrl, downloadToTemp, UrlDownloadError } from "./urlDownloader.js";
@@ -83,6 +91,29 @@ describe("assertPublicHttpsUrl — SSRF guard", () => {
8391
expect(() => assertPublicHttpsUrl("https://[::1]/secret")).toThrow("private/reserved");
8492
});
8593

94+
it("rejects normalized reserved IPv6 and IPv4-mapped forms", () => {
95+
for (const url of [
96+
"https://[::]/secret",
97+
"https://[fe80::1]/secret",
98+
"https://[fc00::1]/secret",
99+
"https://[::ffff:127.0.0.1]/secret",
100+
"https://[::ffff:169.254.169.254]/latest/meta-data/",
101+
]) {
102+
expect(() => assertPublicHttpsUrl(url), url).toThrow("private/reserved");
103+
}
104+
});
105+
106+
it("rejects CGNAT and other non-public IPv4 ranges", () => {
107+
for (const url of [
108+
"https://100.64.0.1/secret",
109+
"https://198.18.0.1/secret",
110+
"https://192.0.2.1/secret",
111+
"https://224.0.0.1/secret",
112+
]) {
113+
expect(() => assertPublicHttpsUrl(url), url).toThrow("private/reserved");
114+
}
115+
});
116+
86117
it("rejects invalid URLs", () => {
87118
expect(() => assertPublicHttpsUrl("not-a-url")).toThrow("Invalid URL");
88119
expect(() => assertPublicHttpsUrl("")).toThrow("Invalid URL");
@@ -134,6 +165,25 @@ describe("downloadToTemp atomic publication and bounded retry", () => {
134165
expect(temporaryDownloadEntries(dir)).toEqual([]);
135166
});
136167

168+
it("rejects an IPv4-mapped IMDS redirect before issuing the second request", async () => {
169+
const fetchMock = vi.fn().mockResolvedValueOnce(
170+
new Response(null, {
171+
status: 302,
172+
headers: { location: "https://[::ffff:169.254.169.254]/latest/meta-data/" },
173+
}),
174+
);
175+
vi.stubGlobal("fetch", fetchMock);
176+
const dir = makeTempDir();
177+
178+
await expect(
179+
downloadToTemp("https://cdn.example/mapped-private-redirect.mp4", dir, 1_000),
180+
).rejects.toMatchObject({
181+
kind: "http_rejected",
182+
retryable: false,
183+
} satisfies Partial<UrlDownloadError>);
184+
expect(fetchMock).toHaveBeenCalledTimes(1);
185+
});
186+
137187
it("retries one HTTP 503 and publishes only the complete final file", async () => {
138188
const fetchMock = vi
139189
.fn()
@@ -330,6 +380,23 @@ describe("downloadToTemp atomic publication and bounded retry", () => {
330380
expect(temporaryDownloadEntries(dir)).toEqual([]);
331381
});
332382

383+
it("does not trust a nonempty symlink at the final cache path", async () => {
384+
const fetchMock = vi.fn().mockResolvedValue(new Response("downloaded"));
385+
vi.stubGlobal("fetch", fetchMock);
386+
const dir = makeTempDir();
387+
const target = join(dir, "attacker-controlled.mp4");
388+
const cachePath = join(dir, "download_eda0de5dc5a3.mp4");
389+
writeFileSync(target, "not-the-download");
390+
symlinkSync(target, cachePath);
391+
392+
const path = await downloadToTemp("https://cdn.example/stale-empty.mp4", dir, 1_000);
393+
394+
expect(path).toBe(cachePath);
395+
expect(fetchMock).toHaveBeenCalledTimes(1);
396+
expect(readFileSync(path, "utf8")).toBe("downloaded");
397+
expect(readFileSync(target, "utf8")).toBe("not-the-download");
398+
});
399+
333400
it("does not retry caller cancellation", async () => {
334401
const fetchMock = vi.fn();
335402
vi.stubGlobal("fetch", fetchMock);

‎packages/engine/src/utils/urlDownloader.ts‎

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
fsyncSync,
66
mkdtempSync,
77
mkdirSync,
8+
lstatSync,
89
openSync,
910
renameSync,
1011
rmSync,
1112
statSync,
1213
} from "fs";
1314
import { createHash } from "crypto";
15+
import { BlockList, isIP } from "node:net";
1416
import { dirname, extname, join } from "path";
1517
import { Readable } from "stream";
1618
import { pipeline } from "stream/promises";
@@ -96,36 +98,47 @@ function isRetryableNetworkCause(error: unknown): boolean {
9698
);
9799
}
98100

99-
// SSRF guard: these prefixes identify non-public address space that
100-
// compositions (customer-supplied) must never be able to reach via the
101-
// download path. Blocks AWS IMDS (169.254.169.254), loopback, RFC1918,
102-
// and unspecified addresses. All comparisons are on the raw hostname
103-
// string; DNS resolution is NOT performed here, so DNS-rebinding bypasses
104-
// are not closed by this check — that gap is acceptable for the risk level.
105-
const BLOCKED_HOST_PREFIXES = [
106-
"169.254.", // link-local / AWS IMDS
107-
"127.", // loopback IPv4
108-
"10.", // RFC1918
109-
"192.168.", // RFC1918
110-
"0.", // unspecified
111-
"[::1]", // loopback IPv6
112-
"[fc", // RFC4193 unique-local IPv6
113-
"[fd", // RFC4193 unique-local IPv6
114-
];
115-
// 172.16.0.0 – 172.31.255.255 (RFC1918)
116-
const BLOCKED_172_RANGE = { min: 16, max: 31 };
101+
const NON_PUBLIC_IPV4_ADDRESSES = new BlockList();
102+
for (const [network, prefix] of [
103+
["0.0.0.0", 8],
104+
["10.0.0.0", 8],
105+
["100.64.0.0", 10],
106+
["127.0.0.0", 8],
107+
["169.254.0.0", 16],
108+
["172.16.0.0", 12],
109+
["192.0.0.0", 24],
110+
["192.0.2.0", 24],
111+
["192.88.99.0", 24],
112+
["192.168.0.0", 16],
113+
["198.18.0.0", 15],
114+
["198.51.100.0", 24],
115+
["203.0.113.0", 24],
116+
["224.0.0.0", 4],
117+
] as const) {
118+
NON_PUBLIC_IPV4_ADDRESSES.addSubnet(network, prefix, "ipv4");
119+
}
120+
const NON_PUBLIC_IPV6_ADDRESSES = new BlockList();
121+
for (const [network, prefix] of [
122+
["::", 128],
123+
["::1", 128],
124+
["::ffff:0:0", 96],
125+
["fc00::", 7],
126+
["fe80::", 10],
127+
["fec0::", 10],
128+
["ff00::", 8],
129+
["2001:db8::", 32],
130+
] as const) {
131+
NON_PUBLIC_IPV6_ADDRESSES.addSubnet(network, prefix, "ipv6");
132+
}
117133

118134
function isBlockedHost(hostname: string): boolean {
119-
const h = hostname.toLowerCase();
135+
const h = hostname.toLowerCase().replace(/^\[|\]$/g, "");
120136
if (h === "localhost") return true;
121-
if (BLOCKED_HOST_PREFIXES.some((p) => h.startsWith(p))) return true;
122-
// 172.16–172.31
123-
const m = h.match(/^172\.(\d{1,3})\./);
124-
if (m) {
125-
const octet = parseInt(m[1] ?? "0", 10);
126-
if (octet >= BLOCKED_172_RANGE.min && octet <= BLOCKED_172_RANGE.max) return true;
127-
}
128-
return false;
137+
const addressType = isIP(h);
138+
if (addressType === 0) return false;
139+
return addressType === 4
140+
? NON_PUBLIC_IPV4_ADDRESSES.check(h, "ipv4")
141+
: NON_PUBLIC_IPV6_ADDRESSES.check(h, "ipv6");
129142
}
130143

131144
/**
@@ -161,10 +174,11 @@ function getFilenameFromUrl(url: string): string {
161174

162175
function hasCompleteFile(path: string): boolean {
163176
if (!existsSync(path)) return false;
164-
if (statSync(path).size > 0) return true;
165-
// Old versions wrote directly to the final path and could leave an empty
166-
// file behind. Never trust that stale cache entry.
167-
rmSync(path, { force: true });
177+
const entry = lstatSync(path);
178+
if (entry.isFile() && entry.size > 0) return true;
179+
// Old versions could leave an empty file behind. Never trust that stale
180+
// cache entry—or a symlink/special entry planted at the final path.
181+
rmSync(path, { recursive: entry.isDirectory(), force: true });
168182
return false;
169183
}
170184

0 commit comments

Comments
 (0)