diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 184e358efd..c8c6e95a9a 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 bdfdb844a9..ae8aab382d 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,37 @@ 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")], + ["a list too long to read whole", hls("#EXTINF:1,\nseg.ts\n".repeat(700_000))], + ])("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 a449582a20..52e08e1502 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 { 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.", @@ -83,11 +84,11 @@ function byteOrderMarkLength(head: Buffer): number { return 0; } -/** Read up to {@link SNIFF_BYTES} from the front of the file. */ -async function readHead(filePath: string): Promise { +/** Read up to `bytes` (default {@link SNIFF_BYTES}) from the front of the file. */ +async function readHead(filePath: string, bytes = SNIFF_BYTES): Promise { const fh = await openFile(filePath, "r"); try { - const buf = Buffer.alloc(SNIFF_BYTES); + const buf = Buffer.alloc(bytes); let filled = 0; // Looped because a single read can come back short on NFS/FUSE and on // FIFOs, which would truncate the window mid-prefix. @@ -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,66 @@ 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; + const body = await readHead(filePath, MAX_PLAYLIST_BYTES + 1); + if (body.length > MAX_PLAYLIST_BYTES) return true; + const entries = playlistEntries(body.toString("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 8986762ff8..63f7821d37 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 5428149749..cbe041d879 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, @@ -28,8 +29,11 @@ export interface AssetMediaTypeMismatch { } export class AssetMediaTypeMismatchError extends Error { + // fallow-ignore-next-line unused-class-member readonly code = ASSET_MEDIA_TYPE_MISMATCH; + // fallow-ignore-next-line unused-class-member readonly owner = "user" as const; + // fallow-ignore-next-line unused-class-member readonly retryable = false as const; readonly mismatches: readonly AssetMediaTypeMismatch[]; @@ -119,6 +123,17 @@ export function dropMediaOutsideProject(input: { composition.images = composition.images.filter(staysInside); } +// STUDIO-5433: the one place every local src is seen whatever its timing. Documents are refused under