Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export {
NOT_MEDIA_PAYLOAD,
NotMediaPayloadError,
assertMediaPayload,
playlistLeavesItsFolder,
fingerprintElementId,
isNotMediaPayload,
} from "./utils/notMediaPayload.js";
Expand Down
35 changes: 35 additions & 0 deletions packages/engine/src/utils/notMediaPayload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
fingerprintElementId,
isNotMediaPayload,
NotMediaPayloadError,
playlistLeavesItsFolder,
} from "./notMediaPayload.js";

function writeFixture(name: string, contents: string | Buffer): string {
Expand Down Expand Up @@ -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);
});
});
76 changes: 71 additions & 5 deletions packages/engine/src/utils/notMediaPayload.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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<Buffer> {
/** Read up to `bytes` (default {@link SNIFF_BYTES}) from the front of the file. */
async function readHead(filePath: string, bytes = SNIFF_BYTES): Promise<Buffer> {
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.
Expand Down Expand Up @@ -132,7 +133,9 @@ function startsWithDocumentByte(head: Buffer): boolean {
*/
export async function isNotMediaPayload(filePath: string): Promise<boolean> {
try {
return startsWithDocumentByte(await readHead(filePath));
return (
startsWithDocumentByte(await readHead(filePath)) || (await playlistLeavesItsFolder(filePath))
);
} catch {
return false;
}
Expand All @@ -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<boolean> {
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;
}
}
11 changes: 11 additions & 0 deletions packages/producer/src/services/assetMediaType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <img> payload and must not be swept up by the sniff.
Expand Down
33 changes: 18 additions & 15 deletions packages/producer/src/services/assetMediaType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from "node:path";
import {
fingerprintElementId,
isNotMediaPayload,
playlistLeavesItsFolder,
NotMediaPayloadError,
probeMediaProfile,
resolveProjectRelativeSrc,
Expand All @@ -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[];

Expand Down Expand Up @@ -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 <video> only
// (an <img> may be SVG, a bad audio track is per-element in audioMixer); a playlist leaving its folder, under any.
async function notMediaReferences(
path: string,
references: MediaReference[],
): Promise<MediaReference[]> {
const videos = references.filter((ref) => ref.expected === "video");
if (videos.length > 0 && (await isNotMediaPayload(path))) return videos;
return (await playlistLeavesItsFolder(path)) ? references : [];
}

/**
* Fail early when a successfully-probed local asset cannot satisfy the HTML
* element that owns it. Missing, corrupt, remote-at-runtime, and unprobeable
Expand Down Expand Up @@ -167,21 +182,9 @@ export async function preflightCompositionAssetMediaTypes(input: {
await Promise.all(
entries.map(([resolvedPath, pathReferences]) =>
withMediaProbeSlot(async () => {
// STUDIO-5433. This preflight is the only place every local media src is
// seen regardless of its authored timing, so it is where a document
// payload behind a `data-end` video gets caught — the compiler's own
// sniff only runs for elements whose duration it has to resolve.
//
// Video only, deliberately. An `<img>` may legitimately be an SVG,
// which ffprobe reads through its `svg_pipe` demuxer. And a bad audio
// source is non-fatal by existing policy (audioStage ships the render
// 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))) {
notMediaFingerprints.push(
...sniffableReferences.map((ref) => fingerprintElementId(ref.id)),
);
const refused = await notMediaReferences(resolvedPath, pathReferences);
if (refused.length > 0) {
notMediaFingerprints.push(...refused.map((ref) => fingerprintElementId(ref.id)));
return;
}

Expand Down
18 changes: 18 additions & 0 deletions packages/producer/src/services/htmlCompiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3286,6 +3286,24 @@ describe("compileForRender non-media payload sniff (STUDIO-5433)", () => {
// error and this assertion would fail.
});

it("aborts before ffprobe when a <video> playlist names a file outside its folder", async () => {
const projectDir = writeProject(
'<video id="v1" src="assets/clip.m3u8" data-start="0" muted></video>',
);
writeFileSync(
join(projectDir, "assets", "clip.m3u8"),
"#EXTM3U\n#EXT-X-TARGETDURATION:1\n#EXTINF:1,\n../../outside.mp4\n#EXT-X-ENDLIST\n",
);
let caught: unknown;
try {
await compileForRender(projectDir, join(projectDir, "index.html"), projectDir);
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(NotMediaPayloadError);
expect((caught as NotMediaPayloadError).message).not.toContain("clip.m3u8");
});

it("drops an <audio> document payload to duration 0 and warns instead of failing the render", async () => {
// The audio/video split is the compiler's contract: an unprobeable audio
// src is excluded from the render, and only video surfaces its probe
Expand Down
Loading