Files
hyperframes/packages/studio-server/src/helpers/proxyCache.test.ts
T
Michael WangandClaude Opus 5.5 b8bb491900 fix(studio-server): harden preview paths, cache folders and local servers (#7510)
* fix(studio-server): keep cache writes inside the project and canonicalize preview paths

- The tool's own folders (.hyperframes records, backups, preview documents,
  .thumbnails, .transcode-cache) are no longer written, pruned or emptied
  through a link that points outside the project. Thumbnails are still
  served when their cache folder is a link; they are just not cached.
- The hf-id stamp record and the history id are replaced atomically instead
  of written through an existing link.
- Sub-composition previews and thumbnails use the composition's canonical
  project-relative path, and the path is attribute-encoded in the document.
- Thumbnail requests from another site are refused before any capture.
- Saved GSAP scripts escape a `</script` sequence so the script element
  keeps its contents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(core): refuse a UNC or device path on another root before any disk access

isSafePath now rejects a target whose root is a UNC or device root other than
the project's own before it calls realpath or lstat. Projects on a share keep working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(studio): answer WebMCP clients only from Studio's own page

The polyfill now starts with its tab transport limited to Studio's origin and
its frame transport off, so a page that frames Studio cannot list or call its tools.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(inspector): check Host on the per-session preview server

The preview page server applies the inspector API's loopback Host rule, so a
rebound DNS name cannot read the bundled composition or project files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* revert(studio-server): keep writing media proxies through a linked .transcode-cache

Refusing the folder made every proxied video in such a project answer 502.
A linked cache folder now receives copies again; it is still never swept,
so nothing already in the folder it points at is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(studio-server): drop a comment the comment ratchet refuses

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): skip HEIF and GIF preview prep when .hyperframes/prepared-assets is a link

The preview transform wrote converted images into .hyperframes/prepared-assets
through a link, so a project shipping .hyperframes as a link to another folder
had files written there. Such a project now gets its preview with the images
as written in the composition, unconverted, and nothing is written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(studio-server): never sweep a linked .transcode-cache, from Studio or hyperframes clean

The link check moves into cleanupProxyCache, which both Studio's cache
maintenance and `hyperframes clean` call, so neither removes files from the
folder a linked .transcode-cache points at.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 20:26:12 +00:00

237 lines
7.8 KiB
TypeScript

