Skip to content

Commit 03e3755

Browse files
fix(studio-server): background removal writes only where it checked, even through planted links (#4540)
* fix(studio-server): background removal writes only where it checked, even through planted links * test(cli): read the cutout through a no-follow handle instead of checking then reading
1 parent 918e39e commit 03e3755

4 files changed

Lines changed: 197 additions & 20 deletions

File tree

‎packages/cli/src/background-removal/pipeline.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* decode the alpha plane.
1515
*/
1616
import { spawn } from "node:child_process";
17-
import { extname } from "node:path";
17+
import { randomUUID } from "node:crypto";
18+
import { renameSync, rmSync } from "node:fs";
19+
import { basename, dirname, extname, join } from "node:path";
1820
import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js";
1921
import { createSession, type Session } from "./inference.js";
2022
import { type Device, type ModelId } from "./manager.js";
@@ -296,16 +298,20 @@ export async function render(options: RenderOptions): Promise<RenderResult> {
296298
onProgress: (msg) => options.onProgress?.({ kind: "info", message: msg }),
297299
});
298300

301+
const output = tempBeside(options.outputPath);
302+
const background = options.backgroundOutputPath && tempBeside(options.backgroundOutputPath);
299303
try {
300304
const start = Date.now();
301305
const framesProcessed = await runPipeline(
302-
options,
306+
{ ...options, outputPath: output, backgroundOutputPath: background },
303307
session,
304308
media,
305309
format,
306310
bgFormat,
307311
ffmpegPath,
308312
);
313+
renameSync(output, options.outputPath);
314+
if (background) renameSync(background, options.backgroundOutputPath!);
309315
const durationSeconds = (Date.now() - start) / 1000;
310316
const avgMsPerFrame = framesProcessed ? (durationSeconds * 1000) / framesProcessed : 0;
311317

@@ -320,9 +326,16 @@ export async function render(options: RenderOptions): Promise<RenderResult> {
320326
};
321327
} finally {
322328
await session.close();
329+
rmSync(output, { force: true, maxRetries: 3 });
330+
if (background) rmSync(background, { force: true, maxRetries: 3 });
323331
}
324332
}
325333

334+
/** ffmpeg follows a link planted at its output mid-job; a fresh unguessable name renamed over the output cannot. */
335+
function tempBeside(path: string): string {
336+
return join(dirname(path), `.tmp-${randomUUID()}-${basename(path)}`);
337+
}
338+
326339
const RECENT_WINDOW = 30;
327340

