From ff34919fb244c69d1482783fc272b3dad52135ba Mon Sep 17 00:00:00 2001 From: James Russo Date: Wed, 25 Nov 2026 02:38:29 +0000 Subject: [PATCH] fix(producer): a private project file stays private under any path spelling (#8786) * fix(producer): a private project file stays private under any path spelling A local render that brings in outside files read a media path spelled outside the project, but resolving to a private project file, as an ordinary outside file. isPrivateUnderAssetRoots can now also pick a root by the real file, and the render callers ask for that when outside files are allowed. The default still never looks at a file spelled outside the project. Co-Authored-By: Claude Fable 5.2 * fix(engine): leave a path on another network root unread when judging private files The real-path lookup added for renders that bring in outside files ran before the network-root refusal in the duration probe. The lookup now answers no for such a path without looking at it, so every caller keeps the order. Co-Authored-By: Claude Fable 5.2 * fix(producer): a private project file is not copied in under another path spelling The outside-asset collector asked only whether a path's real file leaves the project, so a private project file named by a path spelled outside the project was copied into the compiled output. The refusal every collector passes through now answers for that case too. Co-Authored-By: Claude Fable 5.2 * fix(engine): a media source that is no file is not looked up as one With outside files allowed, a long inline data source reached the real-path lookup, which walks one folder per slash when the path does not exist and ran out of stack. The lookup now runs only for a path that exists. Co-Authored-By: Claude Fable 5.2 --------- Co-authored-by: Claude Fable 5.2 --- packages/engine/src/utils/assetRoots.test.ts | 20 ++++++++ packages/engine/src/utils/assetRoots.ts | 30 +++++++++-- .../src/utils/assetRootsNetworkRoot.test.ts | 42 +++++++++++++++ .../src/services/animatedGifPrep.test.ts | 20 ++++++++ .../producer/src/services/animatedGifPrep.ts | 6 ++- .../src/services/assetMediaType.test.ts | 25 +++++++++ .../producer/src/services/assetMediaType.ts | 2 +- .../src/services/htmlCompiler.test.ts | 51 ++++++++++++++++++- .../producer/src/services/htmlCompiler.ts | 4 +- packages/producer/src/utils/paths.ts | 8 ++- 10 files changed, 197 insertions(+), 11 deletions(-) create mode 100644 packages/engine/src/utils/assetRootsNetworkRoot.test.ts diff --git a/packages/engine/src/utils/assetRoots.test.ts b/packages/engine/src/utils/assetRoots.test.ts index 9c6ed0b578..de030230d4 100644 --- a/packages/engine/src/utils/assetRoots.test.ts +++ b/packages/engine/src/utils/assetRoots.test.ts @@ -36,6 +36,26 @@ describe("isPrivateUnderAssetRoots", () => { expect(isPrivateUnderAssetRoots(link, roots)).toBe(true); }); + it("refuses a private project file named through a link outside the project, when asked to follow it", () => { + const alias = join(root, "alias"); + symlinkSync(projectDir, alias); + const named = join(alias, ".git", "objects", "clip.mp4"); + file(".git/objects/clip.mp4"); + expect(isPrivateUnderAssetRoots(named, roots)).toBe(false); + expect(isPrivateUnderAssetRoots(named, roots, true)).toBe(true); + expect( + isPrivateUnderAssetRoots( + join(alias, file("media/clip.mp4").slice(projectDir.length)), + roots, + true, + ), + ).toBe(false); + const outside = join(root, ".ssh", "clip.mp4"); + mkdirSync(dirname(outside), { recursive: true }); + writeFileSync(outside, "x"); + expect(isPrivateUnderAssetRoots(outside, roots, true)).toBe(false); + }); + it("keeps project media and the render's own files inside .hyperframes", () => { expect(isPrivateUnderAssetRoots(file("media/clip.mp4"), roots)).toBe(false); expect(assetRootsRefusal(file("media/clip.mp4"), roots)).toBeNull(); diff --git a/packages/engine/src/utils/assetRoots.ts b/packages/engine/src/utils/assetRoots.ts index 8c6db77378..978462bb18 100644 --- a/packages/engine/src/utils/assetRoots.ts +++ b/packages/engine/src/utils/assetRoots.ts @@ -1,6 +1,15 @@ import { existsSync } from "node:fs"; -import { isPrivateFileUnder, isSafePath, isStudioPrivatePath } from "@hyperframes/core/safe-path"; -import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution"; +import { + isPrivateFileUnder, + isSafePath, + isStudioPrivatePath, + realFilePath, + realPathWithin, +} from "@hyperframes/core/safe-path"; +import { + isWithinProjectRoot, + namesAnotherNetworkRoot, +} from "@hyperframes/parsers/asset-resolution"; export function isInsideAssetRoots(path: string, roots: readonly string[]): boolean { if (roots.some((root) => isSafePath(root, path))) return true; @@ -8,9 +17,22 @@ export function isInsideAssetRoots(path: string, roots: readonly string[]): bool return roots.some((root) => isWithinProjectRoot(root, path)) && !existsSync(path); } +const existingFileWithinUnlessOnAnotherNetworkRoot = (root: string, path: string): boolean => + !namesAnotherNetworkRoot(root, path) && + existsSync(path) && + realPathWithin(realFilePath(root), realFilePath(path)); + // Judged from every root holding it, since a render's own work folder may sit in the project's `.hyperframes/`. -export function isPrivateUnderAssetRoots(path: string, roots: readonly string[]): boolean { - const holders = roots.filter((root) => isSafePath(root, path)); +export function isPrivateUnderAssetRoots( + path: string, + roots: readonly string[], + bySpellingOrRealFile = false, +): boolean { + const holders = roots.filter( + (root) => + isSafePath(root, path) || + (bySpellingOrRealFile && existingFileWithinUnlessOnAnotherNetworkRoot(root, path)), + ); return ( holders.length > 0 && holders.every((root) => isPrivateFileUnder(root, path, isStudioPrivatePath)) diff --git a/packages/engine/src/utils/assetRootsNetworkRoot.test.ts b/packages/engine/src/utils/assetRootsNetworkRoot.test.ts new file mode 100644 index 0000000000..5a1208bbe9 --- /dev/null +++ b/packages/engine/src/utils/assetRootsNetworkRoot.test.ts @@ -0,0 +1,42 @@ +import { fileURLToPath } from "node:url"; +import { describe, expect, it, vi } from "vitest"; + +const looked = vi.hoisted(() => ({ at: [] as string[] })); + +vi.mock("@hyperframes/parsers/asset-resolution", async (original) => ({ + ...(await original()), + // POSIX has no second network root, so the answer Windows gives for `\\server\share` is stood in here. + namesAnotherNetworkRoot: (_base: string, target: string) => target.startsWith("/share/"), +})); +vi.mock("@hyperframes/core/safe-path", async (original) => { + const actual = await original(); + return { + ...actual, + realFilePath: (path: string) => { + looked.at.push(path); + return actual.realFilePath(path); + }, + }; +}); + +const { isPrivateUnderAssetRoots } = await import("./assetRoots.js"); + +describe("isPrivateUnderAssetRoots on another network root", () => { + it("answers without looking at the file", () => { + expect(isPrivateUnderAssetRoots("/share/.git/clip.mp4", ["/project"], true)).toBe(false); + expect(looked.at).toEqual([]); + }); + + it("still looks at a file on this machine when asked to", () => { + const file = fileURLToPath(import.meta.url); + expect(isPrivateUnderAssetRoots(file, ["/project"], true)).toBe(false); + expect(looked.at).toContain(file); + }); + + it("does not look at a name that is no file", () => { + looked.at.length = 0; + const inline = `/project/data:video/mp4;base64,${"A/".repeat(20_000)}`; + expect(isPrivateUnderAssetRoots(inline, ["/project", "/work"], true)).toBe(false); + expect(looked.at).toEqual([]); + }); +}); diff --git a/packages/producer/src/services/animatedGifPrep.test.ts b/packages/producer/src/services/animatedGifPrep.test.ts index a36164c68b..62e36f9fe4 100644 --- a/packages/producer/src/services/animatedGifPrep.test.ts +++ b/packages/producer/src/services/animatedGifPrep.test.ts @@ -5,6 +5,7 @@ import { mkdtempSync, readdirSync, rmSync, + symlinkSync, truncateSync, writeFileSync, } from "node:fs"; @@ -151,6 +152,25 @@ describe("prepareAnimatedGifInputs", () => { }, ); + it("never resolves a private project file named by a path spelled outside the project", () => { + const projectDir = makeProject(); + const alias = `${projectDir}-alias`; + try { + symlinkSync(projectDir, alias); + for (const folder of [".git", "media"]) { + mkdirSync(join(projectDir, folder), { recursive: true }); + writeFileSync(join(projectDir, folder, "sticker.gif"), gif([...frame(5), ...frame(15)], 0)); + } + const options = { projectDir, downloadDir: projectDir, allowOutsideFiles: true }; + const named = (folder: string) => join(alias, folder, "sticker.gif"); + expect(resolveLocalImageSourcePath(named(".git"), options)).toBeNull(); + expect(resolveLocalImageSourcePath(named("media"), options)).toBe(named("media")); + } finally { + rmSync(alias, { force: true }); + rmSync(projectDir, { recursive: true, force: true }); + } + }); + it("does not cache a transcode that ffmpeg cut off at its output size limit", async () => { const projectDir = makeProject(); try { diff --git a/packages/producer/src/services/animatedGifPrep.ts b/packages/producer/src/services/animatedGifPrep.ts index c9cce12e80..03937a7fe0 100644 --- a/packages/producer/src/services/animatedGifPrep.ts +++ b/packages/producer/src/services/animatedGifPrep.ts @@ -115,7 +115,11 @@ export function resolveLocalImageSourcePath( const assetRoot = options.allowOutsideFiles ? undefined : options.projectDir; const allowed = (candidate: string) => !namesAnotherNetworkRoot(options.projectDir, candidate) && - !isPrivateUnderAssetRoots(candidate, [options.projectDir, options.downloadDir]) && + !isPrivateUnderAssetRoots( + candidate, + [options.projectDir, options.downloadDir], + options.allowOutsideFiles, + ) && ((!escapesAssetRoot(candidate, assetRoot) && outsideAssetRefusal(options.projectDir, candidate) !== "blocklisted") || !escapesAssetRoot(candidate, options.downloadDir)); diff --git a/packages/producer/src/services/assetMediaType.test.ts b/packages/producer/src/services/assetMediaType.test.ts index 92799a5a30..190fad52ab 100644 --- a/packages/producer/src/services/assetMediaType.test.ts +++ b/packages/producer/src/services/assetMediaType.test.ts @@ -373,6 +373,25 @@ describe("dropMediaOutsideProject", () => { }, ); + it("drops a private project file named by a path spelled outside the project", () => { + for (const rel of [".git/clip.mp4", "media/clip.mp4"]) { + mkdirSync(dirname(join(projectDir, rel)), { recursive: true }); + writeFileSync(join(projectDir, rel), "x"); + } + const alias = join(root, "alias"); + rmSync(alias, { force: true }); + symlinkSync(projectDir, alias); + const srcs: Record = { + link: join(alias, ".git", "clip.mp4"), + local: join(alias, "media", "clip.mp4"), + }; + // Another letter case names the same folder only where the volume folds case. + const upper = join(root, "PROJECT"); + if (existsSync(upper)) srcs.letterCase = join(upper, ".git", "clip.mp4"); + expect(keptVideos(srcs, true)).toEqual(["local"]); + expect(keptVideos(srcs)).toEqual([]); + }); + it("drops a URL scheme followed by parent folders, in any letter case", () => { const schemes = ["data:", "DATA:", "blob:", "Blob:", "about:", "https:", "HTTP:"]; const srcs = Object.fromEntries( @@ -391,4 +410,10 @@ describe("dropMediaOutsideProject", () => { }; expect(keptVideos(srcs)).toEqual(Object.keys(srcs)); }); + + it("keeps long inline media when outside files are allowed, without walking its text as folders", () => { + // A regression test: each `/` in the base64 text was walked as a folder, and thousands of them overflowed the stack. + const srcs = { long: `data:video/mp4;base64,${"A/".repeat(20_000)}`, local: "clip.mp4" }; + expect(keptVideos(srcs, true)).toEqual(Object.keys(srcs)); + }); }); diff --git a/packages/producer/src/services/assetMediaType.ts b/packages/producer/src/services/assetMediaType.ts index dd24593e8e..9a5b6efa96 100644 --- a/packages/producer/src/services/assetMediaType.ts +++ b/packages/producer/src/services/assetMediaType.ts @@ -114,7 +114,7 @@ export function dropMediaOutsideProject(input: { const staysInside = ({ id, src }: { id: string; src: string }) => { if (!src || isHttpUrl(src)) return true; const path = resolveProjectRelativeSrc(src, projectDir, compiledDir); - if (isPrivateUnderAssetRoots(path, roots)) { + if (isPrivateUnderAssetRoots(path, roots, input.allowOutsideProjectAssets)) { log.warn(`[Render] Media "${id}" source is missing: it is a private project file`, { src }); return false; } diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index 17297549fb..5d362b1854 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -1,7 +1,15 @@ // fallow-ignore-file code-duplication import { afterEach, beforeAll, beforeEach, describe, expect, it, mock } from "bun:test"; import { createHash } from "node:crypto"; -import { mkdtempSync, writeFileSync, mkdirSync, rmSync, readFileSync, readdirSync } from "node:fs"; +import { + mkdtempSync, + writeFileSync, + mkdirSync, + rmSync, + readFileSync, + readdirSync, + symlinkSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { runInThisContext } from "node:vm"; @@ -295,6 +303,20 @@ describe("collectExternalAssets", () => { expect(result.html).toBe(html); // unchanged }); + it("leaves out a private project file named by a path spelled outside the project", () => { + const alias = `${projectDir}-alias`; + symlinkSync(projectDir, alias); + mkdirSync(join(projectDir, ".git"), { recursive: true }); + writeFileSync(join(projectDir, ".git", "clip.png"), "fake-private"); + const warnings: string[] = []; + const log = { ...defaultLogger, warn: (message: string) => warnings.push(message) }; + const html = ``; + const result = collectExternalAssets(html, projectDir, log); + rmSync(alias, { force: true }); + expect([...result.externalAssets.values()]).toEqual([join(alias, "logo.png")]); + expect(warnings).toHaveLength(1); + }); + it("collects and rewrites assets outside projectDir via src attribute", () => { const html = ``; const result = collectExternalAssets(html, projectDir); @@ -3583,6 +3605,33 @@ describe("compileForRender non-media payload sniff (STUDIO-5433)", () => { rmSync(projectDir, { recursive: true, force: true }); }, ); + + it("never probes a private project file named by a path spelled outside the project", async () => { + const projectDir = writeProject(""); + const alias = `${projectDir}-alias`; + symlinkSync(projectDir, alias); + const src = join(alias, ".git", "clip.mp4"); + writeFileSync( + join(projectDir, "index.html"), + readFileSync(join(projectDir, "index.html"), "utf8").replace( + "", + ``, + ), + ); + mkdirSync(join(projectDir, ".git"), { recursive: true }); + writeFileSync(src, readFileSync(join(projectDir, "assets", "nested.html"))); + const warnings: string[] = []; + const log = { ...defaultLogger, warn: (message: string) => warnings.push(message) }; + + await compileForRender(projectDir, join(projectDir, "index.html"), projectDir, { + log, + allowOutsideProjectAssets: true, + }); + + expect(warnings.join("\n")).not.toContain("text document"); + rmSync(alias, { force: true }); + rmSync(projectDir, { recursive: true, force: true }); + }); }); describe("duplicate media ids across nested compositions", () => { diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 7472f84e11..9c332658e5 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -836,7 +836,7 @@ async function resolveMediaDuration( if (!filePath.startsWith("/")) filePath = join(baseDir, filePath); if ( escapesAssetRoot(filePath, assetRoot) || - isPrivateUnderAssetRoots(filePath, [projectDir, downloadDir]) + isPrivateUnderAssetRoots(filePath, [projectDir, downloadDir], assetRoot === undefined) ) { return { duration: null, resolvedPath: filePath }; } @@ -3007,7 +3007,7 @@ async function compileWithAssets( namesAnotherNetworkRoot(projectDir, videoPath) || !existsSync(videoPath) || escapesAssetRoot(videoPath, assetRoot) || - isPrivateUnderAssetRoots(videoPath, [projectDir, downloadDir]) + isPrivateUnderAssetRoots(videoPath, [projectDir, downloadDir], assetRoot === undefined) ) continue; advised.add(videoPath); diff --git a/packages/producer/src/utils/paths.ts b/packages/producer/src/utils/paths.ts index b693ec55e2..01a7d47974 100644 --- a/packages/producer/src/utils/paths.ts +++ b/packages/producer/src/utils/paths.ts @@ -12,7 +12,7 @@ import { } from "node:path"; import { statSync } from "node:fs"; import { fileURLToPath } from "node:url"; -import { isSafePath } from "@hyperframes/core/safe-path"; +import { isPrivateFileUnder, isSafePath, isStudioPrivatePath } from "@hyperframes/core/safe-path"; import { isCopiedOutsideFileType, isPrivateOutsideFile, @@ -92,7 +92,11 @@ export function outsideAssetRefusal( path: string, policy: OutsideFilePolicy = {}, ): LeftOutReason | null { - if (!linksOutside(projectDir, path)) return null; + if (!linksOutside(projectDir, path)) { + const privateByAnotherSpelling = + !isPathInside(path, projectDir) && isPrivateFileUnder(projectDir, path, isStudioPrivatePath); + return privateByAnotherSpelling ? "blocklisted" : null; + } if (isPrivateOutsideFile(projectDir, path)) return "blocklisted"; const folder = statSync(path, { throwIfNoEntry: false })?.isDirectory(); if (folder || isUploadedOutsideFileType(path)) return null;