From 603395c89a680403f370be816331f1507dc9bd5c Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sat, 14 Nov 2026 23:36:46 +0000 Subject: [PATCH] fix: render file servers refuse env and Git files; shown-box sampling stays within the render (#7765) * fix(producer): shown-box sampling stays within the validated render duration Sampling where each oversized video is drawn stepped through the authored start and end in 0.25 s float steps with no bound, before the render duration was checked. An enormous start never advanced the step, and an unbounded end grew the sample set until memory ran out. The render duration is now validated first, each window is cut at the composition's end, a window that is not finite is skipped, and the loop counts integer steps. Building the sample list happens inside the measurement's try, so any failure falls back to full-size extraction. Co-Authored-By: Claude Opus 5.5 * fix: render and capture file servers refuse env files and Git data The producer render file server, the engine file server and the CLI static project server served every regular file under the project to the page, including `.env*` files and anything under `.git`. Compile already refuses to inline those files, and Studio preview and play refuse to serve them. All three now answer 404 for them through the shared isVcsOrSecretPath rule, checked on the requested name and on a link's target, with the case and Windows spelling variants it already covers. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../cli/src/utils/staticProjectServer.test.ts | 20 +++++++++++++ packages/cli/src/utils/staticProjectServer.ts | 2 ++ .../services/fileServer.pathSafety.test.ts | 24 +++++++++++++++ packages/engine/src/services/fileServer.ts | 3 ++ .../producer/src/services/fileServer.test.ts | 30 +++++++++++++++++++ packages/producer/src/services/fileServer.ts | 2 ++ .../services/render/videoShownBoxes.test.ts | 20 +++++++++++-- .../src/services/render/videoShownBoxes.ts | 15 +++++++--- .../src/services/renderOrchestrator.ts | 16 ++++++---- 9 files changed, 121 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/utils/staticProjectServer.test.ts b/packages/cli/src/utils/staticProjectServer.test.ts index e214bad55b..e4e92eaad5 100644 --- a/packages/cli/src/utils/staticProjectServer.test.ts +++ b/packages/cli/src/utils/staticProjectServer.test.ts @@ -690,3 +690,23 @@ describe("serveStaticProjectHtml when a sandbox refuses listen()", () => { }); }); }); + +describe("serveStaticProjectHtml private project files", () => { + it.each([".env", ".env.local", ".git/config", "%2egit/config"])( + "answers %s with 404", + async (requestPath) => { + dir = mkdtempSync(join(tmpdir(), "hf-static-private-")); + mkdirSync(join(dir, ".git")); + writeFileSync(join(dir, ".env"), "SECRET=1"); + writeFileSync(join(dir, ".env.local"), "SECRET=1"); + writeFileSync(join(dir, ".git", "config"), "SECRET=1"); + writeFileSync(join(dir, "asset.txt"), "ok"); + server = await serveStaticProjectHtml(dir, ""); + + const res = await fetch(`${server.url}${requestPath}`); + expect(res.status).toBe(404); + expect(await res.text()).not.toContain("SECRET"); + expect((await fetch(`${server.url}asset.txt`)).status).toBe(200); + }, + ); +}); diff --git a/packages/cli/src/utils/staticProjectServer.ts b/packages/cli/src/utils/staticProjectServer.ts index dcedf2c0c8..177c09ca4a 100644 --- a/packages/cli/src/utils/staticProjectServer.ts +++ b/packages/cli/src/utils/staticProjectServer.ts @@ -2,6 +2,7 @@ import { createServer, request as httpRequest, type Server, type ServerResponse import { Duplex } from "node:stream"; import { createReadStream, existsSync, readFileSync, statSync } from "node:fs"; import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags"; +import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path"; import { isAbsolute, relative, resolve } from "node:path"; import { PREVIEW_PROXY_BOX_PARAM, @@ -330,6 +331,7 @@ export async function serveStaticProjectHtml( const filePath = resolve(root, requestPath); const rel = relative(root, filePath); if (rel.startsWith("..") || isAbsolute(rel)) continue; // traversal guard; try next root + if (isPrivateFileUnder(root, filePath, isVcsOrSecretPath)) continue; if (existsSync(filePath)) { if (proxyRequest) { void serveProxyRequest( diff --git a/packages/engine/src/services/fileServer.pathSafety.test.ts b/packages/engine/src/services/fileServer.pathSafety.test.ts index 4df08092d3..a0d18ac666 100644 --- a/packages/engine/src/services/fileServer.pathSafety.test.ts +++ b/packages/engine/src/services/fileServer.pathSafety.test.ts @@ -61,3 +61,27 @@ describe("fileServer path containment", () => { expect(await response.text()).toContain(""); }); }); + +describe("fileServer private project files", () => { + it.each(["/.env", "/.Env.local", "/.git/config", "/%2egit/config"])( + "answers %s with 404", + async (requestPath) => { + const projectDir = fs.mkdtempSync(path.join(tmpdir(), "hf-fileserver-private-")); + fs.mkdirSync(path.join(projectDir, ".git")); + fs.writeFileSync(path.join(projectDir, ".env"), "SECRET=1"); + fs.writeFileSync(path.join(projectDir, ".Env.local"), "SECRET=1"); + fs.writeFileSync(path.join(projectDir, ".git", "config"), "SECRET=1"); + fs.writeFileSync(path.join(projectDir, "asset.txt"), "ok"); + const server = await createFileServer({ headScripts: [], bodyScripts: [], projectDir }); + try { + const response = await fetch(`${server.url}${requestPath}`); + expect(response.status).toBe(404); + expect(await response.text()).not.toContain("SECRET"); + expect((await fetch(`${server.url}/asset.txt`)).status).toBe(200); + } finally { + server.close(); + fs.rmSync(projectDir, { recursive: true, force: true }); + } + }, + ); +}); diff --git a/packages/engine/src/services/fileServer.ts b/packages/engine/src/services/fileServer.ts index 5ac4149080..5bbfe89568 100644 --- a/packages/engine/src/services/fileServer.ts +++ b/packages/engine/src/services/fileServer.ts @@ -11,6 +11,7 @@ import { serve } from "@hono/node-server"; import { readFileSync, openSync, fstatSync, closeSync, statSync, constants } from "node:fs"; import { join, extname } from "node:path"; import { injectScriptsIntoHtml } from "@hyperframes/core/compiler"; +import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path"; import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution"; const MIME_TYPES: Record = { @@ -98,9 +99,11 @@ function resolveContainedPaths( ): { projectPath: string; compiledPath: string | null } | null { const projectPath = join(projectDir, relativePath); if (!isWithinProjectRoot(projectDir, projectPath)) return null; + if (isPrivateFileUnder(projectDir, projectPath, isVcsOrSecretPath)) return null; if (!compiledDir) return { projectPath, compiledPath: null }; const compiledPath = join(compiledDir, relativePath); if (!isWithinProjectRoot(compiledDir, compiledPath)) return null; + if (isPrivateFileUnder(compiledDir, compiledPath, isVcsOrSecretPath)) return null; return { projectPath, compiledPath }; } diff --git a/packages/producer/src/services/fileServer.test.ts b/packages/producer/src/services/fileServer.test.ts index f6166109bd..af9d3a9fc0 100644 --- a/packages/producer/src/services/fileServer.test.ts +++ b/packages/producer/src/services/fileServer.test.ts @@ -1133,3 +1133,33 @@ describe("createFileServer colour-tagged PNGs", () => { } }); }); + +describe("createFileServer private project files", () => { + it.each(["/.env", "/.ENV.local", "/.env.production", "/.git/config", "/%2egit/HEAD", "/.envrc"])( + "answers %s with 404 and keeps ordinary assets", + async (requestPath) => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-fs-private-")); + try { + writeEmptyIndex(projectDir); + mkdirSync(join(projectDir, ".git")); + for (const name of [".env", ".ENV.local", ".env.production", ".envrc"]) { + writeFileSync(join(projectDir, name), "SECRET=1"); + } + writeFileSync(join(projectDir, ".git", "config"), "SECRET=1"); + writeFileSync(join(projectDir, ".git", "HEAD"), "SECRET=1"); + writeFileSync(join(projectDir, "asset.txt"), "ok"); + symlinkSync(join(projectDir, ".env"), join(projectDir, "linked.txt")); + await withFileServer(projectDir, async (server) => { + const response = await fetch(`${server.url}${requestPath}`); + expect(response.status).toBe(404); + expect(await response.text()).not.toContain("SECRET"); + const linked = await fetch(`${server.url}/linked.txt`); + expect(linked.status).toBe(404); + expect((await fetch(`${server.url}/asset.txt`)).status).toBe(200); + }); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }, + ); +}); diff --git a/packages/producer/src/services/fileServer.ts b/packages/producer/src/services/fileServer.ts index 5ddeabbbdf..443a3439d3 100644 --- a/packages/producer/src/services/fileServer.ts +++ b/packages/producer/src/services/fileServer.ts @@ -29,6 +29,7 @@ import { Readable } from "node:stream"; import { join, extname, resolve, sep } from "node:path"; import { injectScriptsAtHeadStart, injectScriptsIntoHtml } from "@hyperframes/core/compiler"; import { fpsToNumber, type Fps } from "@hyperframes/core"; +import { isPrivateFileUnder, isVcsOrSecretPath } from "@hyperframes/core/safe-path"; import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags"; import { openRangeEnd } from "@hyperframes/studio-server/media-range"; import { getVerifiedHyperframeRuntimeSource } from "./hyperframeRuntimeLoader.js"; @@ -830,6 +831,7 @@ export function createFileServer(options: FileServerOptions): Promise { it("samples each video from its first frame every 0.25 s to its last frame", () => { - expect(shownBoxTimes([video("a", 1, 2)], fps)).toEqual([1, 1.25, 1.5, 1.75, 2 - 1 / 30]); + expect(shownBoxTimes([video("a", 1, 2)], fps, 10)).toEqual([1, 1.25, 1.5, 1.75, 2 - 1 / 30]); }); it("merges the times of several videos once each, in order", () => { - expect(shownBoxTimes([video("a", 0, 0.5), video("b", 0.25, 0.5)], fps)).toEqual([ + expect(shownBoxTimes([video("a", 0, 0.5), video("b", 0.25, 0.5)], fps, 10)).toEqual([ 0, 0.25, 0.5 - 1 / 30, ]); }); + + it("cuts each window at the composition's end", () => { + expect(shownBoxTimes([video("a", 0.5, Number.POSITIVE_INFINITY)], fps, 1)).toEqual([ + 0.5, + 0.75, + 1 - 1 / 30, + ]); + expect(shownBoxTimes([video("a", 0, 1e12)], fps, 0.5)).toEqual([0, 0.25, 0.5 - 1 / 30]); + }); + + it("returns promptly for a start too large for the stride to advance", () => { + expect(shownBoxTimes([video("a", 2 ** 53, 2 ** 54)], fps, 10)).toEqual([]); + expect(shownBoxTimes([video("a", Number.NaN, 1)], fps, 10)).toEqual([]); + }); }); describe("measureLocalShownBoxes", () => { @@ -71,6 +85,7 @@ describe("measureLocalShownBoxes", () => { ]), [video("still", 0, 1), video("zoom", 0, 1)], fps, + 1, log, ); expect(Object.fromEntries(boxes!)).toEqual({ @@ -90,6 +105,7 @@ describe("measureLocalShownBoxes", () => { page(undefined, new Error("Target closed")), [video("clip", 0, 1)], fps, + 1, log, ); expect(boxes).toBeUndefined(); diff --git a/packages/producer/src/services/render/videoShownBoxes.ts b/packages/producer/src/services/render/videoShownBoxes.ts index f1944be1ef..84f0cac8cd 100644 --- a/packages/producer/src/services/render/videoShownBoxes.ts +++ b/packages/producer/src/services/render/videoShownBoxes.ts @@ -35,16 +35,21 @@ export async function oversizedVideos( return videos.filter((_, index) => sized[index]); } -/** Each video's first and last frame and every stride between, in composition seconds, ascending. */ +/** Each video's first and last frame before `duration` and every stride between, in seconds, ascending. */ export function shownBoxTimes( videos: readonly VideoElement[], fps: { num: number; den: number }, + duration: number, ): number[] { const frame = fps.den / fps.num; const times = new Set(); - for (const { start, end } of videos) { + for (const { start, end: authoredEnd } of videos) { + const end = Math.min(authoredEnd, duration); + if (!Number.isFinite(start) || !Number.isFinite(end) || start > end) continue; const last = Math.max(start, end - frame); - for (let t = start; t < last; t += SHOWN_BOX_STRIDE_S) times.add(t); + for (let step = 0; start + step * SHOWN_BOX_STRIDE_S < last; step += 1) { + times.add(start + step * SHOWN_BOX_STRIDE_S); + } times.add(last); } return [...times].sort((a, b) => a - b); @@ -58,11 +63,13 @@ export async function measureLocalShownBoxes( page: Page, videos: readonly VideoElement[], fps: { num: number; den: number }, + duration: number, log: ProducerLogger, ): Promise | undefined> { - const times = shownBoxTimes(videos, fps); + let times: number[] = []; const startedAt = Date.now(); try { + times = shownBoxTimes(videos, fps, duration); const ranges = await measureVideoShownBoxRanges( page, videos.map((video) => video.id), diff --git a/packages/producer/src/services/renderOrchestrator.ts b/packages/producer/src/services/renderOrchestrator.ts index 3721fbbf3b..4ac16a4e52 100644 --- a/packages/producer/src/services/renderOrchestrator.ts +++ b/packages/producer/src/services/renderOrchestrator.ts @@ -3477,11 +3477,6 @@ async function executeRenderPipeline(input: { compositionHash = computeCompositionObservabilityHash(compiled.html); fileServer = probeResult.fileServer; probeSession = probeResult.probeSession; - // Before the video layer may close the probe page; no box means full size, as before. - const shownBoxes = - largerThanCanvas.length > 0 && probeSession?.isInitialized - ? await measureLocalShownBoxes(probeSession.page, largerThanCanvas, job.config.fps, log) - : undefined; job.strictVariablesChecked = probeResult.strictVariablesChecked; lastBrowserConsole = probeResult.lastBrowserConsole; let resolvedCaptureBeyondViewport = probeSession?.options.captureBeyondViewport; @@ -3499,6 +3494,17 @@ async function executeRenderPipeline(input: { totalFrames, fps: fpsToNumber(job.config.fps), }); + // Before the video layer may close the probe page; no box means full size, as before. + const shownBoxes = + largerThanCanvas.length > 0 && probeSession?.isInitialized + ? await measureLocalShownBoxes( + probeSession.page, + largerThanCanvas, + job.config.fps, + probeResult.duration, + log, + ) + : undefined; perfStages.browserProbeMs = probeResult.browserProbeMs; perfStages.compileMs = Date.now() - stage1Start;