fix(cli,studio-server): no lookup of another computer's share path before refusing it (#7914)

On Windows even a stat or realpath of a \\host\share path connects to that
host and signs in as the current user. Three readers looked such paths up
before refusing them:

- `hyperframes compositions` resolved a data-composition-src with
  path.resolve, so an absolute or share src reached readProjectFile. It now
  uses resolveCompositionSrc, the resolver preview and render use.
- The timeline reader (`timeline`, `audio`, measure) realpath'd a
  sub-composition before its containment check. It now refuses a path on
  another network root first.
- Artifact bookkeeping for render, check and snapshot looked up the paths
  recorded in .hyperframes/video-state.json. A recorded path must now be
  spelled inside a .hyperframes/artifacts/<kind> folder on the project's own
  root before cleanup looks at it, and a record naming another share counts
  as gone without a lookup.
This commit is contained in:
Michael Wang
2026-11-16 02:32:06 +00:00
committed by GitHub
parent c06e98e460
commit 9b5a3ce74a
6 changed files with 328 additions and 15 deletions
@@ -0,0 +1,124 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
// Windows path rules on any host, and a disk that records every path it is asked about.
const disk = vi.hoisted(() => ({
files: new Map<string, string>(),
touched: [] as string[],
}));
vi.mock("node:path", async () => {
const path = await vi.importActual<typeof import("node:path")>("node:path");
return { ...path.win32, default: path.win32 };
});
vi.mock("node:fs", async () => {
const fs = await vi.importActual<typeof import("node:fs")>("node:fs");
const missing = (path: string) => Object.assign(new Error(`ENOENT: ${path}`), { code: "ENOENT" });
const look = (path: string) => {
disk.touched.push(String(path));
return disk.files.get(String(path).toLowerCase());
};
const opened = new Map<number, string>();
const existsSync = (path: string) => look(path) !== undefined;
const statSync = (path: string) => {
if (look(path) === undefined) throw missing(path);
return { isFile: () => true, isDirectory: () => false };
};
const realpathSync = (path: string) => {
if (look(path) === undefined && !/^c:\\project$/i.test(path)) throw missing(path);
return path;
};
const openSync = (path: string) => {
if (look(path) === undefined) throw missing(path);
const fd = opened.size + 100;
opened.set(fd, path);
return fd;
};
const fstatSync = () => ({ isFile: () => true });
const readFileSync = (target: string | number, encoding?: string) => {
const path = typeof target === "number" ? opened.get(target)! : target;
const text = look(path);
if (text === undefined) throw missing(path);
return encoding ? text : Buffer.from(text);
};
const closeSync = (fd: number) => opened.delete(fd);
const overrides = {
existsSync,
statSync,
realpathSync,
openSync,
fstatSync,
readFileSync,
closeSync,
};
return { ...fs, ...overrides, default: { ...fs, ...overrides } };
});
const { ensureDOMParser } = await import("../utils/dom.js");
const { parseCompositions } = await import("./compositions.js");
const { describeProject } = await import("../timeline/describeProject.js");
const PROJECT = "C:\\project";
const SUB = `<template><div data-composition-id="sub"><h1 data-start="0" data-duration="2">Hi</h1></div></template>`;
const SHARES = [
"//host/share/x.html",
"\\\\host\\share\\x.html",
"\\\\?\\UNC\\host\\share\\x.html",
"\\\\.\\pipe\\x.html",
];
const onShare = (path: string) => /^[\\/]{2}/.test(path);
const host = (src: string) =>
`<div data-composition-id="main" data-duration="5">
<div id="host" data-composition-id="sub" data-composition-src="${src}" data-start="0" data-duration="2"></div>
</div>`;
const subOf = (found: Array<{ id: string; source?: string }>) => found.find((c) => c.id === "sub");
const timelineOf = (src: string) =>
describeProject(
`${PROJECT}\\index.html`,
async () => 0,
new Map([["index.html", `<html><body>${host(src)}</body></html>`]]),
);
beforeEach(() => {
ensureDOMParser();
disk.files.clear();
disk.touched.length = 0;
disk.files.set("c:\\project\\scenes\\sub.html", SUB);
});
describe("sub-composition sources on another computer's share", () => {
it.each(SHARES)("compositions never looks up %s", (src) => {
const found = subOf(parseCompositions(host(src), PROJECT));
expect(disk.touched.filter(onShare)).toEqual([]);
expect(found).toBeDefined();
expect(found?.source).toBeUndefined();
});
it.each(SHARES)("timeline never looks up %s", async (src) => {
await timelineOf(src);
expect(disk.touched.filter(onShare)).toEqual([]);
});
it("compositions reads no src that leaves the project", () => {
disk.files.set("c:\\outside.html", SUB);
for (const src of ["../outside.html", "C:\\outside.html"]) {
const found = subOf(parseCompositions(host(src), PROJECT));
expect(found?.source).toBeUndefined();
}
expect(disk.touched.filter((path) => /^c:\\outside\.html$/i.test(path))).toEqual([]);
});
it("still reads a sub-composition inside the project", async () => {
const found = subOf(parseCompositions(host("scenes/sub.html"), PROJECT));
expect(found).toMatchObject({
id: "sub",
source: "scenes/sub.html",
duration: 2,
});
disk.touched.length = 0;
const timeline = await timelineOf("scenes/sub.html");
expect(timeline.tracks.flatMap((track) => track.rows).length).toBeGreaterThan(1);
expect(disk.touched).toContain("C:\\project\\scenes\\sub.html");
});
});
+5 -5
View File
@@ -1,10 +1,10 @@
import { defineCommand } from "../utils/defineCommand.js";
import type { Example } from "./_examples.js";
import { readFileSync } from "node:fs";
import { readProjectFile } from "@hyperframes/parsers/asset-resolution";
import { readProjectFile, resolveCompositionSrc } from "@hyperframes/parsers/asset-resolution";
import { resolveCompositionDuration } from "@hyperframes/parsers/composition-duration";
import { readDataDurationSeconds } from "@hyperframes/parsers/media-duration";
import { resolve, dirname } from "node:path";
import { dirname } from "node:path";
export const examples: Example[] = [
["List compositions in the current project", "hyperframes compositions"],
@@ -67,9 +67,9 @@ export function parseCompositions(html: string, baseDir: string): CompositionInf
// If this references an external sub-composition, parse that file
if (compositionSrc) {
const subPath = resolve(baseDir, compositionSrc);
const sub = readProjectFile(subPath);
if (sub.kind === "file") {
const subPath = resolveCompositionSrc(baseDir, compositionSrc);
const sub = subPath ? readProjectFile(subPath) : null;
if (sub?.kind === "file") {
const subInfo = parseSubComposition(sub.text, id, width, height);
compositions.push({ ...subInfo, source: compositionSrc });
return;
+5 -1
View File
@@ -10,7 +10,10 @@ import { HF_AUDIO_GROUP_ATTR } from "@hyperframes/core/audio-groups";
import { byStart, type ClipFact, type ClipLane } from "@hyperframes/core/clip-facts";
import { fpsToNumber, parseFpsWithDefault, parseNumeric } from "@hyperframes/core";
import { findSyncPartner, readLinkTiming, syncOffsetFrames } from "@hyperframes/core/media-link";
import { resolveAuthoredAssetFile } from "@hyperframes/parsers/asset-resolution";
import {
namesAnotherNetworkRoot,
resolveAuthoredAssetFile,
} from "@hyperframes/parsers/asset-resolution";
import { readCompositionFps } from "../utils/compositionFps.js";
import {
readMediaOffsetSeconds,
@@ -394,6 +397,7 @@ function cutRow(row: ClipDraft, slots: readonly HostSlot[]): ClipDraft[] {
/** The file's real path when it is a regular file inside the project (symlinks resolved), else null. */
function realFileInside(projectDir: string, path: string): string | null {
if (namesAnotherNetworkRoot(projectDir, path)) return null;
try {
const real = realpathSync(path);
const inside = relative(realpathSync(projectDir), real);
@@ -2,13 +2,15 @@ import { createHash } from "node:crypto";
import {
closeSync,
constants,
existsSync,
fstatSync,
lstatSync,
openSync,
readFileSync,
unlinkSync,
} from "node:fs";
import { isAbsolute, join, relative, resolve, sep } from "node:path";
import { isAbsolute, join, parse, relative, resolve, sep } from "node:path";
import { namesAnotherNetworkRoot } from "@hyperframes/parsers/asset-resolution";
import { pinProjectRecord, realFilePath } from "../helpers/safePath.js";
import type { ArtifactRecord, RenderRecord, VideoState } from "./videoState.js";
@@ -28,11 +30,38 @@ const records = (state: VideoState, kind: PendingArtifactCleanup["kind"]) => [
const protectedRevisions = (state: VideoState) =>
new Set([state.currentRevision, ...(state.renders?.retainedRevisions ?? [])]);
const canonicalUnlessOnAnotherShare = (projectDir: string, path: string): string =>
namesAnotherNetworkRoot(projectDir, path) ? resolve(path) : realFilePath(path);
export const recordedFileGone = (projectDir: string, path: string): boolean =>
namesAnotherNetworkRoot(projectDir, path) || !existsSync(path);
function spelledInsideArtifactsFolder(
projectDir: string,
path: string,
kind: PendingArtifactCleanup["kind"],
): boolean {
if (!isAbsolute(path) || namesAnotherNetworkRoot(projectDir, path)) return false;
if (parse(resolve(path)).root.toLowerCase() !== parse(resolve(projectDir)).root.toLowerCase())
return false;
const parts = resolve(path)
.split(sep)
.map((part) => part.toLowerCase());
return parts.some(
(part, i) =>
part === ".hyperframes" &&
parts[i + 1] === "artifacts" &&
parts[i + 2] === kind &&
i + 3 < parts.length,
);
}
function managedPath(
projectDir: string,
path: string,
kind: PendingArtifactCleanup["kind"],
): boolean {
if (!spelledInsideArtifactsFolder(projectDir, path, kind)) return false;
const canonicalProject = realFilePath(resolve(projectDir));
const canonicalPath = realFilePath(path);
const root = join(canonicalProject, ".hyperframes", "artifacts", kind);
@@ -84,7 +113,7 @@ export function planArtifactCleanup(
const path = record.image ?? record.report;
if (!path || record.status === "cleaned") continue;
if (removable(record, path, kind)) candidates.push({ kind, record, path });
else keepPaths.add(realFilePath(path));
else keepPaths.add(canonicalUnlessOnAnotherShare(projectDir, path));
}
}
for (const { kind, record, path } of candidates) {
@@ -149,7 +178,7 @@ export function finishArtifactCleanup(projectDir: string, state: VideoState): vo
kinds.flatMap((kind) =>
records(state, kind)
.flatMap((record) => record.image ?? record.report ?? [])
.map(realFilePath),
.map((path) => canonicalUnlessOnAnotherShare(projectDir, path)),
),
);
state.pendingArtifactCleanup = (state.pendingArtifactCleanup ?? []).filter((item) => {
@@ -0,0 +1,147 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ArtifactRecord, VideoState } from "./videoState.js";
// Windows path rules on any host, and a disk that records every path it is asked about.
const disk = vi.hoisted(() => ({
files: new Set<string>(),
touched: [] as string[],
}));
vi.mock("node:path", async () => {
const path = await vi.importActual<typeof import("node:path")>("node:path");
return { ...path.win32, default: path.win32 };
});
vi.mock("node:fs", async () => {
const fs = await vi.importActual<typeof import("node:fs")>("node:fs");
const missing = (path: string) => Object.assign(new Error(`ENOENT: ${path}`), { code: "ENOENT" });
const look = (path: string) => (disk.touched.push(path), disk.files.has(path.toLowerCase()));
const overrides = {
existsSync: look,
openSync: (path: string) => {
if (!look(path)) throw missing(path);
return 100;
},
lstatSync: (path: string) => {
if (!look(path)) throw missing(path);
return { isSymbolicLink: () => false };
},
};
return { ...fs, ...overrides, default: { ...fs, ...overrides } };
});
// The built package would keep the host's own path rules; its source runs under the Windows ones above.
vi.mock(
"@hyperframes/parsers/asset-resolution",
() => import("../../../parsers/src/assetResolution.js"),
);
vi.mock("../helpers/safePath.js", async () => {
const { win32 } = await vi.importActual<typeof import("node:path")>("node:path");
const realFilePath = (path: string) => (disk.touched.push(path), path);
const pinProjectRecord = (base: string, relative: string) => {
disk.touched.push(win32.join(base, relative));
return win32.join(base, relative);
};
return { realFilePath, pinProjectRecord };
});
const { finishArtifactCleanup, planArtifactCleanup, recordedFileGone } =
await import("./artifactCleanup.js");
const PROJECT = "C:\\project";
const LOCAL = `${PROJECT}\\.hyperframes\\artifacts\\checks\\run\\report.json`;
const SHARES = [
"\\\\host\\share\\report.json",
"//host/share/report.json",
"\\\\?\\UNC\\host\\share\\report.json",
"\\\\.\\pipe\\report.json",
];
const onShare = (path: string) => /^[\\/]{2}/.test(path);
function record(id: string, report: string): ArtifactRecord {
return {
id,
report,
revision: "old",
createdAt: 1,
contentHash: "hash",
inputFingerprint: "fp",
inputScope: "local-project-files",
attribution: "recorded-revision",
} as ArtifactRecord;
}
function stateWith(checks: ArtifactRecord[], pending: string[] = []): VideoState {
const collection = (list: ArtifactRecord[]) => ({
latest: null,
revisions: list.length ? { old: list } : {},
unversioned: [],
});
return {
version: 1,
historyId: "h",
currentRevision: "new",
pendingChanges: false,
files: {},
revisions: {},
checks: collection(checks),
snapshots: collection([]),
pendingArtifactCleanup: pending.map((path, i) => ({
path,
hash: "hash",
revision: "old",
id: `p${i}`,
kind: "checks" as const,
})),
} as unknown as VideoState;
}
const render = {
id: "r",
completedAt: 2,
output: "out.mp4",
status: "completed" as const,
};
beforeEach(() => {
disk.files.clear();
disk.touched.length = 0;
});
describe("recorded artifact paths on another computer's share", () => {
it.each(SHARES)("counts %s as gone without looking it up", (path) => {
expect(recordedFileGone(PROJECT, path)).toBe(true);
expect(disk.touched).toEqual([]);
});
it.each(SHARES)("plans cleanup around %s without looking it up", (path) => {
const state = stateWith([record("share", path)]);
planArtifactCleanup(PROJECT, state, render);
expect(disk.touched.filter(onShare)).toEqual([]);
expect(state.checks.revisions.old?.[0]?.status).toBeUndefined();
});
it.each(SHARES)("drops pending cleanup of %s unread", (path) => {
const state = stateWith([record("share", path)], [path]);
finishArtifactCleanup(PROJECT, state);
expect(disk.touched.filter(onShare)).toEqual([]);
expect(state.pendingArtifactCleanup).toEqual([]);
});
it("leaves a recorded path outside the artifacts folder unlooked-up", () => {
const state = stateWith([], ["C:\\elsewhere\\report.json"]);
finishArtifactCleanup(PROJECT, state);
expect(disk.touched).not.toContain("C:\\elsewhere\\report.json");
expect(state.pendingArtifactCleanup).toEqual([]);
});
it("still checks and cleans the project's own artifacts", () => {
disk.files.add(LOCAL.toLowerCase());
expect(recordedFileGone(PROJECT, LOCAL)).toBe(false);
const state = stateWith([record("local", LOCAL)]);
planArtifactCleanup(PROJECT, state, render);
expect(state.checks.revisions.old?.[0]?.status).toBe("cleaned");
expect(state.pendingArtifactCleanup?.map((item) => item.path)).toEqual([LOCAL]);
});
});
@@ -9,6 +9,7 @@ import { takeHistoryOwnership } from "./ownerLock.js";
import {
planArtifactCleanup,
finishArtifactCleanup,
recordedFileGone,
type PendingArtifactCleanup,
} from "./artifactCleanup.js";
import {
@@ -268,7 +269,7 @@ export async function recordArtifacts(
attribution,
};
const artifacts = kind === "check" ? state.checks : state.snapshots;
dropSupersededArtifacts(artifacts, artifact);
dropSupersededArtifacts(projectDir, artifacts, artifact);
const list = revision ? (artifacts.revisions[revision] ??= []) : artifacts.unversioned;
list.push(record);
records.push(record);
@@ -290,18 +291,26 @@ function attributionOf(unchanged: boolean, revision: string | null): ArtifactRec
}
// An explicit output path can be reused; never retain a reference to overwritten evidence.
function supersedes(artifact: ArtifactInput, previous: ArtifactRecord): boolean {
if (previous.status !== "cleaned" && !existsSync(previous.image ?? previous.report ?? ""))
return true;
function supersedes(
projectDir: string,
artifact: ArtifactInput,
previous: ArtifactRecord,
): boolean {
const path = previous.image ?? previous.report ?? "";
if (previous.status !== "cleaned" && recordedFileGone(projectDir, path)) return true;
if (previous.id === artifact.id) return true;
if (artifact.image && previous.image === artifact.image) return true;
return Boolean(artifact.report && previous.report === artifact.report);
}
function dropSupersededArtifacts(artifacts: ArtifactCollection, artifact: ArtifactInput): void {
function dropSupersededArtifacts(
projectDir: string,
artifacts: ArtifactCollection,
artifact: ArtifactInput,
): void {
for (const list of [...Object.values(artifacts.revisions), artifacts.unversioned]) {
for (let i = list.length - 1; i >= 0; i--) {
if (supersedes(artifact, list[i]!)) list.splice(i, 1);
if (supersedes(projectDir, artifact, list[i]!)) list.splice(i, 1);
}
}
}