mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(engine): the playlist folder check reads colon, query and fragment names as ffmpeg resolves them (#8014)
ffmpeg resolves playlist entries with URL rules, not Node path rules. An entry with a colon before its first slash, backslash, query or fragment mark, and an entry resolved from a folder whose name holds a query or fragment mark, now count as leaving the folder, so the check no longer approves a list that ffmpeg opens somewhere else. Entries that are http(s) URLs are unchanged. Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
bffe8d5985
commit
38aaad1b20
@@ -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 ?? "",
|
||||
|
||||
@@ -314,8 +314,8 @@ export async function isNotImagePayload(filePath: string): Promise<boolean> {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user