diff --git a/packages/cli/src/server/studioServer.test.ts b/packages/cli/src/server/studioServer.test.ts index 091843813f..6d08dad7a7 100644 --- a/packages/cli/src/server/studioServer.test.ts +++ b/packages/cli/src/server/studioServer.test.ts @@ -8,9 +8,11 @@ import { existsSync, mkdirSync, mkdtempSync, + readdirSync, readFileSync, renameSync, rmSync, + symlinkSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; @@ -48,6 +50,27 @@ vi.mock("@hyperframes/producer", () => ({ executeRenderJob: (...args: Parameters) => producerState.executeRenderJob(...args), })); +// Stand-ins for the HEIF and GIF steps: each writes a converted file where a real conversion would. +const imagePrep = vi.hoisted(() => ({ + convert: async (html: string, options: { outputDir: string }) => { + const { mkdirSync, writeFileSync } = await import("node:fs"); + mkdirSync(options.outputDir, { recursive: true }); + writeFileSync(`${options.outputDir}/converted`, "image"); + return { html, preparedAssets: new Map() }; + }, +})); +vi.mock("../../../producer/src/services/heifImagePrep.js", () => ({ + prepareHeifImageInputs: async (html: string, options: { outputDir: string }) => ({ + ...(await imagePrep.convert(html, options)), + preparedImages: [], + }), +})); +vi.mock("../../../producer/src/services/animatedGifPrep.js", () => ({ + prepareAnimatedGifInputs: async (html: string, options: { outputDir: string }) => ({ + ...(await imagePrep.convert(html, options)), + preparedGifs: [], + }), +})); const engineState = vi.hoisted(() => ({ acquireBrowser: async (..._args: unknown[]): Promise => { throw new Error("acquireBrowser called without a test double"); @@ -1201,3 +1224,43 @@ describe("createStudioServer Host check on every route", () => { } }); }); + +describe("createStudioServer preview image preparation", () => { + const html = '
'; + const prepared = (projectDir: string) => + ["heif", "gif"].filter((kind) => + existsSync(join(projectDir, ".hyperframes", "prepared-assets", kind, "converted")), + ); + + it("prepares HEIF and GIF images inside a project's own .hyperframes folder", async () => { + const projectDir = tmpProject(); + server = createStudioServer({ projectDir }); + + await server.adapter.transformPreviewHtml?.({ + html, + project: { id: "p", dir: projectDir }, + activeCompositionPath: "index.html", + }); + + expect(prepared(projectDir)).toEqual(["heif", "gif"]); + }); + + it.skipIf(process.platform === "win32")( + "serves images unprepared when .hyperframes is a link, and writes nothing where it points", + async () => { + const projectDir = tmpProject(); + const outside = tmpProject(); + symlinkSync(outside, join(projectDir, ".hyperframes")); + server = createStudioServer({ projectDir }); + + const served = await server.adapter.transformPreviewHtml?.({ + html, + project: { id: "p", dir: projectDir }, + activeCompositionPath: "index.html", + }); + + expect(served).toContain('src="photo.png"'); + expect(readdirSync(outside)).toEqual([]); + }, + ); +}); diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index b55446d66b..9389fe4acc 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -73,6 +73,7 @@ import { historyCache, } from "@hyperframes/studio-server"; import { encoderJobOptions } from "@hyperframes/studio-server/encoder-options"; +import { isLinkFreeWithinProject } from "@hyperframes/studio-server/safe-path"; import { resolveAutoProxy } from "../utils/projectConfig.js"; import { clearElementScreenshotIsolation, @@ -564,6 +565,9 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { const gifOutputDir = join(project.dir, ".hyperframes", "prepared-assets", "gif"); const gifDownloadDir = join(project.dir, ".hyperframes", "prepared-assets", "downloads"); const heifOutputDir = join(project.dir, ".hyperframes", "prepared-assets", "heif"); + const preparedDirs = [gifOutputDir, gifDownloadDir, heifOutputDir]; + if (!preparedDirs.every((dir) => isLinkFreeWithinProject(project.dir, dir))) + return injectDeterministicFontFaces(html); const { html: htmlWithJpegs } = await prepareHeifImageInputs(html, { projectDir: project.dir, downloadDir: gifDownloadDir, diff --git a/packages/core/src/safePath.networkRoot.test.ts b/packages/core/src/safePath.networkRoot.test.ts new file mode 100644 index 0000000000..54a3b5bae9 --- /dev/null +++ b/packages/core/src/safePath.networkRoot.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it, vi } from "vitest"; + +// Windows path rules on any host, and a disk that records every look at it. +const probes = vi.hoisted(() => [] as string[]); +vi.mock("node:path", async () => { + const path = await vi.importActual("node:path"); + return { ...path.win32, default: path.win32 }; +}); +vi.mock("node:fs", async () => { + const fs = await vi.importActual("node:fs"); + const missing = (path: string) => { + probes.push(path); + throw Object.assign(new Error(`ENOENT: ${path}`), { code: "ENOENT" }); + }; + const realpathSync = Object.assign(missing, { native: missing }); + const mocked = { ...fs, realpathSync, lstatSync: missing }; + return { ...mocked, default: mocked }; +}); + +const { resolveWithinProject } = await import("./safePath.js"); + +describe("a request path on another network root", () => { + it.each([ + "\\\\attacker.example\\share\\x.html", + "//attacker.example/share/x.html", + "\\\\?\\UNC\\attacker.example\\share\\x.html", + "\\\\.\\pipe\\x", + ])("is refused without touching the disk: %s", (path) => { + probes.length = 0; + expect(resolveWithinProject("C:\\project", path)).toBeNull(); + expect(probes).toEqual([]); + }); + + it("does not stop a project that itself lives on a share", () => { + probes.length = 0; + resolveWithinProject("\\\\nas\\films\\project", "scenes\\intro.html"); + expect(probes.length).toBeGreaterThan(0); + }); +}); diff --git a/packages/core/src/safePath.ts b/packages/core/src/safePath.ts index fea615e105..71820d3da6 100644 --- a/packages/core/src/safePath.ts +++ b/packages/core/src/safePath.ts @@ -1,4 +1,4 @@ -import { resolve, sep, join, dirname, basename, relative, isAbsolute } from "node:path"; +import { resolve, sep, join, dirname, basename, relative, isAbsolute, parse } from "node:path"; import { lstatSync, mkdirSync, realpathSync, statSync } from "node:fs"; export function realpath(path: string): string { @@ -27,6 +27,12 @@ function isMissingPath(path: string): boolean { } } +// On Windows even a realpath or lstat of a UNC or device path (\\host\share, \\?\, \\.\) connects to that host. +function namesAnotherNetworkRoot(base: string, target: string): boolean { + const root = parse(target).root; + return /^[\\/]{2}/.test(root) && root.toLowerCase() !== parse(base).root.toLowerCase(); +} + /** * Reject paths that escape the `base` directory — including via symlinks. * @@ -50,6 +56,8 @@ function isMissingPath(path: string): boolean { * from there without a backwards edge. */ export function isSafePath(base: string, resolved: string): boolean { + const target = resolve(resolved); + if (namesAnotherNetworkRoot(resolve(base), target)) return false; let baseReal: string; try { baseReal = realpath(resolve(base)); @@ -58,7 +66,6 @@ export function isSafePath(base: string, resolved: string): boolean { return false; } - const target = resolve(resolved); const trailing: string[] = []; let probe = target; diff --git a/packages/inspector/src/pageServer.ts b/packages/inspector/src/pageServer.ts index 410fa9b1db..bb0b5b736c 100644 --- a/packages/inspector/src/pageServer.ts +++ b/packages/inspector/src/pageServer.ts @@ -6,3 +6,12 @@ export function closeServer(server: Server): Promise { server.removeAllListeners("request"); return closed; } + +export function isLoopbackHost(host: string | undefined): boolean { + if (!host) return true; + const name = host + .replace(/:\d+$/, "") + .replace(/^\[|\]$/g, "") + .toLowerCase(); + return name === "127.0.0.1" || name === "localhost" || name === "::1"; +} diff --git a/packages/inspector/src/server.ts b/packages/inspector/src/server.ts index 215f183596..c1642ccd0c 100644 --- a/packages/inspector/src/server.ts +++ b/packages/inspector/src/server.ts @@ -1,6 +1,7 @@ import { createServer, type IncomingMessage, type ServerResponse } from "node:http"; import { readFileSync, realpathSync } from "node:fs"; import { pathToFileURL } from "node:url"; +import { isLoopbackHost } from "./pageServer"; import { Inspector } from "./service"; import type { InspectRequest } from "./types"; @@ -28,15 +29,6 @@ function failureOf(error: unknown) { return { status: message === "no-composition" ? 404 : 400, message }; } -function loopback(host: string | undefined) { - if (!host) return true; - const name = host - .replace(/:\d+$/, "") - .replace(/^\[|\]$/g, "") - .toLowerCase(); - return name === "127.0.0.1" || name === "localhost" || name === "::1"; -} - function readBody(req: IncomingMessage): Promise { return new Promise((resolve, reject) => { const chunks: Buffer[] = []; @@ -73,7 +65,7 @@ export function createInspectorServer(project: string) { const inspector = new Inspector(project); const server = createServer(async (req, res) => { res.setHeader("Content-Type", "application/json"); - if (!loopback(req.headers.host)) { + if (!isLoopbackHost(req.headers.host)) { send(res, 403, "host_not_allowed"); return; } diff --git a/packages/inspector/src/service.ts b/packages/inspector/src/service.ts index 8c96383fff..bfb4fd4860 100644 --- a/packages/inspector/src/service.ts +++ b/packages/inspector/src/service.ts @@ -28,7 +28,7 @@ import { bundleToSingleHtml, type BundleScriptProvenance, } from "../../core/src/compiler/htmlBundler"; -import { closeServer } from "./pageServer"; +import { closeServer, isLoopbackHost } from "./pageServer"; import { Sources, fingerprint } from "./sources"; import type { InspectRequest, Snippet } from "./types"; import { hyperframesCacheDir } from "../../core/src/cacheDir"; @@ -121,6 +121,7 @@ export class Inspector { if (fingerprint(this.project) !== digest) throw new Error("source_changed_during_load: retry against a stable project"); const server = createServer((req, res) => { + if (!isLoopbackHost(req.headers.host)) return void res.writeHead(403).end(); try { const path = decodeURIComponent(new URL(req.url ?? "/", "http://localhost").pathname); if (path === "/" || path === "/index.html") { diff --git a/packages/inspector/tests/previewHost.test.ts b/packages/inspector/tests/previewHost.test.ts new file mode 100644 index 0000000000..8f5f335057 --- /dev/null +++ b/packages/inspector/tests/previewHost.test.ts @@ -0,0 +1,65 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { request } from "node:http"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { expect, it, vi } from "vitest"; +import { Inspector } from "../src/service"; + +const pages = vi.hoisted(() => [] as string[]); + +vi.mock("puppeteer-core", () => ({ + default: { + launch: async () => ({ + close: async () => {}, + newPage: async () => ({ + setViewport: async () => {}, + goto: async (url: string) => { + pages.push(url); + await fetch(url); + }, + waitForFunction: async () => {}, + evaluate: async () => {}, + content: async () => "", + close: async () => {}, + }), + }), + }, +})); + +function statusFor(url: string, host: string): Promise { + return new Promise((resolve, reject) => { + request(url, { headers: { host } }, (res) => { + res.resume(); + resolve(res.statusCode ?? 0); + }) + .on("error", reject) + .end(); + }); +} + +it("serves the preview page to its own browser and refuses a foreign Host", async () => { + const project = mkdtempSync(join(tmpdir(), "inspect-preview-host-")); + writeFileSync( + join(project, "index.html"), + '
', + ); + writeFileSync(join(project, "notes.txt"), "project file"); + const inspector = new Inspector(project, { + executablePath: "browser-stand-in", + instrument: false, + }); + try { + await inspector.withPage(async () => {}); + const origin = new URL(pages.at(-1)!).origin; + const port = new URL(origin).port; + + expect(await statusFor(`${origin}/`, `127.0.0.1:${port}`)).toBe(200); + expect(await statusFor(`${origin}/notes.txt`, `localhost:${port}`)).toBe(200); + for (const path of ["/", "/__hf-runtime.js", "/notes.txt"]) { + expect(await statusFor(`${origin}${path}`, `attacker.example:${port}`)).toBe(403); + } + } finally { + await inspector.dispose(); + rmSync(project, { recursive: true, force: true }); + } +}); diff --git a/packages/studio-server/src/createStudioApi.ts b/packages/studio-server/src/createStudioApi.ts index 06cec729b7..b8ae0b71ac 100644 --- a/packages/studio-server/src/createStudioApi.ts +++ b/packages/studio-server/src/createStudioApi.ts @@ -38,13 +38,21 @@ async function answerRefusedFrameWithPage(c: Context, next: Next) { }); } +const fromAnotherSite = (c: Context) => { + const site = c.req.header("sec-fetch-site"); + return site === "cross-site" || site === "same-site"; +}; + async function sandboxPreviewOpenedFromAnotherSite(c: Context, next: Next) { await next(); c.header("Vary", "Sec-Fetch-Site", { append: true }); - const site = c.req.header("sec-fetch-site"); - if (site === "cross-site" || site === "same-site") { - c.header("Content-Security-Policy", "sandbox allow-scripts"); - } + if (fromAnotherSite(c)) c.header("Content-Security-Policy", "sandbox allow-scripts"); +} + +async function refuseThumbnailForAnotherSite(c: Context, next: Next) { + if (fromAnotherSite(c)) + return c.json({ error: "forbidden: thumbnail requested by another site" }, 403); + return next(); } function urlHost(url: string): string | undefined { @@ -70,6 +78,7 @@ export interface StudioApiOptions { export function createStudioApi(adapter: StudioApiAdapter, options: StudioApiOptions = {}): Hono { const api = new Hono(); api.use("/projects/:id/preview/*", sandboxPreviewOpenedFromAnotherSite); + api.use("/projects/:id/thumbnail/*", refuseThumbnailForAnotherSite); // This API is unauthenticated and simple cross-origin POSTs need no // preflight, so the guard rejects writes the shared predicate refuses. api.use(async function rejectCrossOriginWrites(c, next) { diff --git a/packages/studio-server/src/helpers/backupJournal.ts b/packages/studio-server/src/helpers/backupJournal.ts index 417256fd08..5199eba3e3 100644 --- a/packages/studio-server/src/helpers/backupJournal.ts +++ b/packages/studio-server/src/helpers/backupJournal.ts @@ -2,7 +2,12 @@ import { readdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs"; import { Buffer } from "node:buffer"; import { createHash } from "node:crypto"; import { join, relative } from "node:path"; -import { isProjectRootMissing, isSafePath, mkdirWithinProject } from "./safePath.js"; +import { + isLinkFreeWithinProject, + isProjectRootMissing, + isSafePath, + mkdirWithinProject, +} from "./safePath.js"; const DEFAULT_KEEP_PER_FILE = 10; @@ -37,6 +42,7 @@ export function snapshotBeforeWrite( const relativePath = relative(projectDir, absPath); const backupDir = join(projectDir, ".hyperframes", "backup"); + if (!isLinkFreeWithinProject(projectDir, backupDir)) return { backupPath: null }; mkdirWithinProject(projectDir, backupDir); const pathKey = Buffer.from(relativePath, "utf-8").toString("base64url"); @@ -45,7 +51,7 @@ export function snapshotBeforeWrite( ? pathKey : `sha256~${createHash("sha256").update(relativePath).digest("hex")}`; const backupPath = nextBackupPath(backupDir, backupKey); - writeFileSync(backupPath, content); + writeFileSync(backupPath, content, { flag: "wx" }); pruneBackups(backupDir, [pathKey, backupKey], options.keepPerFile ?? DEFAULT_KEEP_PER_FILE); return { backupPath }; } catch (error) { diff --git a/packages/studio-server/src/helpers/hfIdPersist.ts b/packages/studio-server/src/helpers/hfIdPersist.ts index 9b0cd03ac2..6c80e32078 100644 --- a/packages/studio-server/src/helpers/hfIdPersist.ts +++ b/packages/studio-server/src/helpers/hfIdPersist.ts @@ -1,9 +1,9 @@ import { stampHfIdsInPlace } from "@hyperframes/parsers/hf-ids"; import { createHash } from "node:crypto"; -import { closeSync, constants, fstatSync, openSync, readFileSync, writeFileSync } from "node:fs"; +import { closeSync, constants, fstatSync, openSync, readFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; -import { isInHiddenOrVendorDir, mkdirWithinProject, walkDir } from "./safePath.js"; +import { isInHiddenOrVendorDir, mkdirOwnedWithinProject, walkDir } from "./safePath.js"; export const isCompositionSource = (html: string): boolean => /data-composition-id\s*=/.test(html); @@ -48,8 +48,8 @@ function readStampRecord(recordPath: string): Record { function writeStampRecord(projectDir: string, recordPath: string, next: Record) { try { - mkdirWithinProject(projectDir, dirname(recordPath)); - writeFileSync(recordPath, JSON.stringify(next)); + mkdirOwnedWithinProject(projectDir, dirname(recordPath)); + replaceFileAtomically(recordPath, JSON.stringify(next)); } catch { // read-only project: the next start parses again } diff --git a/packages/studio-server/src/helpers/ownedFolders.test.ts b/packages/studio-server/src/helpers/ownedFolders.test.ts new file mode 100644 index 0000000000..a21b8438cb --- /dev/null +++ b/packages/studio-server/src/helpers/ownedFolders.test.ts @@ -0,0 +1,135 @@ +import { + existsSync, + mkdirSync, + mkdtempSync, + readdirSync, + readFileSync, + rmSync, + symlinkSync, + utimesSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { snapshotBeforeWrite } from "./backupJournal"; +import { stampProjectHfIds } from "./hfIdPersist"; +import { createPreviewDocumentStore } from "./previewDocumentStore"; +import { isLinkFreeWithinProject, mkdirOwnedWithinProject } from "./safePath"; +import { projectHistoryId } from "../history/historyId"; +import { pruneThumbnailCache } from "../routes/thumbnail"; + +const dirs: string[] = []; +afterEach(() => { + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); +}); + +function tempDir(): string { + const dir = mkdtempSync(join(tmpdir(), "hf-owned-folders-")); + dirs.push(dir); + return dir; +} + +/** A project and, beside it, a folder of the person's own files that a link in the project points at. */ +function projectWithOutsideFolder() { + const root = tempDir(); + const project = join(root, "project"); + const outside = join(root, "outside"); + mkdirSync(project); + mkdirSync(outside); + writeFileSync(join(outside, "notes.txt"), "keep me"); + writeFileSync(join(project, "index.html"), '

Hi

'); + return { project, outside }; +} + +const listing = (dir: string) => readdirSync(dir).sort(); + +// Windows needs a privilege to create symlinks. +describe.skipIf(process.platform === "win32")("the tool's own folders inside a project", () => { + it("are made as real folders when nothing is there yet", () => { + const project = tempDir(); + mkdirOwnedWithinProject(project, join(project, ".hyperframes", "preview")); + expect(isLinkFreeWithinProject(project, join(project, ".hyperframes", "preview"))).toBe(true); + }); + + it("are refused when a folder on the way is a link, and nothing is made at its target", () => { + const { project, outside } = projectWithOutsideFolder(); + symlinkSync(outside, join(project, ".hyperframes")); + + expect(() => + mkdirOwnedWithinProject(project, join(project, ".hyperframes", "backup")), + ).toThrow(); + expect(listing(outside)).toEqual(["notes.txt"]); + }); + + it("still work when the project folder itself is reached through a link", () => { + const { project } = projectWithOutsideFolder(); + const linkedProject = join(tempDir(), "linked"); + symlinkSync(project, linkedProject); + + mkdirOwnedWithinProject(linkedProject, join(linkedProject, ".thumbnails")); + expect(existsSync(join(project, ".thumbnails"))).toBe(true); + }); + + it("keep the hf-id stamp record from writing through a linked record file", () => { + const { project, outside } = projectWithOutsideFolder(); + mkdirSync(join(project, ".hyperframes")); + symlinkSync(join(outside, "notes.txt"), join(project, ".hyperframes", "hf-ids-stamped.json")); + + stampProjectHfIds(project); + + expect(readFileSync(join(outside, "notes.txt"), "utf-8")).toBe("keep me"); + }); + + it("keep the hf-id stamp record out of a linked .hyperframes folder", () => { + const { project, outside } = projectWithOutsideFolder(); + symlinkSync(outside, join(project, ".hyperframes")); + + stampProjectHfIds(project); + + expect(listing(outside)).toEqual(["notes.txt"]); + }); + + it("keep a new history id from creating the target of a dangling link", () => { + const { project, outside } = projectWithOutsideFolder(); + mkdirSync(join(project, ".hyperframes")); + symlinkSync(join(outside, "planted"), join(project, ".hyperframes", "history-id")); + + const id = projectHistoryId(project, tempDir()); + + expect(listing(outside)).toEqual(["notes.txt"]); + expect(readFileSync(join(project, ".hyperframes", "history-id"), "utf-8")).toBe(`${id}\n`); + }); + + it("keep edit backups out of a linked backup folder", () => { + const { project, outside } = projectWithOutsideFolder(); + mkdirSync(join(project, ".hyperframes")); + symlinkSync(outside, join(project, ".hyperframes", "backup")); + + const result = snapshotBeforeWrite(project, join(project, "index.html")); + + expect(result.backupPath).toBeNull(); + expect(listing(outside)).toEqual(["notes.txt"]); + }); + + it("keep built preview documents from writing into or emptying a linked preview folder", () => { + const { project, outside } = projectWithOutsideFolder(); + mkdirSync(join(project, ".hyperframes")); + symlinkSync(outside, join(project, ".hyperframes", "preview")); + + createPreviewDocumentStore(project, "salt").write("key", ""); + + expect(listing(outside)).toEqual(["notes.txt"]); + }); + + it("keep the thumbnail cache sweep out of a linked .thumbnails folder", () => { + const { project, outside } = projectWithOutsideFolder(); + const old = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000); + utimesSync(join(outside, "notes.txt"), old, old); + symlinkSync(outside, join(project, ".thumbnails")); + + pruneThumbnailCache(join(project, ".thumbnails"), new Set()); + + expect(listing(outside)).toEqual(["notes.txt"]); + }); +}); diff --git a/packages/studio-server/src/helpers/previewDocumentStore.ts b/packages/studio-server/src/helpers/previewDocumentStore.ts index 036821f9a1..2ada04e5bc 100644 --- a/packages/studio-server/src/helpers/previewDocumentStore.ts +++ b/packages/studio-server/src/helpers/previewDocumentStore.ts @@ -2,7 +2,7 @@ import { createHash } from "node:crypto"; import { readFileSync, readdirSync, rmSync } from "node:fs"; import { join } from "node:path"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; -import { mkdirWithinProject } from "./safePath.js"; +import { mkdirOwnedWithinProject } from "./safePath.js"; import type { PreviewDocumentStore } from "./mediaProxyPreview.js"; /** One built preview document under the project's `.hyperframes/preview`. `salt` names the server build, so new @@ -21,7 +21,7 @@ export function createPreviewDocumentStore(projectDir: string, salt: string): Pr }, write(key, html) { try { - mkdirWithinProject(projectDir, dir); + mkdirOwnedWithinProject(projectDir, dir); const file = fileFor(key); replaceFileAtomically(file, html, 0o644); for (const name of readdirSync(dir)) { diff --git a/packages/studio-server/src/helpers/proxyCache.test.ts b/packages/studio-server/src/helpers/proxyCache.test.ts index 32b8fce9ed..e42d3cdf02 100644 --- a/packages/studio-server/src/helpers/proxyCache.test.ts +++ b/packages/studio-server/src/helpers/proxyCache.test.ts @@ -1,5 +1,13 @@ import { createHash, randomUUID } from "node:crypto"; -import { existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; +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"; @@ -30,6 +38,29 @@ afterEach(() => { }); 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; diff --git a/packages/studio-server/src/helpers/proxyCache.ts b/packages/studio-server/src/helpers/proxyCache.ts index dcbbb52305..6adaede311 100644 --- a/packages/studio-server/src/helpers/proxyCache.ts +++ b/packages/studio-server/src/helpers/proxyCache.ts @@ -1,6 +1,7 @@ import { existsSync, readdirSync, statSync, unlinkSync } from "node:fs"; -import { extname, join } from "node:path"; +import { dirname, extname, join } from "node:path"; import { PROXY_STILL_EXTENSION, PROXY_VARIANT_CONFIG } from "./mediaCodecMap.js"; +import { isLinkFreeWithinProject } from "./safePath.js"; const DEFAULT_MAX_BYTES = 10 * 1024 * 1024 * 1024; const DEFAULT_STALE_TEMP_MS = 60 * 60 * 1000; @@ -167,7 +168,7 @@ export function cleanupProxyCache( if (shouldSkipSweep(cacheDir, now, minSweepIntervalMs)) { return { removed: [], bytesBefore: 0, bytesAfter: 0, skipped: true }; } - if (!existsSync(cacheDir)) { + if (!existsSync(cacheDir) || !isLinkFreeWithinProject(dirname(cacheDir), cacheDir)) { return { removed: [], bytesBefore: 0, bytesAfter: 0, skipped: false }; } diff --git a/packages/studio-server/src/helpers/proxyTranscoder.test.ts b/packages/studio-server/src/helpers/proxyTranscoder.test.ts index c72571bf3d..6cf6698165 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.test.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.test.ts @@ -1079,6 +1079,34 @@ describe("resolveProxy", () => { expect(existsSync(live)).toBe(true); }); + it.skipIf(process.platform === "win32")( + "still makes a copy in a .transcode-cache that links outside the project, but never sweeps it", + async () => { + const { spawn, calls } = createSpawnSpy(); + const { resolveProxies } = await loadModule(spawn, FFMPEG_PATH); + const projectDir = tmpProject(); + const sourcePath = join(projectDir, "clip.mov"); + writeFileSync(sourcePath, "source"); + const outside = mkdtempSync(join(tmpdir(), "hf-proxy-outside-")); + const stale = join( + outside, + `.tmp-11111111-1111-1111-1111-111111111111-${"a".repeat(64)}.mp4`, + ); + writeFileSync(stale, "someone else's file"); + const longAgo = new Date(Date.now() - 60 * 60_000); + utimesSync(stale, longAgo, longAgo); + symlinkSync(outside, join(projectDir, ".transcode-cache")); + + const batch = resolveProxies(projectDir, [{ sourcePath, variant: "h264" }]); + await answerUntilIdle(calls, (call) => succeed(call)); + const [result] = await batch; + + expect(result?.status).toBe("fulfilled"); + expect(existsSync(stale)).toBe(true); + rmSync(outside, { recursive: true, force: true }); + }, + ); + it("settles each request of a batch on its own when one of them fails", async () => { const { spawn, calls } = createSpawnSpy(); const { resolveProxies } = await loadModule(spawn, FFMPEG_PATH); diff --git a/packages/studio-server/src/helpers/safePath.ts b/packages/studio-server/src/helpers/safePath.ts index cb7221945a..82be9b11dd 100644 --- a/packages/studio-server/src/helpers/safePath.ts +++ b/packages/studio-server/src/helpers/safePath.ts @@ -1,6 +1,10 @@ -import { basename, dirname, isAbsolute, join, relative, sep } from "node:path"; -import { readdirSync, type Dirent } from "node:fs"; -import { realpath, resolveWithinProject as resolveInProject } from "@hyperframes/core/safe-path"; +import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; +import { lstatSync, readdirSync, type Dirent } from "node:fs"; +import { + mkdirWithinProject, + realpath, + resolveWithinProject as resolveInProject, +} from "@hyperframes/core/safe-path"; import { RENDER_TEMP_DIR } from "@hyperframes/core/project-folders"; import { STUDIO_MANUAL_EDITS_PATH } from "./manualEditsRenderScript.js"; import { STUDIO_MOTION_PATH } from "./studioMotionRenderScript.js"; @@ -23,6 +27,11 @@ export function resolveWithinProject(base: string, relativePath: string): string return resolved && !isPrivateProjectFile(base, resolved) ? resolved : null; } +/** A path inside the project spelled one way: relative to `base`, `/`-separated, no `.` or `..` segments. */ +export function projectRelativePath(base: string, file: string): string { + return relative(resolve(base), resolve(file)).split(sep).join("/"); +} + /** The real path; for a path not there (yet, or any more), the nearest existing folder's real path plus the rest. */ export function realFilePath(filePath: string): string { try { @@ -75,6 +84,26 @@ function pinResolved(base: string, checked: string | null): string | null { return escapes ? null : join(base, inside); } +/** True when no entry from `root` down to `dir` is a link. A file in the way is left for mkdir or readdir to report. */ +export function isLinkFreeWithinProject(root: string, dir: string): boolean { + const inside = relative(resolve(root), resolve(dir)); + if (inside === ".." || inside.startsWith(`..${sep}`) || isAbsolute(inside)) return false; + let path = resolve(root); + for (const part of inside.split(sep).filter(Boolean)) { + path = join(path, part); + const stat = lstatSync(path, { throwIfNoEntry: false }); + if (stat?.isSymbolicLink()) return false; + if (!stat?.isDirectory()) return true; + } + return true; +} + +/** `mkdirWithinProject` for the tool's own caches and records: a link on the way would put their files outside the project. */ +export function mkdirOwnedWithinProject(root: string, dir: string): void { + if (!isLinkFreeWithinProject(root, dir)) throw new Error(`A link stands in for a folder: ${dir}`); + mkdirWithinProject(root, dir); +} + const IGNORE_DIRS = new Set([ ".thumbnails", ".transcode-cache", diff --git a/packages/studio-server/src/helpers/subComposition.test.ts b/packages/studio-server/src/helpers/subComposition.test.ts index 5866bd5c2d..fab5ef96a1 100644 --- a/packages/studio-server/src/helpers/subComposition.test.ts +++ b/packages/studio-server/src/helpers/subComposition.test.ts @@ -20,6 +20,23 @@ function makeTempProject(files: Record): string { } describe("buildSubCompositionHtml", () => { + // Windows does not allow a quote in a file name. + it.skipIf(process.platform === "win32")( + "writes the composition's path into its root as attribute text", + () => { + const name = 'a"b&c.html'; + const dir = makeTempProject({ + "index.html": "", + [name]: '
', + }); + const html = buildSubCompositionHtml(dir, name, "/runtime.js") ?? ""; + const { document } = parseHTML(html); + const root = document.querySelector("[data-composition-id]"); + expect(root?.getAttribute("data-composition-file")).toBe(name); + expect(root?.getAttributeNames()).not.toContain('b&c.html"'); + }, + ); + it("returns null for a composition path that resolves outside the project", () => { const outside = makeTempProject({ "secret.txt": "outside-secret" }); const dir = makeTempProject({ diff --git a/packages/studio-server/src/helpers/subComposition.ts b/packages/studio-server/src/helpers/subComposition.ts index 5854617935..8d2e3f73ef 100644 --- a/packages/studio-server/src/helpers/subComposition.ts +++ b/packages/studio-server/src/helpers/subComposition.ts @@ -240,7 +240,9 @@ function tagRootCompositionFile(bodyHtml: string, compPath: string): string { if (tagEnd === -1) return bodyHtml; if (bodyHtml.slice(match.index, tagEnd).includes("data-composition-file")) return bodyHtml; return ( - bodyHtml.slice(0, tagEnd) + ` data-composition-file="${compPath}"` + bodyHtml.slice(tagEnd) + bodyHtml.slice(0, tagEnd) + + ` data-composition-file="${escapeAttrValue(compPath)}"` + + bodyHtml.slice(tagEnd) ); } diff --git a/packages/studio-server/src/history/historyId.ts b/packages/studio-server/src/history/historyId.ts index db23f7d956..84c8cbb2af 100644 --- a/packages/studio-server/src/history/historyId.ts +++ b/packages/studio-server/src/history/historyId.ts @@ -1,8 +1,8 @@ import { randomUUID } from "node:crypto"; -import { existsSync, mkdirSync, readFileSync, statSync, utimesSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readFileSync, statSync, utimesSync } from "node:fs"; import { basename, join, resolve } from "node:path"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; -import { mkdirWithinProject } from "../helpers/safePath.js"; +import { mkdirOwnedWithinProject } from "../helpers/safePath.js"; export const ID_PATH = join(".hyperframes", "history-id"); /** The only shape minted here; the id is project content and becomes a path, so nothing else is trusted. */ @@ -70,8 +70,8 @@ export function projectHistoryId(projectDir: string, historyRoot: string): strin (existsSync(join(historyRoot, id)) && !isRecordedFolder(join(historyRoot, id), folder)) ) { id = randomUUID(); - mkdirWithinProject(dir, join(dir, ".hyperframes")); - writeFileSync(join(dir, ID_PATH), `${id}\n`); + mkdirOwnedWithinProject(dir, join(dir, ".hyperframes")); + replaceFileAtomically(join(dir, ID_PATH), `${id}\n`); } recordProject(join(historyRoot, id), dir, folder); return id; diff --git a/packages/studio-server/src/routes/files.test.ts b/packages/studio-server/src/routes/files.test.ts index 2a1d7b1bb7..bef56783a5 100644 --- a/packages/studio-server/src/routes/files.test.ts +++ b/packages/studio-server/src/routes/files.test.ts @@ -1852,6 +1852,30 @@ const tl = gsap.timeline({ paused: true }); expect(readFileSync(join(projectDir, "comp.html"), "utf8")).toBe(FROMTO_COMP); }); + it("keeps a saved ease that holds a script end tag inside the script", async () => { + const projectDir = createProjectDir(); + writeHtml(projectDir, "comp.html", FROMTO_COMP); + const app = new Hono(); + registerFileRoutes(app, createAdapter(projectDir)); + const animation = await getFirstAnimation(app, "comp.html"); + const scriptsBefore = parseHTML(FROMTO_COMP).document.querySelectorAll("script").length; + + const response = await app.request("http://localhost/projects/demo/gsap-mutations/comp.html", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + type: "update-meta", + animationId: animation.id, + updates: { ease: "none" }, + }), + }); + + expect(response.status).toBe(200); + const saved = readFileSync(join(projectDir, "comp.html"), "utf8"); + expect(saved).not.toContain("