From 524ea46bf9d3fbf965fdb129506a830dbacd98df Mon Sep 17 00:00:00 2001 From: Andy Cheigh Date: Tue, 17 Nov 2026 09:41:24 +0000 Subject: [PATCH] fix(producer,parsers): render compile never looks for a scene's assets on another computer (#8063) A scene mounted as `\\host\share/scene.html` read its own file from inside the project, but its sibling assets were looked up next to `\\host\share`, which on Windows connects to that host and sends the user's login. The compile's asset checks now report any path outside the project as missing without touching the disk, and a data-composition-src with a leading `\` or `./` is rewritten to the project file it names before its assets are resolved. Co-authored-by: Claude Opus 5.5 --- packages/parsers/src/assetResolution.ts | 10 +++- .../parsers/src/assetResolution.win32.test.ts | 42 +++++++++++++++++ .../services/htmlCompiler.networkRoot.test.ts | 47 ++++++++++++++++++- .../producer/src/services/htmlCompiler.ts | 15 ++++-- 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/packages/parsers/src/assetResolution.ts b/packages/parsers/src/assetResolution.ts index 0be3767a53..3f5e386bae 100644 --- a/packages/parsers/src/assetResolution.ts +++ b/packages/parsers/src/assetResolution.ts @@ -5,7 +5,7 @@ import { fileURLToPath } from "node:url"; import { isNonRelativeUrl } from "./assetPaths.js"; import { decodeUrlPathVariants } from "./composition.js"; import { decodedUrlPath } from "./utils/urlPath.js"; -import { rewriteAssetPath } from "./rewriteSubCompPaths.js"; +import { rewriteAssetPath, type AssetExists } from "./rewriteSubCompPaths.js"; /** The subset of `node:path` that `isWithinProjectRoot` needs to run under * an injected platform (tests pass `path.win32` / `path.posix`). */ @@ -138,6 +138,14 @@ export function namesAnotherNetworkRoot(base: string, target: string): boolean { return /^[\\/]{2}/.test(root) && root.toLowerCase() !== parse(resolve(base)).root.toLowerCase(); } +/** `AssetExists` for a project on disk; a path outside it (a share, another drive) is missing, never looked at. */ +export function projectAssetExists(projectDir: string): AssetExists { + return (path) => { + const file = resolve(projectDir, path); + return isWithinProjectRoot(projectDir, file) && existsSync(file); + }; +} + export function srcOnAnotherNetworkRoot(projectDir: string, src: string): boolean { const path = fileUrlAsPath(cleanAssetUrl(src)); return isAbsolute(path) && namesAnotherNetworkRoot(projectDir, path); diff --git a/packages/parsers/src/assetResolution.win32.test.ts b/packages/parsers/src/assetResolution.win32.test.ts index 0b6c23d3cd..1f2ee76c15 100644 --- a/packages/parsers/src/assetResolution.win32.test.ts +++ b/packages/parsers/src/assetResolution.win32.test.ts @@ -30,7 +30,9 @@ const { resolveLocalAssetCandidates, resolveProjectRelativeSrc, srcOnAnotherNetworkRoot, + projectAssetExists, } = await import("./assetResolution.js"); +const { rewriteAssetPath } = await import("./rewriteSubCompPaths.js"); const { resolve } = await import("node:path"); const PROJECT = "C:\\project"; @@ -107,6 +109,46 @@ describe("resolveCompositionSrc under Windows rules", () => { }); }); +describe("projectAssetExists under Windows rules", () => { + it.each([ + "\\\\attacker.example\\share/img.png", + "//attacker.example/share/img.png", + "/\\attacker.example\\share\\img.png", + "\\\\?\\UNC\\attacker.example\\share\\img.png", + "\\\\.\\pipe\\x", + "D:/media/img.png", + "C:\\Users\\me\\img.png", + "\\Users\\me\\img.png", + "..\\img.png", + "scenes\\..\\..\\img.png", + ])("reports %s missing without touching the disk, even when it is there", (path) => { + present.clear(); + present.add(resolve(PROJECT, path)); + touched.length = 0; + expect(projectAssetExists(PROJECT)(path)).toBe(false); + expect(touched).toEqual([]); + }); + + it("finds a file inside the project", () => { + present.clear(); + present.add("C:\\project\\scenes\\img.png"); + touched.length = 0; + expect(projectAssetExists(PROJECT)("scenes/img.png")).toBe(true); + expect(touched).toEqual(["C:\\project\\scenes\\img.png"]); + }); + + it("never stats a UNC-looking scene's sibling asset on that share", () => { + present.clear(); + present.add("\\\\attacker.example\\share\\img.png"); + touched.length = 0; + const exists = projectAssetExists(PROJECT); + expect(rewriteAssetPath("\\\\attacker.example\\share/scene.html", "img.png", exists)).toBe( + "img.png", + ); + expect(touched).toEqual([]); + }); +}); + describe("localAbsoluteAssetFile under Windows rules", () => { it.each([ ["C:\\media\\clip.mp4", "C:\\media\\clip.mp4"], diff --git a/packages/producer/src/services/htmlCompiler.networkRoot.test.ts b/packages/producer/src/services/htmlCompiler.networkRoot.test.ts index a327ad1206..e3c45376ea 100644 --- a/packages/producer/src/services/htmlCompiler.networkRoot.test.ts +++ b/packages/producer/src/services/htmlCompiler.networkRoot.test.ts @@ -1,5 +1,6 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { parseHTML } from "linkedom"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; @@ -85,6 +86,50 @@ describe("composition paths on another network root", () => { expect(touched.paths.some((path) => path.endsWith("local.mp4"))).toBe(true); }); + it.each(["\\\\scenes/scene.html", "\\scenes/scene.html", ".\\scenes/scene.html"])( + "are never built from a backslash-prefixed scene src (%s)", + async (src) => { + // On Windows `\\scenes` is the computer `scenes`: probing its siblings sends the user's NTLM login to it. + projectDir = mkdtempSync(join(tmpdir(), "hf-network-root-scene-")); + mkdirSync(join(projectDir, "scenes")); + for (const name of ["img.png", "wide.png", "style.css", "bg.png", "anim.js"]) { + writeFileSync(join(projectDir, "scenes", name), "fixture"); + } + writeFileSync( + join(projectDir, "scenes", "scene.html"), + ` +
+ + + +
+ +
`, + ); + const htmlPath = join(projectDir, "index.html"); + writeFileSync( + htmlPath, + ` +
+
+
+ `, + ); + + const compiled = await compileForRender(projectDir, htmlPath, join(projectDir, "downloads")); + + expect(touched.paths.filter((path) => path.includes("\\"))).toEqual([]); + const scene = (name: string) => join(projectDir as string, "scenes", name); + expect(touched.paths).toEqual( + expect.arrayContaining(["img.png", "wide.png", "style.css", "bg.png"].map(scene)), + ); + const pic = parseHTML(compiled.html).document.getElementById("pic"); + expect(pic?.getAttribute("src")).toBe("scenes/img.png"); + expect(pic?.getAttribute("srcset")).toBe("scenes/wide.png 2x"); + expect(compiled.html).toContain('src="scenes/anim.js"'); + }, + ); + it("are never looked at when an image src is resolved", () => { projectDir = mkdtempSync(join(tmpdir(), "hf-network-root-img-")); const downloadDir = join(projectDir, "downloads"); diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index e8255e4edc..0a6cd8449e 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -95,6 +95,7 @@ import { import { isUnresolvedAssetPlaceholder, namesAnotherNetworkRoot, + projectAssetExists, readProjectFile, readTextFile, resolveCompositionSrc, @@ -253,10 +254,10 @@ class EmptyCompositionError extends Error { } } -/** The html with each data-composition-src as the project-relative path it names (%xx decoded, no leading /). */ +/** The html with each data-composition-src as the project-relative path it names (%xx decoded, no leading /, \ or ./). */ function normalizeCompositionSrcs(html: string, projectDir: string): string { return html.replace(/(\bdata-composition-src=)(["'])([^"']*)\2/g, (whole, name, quote, value) => { - if (!value.includes("%") && !value.startsWith("/")) return whole; + if (!value.includes("%") && !/^\.?[\\/]/.test(value)) return whole; const file = resolveCompositionSrc(projectDir, value); if (file === null) return whole; return `${name}${quote}${relative(projectDir, file).split(sep).join("/")}${quote}`; @@ -1216,7 +1217,11 @@ function inlineSubCompositions( ? readProjectFile(filePath) : null; return read?.kind === "file" - ? rewriteUnresolvableGsapToCdn(read.text, projectDir, srcPath) + ? rewriteUnresolvableGsapToCdn( + normalizeCompositionSrcs(read.text, projectDir), + projectDir, + srcPath, + ) : null; }; const resolveMountHtml = (srcPath: string): string | null => { @@ -1260,7 +1265,7 @@ function inlineSubCompositions( parseHtml: (htmlStr: string) => parseHTML(htmlStr).document as unknown as Document, // Mirrors the preview bundler: a sub-composition's SIBLING assets resolve // against its own directory, project-root refs stay as authored. - assetExists: (path: string) => existsSync(resolve(projectDir, path)), + assetExists: projectAssetExists(projectDir), // Preserve the authored root wrapper as a child of the host, matching // the preview bundler's shape (htmlBundler.ts's prepareFlattenedInnerRoot, // which the runtime compositionLoader mirrors with its own copy for the @@ -2229,7 +2234,7 @@ function rebaseDirectEntryAssetPaths(html: string, projectDir: string, htmlPath: if (!entryPath.includes("/")) return html; const { document } = parseHTML(html); - const assetExists = (path: string) => existsSync(resolve(projectDir, path)); + const assetExists = projectAssetExists(projectDir); rewriteAssetPaths( document.querySelectorAll("[src], [href], [srcset]"), entryPath,