mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9b5a3ce74a
commit
053653c4b4
@@ -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<typeof import("node:path")>("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<typeof import("node:fs")>("node:fs");
|
||||
const record =
|
||||
<Args extends unknown[], Result>(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) => `<img src="${src}">`).join("")}<img src="local.gif">`;
|
||||
|
||||
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");
|
||||
});
|
||||
});
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<typeof import("node:path")>("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<typeof import("node:fs")>("node:fs");
|
||||
const record =
|
||||
<Args extends unknown[], Result>(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) => `<img src="${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)}<img src="..\\shared\\local.png">
|
||||
<style>.a { background: url("\\\\host\\share\\bg.png"); }</style>
|
||||
<div style="background: url('\\\\?\\UNC\\host\\share\\inline.png')"></div>`;
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user