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"), + `
+
+
+
+