From 089e5f6b4911f95880d9fd1012aa0f87b6d7e622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 19 Nov 2026 00:08:22 +0000 Subject: [PATCH] fix(engine): refuse to join files whose path holds a line break (#8219) * test: a concat list refuses paths whose folder name holds a line break * fix(engine): write every concat list through one writer that refuses control characters * test: name the line-break folders without slashes; skip them on Windows * Revert "fix(engine): write every concat list through one writer that refuses control characters" This reverts commit 97d482340ad4e741383ec7cce712406288222236. * Reapply "fix(engine): write every concat list through one writer that refuses control characters" This reverts commit 7d723fd27674ae18a7d812aa6903a735fe5e10c9. * test(engine): a chunk concat list ends with a newline --- packages/engine/src/index.ts | 2 +- .../chunkEncoder.relativePaths.test.ts | 27 ++++++++++++++++++- .../engine/src/services/chunkEncoder.test.ts | 2 +- packages/engine/src/services/chunkEncoder.ts | 11 +++++--- packages/parsers/src/ffBinaries.test.ts | 18 +++++++++++++ packages/parsers/src/ffBinaries.ts | 17 ++++++++++++ .../src/services/distributed/assemble.test.ts | 21 +++++++++++++++ .../src/services/distributed/assemble.ts | 5 ++-- .../src/helpers/proxyTranscoder.ts | 10 ++++--- 9 files changed, 100 insertions(+), 13 deletions(-) diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 93c667f044..5996b80d4d 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -198,7 +198,7 @@ export { } from "./services/screenshotService.js"; // ── Encoding ─────────────────────────────────────────────────────────────────── -export { concatListInputArgs } from "@hyperframes/parsers/ff-binaries"; +export { concatListInputArgs, concatListText } from "@hyperframes/parsers/ff-binaries"; export { buildEncoderArgs, encodeFramesFromDir, diff --git a/packages/engine/src/services/chunkEncoder.relativePaths.test.ts b/packages/engine/src/services/chunkEncoder.relativePaths.test.ts index db2d78c3c8..5266304d42 100644 --- a/packages/engine/src/services/chunkEncoder.relativePaths.test.ts +++ b/packages/engine/src/services/chunkEncoder.relativePaths.test.ts @@ -1,5 +1,5 @@ import { spawnSync } from "node:child_process"; -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, relative } from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; @@ -135,6 +135,31 @@ describe.skipIf(!HAS_FFMPEG)("concat input formats", () => { expect(made.status, made.stderr.toString()).toBe(0); } + // fails without the fix: the line break ended the entry and ffmpeg read the rest as list lines. + // Windows paths cannot hold a line break. + it.skipIf(process.platform === "win32")( + "refuses inputs whose folder name holds a line break, before writing a list", + async () => { + const folder = join(root, "proj\nfile 'other.mov'\n#"); + mkdirSync(folder); + makeClip(join(folder, "a.mp4"), 2); + makeClip(join(folder, "b.mp4"), 2); + + const result = await concatVideoFiles( + [join(folder, "a.mp4"), join(folder, "b.mp4")], + join(root, "joined.mp4"), + ); + + expect(result).toEqual({ + success: false, + error: expect.stringContaining("control character"), + externalInterruption: false, + }); + expect(readdirSync(folder).filter((name) => name.startsWith("concat-list-"))).toEqual([]); + }, + 30_000, + ); + function frameCount(path: string): string { const probe = spawnSync( getFfprobeBinary(), diff --git a/packages/engine/src/services/chunkEncoder.test.ts b/packages/engine/src/services/chunkEncoder.test.ts index 09eaf9b36f..87a12b6a15 100644 --- a/packages/engine/src/services/chunkEncoder.test.ts +++ b/packages/engine/src/services/chunkEncoder.test.ts @@ -314,7 +314,7 @@ describe("encodeFramesChunkedConcat ffmpegEncodeTimeout", () => { expect(dirname(chunkPath)).not.toBe(legacyDir); expect(dirname(list)).toBe(dirname(chunkPath)); expect(dirname(dirname(list))).toBe(root); - expect(readFileSync(list, "utf8")).toBe(`file '${chunkPath.replace(/'/g, "'\\''")}'`); + expect(readFileSync(list, "utf8")).toBe(`file '${chunkPath.replace(/'/g, "'\\''")}'\n`); } expect(readFileSync(legacyList, "utf8")).toBe("another render's concat list"); }); diff --git a/packages/engine/src/services/chunkEncoder.ts b/packages/engine/src/services/chunkEncoder.ts index 0a939ff4eb..6e00f22515 100644 --- a/packages/engine/src/services/chunkEncoder.ts +++ b/packages/engine/src/services/chunkEncoder.ts @@ -50,7 +50,7 @@ import { appendVp9CpuUsedArg } from "./vp9Options.js"; import { appendSoftwareEncoderThreadsArg } from "./softwareEncoderThreads.js"; import { appendRenderProvenanceArgs } from "../utils/renderProvenance.js"; import { createStagingDir } from "@hyperframes/core/atomic-file"; -import { concatListInputArgs } from "@hyperframes/parsers/ff-binaries"; +import { concatListInputArgs, concatListText } from "@hyperframes/parsers/ff-binaries"; export type { EncoderOptions, EncodeResult, MuxResult } from "./chunkEncoder.types.js"; @@ -693,7 +693,7 @@ let concatListSeq = 0; function writeConcatList(dir: string, inputPaths: readonly string[]): string { concatListSeq += 1; const listPath = join(dir, `concat-list-${process.pid}-${concatListSeq}.txt`); - const body = inputPaths.map((p) => `file '${resolve(p).replace(/'/g, "'\\''")}'`).join("\n"); + const body = concatListText(inputPaths.map((p) => resolve(p))); writeFileSync(listPath, body, "utf-8"); return listPath; } @@ -721,7 +721,12 @@ export async function concatVideoFiles( // get their own chunk directory but can share an output directory. // The list is left on disk deliberately: it is removed with the work dir, // and `--debug` keeps both so a bad concat can be reproduced from its list. - const listPath = writeConcatList(dirname(firstInput), inputPaths); + let listPath: string; + try { + listPath = writeConcatList(dirname(firstInput), inputPaths); + } catch (error) { + return { success: false, error: (error as Error).message, externalInterruption: false }; + } const encodeTimeout = config?.ffmpegEncodeTimeout ?? DEFAULT_CONFIG.ffmpegEncodeTimeout; const result = await runFfmpeg(buildConcatArgs(listPath, outputPath), { signal, diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts index 9817f91787..f2ad2bf175 100644 --- a/packages/parsers/src/ffBinaries.test.ts +++ b/packages/parsers/src/ffBinaries.test.ts @@ -641,3 +641,21 @@ describe.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)("concatListInput expect(run.stderr).toContain("not on whitelist 'file'"); }); }); + +describe("concatListText", () => { + // fails without the fix: every list writer escaped only the single quote, so a line break started a new list line. + it("writes one quoted entry per path and refuses a path holding a control character", async () => { + const { concatListText } = await importFresh(); + expect(concatListText(["/a/it's.mp4", "/b/c.mp4"])).toBe( + "file '/a/it'\\''s.mp4'\nfile '/b/c.mp4'\n", + ); + for (const path of [ + "/p\nfile '/etc/x.mov'\n#/c.mp4", + "/p\r/c.mp4", + "/p\u0000/c.mp4", + "/p\u007f/c.mp4", + ]) { + expect(() => concatListText([path])).toThrow("control character"); + } + }); +}); diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts index 9f2da0b89c..68f04ab606 100644 --- a/packages/parsers/src/ffBinaries.ts +++ b/packages/parsers/src/ffBinaries.ts @@ -249,6 +249,23 @@ export function clearFfBinaryLookupCache(): void { skippedX86Binaries.clear(); } +const holdsControlCharacter = (text: string) => + [...text].some((char) => char < " " || char === "\u007f"); + +/** One `file` entry per path; a control character (a line break starts a list line) is refused. */ +export function concatListText(paths: readonly string[]): string { + return paths + .map((path) => { + if (holdsControlCharacter(path)) { + throw new Error( + `A file to join holds a control character in its path: ${JSON.stringify(path)}`, + ); + } + return `file '${path.replace(/'/g, "'\\''")}'\n`; + }) + .join(""); +} + /** * Concat input for a list HyperFrames wrote: entries open only as local mov, matroska or concat * files (an ffconcat entry is followed in safe mode), and `-xerror` fails at a refused entry. diff --git a/packages/producer/src/services/distributed/assemble.test.ts b/packages/producer/src/services/distributed/assemble.test.ts index 40ef14d112..738d62f803 100644 --- a/packages/producer/src/services/distributed/assemble.test.ts +++ b/packages/producer/src/services/distributed/assemble.test.ts @@ -387,6 +387,27 @@ describe("assemble()", () => { TIMEOUT_MS, ); + // fails without the fix: the line break ended the entry and ffmpeg read the rest as list lines. + it( + "refuses chunk paths whose folder name holds a line break, before writing a list", + async () => { + const chunks: ChunkSliceJson[] = [ + { index: 0, startFrame: 0, endFrame: 5 }, + { index: 1, startFrame: 5, endFrame: 10 }, + ]; + const planDir = buildPlanDir("mp4", chunks, 10, false); + if (process.platform === "win32") return; // Windows paths cannot hold a line break + const folder = join(planDir, "chunks\nfile 'other.mov'\n#"); + mkdirSync(folder); + const chunkPaths = ["0.mp4", "1.mp4"].map((name) => join(folder, name)); + for (const path of chunkPaths) writeFileSync(path, "not a video"); + await expect(assemble(planDir, chunkPaths, null, join(planDir, "out.mp4"))).rejects.toThrow( + "control character", + ); + }, + TIMEOUT_MS, + ); + it( "single-chunk render stamps exact r_frame_rate on the output container", async () => { diff --git a/packages/producer/src/services/distributed/assemble.ts b/packages/producer/src/services/distributed/assemble.ts index 4f700bc528..3e16e3dd36 100644 --- a/packages/producer/src/services/distributed/assemble.ts +++ b/packages/producer/src/services/distributed/assemble.ts @@ -39,6 +39,7 @@ import { appendRateCapArgs, appendRenderProvenanceArgs, concatListInputArgs, + concatListText, describeFfmpegFailure, getEncoderPreset, MIXED_AUDIO_FILENAME, @@ -393,10 +394,8 @@ function concatDemuxerInput( workDir: string, fpsArg: string, ): string[] { - // ffmpeg's concat list escapes a single quote as `'\''`. const listPath = join(workDir, "concat-list.txt"); - const body = chunkPaths.map((path) => `file '${path.replace(/'/g, "'\\''")}'`).join("\n"); - writeFileSync(listPath, `${body}\n`, "utf-8"); + writeFileSync(listPath, concatListText(chunkPaths), "utf-8"); return ["-r", fpsArg, ...concatListInputArgs(listPath)]; } diff --git a/packages/studio-server/src/helpers/proxyTranscoder.ts b/packages/studio-server/src/helpers/proxyTranscoder.ts index e6d03e4c25..bc48bca058 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.ts @@ -7,6 +7,7 @@ import { hdrToSdrToneMapFilter, type PreviewProxyBox } from "@hyperframes/core"; import { previewToken } from "@hyperframes/core/preview-proof"; import { concatListInputArgs, + concatListText, findFfBinary, followFfmpegProgress, } from "@hyperframes/parsers/ff-binaries"; @@ -659,10 +660,11 @@ async function encodeInChunks( try { writeFileSync( listPath, - pieces - .map((piece) => (JOIN_FROM_CACHE_FOLDER ? basename(piece) : piece.replace(/\\/g, "/"))) - .map((piece) => `file '${piece.replace(/'/g, "'\\''")}'\n`) - .join(""), + concatListText( + pieces.map((piece) => + JOIN_FROM_CACHE_FOLDER ? basename(piece) : piece.replace(/\\/g, "/"), + ), + ), ); await concat(listPath, tempPath); } finally {