From a5ea036f67bed85037c6ac1dbfc6a1a45227e292 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Sat, 14 Nov 2026 18:01:39 -0500 Subject: [PATCH 1/3] fix(engine): refuse media playlists that name files outside their folder --- packages/engine/src/index.ts | 1 + .../engine/src/utils/notMediaPayload.test.ts | 34 +++++++++ packages/engine/src/utils/notMediaPayload.ts | 71 ++++++++++++++++++- .../src/services/assetMediaType.test.ts | 11 +++ .../producer/src/services/assetMediaType.ts | 8 ++- .../src/services/htmlCompiler.test.ts | 18 +++++ 6 files changed, 138 insertions(+), 5 deletions(-) diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 184e358efd7..c8c6e95a9a0 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -409,6 +409,7 @@ export { NOT_MEDIA_PAYLOAD, NotMediaPayloadError, assertMediaPayload, + playlistLeavesItsFolder, fingerprintElementId, isNotMediaPayload, } from "./utils/notMediaPayload.js"; diff --git a/packages/engine/src/utils/notMediaPayload.test.ts b/packages/engine/src/utils/notMediaPayload.test.ts index bdfdb844a99..b42072efbe4 100644 --- a/packages/engine/src/utils/notMediaPayload.test.ts +++ b/packages/engine/src/utils/notMediaPayload.test.ts @@ -7,6 +7,7 @@ import { fingerprintElementId, isNotMediaPayload, NotMediaPayloadError, + playlistLeavesItsFolder, } from "./notMediaPayload.js"; function writeFixture(name: string, contents: string | Buffer): string { @@ -175,3 +176,36 @@ describe("assertMediaPayload", () => { expect(error.message.length).toBeLessThan(500); }); }); + +describe("playlistLeavesItsFolder", () => { + const hls = (...lines: string[]) => + ["#EXTM3U", "#EXT-X-TARGETDURATION:1", ...lines, "#EXT-X-ENDLIST"].join("\n"); + const concat = (entry: string) => `ffconcat version 1.0\nfile ${entry}\n`; + + it.each([ + ["an absolute segment", hls("#EXTINF:1,", "/var/renders/other/out.mp4")], + ["a segment above its folder", hls("#EXTINF:1,", "../other/out.mp4")], + ["an encoded escape", hls("#EXTINF:1,", "%2e%2e/other/out.mp4")], + ["a file: segment", hls("#EXTINF:1,", "file:///var/renders/other/out.ts")], + [ + "a key outside its folder", + hls('#EXT-X-KEY:METHOD=AES-128,URI="/etc/key.bin"', "#EXTINF:1,", "seg.ts"), + ], + ["a Windows drive path", hls("#EXTINF:1,", "C:\\renders\\out.mp4")], + ["an absolute concat entry", concat("'/var/renders/other/out.mp4'")], + ["a concat entry above its folder", concat("../other/out.mp4")], + ])("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([ + ["segments beside it", hls("#EXTINF:1,", "seg0.ts", "#EXTINF:1,", "parts/seg1.ts")], + ["https segments", hls("#EXTINF:1,", "https://cdn.example.com/seg0.ts")], + ["a concat entry beside it", concat("'part 1.mp4'")], + ["a media file", Buffer.from([0x00, 0x00, 0x00, 0x20, 0x66, 0x74, 0x79, 0x70])], + ])("keeps %s", async (_name, contents) => { + expect(await playlistLeavesItsFolder(writeFixture("clip.m3u8", contents))).toBe(false); + }); +}); diff --git a/packages/engine/src/utils/notMediaPayload.ts b/packages/engine/src/utils/notMediaPayload.ts index a449582a205..eb249d0b0a4 100644 --- a/packages/engine/src/utils/notMediaPayload.ts +++ b/packages/engine/src/utils/notMediaPayload.ts @@ -1,5 +1,6 @@ import { createHash } from "node:crypto"; -import { open as openFile } from "node:fs/promises"; +import { open as openFile, readFile, stat } from "node:fs/promises"; +import { dirname, isAbsolute, relative, resolve } from "node:path"; export const NOT_MEDIA_PAYLOAD = "NOT_MEDIA_PAYLOAD" as const; @@ -44,7 +45,7 @@ export class NotMediaPayloadError extends Error { const elided = elementFingerprints.length - MAX_LISTED_FINGERPRINTS; super( `${elementFingerprints.length} media source(s) are text documents ` + - `(HTML/XML/JSON), not media containers ` + + `(HTML/XML/JSON, or a playlist naming files outside its own folder), not media containers ` + `[elements=${listed}${elided > 0 ? `,+${elided}` : ""}]. Either an unresolved ` + "nested-composition preview URL was authored as a media src, or the source answered with " + "an error page or API error body carrying a success status.", @@ -132,7 +133,9 @@ function startsWithDocumentByte(head: Buffer): boolean { */ export async function isNotMediaPayload(filePath: string): Promise { try { - return startsWithDocumentByte(await readHead(filePath)); + return ( + startsWithDocumentByte(await readHead(filePath)) || (await playlistLeavesItsFolder(filePath)) + ); } catch { return false; } @@ -150,3 +153,65 @@ export async function assertMediaPayload(filePath: string, elementId: string): P throw new NotMediaPayloadError([fingerprintElementId(elementId)]); } } + +const MAX_PLAYLIST_BYTES = 8 * 1024 * 1024; +const HTTP_URL = /^https?:\/\//i; +const OTHER_SCHEME = /^[a-z][a-z0-9+.-]+:/i; +const DRIVE_PATH = /^[a-z]:[\\/]/i; + +function playlistEntries(text: string): string[] | undefined { + const body = text.replace(/^\uFEFF/, "").trimStart(); + if (body.startsWith("#EXTM3U")) { + return body.split(/\r?\n/).flatMap((raw) => { + const line = raw.trim(); + const uris = [...line.matchAll(/URI="([^"]*)"/gi)].map((match) => match[1]!); + return line && !line.startsWith("#") ? [line, ...uris] : uris; + }); + } + if (/^ffconcat\s+version/i.test(body)) { + return body.split(/\r?\n/).flatMap((raw) => { + const match = /^\s*file\s+(.*)$/.exec(raw); + if (!match) return []; + const value = match[1]!.trim(); + return [value.startsWith("'") ? value.slice(1, -1).replace(/\\(.)/g, "$1") : value]; + }); + } + return undefined; +} + +function entryLeavesFolder(entry: string, folder: string): boolean { + if (HTTP_URL.test(entry)) return false; + if (OTHER_SCHEME.test(entry) && !DRIVE_PATH.test(entry)) return true; + return [entry, safeDecode(entry)].some((path) => { + if (isAbsolute(path) || DRIVE_PATH.test(path) || path.startsWith("\\")) return true; + const inside = relative(folder, resolve(folder, path)); + return ( + inside === ".." || inside.startsWith("../") || inside.startsWith("..\\") || isAbsolute(inside) + ); + }); +} + +function safeDecode(value: string): string { + try { + return decodeURIComponent(value); + } catch { + return value; + } +} + +/** True for an HLS or ffconcat list naming a file outside its folder; ffmpeg would open it. Never throws. */ +export async function playlistLeavesItsFolder(filePath: string): Promise { + try { + const head = (await readHead(filePath)) + .toString("utf8") + .replace(/^\uFEFF/, "") + .trimStart(); + if (!head.startsWith("#EXTM3U") && !/^ffconcat\s+version/i.test(head)) return false; + if ((await stat(filePath)).size > MAX_PLAYLIST_BYTES) return true; + const entries = playlistEntries(await readFile(filePath, "utf8")) ?? []; + const folder = dirname(resolve(filePath)); + return entries.some((entry) => entryLeavesFolder(entry, folder)); + } catch { + return false; + } +} diff --git a/packages/producer/src/services/assetMediaType.test.ts b/packages/producer/src/services/assetMediaType.test.ts index 8986762ff80..63f7821d37b 100644 --- a/packages/producer/src/services/assetMediaType.test.ts +++ b/packages/producer/src/services/assetMediaType.test.ts @@ -243,6 +243,17 @@ describe("preflightCompositionAssetMediaTypes", () => { await expect(run({ audioSrc: "streamed-preview.html" })).resolves.toBeUndefined(); }); + it.each([{ audioSrc: "outside.m3u8" }, { imageSrc: "outside.m3u8" }])( + "refuses a playlist naming a file outside its folder under any element: %o", + async (input) => { + writeFileSync( + join(projectDir, "outside.m3u8"), + "#EXTM3U\n#EXT-X-TARGETDURATION:1\n#EXTINF:1,\n../../outside.mp4\n#EXT-X-ENDLIST\n", + ); + await expect(run(input)).rejects.toBeInstanceOf(NotMediaPayloadError); + }, + ); + it("leaves an SVG image source alone", async () => { // ffprobe reads SVG through its svg_pipe demuxer, so an XML body is a // legitimate payload and must not be swept up by the sniff. diff --git a/packages/producer/src/services/assetMediaType.ts b/packages/producer/src/services/assetMediaType.ts index 5428149749c..af3467e9961 100644 --- a/packages/producer/src/services/assetMediaType.ts +++ b/packages/producer/src/services/assetMediaType.ts @@ -3,6 +3,7 @@ import { join } from "node:path"; import { fingerprintElementId, isNotMediaPayload, + playlistLeavesItsFolder, NotMediaPayloadError, probeMediaProfile, resolveProjectRelativeSrc, @@ -178,9 +179,12 @@ export async function preflightCompositionAssetMediaTypes(input: { // without the track and reports `audioError`), so it is classified // per-element in audioMixer instead of aborted here. const sniffableReferences = pathReferences.filter((ref) => ref.expected === "video"); - if (sniffableReferences.length > 0 && (await isNotMediaPayload(resolvedPath))) { + const sniffed = sniffableReferences.length > 0 && (await isNotMediaPayload(resolvedPath)); + if (sniffed || (await playlistLeavesItsFolder(resolvedPath))) { notMediaFingerprints.push( - ...sniffableReferences.map((ref) => fingerprintElementId(ref.id)), + ...(sniffed ? sniffableReferences : pathReferences).map((ref) => + fingerprintElementId(ref.id), + ), ); return; } diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index 518ea7cfb1e..d9a784a9909 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -3286,6 +3286,24 @@ describe("compileForRender non-media payload sniff (STUDIO-5433)", () => { // error and this assertion would fail. }); + it("aborts before ffprobe when a