import { test } from "node:test"; import assert from "node:assert/strict"; import { connectedArchives, disconnectedArchiveRefusal, isPlatformHost, recordPage, recordPageRefusal, sameOrigin } from "./remoteArchives"; import { fetchOnOrigin, OffOriginError, plainGetJson, type FetchLike } from "./remoteArchives-server"; import { detectPlatform } from "./detectPlatform.mjs"; // demo epic D1 review, blocker 2: an archive is untrusted input. test("a record's page URL: only a video platform's host, the channel's own platform, no credentials", () => { const r = (u: string | undefined, p: string | null = null) => recordPageRefusal(u, p, detectPlatform); assert.equal(r("https://www.youtube.com/watch?v=a1", "youtube"), null); assert.equal(r("https://rumble.com/v9-x.html"), null); assert.match(r("http://127.0.0.1:3001/api/ops/settings")!, /127\.0\.0\.1:3001 is not a video platform's host/); assert.match(r("http://10.0.0.5/threads/123/")!, /is not a video platform's host/, "xenforo is a path on any host: never a window's page"); assert.match(r("https://evil.example/watch?v=a1")!, /is not a video platform's host/); assert.match(r("https://rumble.com/v9-x.html", "youtube")!, /rumble\.com is rumble, and the channel is youtube's/); assert.match(r("https://user:pw@www.youtube.com/watch?v=a1")!, /is not a page URL a window is fetched from/); assert.match(r("file:///etc/passwd")!, /is not a page URL a window is fetched from/); assert.equal(r(undefined), "no page URL"); }); test("a record's page URL: a look-alike of a platform's host is not that platform (re-review, must-fix 1)", () => { const r = (u: string, p: string | null = null) => recordPageRefusal(u, p, detectPlatform); // detectPlatform says "youtube" of each of these (a bare endsWith); the host table does not. for (const u of [ "https://evilyoutube.com/watch?v=a1", "https://notrumble.com/v9-x.html", "https://fakeodysee.com/@a/b", "https://xtwitch.tv/videos/1", "https://akick.com/video/1", "https://www.youtube.com.evil.example/watch?v=a1", "https://youtu.be.evil.example/a1", "https://web.archive.org/web/2020/https://www.youtube.com/watch?v=a1", "https://notbitchute.com/video/a/", "https://notyoutube.com/watch?v=a1", "https://evil-rumble.com/v9-x.html", "https://rumble.comevil.example/v9-x.html", "https://evil.example/youtube.com/watch?v=a1", "https://youtube.com%2eevil.example/watch?v=a1", "https://youtubе.com/watch?v=a1", // a Cyrillic е ]) { assert.match(r(u)!, /is not a video platform's host/, u); } assert.match(r("https://evilyoutube.com/watch?v=a1", "youtube")!, /^evilyoutube\.com is not a video platform's host/); // The platforms' own hosts, bare and under a subdomain. for (const u of [ "https://youtube.com/watch?v=a1", "https://m.youtube.com/watch?v=a1", "https://music.youtube.com/watch?v=a1", "https://youtu.be/a1", "https://rumble.com/v9-x.html", "https://www.rumble.com/v9-x.html", "https://odysee.com/@a/b", "https://www.twitch.tv/videos/1", "https://kick.com/video/1", "https://archive.org/details/x", "https://www.archive.org/details/x", "https://www.bitchute.com/video/a/", "https://old.bitchute.com/video/a/", ]) { assert.equal(r(u), null, u); } assert.equal(isPlatformHost("youtube", "YOUTUBE.com"), true); assert.equal(isPlatformHost("youtube", "youtube.com.evil.example"), false); assert.equal(isPlatformHost("twitter", "x.com"), false, "not a video platform: no table, no host"); }); // demo epic D1 re-check: the host judged must be the host fetched. Node's URL // parser and Python's urlsplit (yt-dlp's) read these differently. test("a page URL two parsers read differently is refused unparsed: a backslash, whitespace, a control character", () => { const r = (u: string, p: string | null = null) => recordPageRefusal(u, p, detectPlatform); // What Node makes of the first: the platform's own host. Python's urlsplit says evil.example. assert.equal(new URL("https://youtube.com\\@evil.example/watch?v=x").hostname, "youtube.com"); assert.equal(new URL("https://you\ttube.com/watch?v=x").hostname, "youtube.com"); for (const u of [ "https://youtube.com\\@evil.example/watch?v=x", "https://youtube.com\\@127.0.0.1/x", "https://rumble.com\\.evil.example/v1", "https://you\ttube.com/watch?v=x", " https://youtube.com/watch?v=x", "https://youtube.com/watch?v=x ", "https://youtube.com/watch?v=x\n", "https://youtube.com\r\n@evil.example/", "https://youtube.com/\\evil.example", "https:\\\\youtube.com/watch?v=x", "https://youtube.com/watch?v=x\u0000", "https://youtube.com/wa tch?v=x", "https://youtube.com /watch?v=x", "https://youtube.com @evil.example/", "​https://youtube.com/watch?v=x", "https://youtube.com/watch?v=x", "https://youtube.com/watch?v=x\u007f", ]) { assert.match(r(u) ?? "(allowed)", /carries a backslash, whitespace or a control character/, JSON.stringify(u)); assert.match(r(u, "youtube") ?? "(allowed)", /carries a backslash, whitespace or a control character/, JSON.stringify(u)); } }); test("a page URL that passes is handed on as the parser wrote it back, never as it was given", () => { const page = (u: string, p: string | null = null) => recordPage(u, p, detectPlatform); for (const [given, href, platform] of [ ["https://www.youtube.com/watch?v=a1", "https://www.youtube.com/watch?v=a1", "youtube"], ["HTTPS://WWW.YouTube.com:443/watch?v=a1", "https://www.youtube.com/watch?v=a1", "youtube"], ["https:youtube.com/watch?v=a1", "https://youtube.com/watch?v=a1", "youtube"], ["https:///youtu.be/a1", "https://youtu.be/a1", "youtube"], ["http://RUMBLE.com", "http://rumble.com/", "rumble"], ["https://rumble.com/a/../v9-x.html", "https://rumble.com/v9-x.html", "rumble"], ["https://youtube.com./watch?v=a1", "https://youtube.com./watch?v=a1", "youtube"], ] as const) { const got = page(given); if ("refused" in got) { // A spelling the platform table does not vouch for is refused, not passed raw. assert.equal(given, "https://youtube.com./watch?v=a1", `${given}: ${got.refused}`); continue; } assert.deepEqual(got, { href, platform }, given); assert.equal(got.href, new URL(given).href); // And the href is its own fixed point: judged again it is the same URL. assert.deepEqual(page(got.href), got); } assert.deepEqual(page("https://rumble.com/v9-x.html", "youtube"), { refused: "rumble.com is rumble, and the channel is youtube's: the archive's page URL is not fetched" }); }); test("an archive removed from settings is not read for the channel registered from it (re-review, item 6)", () => { const list = connectedArchives([{ id: "stub", origin: "https://stub.test", title: "Stub", addedAt: "2026-11-11T00:00:00Z" }], {}); assert.equal(disconnectedArchiveRefusal("https://stub.test", list), null); assert.equal(disconnectedArchiveRefusal("https://stub.test/", list), null); assert.equal(disconnectedArchiveRefusal(undefined, list), null, "a channel naming no archive has none to lose"); assert.match(disconnectedArchiveRefusal("https://stub.test", [])!, /^its archive https:\/\/stub\.test is no longer connected/); // The environment's archive counts as connected. assert.equal(disconnectedArchiveRefusal("https://env.test", connectedArchives([], { TRANSCRIPT_SITE_URL: "https://env.test" })), null); }); function redirecting(map: Record): FetchLike & { asked: string[] } { const asked: string[] = []; const f = (async (url: string) => { asked.push(url); const d = map[url]; if (!d) return new Response("nope", { status: 404 }); return new Response(d.body === undefined ? null : JSON.stringify(d.body), { status: d.status, headers: d.location ? { location: d.location } : {} }); }) as FetchLike & { asked: string[] }; f.asked = asked; return f; } test("an archive's documents are read on its own origin: a same-origin redirect is followed, one elsewhere refused unfollowed", async () => { const f = redirecting({ "https://stub.test/corpus.json": { status: 301, location: "/v2/corpus.json" }, "https://stub.test/v2/corpus.json": { status: 200, body: { ok: 1 } }, "https://stub.test/transcripts/a/manifest.json": { status: 302, location: "http://127.0.0.1:3001/api/ops/settings" }, }); assert.deepEqual(await plainGetJson(f)("https://stub.test/corpus.json"), { ok: 1 }); await assert.rejects(fetchOnOrigin(f, "https://stub.test/transcripts/a/manifest.json"), (e: unknown) => e instanceof OffOriginError && /redirects to http:\/\/127\.0\.0\.1:3001/.test((e as Error).message)); assert.equal(f.asked.includes("http://127.0.0.1:3001/api/ops/settings"), false); assert.equal(sameOrigin("https://stub.test:443/x", "https://stub.test"), true); assert.equal(sameOrigin("https://stub.test:8443/x", "https://stub.test"), false); });