From 053653c4b4839bcfc37529735c1bba45db33bc88 Mon Sep 17 00:00:00 2001 From: andybpan <108543122+andybpan@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:43:52 +0000 Subject: [PATCH] fix(producer): image prep and outside-asset collection check every path's root before reading it (#7912) Image prep (HEIF and GIF, in renders and the Studio preview) now applies the other-network-root test to every candidate path, whatever its leading character, and decides whether a candidate is allowed before checking that it exists. Outside-project asset collection leaves out paths on another network root with the existing network_share reason before any file lookup. Tests run both under Windows path rules and record every path the disk is asked about. Co-authored-by: Claude Opus 5.5 --- ...ioServer.previewNetworkShare.win32.test.ts | 79 +++++++++++++ .../producer/src/services/animatedGifPrep.ts | 13 +-- .../producer/src/services/htmlCompiler.ts | 5 +- .../imagePrep.networkShare.win32.test.ts | 106 ++++++++++++++++++ 4 files changed, 193 insertions(+), 10 deletions(-) create mode 100644 packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts create mode 100644 packages/producer/src/services/imagePrep.networkShare.win32.test.ts diff --git a/packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts b/packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts new file mode 100644 index 0000000000..f7f24ae274 --- /dev/null +++ b/packages/cli/src/server/studioServer.previewNetworkShare.win32.test.ts @@ -0,0 +1,79 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; + +// The server starts under this host's path rules; only the preview transform runs under Windows rules. +const host = vi.hoisted(() => ({ windows: false, touched: [] as string[] })); +vi.mock("node:path", async () => { + const path = await vi.importActual("node:path"); + const rules = () => (host.windows ? path.win32 : path.posix); + const switched = Object.fromEntries( + Object.entries(path.posix).map(([name, value]) => [ + name, + typeof value === "function" + ? (...args: unknown[]) => Reflect.apply(Reflect.get(rules(), name), undefined, args) + : value, + ]), + ); + return { ...switched, default: switched }; +}); +vi.mock("node:fs", async () => { + const fs = await vi.importActual("node:fs"); + const record = + (look: (...args: Args) => Result) => + (...args: Args): Result => { + if (host.windows) host.touched.push(String(args[0])); + return look(...args); + }; + const realpath = record(fs.realpathSync); + const overrides = { + existsSync: record(fs.existsSync), + statSync: record(fs.statSync), + lstatSync: record(fs.lstatSync), + readFileSync: record(fs.readFileSync), + openSync: record(fs.openSync), + realpathSync: Object.assign(realpath, { native: record(fs.realpathSync.native) }), + }; + return { ...fs, ...overrides, default: { ...fs, ...overrides } }; +}); + +const { createStudioServer } = await import("./studioServer.js"); +const { cleanupStudioServerRoot, makeStudioServerRoot } = + await import("./studioServerTestFixture.js"); + +const SHARES = [ + "\\\\host\\share\\a.gif", + "/\\host\\share\\b.gif", + "\\/host/share/c.heic", + "//host/share/d.gif", + "\\\\?\\UNC\\host\\share\\e.gif", + "\\\\.\\pipe\\g.gif", + "\\\\.\\UNC\\host\\share\\h.heic", +]; +const onAShare = (path: string) => /^[\\/]{2}[^\\/]/.test(path); + +let cleanup: (() => void) | undefined; +afterEach(() => { + host.windows = false; + host.touched.length = 0; + cleanup?.(); + cleanup = undefined; +}); + +describe("studio preview image preparation under Windows path rules", () => { + it("looks only at local paths for image srcs on another computer's share", async () => { + const { root, projectDir } = makeStudioServerRoot("hf-preview-share-"); + const server = createStudioServer({ projectDir }); + cleanup = () => cleanupStudioServerRoot(server, root); + const html = `${SHARES.map((src) => ``).join("")}`; + + host.windows = true; + await server.adapter.transformPreviewHtml?.({ + html, + project: { id: "p", dir: "C:\\project" }, + activeCompositionPath: "index.html", + }); + host.windows = false; + + expect(host.touched.filter(onAShare)).toEqual([]); + expect(host.touched).toContain("C:\\project\\local.gif"); + }); +}); diff --git a/packages/producer/src/services/animatedGifPrep.ts b/packages/producer/src/services/animatedGifPrep.ts index 6c5583bcc0..ae5249ca3f 100644 --- a/packages/producer/src/services/animatedGifPrep.ts +++ b/packages/producer/src/services/animatedGifPrep.ts @@ -98,21 +98,18 @@ export function resolveLocalImageSourcePath( if (mapped && existsSync(mapped)) return mapped; if (isHttpUrl(trimmed)) return null; - const protocolRelativeUrlOnShare = - basePath.startsWith("/") && namesAnotherNetworkRoot(options.projectDir, basePath); const projectRelative = basePath.startsWith("/") ? basePath.slice(1) : basePath; const candidates = [ - ...(protocolRelativeUrlOnShare - ? [] - : [isAbsolute(basePath) ? basePath : resolve(options.projectDir, projectRelative)]), + isAbsolute(basePath) ? basePath : resolve(options.projectDir, projectRelative), resolve(options.downloadDir, normalizedBase), ]; const allowed = (candidate: string) => - (!escapesAssetRoot(candidate, options.assetRoot) && + !namesAnotherNetworkRoot(options.projectDir, candidate) && + ((!escapesAssetRoot(candidate, options.assetRoot) && outsideAssetRefusal(options.projectDir, candidate) !== "blocklisted") || - !escapesAssetRoot(candidate, options.downloadDir); - return candidates.find((candidate) => existsSync(candidate) && allowed(candidate)) ?? null; + !escapesAssetRoot(candidate, options.downloadDir)); + return candidates.find((candidate) => allowed(candidate) && existsSync(candidate)) ?? null; } function isUsableFile(path: string): boolean { diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 951b0a82a5..ee8eacda68 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -1471,8 +1471,9 @@ export function collectExternalAssets( if (isPathInside(absPath, absProjectDir)) { return null; // inside projectDir, file server handles this } - if (!existsSync(absPath)) return null; - const refusal = outsideAssetRefusal(absProjectDir, absPath); + const onShare = namesAnotherNetworkRoot(absProjectDir, absPath); + if (!onShare && !existsSync(absPath)) return null; + const refusal = onShare ? "network_share" : outsideAssetRefusal(absProjectDir, absPath); if (refusal) { if (!leftOut.has(trimmed)) log.warn(`[Compiler] ${leftOutOfRender(file, trimmed, refusal)}`); leftOut.add(trimmed); diff --git a/packages/producer/src/services/imagePrep.networkShare.win32.test.ts b/packages/producer/src/services/imagePrep.networkShare.win32.test.ts new file mode 100644 index 0000000000..777b0e8f3f --- /dev/null +++ b/packages/producer/src/services/imagePrep.networkShare.win32.test.ts @@ -0,0 +1,106 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; + +// Windows path rules on any host, and a disk that records every path it is asked about. +const touched = vi.hoisted(() => [] as string[]); +vi.mock("node:path", async () => { + const path = await vi.importActual("node:path"); + return { ...path.win32, default: path.win32 }; +}); +// Built workspace packages load outside the path mock; the share test runs from source under it. +vi.mock( + "@hyperframes/parsers/asset-resolution", + () => import("../../../parsers/src/assetResolution.js"), +); +for (const fsModule of ["fs", "node:fs"]) { + vi.doMock(fsModule, async () => { + const fs = await vi.importActual("node:fs"); + const record = + (look: (...args: Args) => Result) => + (...args: Args): Result => { + touched.push(String(args[0])); + return look(...args); + }; + const realpath = record(fs.realpathSync); + const overrides = { + existsSync: record(fs.existsSync), + statSync: record(fs.statSync), + lstatSync: record(fs.lstatSync), + readFileSync: record(fs.readFileSync), + openSync: record(fs.openSync), + realpathSync: Object.assign(realpath, { native: record(fs.realpathSync.native) }), + }; + return { ...fs, ...overrides, default: { ...fs, ...overrides } }; + }); +} + +const { prepareAnimatedGifInputs, resolveLocalImageSourcePath } = + await import("./animatedGifPrep.js"); +const { prepareHeifImageInputs } = await import("./heifImagePrep.js"); +const { collectExternalAssets } = await import("./htmlCompiler.js"); + +const PROJECT = "C:\\project"; +const DOWNLOADS = "C:\\project\\.hyperframes\\prepared-assets\\downloads"; +const SHARES = [ + "\\\\host\\share\\a.gif", + "/\\host\\share\\b.gif", + "\\/host/share/c.gif", + "//host/share/d.gif", + "\\\\?\\UNC\\host\\share\\e.gif", + "\\\\?\\D:\\media\\f.gif", + "\\\\.\\pipe\\g.gif", + "\\\\.\\UNC\\host\\share\\h.gif", + "\\\\host\\share\\i.gif?v=2", +]; +// Names a host or device namespace; a bare "\\" (a \\?\ src cut at "?") names neither. +const onAShare = (path: string) => /^[\\/]{2}[^\\/]/.test(path); +const page = (srcs: string[]) => srcs.map((src) => ``).join(""); + +afterEach(() => { + touched.length = 0; +}); + +describe("image prep under Windows path rules", () => { + it.each(SHARES)("never looks at %s when resolving an image src", (src) => { + const options = { projectDir: PROJECT, downloadDir: DOWNLOADS }; + expect(resolveLocalImageSourcePath(src, options)).toBeNull(); + expect(touched.filter(onAShare)).toEqual([]); + }); + + it("HEIF prep looks only at local paths", async () => { + await prepareHeifImageInputs(page([...SHARES, "local.gif"]), { + projectDir: PROJECT, + downloadDir: DOWNLOADS, + }); + expect(touched.filter(onAShare)).toEqual([]); + expect(touched).toContain("C:\\project\\local.gif"); + }); + + it("GIF prep looks only at local paths", async () => { + await prepareAnimatedGifInputs(page([...SHARES, "local.gif"]), { + projectDir: PROJECT, + downloadDir: DOWNLOADS, + }); + expect(touched.filter(onAShare)).toEqual([]); + expect(touched).toContain("C:\\project\\local.gif"); + }); +}); + +describe("collectExternalAssets under Windows path rules", () => { + it("leaves out references on another computer's share before any filesystem call", () => { + const warn = vi.fn(); + const log = { debug: vi.fn(), info: vi.fn(), warn, error: vi.fn() }; + const html = `${page(SHARES)} + +
`; + + const { externalAssets } = collectExternalAssets(html, PROJECT, log); + + expect(externalAssets.size).toBe(0); + expect(touched.filter(onAShare)).toEqual([]); + expect(touched).toContain("C:\\shared\\local.png"); + const warnings = warn.mock.calls.map(([line]) => String(line)); + expect(warnings.some((line) => line.includes("\\\\host\\share\\a.gif"))).toBe(true); + expect(warnings.some((line) => line.includes("\\\\host\\share\\bg.png"))).toBe(true); + expect(warnings.every((line) => line.includes("network share"))).toBe(true); + }); +});