328341
interface FfmpegProc {

‎packages/cli/src/background-removal/pipeline.windowsHide.test.ts‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
import { EventEmitter } from "node:events";
2+
import {
3+
closeSync,
4+
constants,
5+
mkdtempSync,
6+
openSync,
7+
readFileSync,
8+
rmSync,
9+
symlinkSync,
10+
writeFileSync,
11+
} from "node:fs";
12+
import { tmpdir } from "node:os";
13+
import { join } from "node:path";
214
import { Readable } from "node:stream";
315
import { describe, expect, it, vi } from "vitest";
416

@@ -29,7 +41,7 @@ vi.mock("@hyperframes/engine", () => ({
2941

3042
import { render } from "./pipeline.js";
3143

32-
function fakeFfmpeg(stdout: Readable) {
44+
function fakeFfmpeg(stdout: Readable, output?: string) {
3345
const proc = new EventEmitter() as EventEmitter & {
3446
stdout: Readable;
3547
stderr: EventEmitter;
@@ -40,21 +52,54 @@ function fakeFfmpeg(stdout: Readable) {
4052
proc.stderr = new EventEmitter();
4153
proc.stdin = Object.assign(new EventEmitter(), { write: vi.fn(() => true), end: vi.fn() });
4254
proc.kill = vi.fn();
55+
if (output) writeFileSync(output, "rendered");
4356
queueMicrotask(() => proc.emit("exit", 0, null));
4457
return proc;
4558
}
4659

4760
describe("background-removal FFmpeg child-process options", () => {
4861
it("hides every FFmpeg console window", async () => {
62+
const dir = mkdtempSync(join(tmpdir(), "hf-bg-removal-"));
4963
spawnMock
5064
.mockImplementationOnce(() => fakeFfmpeg(Readable.from([Buffer.alloc(3)])))
51-
.mockImplementationOnce(() => fakeFfmpeg(Readable.from([])));
65+
.mockImplementationOnce((_, args: string[]) => fakeFfmpeg(Readable.from([]), args.at(-1)));
5266

53-
await render({ inputPath: "/tmp/input.mp4", outputPath: "/tmp/output.webm" });
67+
await render({ inputPath: "/tmp/input.mp4", outputPath: join(dir, "output.webm") });
68+
rmSync(dir, { recursive: true, force: true });
5469

5570
expect(spawnMock).toHaveBeenCalledTimes(2);
5671
for (const call of spawnMock.mock.calls) {
5772
expect(call[2]).toEqual(expect.objectContaining({ windowsHide: true }));
5873
}
5974
});
6075
});
76+
77+
describe("background-removal output", () => {
78+
it.skipIf(process.platform === "win32")(
79+
"lands at the output path even when a link is planted there mid-job, leaving its target alone",
80+
async () => {
81+
const dir = mkdtempSync(join(tmpdir(), "hf-bg-removal-"));
82+
const outside = join(dir, "outside.webm");
83+
const output = join(dir, "cutout.webm");
84+
writeFileSync(outside, "keep");
85+
spawnMock
86+
.mockImplementationOnce(() => fakeFfmpeg(Readable.from([Buffer.alloc(3)])))
87+
.mockImplementationOnce((_, args: string[]) => {
88+
symlinkSync(outside, output);
89+
return fakeFfmpeg(Readable.from([]), args.at(-1));
90+
});
91+
try {
92+
await render({ inputPath: "/tmp/input.mp4", outputPath: output });
93+
expect(readFileSync(outside, "utf-8")).toBe("keep");
94+
const fd = openSync(output, constants.O_RDONLY | constants.O_NOFOLLOW);
95+
try {
96+
expect(readFileSync(fd, "utf-8")).toBe("rendered");
97+
} finally {
98+
closeSync(fd);
99+
}
100+
} finally {
101+
rmSync(dir, { recursive: true, force: true });
102+
}
103+
},
104+
);
105+
});

‎packages/studio-server/src/routes/media.test.ts‎

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// fallow-ignore-file code-duplication
22
import { afterEach, describe, expect, it, vi } from "vitest";
33
import { Hono } from "hono";
4-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
4+
import {
5+
mkdirSync,
6+
mkdtempSync,
7+
readdirSync,
8+
readFileSync,
9+
rmSync,
10+
symlinkSync,
11+
writeFileSync,
12+
} from "node:fs";
513
import { tmpdir } from "node:os";
614
import { join } from "node:path";
715
import { registerMediaRoutes } from "./media";
@@ -117,6 +125,34 @@ describe("registerMediaRoutes", () => {
117125
expect(response.status).toBe(501);
118126
});
119127

128+
// Windows needs a privilege to create symlinks.
129+
it.skipIf(process.platform === "win32")(
130+
"writes background-removal output to the folder it checked, though a link on its path is retargeted",
131+
async () => {
132+
const outside = mkdtempSync(join(tmpdir(), "hf-media-outside-"));
133+
tempProjectDirs.push(outside);
134+
let dir = "";
135+
const { app, projectDir } = createAdapter((opts) => {
136+
rmSync(join(dir, "cutouts"));
137+
symlinkSync(outside, join(dir, "cutouts"), "dir");
138+
writeFileSync(opts.outputPath, "cutout");
139+
return completeJob(opts);
140+
});
141+
dir = projectDir;
142+
mkdirSync(join(projectDir, "real"));
143+
symlinkSync(join(projectDir, "real"), join(projectDir, "cutouts"), "dir");
144+
145+
await app.request("http://localhost/projects/demo/media/remove-background", {
146+
method: "POST",
147+
headers: { "content-type": "application/json" },
148+
body: JSON.stringify({ inputPath: "assets/photo.jpg", outputPath: "cutouts/photo.png" }),
149+
});
150+
151+
expect(readdirSync(outside)).toEqual([]);
152+
expect(readFileSync(join(projectDir, "real", "photo.png"), "utf-8")).toBe("cutout");
153+
},
154+
);
155+
120156
it("rejects remote input paths", async () => {
121157
const { app, startBackgroundRemoval } = createAdapter(completeJob);
122158

@@ -185,6 +221,62 @@ describe("registerMediaRoutes", () => {
185221
);
186222
});
187223

224+
it("gives a second removal of the same clip its own output names while the first still runs", async () => {
225+
const { app } = createAdapter((opts) => ({
226+
...completeJob(opts),
227+
status: "processing" as const,
228+
backgroundOutputAssetPath: opts.backgroundOutputAssetPath,
229+
}));
230+
const start = async () =>
231+
(await (
232+
await app.request("http://localhost/projects/demo/media/remove-background", {
233+
method: "POST",
234+
headers: { "content-type": "application/json" },
235+
body: JSON.stringify({ inputPath: "assets/clip.mp4", createBackgroundPlate: true }),
236+
})
237+
).json()) as { outputPath: string; backgroundOutputPath: string };
238+
239+
const [first, second] = [await start(), await start()];
240+
expect([first.outputPath, second.outputPath]).toEqual([
241+
"assets/cutouts/clip-cutout.webm",
242+
"assets/cutouts/clip-cutout-2.webm",
243+
]);
244+
expect(second.backgroundOutputPath).toBe("assets/cutouts/clip-plate-2.webm");
245+
});
246+
247+
it.each([
248+
[
249+
"an explicit output spelled another way",
250+
{ outputPath: "assets//cutouts/clip-cutout.webm" },
251+
"clip-cutout-2.webm",
252+
],
253+
[
254+
"a plate asked for under the cutout's own name",
255+
{ outputPath: "assets/cutouts/clip-plate.webm" },
256+
"clip-plate-2.webm",
257+
],
258+
])("never gives two outputs one name: %s", async (_, extra, plate) => {
259+
const { app } = createAdapter((opts) => ({
260+
...completeJob(opts),
261+
status: "processing" as const,
262+
backgroundOutputAssetPath: opts.backgroundOutputAssetPath,
263+
}));
264+
const start = async (body: object) =>
265+
(await (
266+
await app.request("http://localhost/projects/demo/media/remove-background", {
267+
method: "POST",
268+
headers: { "content-type": "application/json" },
269+
body: JSON.stringify({ inputPath: "assets/clip.mp4", ...body }),
270+
})
271+
).json()) as { outputPath: string; backgroundOutputPath?: string };
272+
273+
const first = await start({});
274+
const second = await start({ ...extra, createBackgroundPlate: true });
275+
const names = [first.outputPath, second.outputPath, second.backgroundOutputPath];
276+
expect(new Set(names).size).toBe(3);
277+
expect(names.join(" ")).toContain(plate);
278+
});
279+
188280
it("normalizes query strings from local media paths", async () => {
189281
const { app, startBackgroundRemoval } = createAdapter(completeJob);
190282

‎packages/studio-server/src/routes/media.ts‎

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Hono } from "hono";
22
import { streamSSE } from "hono/streaming";
33
import { existsSync, mkdirSync } from "node:fs";
4-
import { basename, dirname, extname, join } from "node:path";
4+
import { basename, dirname, extname, join, posix } from "node:path";
55
import type { MediaProcessingJobState, StudioApiAdapter } from "../types.js";
6-
import { resolveWithinProject } from "../helpers/safePath.js";
6+
import { pinWithinProject, resolveWithinProject } from "../helpers/safePath.js";
77
import { probeMediaMetadata } from "../helpers/mediaMetadata.js";
88

99
const VIDEO_EXTENSIONS = new Set([
@@ -63,23 +63,46 @@ function slugFileBase(path: string): string {
6363
return name || "media";
6464
}
6565

66-
function uniqueAssetPath(projectDir: string, assetPath: string): string {
66+
function uniqueAssetPath(
67+
projectDir: string,
68+
assetPath: string,
69+
taken: ReadonlySet<string>,
70+
): string {
6771
const ext = extname(assetPath);
6872
const withoutExt = assetPath.slice(0, -ext.length);
6973
let candidate = assetPath;
70-
for (let index = 2; existsSync(join(projectDir, candidate)); index++) {
74+
for (let index = 2; taken.has(candidate) || existsSync(join(projectDir, candidate)); index++) {
7175
candidate = `${withoutExt}-${index}${ext}`;
7276
}
7377
return candidate;
7478
}
7579

76-
function defaultOutputPath(projectDir: string, inputPath: string): string {
80+
function defaultOutputPath(
81+
projectDir: string,
82+
inputPath: string,
83+
taken: ReadonlySet<string>,
84+
): string {
7785
const ext = isImagePath(inputPath) ? ".png" : ".webm";
78-
return uniqueAssetPath(projectDir, `assets/cutouts/${slugFileBase(inputPath)}-cutout${ext}`);
86+
return uniqueAssetPath(
87+
projectDir,
88+
`assets/cutouts/${slugFileBase(inputPath)}-cutout${ext}`,
89+
taken,
90+
);
91+
}
92+
93+
function defaultPlatePath(
94+
projectDir: string,
95+
inputPath: string,
96+
taken: ReadonlySet<string>,
97+
): string {
98+
return uniqueAssetPath(projectDir, `assets/cutouts/${slugFileBase(inputPath)}-plate.webm`, taken);
7999
}
80100

81-
function defaultPlatePath(projectDir: string, inputPath: string): string {
82-
return uniqueAssetPath(projectDir, `assets/cutouts/${slugFileBase(inputPath)}-plate.webm`);
101+
function outputsInFlight(mediaJobs: Map<string, JobWithCreatedAt>): Set<string> {
102+
const running = [...mediaJobs.values()].filter((job) => job.status === "processing");
103+
return new Set(
104+
running.flatMap((job) => [job.outputAssetPath, job.backgroundOutputAssetPath ?? ""]),
105+
);
83106
}
84107

85108
function makeJobId(projectId: string, mediaJobs: Map<string, JobWithCreatedAt>): string {
@@ -171,17 +194,20 @@ export function registerMediaRoutes(
171194
return c.json({ error: "background removal supports video or image assets only" }, 400);
172195
}
173196

174-
const requestedOutput = body.outputPath ? normalizeProjectAssetPath(body.outputPath) : "";
197+
const requestedOutput = body.outputPath
198+
? posix.normalize(normalizeProjectAssetPath(body.outputPath))
199+
: "";
175200
if (requestedOutput && containsNullByte(requestedOutput)) {
176201
return c.json({ error: "forbidden" }, 403);
177202
}
178203
if (requestedOutput && !resolveWithinProject(project.dir, requestedOutput)) {
179204
return c.json({ error: "forbidden" }, 403);
180205
}
206+
const taken = outputsInFlight(mediaJobs);
181207
const outputAssetPath = requestedOutput
182-
? uniqueAssetPath(project.dir, requestedOutput)
183-
: defaultOutputPath(project.dir, inputAssetPath);
184-
const outputPath = resolveWithinProject(project.dir, outputAssetPath);
208+
? uniqueAssetPath(project.dir, requestedOutput, taken)
209+
: defaultOutputPath(project.dir, inputAssetPath, taken);
210+
const outputPath = pinWithinProject(project.dir, outputAssetPath);
185211
if (!outputPath) return c.json({ error: "forbidden" }, 403);
186212
if (inputIsVideo && !VIDEO_OUTPUT_EXTENSIONS.has(extname(outputAssetPath).toLowerCase())) {
187213
return c.json({ error: "video background removal output must be .webm or .mov" }, 400);
@@ -196,9 +222,10 @@ export function registerMediaRoutes(
196222
if (!inputIsVideo) {
197223
return c.json({ error: "background plates are only supported for video inputs" }, 400);
198224
}
199-
backgroundOutputAssetPath = defaultPlatePath(project.dir, inputAssetPath);
225+
taken.add(outputAssetPath);
226+
backgroundOutputAssetPath = defaultPlatePath(project.dir, inputAssetPath, taken);
200227
backgroundOutputPath =
201-
resolveWithinProject(project.dir, backgroundOutputAssetPath) ?? undefined;
228+
pinWithinProject(project.dir, backgroundOutputAssetPath) ?? undefined;
202229
if (!backgroundOutputPath) {
203230
return c.json({ error: "forbidden" }, 403);
204231
}

0 commit comments

Comments
 (0)