import { createHash, randomUUID } from "node:crypto";
import {
existsSync,
mkdirSync,
mkdtempSync,
rmSync,
symlinkSync,
utimesSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { basename, join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { cleanupProxyCache } from "./proxyCache.js";
const tempDirs: string[] = [];
function cacheDir(): string {
const root = mkdtempSync(join(tmpdir(), "hf-proxy-cache-"));
tempDirs.push(root);
const cache = join(root, ".transcode-cache");
mkdirSync(cache);
return cache;
}
/** A proxy path named the way the transcoder names one. */
const proxy = (cache: string, label: string, extension = ".mp4") =>
join(cache, `${createHash("sha256").update(label).digest("hex")}${extension}`);
function writeEntry(path: string, bytes: number, modifiedAt: number): void {
writeFileSync(path, Buffer.alloc(bytes));
const date = new Date(modifiedAt);
utimesSync(path, date, date);
}
afterEach(() => {
for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
describe("cleanupProxyCache", () => {
it.skipIf(process.platform === "win32")(
"removes nothing from the folder a linked .transcode-cache points at",
() => {
const project = mkdtempSync(join(tmpdir(), "hf-proxy-cache-project-"));
const outside = mkdtempSync(join(tmpdir(), "hf-proxy-cache-outside-"));
tempDirs.push(project, outside);
const now = 1_800_000_000_000;
const theirs = proxy(outside, "someone-elses-file");
writeEntry(theirs, 4, now - 31 * 24 * 60 * 60 * 1000);
symlinkSync(outside, join(project, ".transcode-cache"));
const result = cleanupProxyCache(join(project, ".transcode-cache"), {
now,
maxBytes: Number.POSITIVE_INFINITY,
maxIdleMs: 60 * 60 * 1000,
minSweepIntervalMs: 0,
});
expect(result.removed).toEqual([]);
expect(existsSync(theirs)).toBe(true);
},
);
it("removes idle and oldest entries until the cache is within its byte budget", () => {
const cache = cacheDir();
const now = 1_800_000_000_000;
const expired = proxy(cache, "expired");
const oldest = proxy(cache, "oldest");
const newest = proxy(cache, "newest");
writeEntry(expired, 4, now - 31 * 24 * 60 * 60 * 1000);
writeEntry(oldest, 6, now - 3_000);
writeEntry(newest, 6, now - 1_000);
const result = cleanupProxyCache(cache, {
now,
maxBytes: 8,
maxIdleMs: 30 * 24 * 60 * 60 * 1000,
minSweepIntervalMs: 0,
});
expect(result.removed).toEqual([expired, oldest]);
expect(result.bytesAfter).toBe(6);
expect(existsSync(expired)).toBe(false);
expect(existsSync(oldest)).toBe(false);
expect(existsSync(newest)).toBe(true);
});
it("counts the cached still frames toward the budget and sweeps them with the copies", () => {
const cache = cacheDir();
const now = 1_800_000_000_000;
const oldStill = proxy(cache, "old-still", ".jpg");
const copy = proxy(cache, "copy");
writeEntry(oldStill, 6, now - 3_000);
writeEntry(copy, 6, now - 1_000);
const result = cleanupProxyCache(cache, {
now,
maxBytes: 8,
maxIdleMs: 30 * 24 * 60 * 60 * 1000,
minSweepIntervalMs: 0,
});
expect(result.removed).toEqual([oldStill]);
expect(existsSync(oldStill)).toBe(false);
expect(existsSync(copy)).toBe(true);
});
it("reports the same removals in a dry run and removes nothing", () => {
const cache = cacheDir();
const now = 1_800_000_000_000;
const idle = proxy(cache, "idle");
const older = proxy(cache, "older");
const recent = proxy(cache, "recent");
writeEntry(idle, 4, now - 2 * 60 * 60 * 1000);
writeEntry(older, 3, now - 10_000);
writeEntry(recent, 6, now - 1_000);
const options = { now, maxBytes: 7, maxIdleMs: 60 * 60 * 1000, minSweepIntervalMs: 0 };
const planned = cleanupProxyCache(cache, { ...options, dryRun: true });
expect(existsSync(idle)).toBe(true);
expect(cleanupProxyCache(cache, options)).toEqual(planned);
expect(planned).toMatchObject({ removed: [idle, older], bytesBefore: 13, bytesAfter: 6 });
expect(existsSync(idle)).toBe(false);
expect(existsSync(recent)).toBe(true);
});
it("counts and evicts WebM proxies alongside MP4 proxies", () => {
const cache = cacheDir();
const now = 1_800_000_000_000;
const webm = proxy(cache, "alpha", ".webm");
const mp4 = proxy(cache, "opaque");
writeEntry(webm, 6, now - 2_000);
writeEntry(mp4, 6, now - 1_000);
const result = cleanupProxyCache(cache, {
now,
maxBytes: 6,
maxIdleMs: 10_000,
minSweepIntervalMs: 0,
});
expect(result.bytesBefore).toBe(12);
expect(result.removed).toEqual([webm]);
expect(result.bytesAfter).toBe(6);
});
it("preserves in-flight entries and removes stale temporary files", () => {
const cache = cacheDir();
const now = 1_800_000_000_000;
const inFlight = proxy(cache, "active");
const staleTemp = join(cache, `.tmp-${randomUUID()}-${basename(inFlight)}`);
writeEntry(inFlight, 12, now - 40 * 24 * 60 * 60 * 1000);
writeEntry(staleTemp, 3, now - 2 * 60 * 60 * 1000);
const result = cleanupProxyCache(cache, {
now,
maxBytes: 1,
maxIdleMs: 30 * 24 * 60 * 60 * 1000,
staleTempMs: 60 * 60 * 1000,
minSweepIntervalMs: 0,
protectedPaths: new Set([inFlight]),
});
expect(result.removed).toEqual([staleTemp]);
expect(result.bytesAfter).toBe(12);
expect(existsSync(inFlight)).toBe(true);
});
it("sweeps stale pieces of a stopped long copy, but not while its copy runs or within the hour", () => {
const cache = cacheDir();
const now = 1_800_000_000_000;
const hour = 60 * 60 * 1000;
const key = basename(proxy(cache, "long"), ".mp4");
const runningKey = basename(proxy(cache, "running"), ".mp4");
const stalePiece = join(cache, `.part-${key}-0.mp4`);
const stalePieceTemp = join(cache, `.tmp-${randomUUID()}-.part-${key}-1.mp4`);
const freshPiece = join(cache, `.part-${key}-2.mp4`);
const runningPiece = join(cache, `.part-${runningKey}-0.mp4`);
writeEntry(stalePiece, 3, now - 2 * hour);
writeEntry(stalePieceTemp, 3, now - 2 * hour);
writeEntry(freshPiece, 3, now - 11 * 60 * 1000);
writeEntry(runningPiece, 3, now - 2 * hour);
const result = cleanupProxyCache(cache, {
now,
staleTempMs: 10 * 60 * 1000,
minSweepIntervalMs: 0,
protectedPaths: new Set([join(cache, `${runningKey}.mp4`)]),
});
expect(result.removed.sort()).toEqual([stalePiece, stalePieceTemp].sort());
expect(existsSync(freshPiece)).toBe(true);
expect(existsSync(runningPiece)).toBe(true);
});
it("never removes a file the transcoder did not name, even from an idle cache over budget", () => {
const cache = cacheDir();
const now = 1_800_000_000_000;
const userFiles = [join(cache, "holiday.mp4"), join(cache, ".tmp-notes.mp4")];
for (const path of userFiles) writeEntry(path, 4, now - 40 * 24 * 60 * 60 * 1000);
const result = cleanupProxyCache(cache, {
now,
maxBytes: 0,
maxIdleMs: 1,
staleTempMs: 1,
minSweepIntervalMs: 0,
});
expect(result.removed).toEqual([]);
for (const path of userFiles) expect(existsSync(path)).toBe(true);
});
it("rate-limits repeated directory sweeps", () => {
const cache = cacheDir();
const path = proxy(cache, "entry");
writeEntry(path, 2, 1_000);
const first = cleanupProxyCache(cache, {
now: 2_000,
maxBytes: 10,
maxIdleMs: 10_000,
minSweepIntervalMs: 5_000,
});
const second = cleanupProxyCache(cache, {
now: 3_000,
maxBytes: 1,
maxIdleMs: 10_000,
minSweepIntervalMs: 5_000,
});
expect(first.skipped).toBe(false);
expect(second.skipped).toBe(true);
expect(existsSync(path)).toBe(true);
});
});