mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
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 commit97d482340a. * Reapply "fix(engine): write every concat list through one writer that refuses control characters" This reverts commit7d723fd276. * test(engine): a chunk concat list ends with a newline
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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)];
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user