|
1 | 1 | // fallow-ignore-file code-duplication |
2 | 2 | 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"; |
4 | 12 | import { tmpdir } from "node:os"; |
5 | 13 | import { join } from "node:path"; |
6 | 14 | import { assertPublicHttpsUrl, downloadToTemp, UrlDownloadError } from "./urlDownloader.js"; |
@@ -83,6 +91,29 @@ describe("assertPublicHttpsUrl — SSRF guard", () => { |
83 | 91 | expect(() => assertPublicHttpsUrl("https://[::1]/secret")).toThrow("private/reserved"); |
84 | 92 | }); |
85 | 93 |
|
| 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 | + |
86 | 117 | it("rejects invalid URLs", () => { |
87 | 118 | expect(() => assertPublicHttpsUrl("not-a-url")).toThrow("Invalid URL"); |
88 | 119 | expect(() => assertPublicHttpsUrl("")).toThrow("Invalid URL"); |
@@ -134,6 +165,25 @@ describe("downloadToTemp atomic publication and bounded retry", () => { |
134 | 165 | expect(temporaryDownloadEntries(dir)).toEqual([]); |
135 | 166 | }); |
136 | 167 |
|
| 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 | + |
137 | 187 | it("retries one HTTP 503 and publishes only the complete final file", async () => { |
138 | 188 | const fetchMock = vi |
139 | 189 | .fn() |
@@ -330,6 +380,23 @@ describe("downloadToTemp atomic publication and bounded retry", () => { |
330 | 380 | expect(temporaryDownloadEntries(dir)).toEqual([]); |
331 | 381 | }); |
332 | 382 |
|
| 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 | + |
333 | 400 | it("does not retry caller cancellation", async () => { |
334 | 401 | const fetchMock = vi.fn(); |
335 | 402 | vi.stubGlobal("fetch", fetchMock); |
|
0 commit comments