diff --git a/packages/engine/src/utils/notMediaPayload.test.ts b/packages/engine/src/utils/notMediaPayload.test.ts index 0d17055c60..3a4719b1a6 100644 --- a/packages/engine/src/utils/notMediaPayload.test.ts +++ b/packages/engine/src/utils/notMediaPayload.test.ts @@ -389,6 +389,66 @@ describe("playlistLeavesItsFolder", () => { }); }); +describe("playlistLeavesItsFolder reads entries and folders as ffmpeg resolves them", () => { + const hls = (...lines: string[]) => ["#EXTM3U", ...lines, "#EXT-X-ENDLIST"].join("\n"); + const listIn = (folder: string, contents: string | Buffer, name = "list.m3u8"): string => { + const dir = join(mkdtempSync(join(tmpdir(), "hf-playlist-folder-")), folder); + mkdirSync(dir); + writeFileSync(join(dir, name), contents); + return join(dir, name); + }; + + it.each([ + ["a drive-relative path", hls("C:Videos\\a.mp4")], + ["a drive-relative file name", hls("c:x.mp4")], + ["a colon after an underscore", hls("_:x.mp4")], + ["an encoded colon", hls("%63%3Ax.mp4")], + ["a colon in a map URI", hls('#EXT-X-MAP:URI="c:x.mp4"', "seg.ts")], + ["a colon in a concat entry", "ffconcat version 1.0\nfile c:x.mp4\n"], + ])("refuses %s", async (_name, contents) => { + const file = writeFixture("clip.m3u8", contents); + expect(await playlistLeavesItsFolder(file)).toBe(true); + expect(await isNotMediaPayload(file)).toBe(true); + }); + + it.each(process.platform === "win32" ? ["#"] : ["#", "?"])( + "refuses a list naming a file from a folder with %s in its name", + async (mark) => { + const file = listIn(`sub${mark}x`, hls("seg.ts")); + expect(await playlistLeavesItsFolder(file)).toBe(true); + expect(await isNotMediaPayload(file)).toBe(true); + }, + ); + + it("refuses a nested list read from a folder with # in its name, and a list placed in one", async () => { + const top = writeTree({ + "top.m3u8": hls("sub#x/inner.m3u8"), + "sub#x/inner.m3u8": hls("seg.ts"), + }); + expect(await playlistLeavesItsFolder(top)).toBe(true); + const placedAt = join(tmpdir(), "sub#x", "list.m3u8"); + expect(await playlistLeavesItsFolder(writeFixture("clip.m3u8", hls("seg.ts")), placedAt)).toBe( + true, + ); + }); + + it("keeps entries beside the list, https entries, and a media file in a folder with # in its name", async () => { + const beside = hls("seg.ts", "sub/seg.ts", "seg#1.ts", "https://cdn.example.com/seg.ts"); + expect(await playlistLeavesItsFolder(writeFixture("clip.m3u8", beside))).toBe(false); + const httpsOnly = listIn("sub#x", hls("https://cdn.example.com/a.ts")); + expect(await playlistLeavesItsFolder(httpsOnly)).toBe(false); + const mp4 = Buffer.from([0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70, ...Array(24).fill(0)]); + expect(await isNotMediaPayload(listIn("sub#x", mp4, "clip.mp4"))).toBe(false); + }); + + it.skipIf(process.platform === "win32")( + "keeps a list in a folder with : in its name", + async () => { + expect(await playlistLeavesItsFolder(listIn("c:d", hls("seg.ts")))).toBe(false); + }, + ); +}); + describe("playlistLeavesItsFolder reads a list as ffprobe does", () => { const FFPROBE_8 = /^ffprobe version n?8\./.test( spawnSync(getFfprobeBinary(), ["-version"], { encoding: "utf-8" }).stdout ?? "", diff --git a/packages/engine/src/utils/notMediaPayload.ts b/packages/engine/src/utils/notMediaPayload.ts index b9d58b047e..132cd3d2a5 100644 --- a/packages/engine/src/utils/notMediaPayload.ts +++ b/packages/engine/src/utils/notMediaPayload.ts @@ -314,8 +314,8 @@ export async function isNotImagePayload(filePath: string): Promise { const MAX_PLAYLIST_BYTES = 8 * 1024 * 1024; const MAX_PLAYLIST_DEPTH = 8; const HTTP_URL = /^https?:\/\//i; -const OTHER_SCHEME = /^[a-z][a-z0-9+.-]+:/i; -const DRIVE_PATH = /^[a-z]:[\\/]/i; +const COLON_IN_FIRST_SEGMENT = /^[^/\\?#]*:/; +const FOLDER_CUT_AT_QUERY_OR_FRAGMENT = /[?#]/; // ffmpeg reads a playlist line into a 4096-byte buffer and drops the rest of it. const MAX_HLS_LINE_BYTES = 4095; @@ -389,9 +389,10 @@ function playlistEntries(text: string): string[] | undefined { function entryLeavesFolder(entry: string, base: string, folder: string): boolean { if (HTTP_URL.test(entry)) return false; - if (OTHER_SCHEME.test(entry) && !DRIVE_PATH.test(entry)) return true; + if (FOLDER_CUT_AT_QUERY_OR_FRAGMENT.test(base)) return true; return [entry, percentDecoded(entry)].some((path) => { - if (isAbsolute(path) || DRIVE_PATH.test(path) || path.startsWith("\\")) return true; + if (COLON_IN_FIRST_SEGMENT.test(path)) return true; + if (isAbsolute(path) || path.startsWith("\\")) return true; const inside = relative(folder, resolve(base, path)); return ( inside === ".." || inside.startsWith("../") || inside.startsWith("..\\") || isAbsolute(inside)