From ac02474105bc6d2893a0ef1a79526f2952645aca Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Sun, 15 Nov 2026 09:48:01 +0000 Subject: [PATCH] fix(cli): play and present check the Host like preview; local render leaves out outside files for a received composition (#7792) * fix(cli): a subfolder of an imported project keeps its received-files record Publish, cloud render and feedback now read the received-files record at the nearest folder at or above the one they stage, so staging a subfolder of an imported project still leaves out the outside files its received files reference. Co-Authored-By: Claude Opus 5.5 * fix(cli): a received file counts under any spelling of the same file on disk The received-files lookup now also matches a project file that is the same file on disk as a listed path, so a name respelled in letter case on a case-insensitive disk keeps its received status. Co-Authored-By: Claude Opus 5.5 * fix(cli): cloud render prepares only the GIFs staging kept The entry GIF prep before a cloud render upload now reads only the staged copies it is handed. An image whose source staging left out keeps its reference as written and is never read from disk. Co-Authored-By: Claude Opus 5.5 * fix(cli,studio-server): never hand a playlist named as media to ffmpeg A media file whose first bytes are an HLS playlist, an XML manifest or a concat script now stays out of ffprobe and ffmpeg: cloud render leaves it in the zip as authored, the media probe reports it as not media, and the proxy copy refuses it. Real media never starts with these bytes, so it probes and transcodes as before. Co-Authored-By: Claude Opus 5.5 * fix(studio-server): catch playlists with control bytes, and keep them from stills and audio probes A text playlist or concat script is detected whatever bytes follow its first line; only markup must be all text. A still frame and the audio probe behind waveforms, peaks and loudness now refuse a file the probe reads as not media, as the proxy copy already does. Co-Authored-By: Claude Opus 5.5 * fix(cli): a parent folder's state the CLI cannot read never marks a project received The walk up for a received-files record now skips a parent whose record cannot be reached. Only a record file that is there but cannot be read or parsed counts every file as received. Co-Authored-By: Claude Opus 5.5 * fix(cli): a drive that reports no file ids matches no received file by identity Some network and virtual drives report a file id of 0 for every file, so the same-file match now gives such a file no identity instead of matching it to every listed file. Co-Authored-By: Claude Opus 5.5 * refactor(studio-server): read a media file's first bytes with a read stream The playlist check reads the first 64 bytes through a bounded read stream instead of a raw file descriptor, and the audio-track probe's doc comment sits on its function again. Co-Authored-By: Claude Opus 5.5 * chore(studio-server): raise the packed size budget for the media playlist check Main packs @hyperframes/studio-server close to the 1% tolerance on the 168500 budget, and the playlist check adds about 320 bytes, which put the Build over. Set the budget to 172000. Co-Authored-By: Claude Opus 5.5 * fix(cli,studio-server): open media only through self-contained media demuxers Every ffprobe and ffmpeg input that cloud render offload, the publish proxy bake and Studio's media helpers open now carries a demuxer whitelist built from the supported media extensions: MP4/MOV/M4A/AVIF, Matroska/WebM, MXF, AVI, MPEG-PS/TS, FLV, ASF, Ogg, WAV, MP3, AAC, FLAC, AIFF and the still-image formats. Playlist, manifest and concat demuxers can no longer open, whatever a file's bytes say. The proxy's own concat step is left as it is. Co-Authored-By: Claude Opus 5.5 * fix(cli,studio-server): keep media probe argv flat, and expect the demuxer list in loudness args Two probe argv literals built from nested spreads are flat again, so "--" stays the visible second-to-last entry the argv contract checks. The normalize-audio test expects the demuxer list before the input. Co-Authored-By: Claude Opus 5.5 * fix(cli): play and present answer only local host names and allowed hosts play and present listen on every interface. Each request now passes the same Host check preview uses: localhost, loopback addresses and this machine's own addresses and names are answered, and any other host name gets a 403 naming HYPERFRAMES_PREVIEW_ALLOWED_HOSTS until it is listed there. Phones and other machines on the network still open them by LAN address or .local name. * fix(cli): a local render of a received composition leaves out files outside the project When the composition a local render opens is listed in the project's received-files record, hyperframes render (full and range) no longer brings in files outside the project. It prints one line naming the references it leaves out and why. An unreadable record counts as received. A project whose entry is the person's own, or that has no record, renders exactly as before. * fix(cli): received-render scan reads safely and treats an unreadable record as unknown The scan behind the received-composition notice skips a file it cannot read, finds url() references with a linear forward scan, and reads at most 2 MB of each file. An unreadable received-files record no longer counts as everything received for local render: the render keeps outside files and names the record it could not read. Publish and cloud upload still treat it as received. The notice now says the render stops on a sub-composition outside the project rather than leaving it out. * fix(cli): the studio render of a received composition leaves out files outside the project The Studio render route applies the same rule as hyperframes render: when the composition it renders is listed in the received-files record, the job does not bring in files outside the project, and the server log names the references it refuses. * chore(cli): name the unreadable received-files record helper without a comment * fix(cli): local render refuses outside files when any composition it plays was received The received-file rule for local render (CLI and Studio) now checks every page and stylesheet the render pulls in, not only the entry, so an own index.html that plays a received scene also reads nothing outside the project, and the notice names the received file. An unreadable, truncated or empty received-files record refuses outside files too, as publish and cloud upload do, and the notice names the record it could not read. * fix(cli): decide a received render from the files the compiler reads Local render (CLI and Studio) now leaves outside-file handling as it is for a project with no received-files record or an empty one, without scanning anything. When the record lists files, the compiler's own read list for the composition decides: the bundler runs out of process with a deadline and reports every project file it reads, so stylesheet imports and files past any scan limit count. A received file in that list, a build that fails or runs past its deadline, or an unreadable record refuses outside files. The scan that names refused references in the notice is advisory only: it guards every file system call, tracks visited files by real path, and caps the files and bytes it reads. * fix(cli,producer): decide a received render with the render compiler's own composition traversal The producer exports renderCompositionFiles, which walks data-composition-src with the same resolution, normalization and private file rules the render's sub-composition pass uses; that pass now calls the same helper. The received-render decision adds those files to the bundler's read list, so leading-slash and percent-encoded references the render reads count. The advisory scan resolves compositions the same way and reads template contents. * fix(cli): refuse outside files in a local render whenever the project has received files A local render (CLI and Studio) reads nothing outside the project when the project's received-files record lists any file, or cannot be read. No static list matches what a render consumes: the page loads its stylesheets and scripts through the browser at capture time, and scripts can load more, so the decision no longer depends on one. The reference scan stays only to name what the notice says is left out. This drops the out-of-process bundler read list and the producer composition walk added for that decision. * fix(cli): refuse outside files only when a received file can load other files A local render (CLI and Studio) reads nothing outside the project when the received-files record cannot be read, or lists a received file that can reference others: an .html, .htm, .css, .js, .mjs, .cjs or .svg name, or content that reads as HTML, SVG or CSS. A project that only received images, video, audio or fonts keeps bringing in outside files as before. * fix(cli): keep outside files only when every received file is positively media, a font or an image A local render keeps bringing in outside files only when each file the received-files record lists has a known media, font or image extension and starts with that type's signature bytes. Any other received file, a listed file that is missing or unreadable, or a mismatch between name and content refuses outside files. An ancestor record's entries now match a subfolder whose name is spelled in another Unicode form; the entry keeps its own spelling on disk. * fix(producer,core): a sub-composition must be an .html file The render compiler and the runtime load a data-composition-src only when it names an .html or .htm file. Any other file, such as an image or video, is refused as a page: render stops naming it, and the runtime reports the load failure without fetching it. * fix(producer,cli): the render and static file servers forbid type sniffing Both servers send X-Content-Type-Options: nosniff with every response, so the browser loads a file only as the type its extension names: an image or video cannot run as a script or apply as a stylesheet. .cjs is served as JavaScript. * test(cli): a received media file in a composition slot is refused as a page * test(cli): an empty nested received-files record cannot hide an outer record's received page * fix(cli): a received media file reached under another name refuses outside files The received-render decision now compares file identities, not names. It walks the project once, bounded by entry count and time, and refuses outside files when a hardlink or a symlink with a name that is not media reaches a received file, or when the walk cannot finish. A received file with no identity matches nothing, as in the received files check. * fix(cli): compare received files by link target and keep outside files for an empty record A received media file and every other project name are now compared by the identity of the file they reach with links followed. A linked folder that leads outside the project, or a file whose identity cannot be read, counts as unchecked and refuses outside files. An empty received-files record keeps outside files without walking the project. * chore: raise the producer unpacked size budget for the composition file checks --------- Co-authored-by: Claude Opus 5.5 --- docs/packages/cli.mdx | 20 + packages/cli/src/commands/play.ts | 2 + packages/cli/src/commands/present.ts | 2 + packages/cli/src/commands/render.test.ts | 50 +- packages/cli/src/commands/render.ts | 10 +- .../server/studioServer.renderBrowser.test.ts | 54 +- packages/cli/src/server/studioServer.ts | 8 +- packages/cli/src/utils/compositionEntry.ts | 9 +- .../cli/src/utils/compositionServer.test.ts | 63 +- packages/cli/src/utils/compositionServer.ts | 29 + packages/cli/src/utils/privateProjectFile.ts | 19 + packages/cli/src/utils/receivedRender.test.ts | 540 ++++++++++++++++++ packages/cli/src/utils/receivedRender.ts | 426 ++++++++++++++ packages/cli/src/utils/renderArgs.test.ts | 10 + .../cli/src/utils/staticProjectServer.test.ts | 11 + packages/cli/src/utils/staticProjectServer.ts | 1 + .../src/runtime/compositionLoader.test.ts | 19 + .../core/src/runtime/compositionLoader.ts | 3 + .../producer/src/services/fileServer.test.ts | 27 + packages/producer/src/services/fileServer.ts | 6 + .../src/services/htmlCompiler.test.ts | 66 ++- .../producer/src/services/htmlCompiler.ts | 23 +- .../src/createStudioApi.originGuard.test.ts | 33 +- packages/studio-server/src/helpers/mime.ts | 1 + .../src/helpers/requestIdentity.ts | 17 +- scripts/package-size-budgets.json | 2 +- 26 files changed, 1426 insertions(+), 25 deletions(-) create mode 100644 packages/cli/src/utils/receivedRender.test.ts create mode 100644 packages/cli/src/utils/receivedRender.ts diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index 8630482552..18335380e2 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -1081,6 +1081,15 @@ the CLI itself, or when you set `HYPERFRAMES_LOCAL_STUDIO=1`; a Studio in the project's own `node_modules` is not used otherwise. `--background` works with the embedded server only. +The preview server answers only the host names it serves. To open it through +a tunnel, Codespaces or a reverse proxy, name that host in +`HYPERFRAMES_PREVIEW_ALLOWED_HOSTS` (comma separated). A refused request +answers 403 with the host named and this variable as the fix: + +```bash +HYPERFRAMES_PREVIEW_ALLOWED_HOSTS=demo.tunnels.example hyperframes preview +``` + The CLI ships GSAP, CustomEase, and MotionPathPlugin, so the GSAP scripts Studio adds to previews load from the preview server itself, not from jsDelivr. To use your own mirror instead, set `HYPERFRAMES_GSAP_BASE_URL` before starting @@ -1114,6 +1123,17 @@ npx hyperframes present [dir] --port 3004 editor, just playback. Same flags, `--port` defaults to 3003, plus `--proxy` / `--no-proxy`. +Both listen on every network interface, so a phone or another computer on your +network can open them at this machine's LAN address or `.local` name. They +answer `localhost`, loopback addresses, and this machine's own addresses and +names. Any other host name, such as a tunnel, Codespaces or a reverse proxy, +gets a 403 until you name it in `HYPERFRAMES_PREVIEW_ALLOWED_HOSTS`, the same +setting `preview` uses: + +```bash +HYPERFRAMES_PREVIEW_ALLOWED_HOSTS=demo.tunnels.example hyperframes present +``` + ### `publish` For new video reviews, use [`share`](#share). Legacy source publishing stays diff --git a/packages/cli/src/commands/play.ts b/packages/cli/src/commands/play.ts index 37115d6204..ebaf1c09aa 100644 --- a/packages/cli/src/commands/play.ts +++ b/packages/cli/src/commands/play.ts @@ -36,6 +36,7 @@ import { resolveRuntimePath, resolvePlayerPath, listenOnFreePort, + refuseUnknownHosts, injectRuntime, injectMediaCodecMap, buildRangeResponse, @@ -145,6 +146,7 @@ export default defineCommand({ const { createAdaptorServer } = await import("@hono/node-server"); const app = new Hono(); + refuseUnknownHosts(app); // Serve the player JS app.get("/player.js", (ctx) => diff --git a/packages/cli/src/commands/present.ts b/packages/cli/src/commands/present.ts index 684ad4b921..2e672143bd 100644 --- a/packages/cli/src/commands/present.ts +++ b/packages/cli/src/commands/present.ts @@ -27,6 +27,7 @@ import { resolvePlayerPath, resolveSlideshowPath, listenOnFreePort, + refuseUnknownHosts, assetContentType, } from "../utils/compositionServer.js"; import { isPrivateCompositionFile } from "../utils/privateProjectFile.js"; @@ -123,6 +124,7 @@ export default defineCommand({ const { createAdaptorServer } = await import("@hono/node-server"); const app = new Hono(); + refuseUnknownHosts(app); app.get("/player.js", (ctx) => ctx.body(readFileSync(playerPath, "utf-8"), 200, { diff --git a/packages/cli/src/commands/render.test.ts b/packages/cli/src/commands/render.test.ts index 4b79561f33..348fed694b 100644 --- a/packages/cli/src/commands/render.test.ts +++ b/packages/cli/src/commands/render.test.ts @@ -124,9 +124,17 @@ vi.mock("../utils/producer.js", () => ({ }), ), renderConfigFromRequest: vi.fn( - (request: { options: Record }, runtime: { logger?: unknown }) => { + ( + request: { options: Record }, + runtime: { logger?: unknown; allowOutsideProjectAssets?: boolean }, + ) => { const { engineConfig, ...options } = request.options; - return { ...options, producerConfig: engineConfig, logger: runtime.logger }; + return { + ...options, + producerConfig: engineConfig, + logger: runtime.logger, + allowOutsideProjectAssets: runtime.allowOutsideProjectAssets, + }; }, ), createRenderJob: vi.fn((config: Record) => { @@ -580,6 +588,44 @@ describe("renderLocal browser GPU config", () => { warn.mockRestore(); }); + it.each([ + { name: "a received entry", files: ["index.html"], allowed: false }, + { name: "a record listing only an image", files: ["assets/in.png"], allowed: true }, + { name: "a record listing a received stylesheet", files: ["style.css"], allowed: false }, + { name: "a record listing no files", files: [], allowed: true }, + { name: "no received-files record", files: undefined, allowed: true }, + ])("brings in outside files for $name: $allowed", async ({ files, allowed }) => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-render-received-")); + writeFileSync(join(projectDir, "index.html"), "
"); + mkdirSync(join(projectDir, "assets")); + writeFileSync( + join(projectDir, "assets", "in.png"), + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), + ); + if (files) { + mkdirSync(join(projectDir, ".hyperframes")); + writeFileSync( + join(projectDir, ".hyperframes", "received-files.json"), + JSON.stringify({ files }), + ); + } + try { + await renderLocal(projectDir, join(projectDir, "out.mp4"), { + fps: { num: 30, den: 1 }, + quality: "standard", + format: "mp4", + gpu: false, + browserGpuMode: "software", + hdrMode: "auto", + quiet: true, + throwOnError: true, + }); + expect(producerState.createdJobs.at(-1)?.allowOutsideProjectAssets).toBe(allowed); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); + it("cleans orphaned browser trees before starting a local render", async () => { orphanCleanupState.killed = 1; diff --git a/packages/cli/src/commands/render.ts b/packages/cli/src/commands/render.ts index 9e4380998f..a19e0f0084 100644 --- a/packages/cli/src/commands/render.ts +++ b/packages/cli/src/commands/render.ts @@ -72,6 +72,7 @@ import { freemem, tmpdir } from "node:os"; import { resolve, dirname, join, basename } from "node:path"; import { execFileSync, spawn } from "node:child_process"; import { loadProducer } from "../utils/producer.js"; +import { outsideAssetsAllowed } from "../utils/receivedRender.js"; import { c } from "../ui/colors.js"; import { desktopHint } from "../utils/desktopApp.js"; import { @@ -1148,6 +1149,9 @@ async function executeLocalRender( process.env.HYPERFRAMES_GSAP_VENDOR_DIR = gsapVendorDir; } const producer = await loadProducer(); + const allowOutsideProjectAssets = outsideAssetsAllowed(projectDir, options.entryFile, (line) => + console.warn(c.warn(` ${line}`)), + ); const deParallelRouterActive = options.manageDeParallelRouterBreaker === true ? applyDeParallelRouterCircuitBreaker(options.quiet) @@ -1227,10 +1231,11 @@ async function executeLocalRender( startTime, cpu, signal: cancellation.signal, + allowOutsideProjectAssets, }); } const job = producer.createRenderJob( - producer.renderConfigFromRequest(request, { logger, allowOutsideProjectAssets: true }), + producer.renderConfigFromRequest(request, { logger, allowOutsideProjectAssets }), ); const onProgress = options.quiet @@ -1348,6 +1353,7 @@ async function renderRange(input: { startTime: number; cpu: RenderCpuSampler; signal: AbortSignal; + allowOutsideProjectAssets: boolean; }): Promise { const { producer, options } = input; let framesEncoded = 0; @@ -1362,7 +1368,7 @@ async function renderRange(input: { config, input.range, input.outputPath, - { allowOutsideProjectAssets: true }, + { allowOutsideProjectAssets: input.allowOutsideProjectAssets }, ); framesEncoded = spanResult.framesEncoded; reference = producer.referenceBackgroundResult(spanResult[producer.REFERENCE_BACKGROUND_FIELD]); diff --git a/packages/cli/src/server/studioServer.renderBrowser.test.ts b/packages/cli/src/server/studioServer.renderBrowser.test.ts index 5377205ede..73f58ba00b 100644 --- a/packages/cli/src/server/studioServer.renderBrowser.test.ts +++ b/packages/cli/src/server/studioServer.renderBrowser.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { mkdtempSync, rmSync } from "node:fs"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createStudioServer, type StudioServer } from "./studioServer.js"; @@ -43,9 +43,19 @@ afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); -function startStudioRender(extra: Record = {}) { +function startStudioRender(extra: Record = {}, receivedFiles?: string[]) { const dir = mkdtempSync(join(tmpdir(), "hf-studio-render-browser-")); dirs.push(dir); + writeFileSync(join(dir, "index.html"), `
`); + writeFileSync(join(dir, "scene.html"), "
"); + writeFileSync(join(dir, "other.html"), "
"); + if (receivedFiles) { + mkdirSync(join(dir, ".hyperframes")); + writeFileSync( + join(dir, ".hyperframes", "received-files.json"), + JSON.stringify({ files: receivedFiles }), + ); + } server = createStudioServer({ projectDir: dir }); return server.adapter.startRender({ project: { dir, id: "p", title: "p" }, @@ -137,3 +147,43 @@ describe("Studio render motion blur", () => { ); }); }); + +describe("Studio render of a received composition", () => { + it.each([ + { name: "a received entry", extra: {}, files: ["index.html"], allowed: false }, + { + name: "a received composition picked to render", + extra: { composition: "scene.html" }, + files: ["scene.html"], + allowed: false, + }, + { + name: "the person's own entry playing a received composition", + extra: {}, + files: ["scene.html"], + allowed: false, + }, + { + name: "a record listing a file the render never opens", + extra: {}, + files: ["other.html"], + allowed: false, + }, + { name: "a record listing no files", extra: {}, files: [], allowed: true }, + { name: "no received-files record", extra: {}, files: undefined, allowed: true }, + ])("brings in outside files for $name: $allowed", async ({ extra, files, allowed }) => { + mocks.resolveRenderBrowser.mockResolvedValue({ + executablePath: "/opt/chrome", + source: "cache", + }); + + startStudioRender(extra, files); + await vi.waitFor(() => expect(mocks.createRenderJob).toHaveBeenCalledTimes(1), { + timeout: 10_000, + }); + + expect(mocks.createRenderJob).toHaveBeenCalledWith( + expect.objectContaining({ allowOutsideProjectAssets: allowed }), + ); + }); +}); diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index aa10d8b6cd..4be70c217e 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -43,6 +43,7 @@ import { } from "@hyperframes/studio-server/request-identity"; import { mountShareRoutes } from "./shareRoutes.js"; import { isDevMode } from "../utils/env.js"; +import { outsideAssetsAllowed } from "../utils/receivedRender.js"; import { insideHostApp } from "../utils/desktopApp.js"; import { runRenderSetupWorker } from "../utils/cancellableProcess.js"; import { acquireRenderLock, describeRenderLockHolder } from "../utils/renderLock.js"; @@ -667,6 +668,11 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { process.env.PRODUCER_HEADLESS_SHELL_PATH = browser.executablePath; } + const allowOutsideProjectAssets = outsideAssetsAllowed( + opts.project.dir, + opts.composition, + (line) => console.warn(`[studio] ${line}`), + ); const manifestContent = readStudioManualEditManifestContent(opts.project.dir); const manualEditsRenderScript = createStudioManualEditsRenderBodyScript(manifestContent); const job = createRenderJob({ @@ -683,7 +689,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { ...encoderJobOptions(opts), ...(opts.useGpu ? { useGpu: true } : {}), ...(opts.pageMotionBlur ? { pageMotionBlur: opts.pageMotionBlur } : {}), - allowOutsideProjectAssets: true, + allowOutsideProjectAssets, }); renderJob = job; const onProgress = (j: { progress: number; currentStage?: string }) => { diff --git a/packages/cli/src/utils/compositionEntry.ts b/packages/cli/src/utils/compositionEntry.ts index e5980149ac..c0eb40e06b 100644 --- a/packages/cli/src/utils/compositionEntry.ts +++ b/packages/cli/src/utils/compositionEntry.ts @@ -4,7 +4,7 @@ import { errorBox } from "../ui/format.js"; import { failUsage } from "./commandResult.js"; export type CompositionEntryParseError = { - kind: "outside-project" | "not-found" | "not-a-file"; + kind: "outside-project" | "not-found" | "not-a-file" | "not-html"; entryFile: string; entryPath: string; projectDir: string; @@ -56,6 +56,7 @@ export function parseCompositionEntryArg( // A directory would pass existsSync downstream and fail with EISDIR in the producer. return fail("not-a-file"); } + if (!/\.html?$/i.test(entryPath)) return fail("not-html"); return { ok: true, value: relativeEntry.split(sep).join("/") }; } @@ -92,6 +93,12 @@ export function compositionEntryErrorMessage(error: CompositionEntryParseError): message: `${resolved}, a directory, not an .html file (project directory ${error.projectDir}).`, hint: "Pass a path to a .html file (e.g. compositions/intro.html), or omit --composition to render index.html.", }; + case "not-html": + return { + title: "Invalid composition path", + message: `${resolved}, which is not an .html file, so it is refused.`, + hint: "Pass a path to a .html or .htm file (e.g. compositions/intro.html).", + }; } } diff --git a/packages/cli/src/utils/compositionServer.test.ts b/packages/cli/src/utils/compositionServer.test.ts index e4474be007..dc7b3db95f 100644 --- a/packages/cli/src/utils/compositionServer.test.ts +++ b/packages/cli/src/utils/compositionServer.test.ts @@ -1,8 +1,9 @@ import { afterEach, describe, expect, it } from "vitest"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; +import { hostname, networkInterfaces, tmpdir } from "node:os"; import { join } from "node:path"; -import { buildRangeResponse } from "./compositionServer.js"; +import { Hono } from "hono"; +import { buildRangeResponse, refuseUnknownHosts } from "./compositionServer.js"; const TAGGED_PNG = Buffer.from( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAABGNJQ1ABAQABlXPCVgAAAAxJREFUeJxj0NfXBwABHgCOTp25kgAAAABJRU5ErkJggg==", @@ -30,3 +31,61 @@ describe("buildRangeResponse colour-tagged PNGs", () => { expect(res.headers.get("content-length")).toBe(String(body.length)); }); }); + +describe("refuseUnknownHosts", () => { + const ENV = "HYPERFRAMES_PREVIEW_ALLOWED_HOSTS"; + + afterEach(() => { + delete process.env[ENV]; + }); + + function guardedApp(): Hono { + const app = new Hono(); + refuseUnknownHosts(app); + app.get("/composition/*", (c) => c.text("project file")); + return app; + } + + function get(app: Hono, host: string): Promise { + return Promise.resolve(app.request("/composition/package.json", { headers: { host } })); + } + + function lanAddress(): string | undefined { + for (const entries of Object.values(networkInterfaces())) { + for (const entry of entries ?? []) { + if (entry.family === "IPv4" && !entry.internal) return entry.address; + } + } + return undefined; + } + + it("answers localhost and loopback addresses", async () => { + const app = guardedApp(); + for (const host of ["localhost:3003", "127.0.0.1:3003", "[::1]:3003"]) { + expect((await get(app, host)).status, host).toBe(200); + } + }); + + it("answers this machine's LAN address and .local name, so another device can open it", async () => { + const app = guardedApp(); + const lan = lanAddress(); + if (lan) expect((await get(app, `${lan}:3003`)).status).toBe(200); + const short = hostname().toLowerCase().split(".")[0]; + if (short) expect((await get(app, `${short}.local:3003`)).status).toBe(200); + }); + + it("refuses any other host name and names the setting that allows it", async () => { + const res = await get(guardedApp(), "rebound.example:3003"); + expect(res.status).toBe(403); + const text = await res.text(); + expect(text).not.toContain("project file"); + expect(text).toContain(`${ENV}=rebound.example:3003`); + }); + + it("answers a host named in HYPERFRAMES_PREVIEW_ALLOWED_HOSTS", async () => { + process.env[ENV] = "other.example, demo.tunnels.example"; + const res = await get(guardedApp(), "demo.tunnels.example"); + expect(res.status).toBe(200); + expect(await res.text()).toBe("project file"); + }); +}); diff --git a/packages/cli/src/utils/compositionServer.ts b/packages/cli/src/utils/compositionServer.ts index c64ae46f06..1be0b61fcc 100644 --- a/packages/cli/src/utils/compositionServer.ts +++ b/packages/cli/src/utils/compositionServer.ts @@ -9,6 +9,12 @@ import { fileURLToPath } from "node:url"; import { getMimeType, openRangeEnd } from "@hyperframes/studio-server"; import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags"; import { injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document"; +import { + PREVIEW_ALLOWED_HOSTS_ENV, + previewAllowedHostsFromEnv, + wildcardBindHostAllowed, +} from "@hyperframes/studio-server/request-identity"; +import type { Hono } from "hono"; /** * `window.__HF_MEDIA_CODEC_MAP__` injection + proxy pre-warm for HTML served @@ -175,6 +181,29 @@ export function buildRangeResponse( }); } +/** + * The server listens on every interface, so every route answers only loopback, + * this machine's own addresses and names, or a host named in + * HYPERFRAMES_PREVIEW_ALLOWED_HOSTS. A browser cannot forge Host, which keeps a + * rebound page name from reading project files. + */ +export function refuseUnknownHosts(app: Hono): void { + const allowedHosts = previewAllowedHostsFromEnv(); + app.use(async function refuseUnknownHost(c, next) { + const host = c.req.header("host") ?? new URL(c.req.url).host; + if (wildcardBindHostAllowed(host, allowedHosts)) return next(); + return c.json( + { + error: + `forbidden: host "${host}" is not allowed. ` + + `Set ${PREVIEW_ALLOWED_HOSTS_ENV}=${host} to allow it ` + + `(comma separated for several hosts).`, + }, + 403, + ); + }); +} + /** * Bind `server` to the first free port at or after `startPort` (scanning up to * 10 ports). Returns the bound port. Rejects if all candidates are in use or on diff --git a/packages/cli/src/utils/privateProjectFile.ts b/packages/cli/src/utils/privateProjectFile.ts index 299fa09663..654f683468 100644 --- a/packages/cli/src/utils/privateProjectFile.ts +++ b/packages/cli/src/utils/privateProjectFile.ts @@ -160,6 +160,25 @@ export function receivedFiles(projectDir: string): (path: string) => boolean { return (path) => listedInAnyRecord.some((listed) => listed(path)); } +function listedUnder(record: ReceivedRecord): string[] { + const prefix = record.projectDirFromRoot ? `${record.projectDirFromRoot}/`.normalize("NFC") : ""; + return (record.listed as unknown[]) + .map(String) + .filter((entry) => entry.normalize("NFC").startsWith(prefix)) + .map((entry) => join(record.root, entry)); +} + +export function receivedRecordStatus( + projectDir: string, +): { path: string; listed: string[] | null } | null { + const records = receivedRecordsAtOrAbove(projectDir); + const [nearest] = records; + if (!nearest) return null; + const unreadable = records.find((record) => !Array.isArray(record.listed)); + if (unreadable) return { path: join(unreadable.root, RECEIVED_RECORD), listed: null }; + return { path: join(nearest.root, RECEIVED_RECORD), listed: records.flatMap(listedUnder) }; +} + /** Project files no composition loads, so none is served or uploaded for one: env files, Git data, tool records. */ export function isPrivateCompositionFile(projectDir: string, filePath: string): boolean { return ( diff --git a/packages/cli/src/utils/receivedRender.test.ts b/packages/cli/src/utils/receivedRender.test.ts new file mode 100644 index 0000000000..dd07c4b868 --- /dev/null +++ b/packages/cli/src/utils/receivedRender.test.ts @@ -0,0 +1,540 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { + chmodSync, + linkSync, + mkdirSync, + mkdtempSync, + realpathSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { compileForRender } from "../../../producer/src/services/htmlCompiler.js"; +import { outsideAssetsAllowed } from "./receivedRender.js"; + +const SIGNATURES = { + png: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0x0d]), + mp4: Buffer.concat([Buffer.from([0, 0, 0, 0x18]), Buffer.from("ftypisom\0\0\0\0")]), + mp3: Buffer.from("ID3\u0004\0\0\0\0\0\0"), + woff2: Buffer.from("wOF2\0\u0001\0\0\0\0\0\0"), + jpg: Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0, 0x10]), +}; + +let root: string | undefined; + +afterEach(() => { + if (root) rmSync(root, { recursive: true, force: true }); + root = undefined; +}); + +function write(path: string, text: string | Buffer): void { + mkdirSync(join(path, ".."), { recursive: true }); + writeFileSync(path, text); +} + +function project(record?: string): string { + root = mkdtempSync(join(tmpdir(), "hf-received-render-")); + for (const name of ["secret.js", "pic.png", "clip.mp4", "bg.png"]) { + write(join(root, "outside", name), "x"); + } + const dir = join(root, "project"); + write( + join(dir, "index.html"), + ` + + + +
`, + ); + write(join(dir, "compositions", "scene.html"), ``); + write(join(dir, "style.css"), `body { background: url(../outside/bg.png); }`); + write(join(dir, "assets", "in.png"), SIGNATURES.png); + write(join(dir, "unused.html"), ``); + if (record !== undefined) write(join(dir, ".hyperframes", "received-files.json"), record); + return dir; +} + +const received = (...files: string[]) => JSON.stringify({ files }); + +const SOMEONE_ELSE = "came from someone else and is not a media, font or image file"; + +function decide(dir: string, entry?: string) { + const warn = vi.fn(); + const allowed = outsideAssetsAllowed(dir, entry, warn); + return { allowed, warn, line: String(warn.mock.calls[0]?.[0] ?? "") }; +} + +describe("outsideAssetsAllowed", () => { + it("refuses outside files for a received entry and names every reference it leaves out", () => { + const { allowed, warn, line } = decide(project(received("index.html"))); + expect(allowed).toBe(false); + expect(warn).toHaveBeenCalledTimes(1); + expect(line).toContain(SOMEONE_ELSE); + for (const ref of [ + "../outside/secret.js", + "../outside/pic.png", + "../../outside/clip.mp4", + "../outside/bg.png", + ]) { + expect(line).toContain(ref); + } + expect(line).not.toContain("assets/in.png"); + expect(line).not.toContain("cdn.example"); + }); + + it("refuses outside files when the own entry plays a received composition, and names it", () => { + const dir = project(received("compositions/scene.html")); + for (const entry of ["compositions/scene.html", undefined]) { + const { allowed, line } = decide(dir, entry); + expect(allowed).toBe(false); + expect(line).toContain(SOMEONE_ELSE); + } + }); + + it("refuses outside files when a linked stylesheet was received", () => { + const { allowed, line } = decide(project(received("style.css"))); + expect(allowed).toBe(false); + expect(line).toContain(SOMEONE_ELSE); + }); + + it("refuses outside files when a stylesheet @imports a received stylesheet", () => { + const dir = project(received("theme.css")); + write(join(dir, "style.css"), `@import "theme.css";\nbody { color: red; }`); + write(join(dir, "theme.css"), `body { background: url(../outside/bg.png); }`); + const { allowed } = decide(dir); + expect(allowed).toBe(false); + }); + + it("refuses outside files when a received composition sits past the scan limit", () => { + const dir = project(received("compositions/scene.html")); + write( + join(dir, "index.html"), + `

${"a".repeat(2_200_000)}

`, + ); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain(SOMEONE_ELSE); + }); + + it("refuses outside files whenever the record lists a file, even one the render never opens", () => { + const { allowed, line } = decide(project(received("unused.html"))); + expect(allowed).toBe(false); + expect(line).toContain(SOMEONE_ELSE); + expect(line).toContain("../outside/secret.js"); + }); + + it.each([ + ["an image", "assets/in.png", SIGNATURES.png], + ["a JPEG", "media/photo.jpg", SIGNATURES.jpg], + ["a video", "media/clip.mp4", SIGNATURES.mp4], + ["audio", "media/voice.mp3", SIGNATURES.mp3], + ["a font", "fonts/brand.woff2", SIGNATURES.woff2], + ])("keeps outside files when only %s was received", (_kind, file, bytes) => { + const dir = project(received(file)); + write(join(dir, file), bytes); + const { allowed, warn } = decide(dir); + expect(allowed).toBe(true); + expect(warn).not.toHaveBeenCalled(); + }); + + it.each([ + ["a media name over HTML", "media/clip.mp4", "
"], + ["an image name over a script", "assets/in.png", "window.x = 1;"], + ["UTF-16 HTML under an unknown name", "scene.dat", Buffer.from("\uFEFF
", "utf16le")], + ["an empty media file", "media/empty.mp4", ""], + ])("refuses outside files for %s", (_kind, file, content) => { + const dir = project(received(file)); + write(join(dir, file), content); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain(SOMEONE_ELSE); + }); + + it("refuses outside files when a listed media file is missing", () => { + expect(decide(project(received("media/gone.mp4"))).allowed).toBe(false); + }); + + it("keeps a received entry listed by an ancestor record for a decomposed-Unicode subfolder", () => { + root = mkdtempSync(join(tmpdir(), "hf-received-render-")); + write(join(root, "outside", "pic.png"), SIGNATURES.png); + const nfd = "Su\u0308bfolder"; + const dir = join(root, nfd); + write(join(dir, "index.html"), ``); + write( + join(root, ".hyperframes", "received-files.json"), + received(`${nfd.normalize("NFC")}/index.html`), + ); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain("../outside/pic.png"); + }); + + it.each([ + ["HTML", "notes.txt", "
"], + ["CSS", "theme.dat", "body { background: url(../outside/bg.png); }"], + ["SVG", "logo.img", ""], + ])("refuses outside files when a received file sniffs as %s", (_kind, file, text) => { + const dir = project(received(file)); + write(join(dir, file), text); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain(`${file} ${SOMEONE_ELSE}`); + }); + + it("refuses outside files when an outer record lists received HTML under an empty nested record", () => { + root = mkdtempSync(join(tmpdir(), "hf-received-render-")); + write(join(root, "outside", "pic.png"), SIGNATURES.png); + const dir = join(root, "sub"); + write(join(dir, "index.html"), ``); + write(join(dir, ".hyperframes", "received-files.json"), received()); + write(join(root, ".hyperframes", "received-files.json"), received("sub/index.html")); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain(`index.html ${SOMEONE_ELSE}`); + }); + + it("keeps outside files when the record lists no files", () => { + expect(decide(project(received())).allowed).toBe(true); + }); + + it("keeps outside files for an empty record in a project with very many files", () => { + const dir = project(received()); + const many = join(dir, "node_modules"); + mkdirSync(many); + for (let i = 0; i <= 20_000; i++) writeFileSync(join(many, `f${i}`), ""); + const { allowed, warn } = decide(dir); + expect(allowed).toBe(true); + expect(warn).not.toHaveBeenCalled(); + }); + + it("keeps outside files for a project with no received-files record", () => { + const { allowed, warn } = decide(project()); + expect(allowed).toBe(true); + expect(warn).not.toHaveBeenCalled(); + }); + + it.runIf(process.platform !== "win32")( + "renders an owned project with a symlink loop normally", + () => { + const dir = project(); + symlinkSync("loop", join(dir, "loop")); + write( + join(dir, "index.html"), + `x`, + ); + expect(decide(dir).allowed).toBe(true); + write(join(dir, ".hyperframes", "received-files.json"), received("unused.html")); + expect(() => decide(dir)).not.toThrow(); + }, + ); + + it.each(["{not json", "", "{}"])( + "refuses outside files and names the record when it cannot be read: %j", + (record) => { + const dir = project(record); + const { allowed, warn, line } = decide(dir); + expect(allowed).toBe(false); + expect(warn).toHaveBeenCalledTimes(1); + expect(line).toContain( + `Could not read ${join(realpathSync(dir), ".hyperframes", "received-files.json")}`, + ); + expect(line).toContain("../outside/secret.js"); + }, + ); + + it("says the render stops on a composition outside the project", () => { + const dir = project(received("index.html")); + write(join(dir, "..", "outside", "shared.html"), "
"); + write(join(dir, "index.html"), `
`); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain("stops on the compositions ../outside/shared.html"); + }); + + it.runIf(process.platform !== "win32")( + "skips a stylesheet it cannot read instead of failing", + () => { + const dir = project(received("index.html")); + write(join(dir, "locked.css"), "body { background: url(../outside/bg.png); }"); + chmodSync(join(dir, "locked.css"), 0o000); + write(join(dir, "index.html"), ``); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain("../outside/pic.png"); + }, + ); + + it("scans a long unclosed url( run in linear time", () => { + const dir = project("{not json"); + write(join(dir, "style.css"), "url(".repeat(1_000_000)); + const started = performance.now(); + decide(dir); + expect(performance.now() - started).toBeLessThan(5_000); + }); + + describe("parity with the render compiler", () => { + const scene = (id: string) => + ``; + + it.each([ + { + name: "a root-relative reference with a leading slash", + files: { "compositions/scene.html": scene("received-scene") }, + src: "/compositions/scene.html", + receivedFile: "compositions/scene.html", + consumed: "received-scene-marker", + }, + { + name: "a percent-encoded reference", + files: { "compositions/my scene.html": scene("received-scene") }, + src: "compositions/my%20scene.html", + receivedFile: "compositions/my scene.html", + consumed: "received-scene-marker", + }, + { + name: "a nested reference resolved from the project root", + files: { + "compositions/outer.html": + ``, + "compositions/inner.html": scene("received-scene"), + }, + src: "compositions/outer.html", + receivedFile: "compositions/inner.html", + consumed: "received-scene-marker", + }, + { + name: "an owned scene's received stylesheet", + files: { + "compositions/scene.html": + ``, + "compositions/scene.css": "body { color: red; }", + }, + src: "compositions/scene.html", + receivedFile: "compositions/scene.css", + consumed: "scene.css", + }, + { + name: "a received stylesheet of an owned scene nested in an owned scene", + files: { + "compositions/outer.html": + ``, + "compositions/scene.html": + ``, + "compositions/scene.css": "body { color: red; }", + }, + src: "compositions/outer.html", + receivedFile: "compositions/scene.css", + consumed: "scene.css", + }, + { + name: "an owned scene's received script", + files: { + "compositions/scene.html": + ``, + "compositions/scene.js": "window.sceneLoaded = true;", + }, + src: "compositions/scene.html", + receivedFile: "compositions/scene.js", + consumed: "scene.js", + }, + { + name: "a received script of an owned scene nested in an owned scene", + files: { + "compositions/outer.html": + ``, + "compositions/scene.html": + ``, + "compositions/scene.js": "window.sceneLoaded = true;", + }, + src: "compositions/outer.html", + receivedFile: "compositions/scene.js", + consumed: "scene.js", + }, + ])("refuses outside files for $name the render compiler reads", async (probe) => { + const dir = project(received(probe.receivedFile)); + for (const [file, text] of Object.entries(probe.files)) write(join(dir, file), text); + write( + join(dir, "index.html"), + `
` + + `
`, + ); + + const compiled = await compileForRender(dir, join(dir, "index.html"), join(dir, ".dl"), { + allowOutsideProjectAssets: true, + }); + const rendered = [compiled.html, ...compiled.subCompositions.values()].join("\n"); + expect(rendered).toContain(probe.consumed); + + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain(SOMEONE_ELSE); + expect(line).toContain("../../outside/pic.png"); + }); + }); + + describe("a received media file in a composition slot", () => { + it.each(["assets/scene.png", "/assets/scene.png"])( + "the render compiler refuses %s as a page, so the media exemption holds", + async (src) => { + const dir = project(received("assets/scene.png")); + write( + join(dir, "assets", "scene.png"), + Buffer.concat([ + SIGNATURES.png, + Buffer.from( + ``, + ), + ]), + ); + write( + join(dir, "index.html"), + `
` + + `
`, + ); + + await expect( + compileForRender(dir, join(dir, "index.html"), join(dir, ".dl"), { + allowOutsideProjectAssets: true, + }), + ).rejects.toThrow(/assets\/scene\.png: it is not an \.html file/); + expect(decide(dir).allowed).toBe(true); + }, + ); + }); + + describe("another name for a received media file", () => { + const mediaPage = (outsideRef: string) => + Buffer.concat([ + SIGNATURES.png, + Buffer.from( + ``, + ), + ]); + const MEDIA_PAGE = mediaPage("../outside/pic.png"); + + function aliasProject(makeAlias: (target: string, alias: string) => void, alias: string) { + const dir = project(received("assets/scene.png")); + write(join(dir, "assets", "scene.png"), MEDIA_PAGE); + makeAlias(join(dir, "assets", "scene.png"), join(dir, alias)); + write( + join(dir, "index.html"), + `
` + + `
`, + ); + return dir; + } + + it.each([ + ["hardlink", (target: string, alias: string) => linkSync(target, alias)], + ["symlink", (target: string, alias: string) => symlinkSync(target, alias)], + ])("refuses outside files when a %s with an HTML name reaches it", async (_kind, makeAlias) => { + const dir = aliasProject(makeAlias, "scene.html"); + const compiled = await compileForRender(dir, join(dir, "index.html"), join(dir, ".dl"), { + allowOutsideProjectAssets: true, + }); + const rendered = [compiled.html, ...compiled.subCompositions.values()].join("\n"); + expect(rendered).toContain("received-scene-marker"); + + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain("scene.html is another name for a file that came from someone else"); + }); + + async function compiledPage(dir: string) { + const compiled = await compileForRender(dir, join(dir, "index.html"), join(dir, ".dl"), { + allowOutsideProjectAssets: true, + }); + const rendered = [compiled.html, ...compiled.subCompositions.values()].join("\n"); + return { rendered, outside: [...compiled.externalAssets.values()] }; + } + + function playScene(dir: string, src: string): void { + write( + join(dir, "index.html"), + `
` + + `
`, + ); + } + + it.runIf(process.platform !== "win32")( + "refuses outside files when the received name is a link and an HTML link reaches the same file", + async () => { + const dir = project(received("assets/scene.png")); + write(join(dir, "media", "real.png"), MEDIA_PAGE); + symlinkSync(join(dir, "media", "real.png"), join(dir, "assets", "scene.png")); + symlinkSync(join(dir, "media", "real.png"), join(dir, "scene.html")); + playScene(dir, "scene.html"); + const { rendered, outside } = await compiledPage(dir); + expect(rendered).toContain("received-scene-marker"); + expect(outside).toContainEqual(expect.stringMatching(/[\\/]outside[\\/]pic\.png$/)); + + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain("scene.html is another name for a file that came from someone else"); + }, + ); + + it.runIf(process.platform !== "win32")( + "refuses outside files when a linked folder leading outside the project holds the received file", + async () => { + const dir = project(received("pages/scene.png")); + const shared = join(dir, "..", "shared"); + write(join(shared, "scene.png"), mediaPage("assets/../../outside/pic.png")); + symlinkSync("scene.png", join(shared, "scene.html")); + symlinkSync(shared, join(dir, "pages")); + playScene(dir, "pages/scene.html"); + const { rendered, outside } = await compiledPage(dir); + expect(rendered).toContain("received-scene-marker"); + expect(outside).toContainEqual(expect.stringMatching(/[\\/]outside[\\/]pic\.png$/)); + + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain("Could not check every project file"); + }, + ); + + it.runIf(process.platform !== "win32")( + "keeps outside files when a linked folder stays inside the project", + () => { + const dir = project(received("assets/in.png")); + symlinkSync(join(dir, "assets"), join(dir, "pictures")); + expect(decide(dir).allowed).toBe(true); + }, + ); + + it("keeps outside files when the other name is also a media name", () => { + const dir = project(received("assets/scene.png")); + write(join(dir, "assets", "scene.png"), SIGNATURES.png); + linkSync(join(dir, "assets", "scene.png"), join(dir, "assets", "copy.png")); + expect(decide(dir).allowed).toBe(true); + }); + + it.runIf(process.platform !== "win32")( + "refuses outside files when it cannot check every project file", + () => { + const dir = project(received("assets/in.png")); + mkdirSync(join(dir, "locked")); + chmodSync(join(dir, "locked"), 0o000); + try { + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toContain("Could not check every project file"); + } finally { + chmodSync(join(dir, "locked"), 0o755); + } + }, + ); + }); +}); diff --git a/packages/cli/src/utils/receivedRender.ts b/packages/cli/src/utils/receivedRender.ts new file mode 100644 index 0000000000..58fe07824f --- /dev/null +++ b/packages/cli/src/utils/receivedRender.ts @@ -0,0 +1,426 @@ +import { + closeSync, + openSync, + readdirSync, + readSync, + realpathSync, + statSync, + type Dirent, +} from "node:fs"; +import { dirname, extname, isAbsolute, join, relative, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { parseHTML } from "linkedom"; +import { isNonRelativeUrl, isPathInside } from "@hyperframes/core"; +import { resolveCompositionSrc } from "@hyperframes/parsers/asset-resolution"; +import { linksOutside, receivedRecordStatus } from "./privateProjectFile.js"; + +const REFERENCE_ATTRS = ["src", "href", "poster"]; +const MAX_SCANNED_BYTES = 2 * 1024 * 1024; +const MAX_SCANNED_FILES = 500; + +interface Reference { + raw: string; + composition: boolean; + follow: boolean; +} + +function unquote(value: string): string { + const first = value[0]; + return value.length >= 2 && (first === '"' || first === "'") && value.at(-1) === first + ? value.slice(1, -1).trim() + : value; +} + +function cssReferences(css: string): Reference[] { + const refs: Reference[] = []; + for (let at = css.indexOf("url("); at !== -1; ) { + const close = css.indexOf(")", at + 4); + if (close === -1) break; + const raw = unquote(css.slice(at + 4, close).trim()); + if (raw) refs.push({ raw, composition: false, follow: true }); + at = css.indexOf("url(", close + 1); + } + return refs; +} + +function elementReferences(el: Element): Reference[] { + const follow = el.localName === "link"; + const refs: Reference[] = REFERENCE_ATTRS.flatMap((attr) => { + const raw = el.getAttribute(attr)?.trim(); + return raw ? [{ raw, composition: false, follow }] : []; + }); + const composition = el.getAttribute("data-composition-src")?.trim(); + if (composition) refs.push({ raw: composition, composition: true, follow: true }); + return refs; +} + +function htmlReferences(html: string): Reference[] { + const { document } = parseHTML(html); + const hosts = document.querySelectorAll("[src], [href], [poster], [data-composition-src]"); + const refs = [...hosts].flatMap(elementReferences); + for (const style of document.querySelectorAll("style")) { + refs.push(...cssReferences(style.textContent ?? "")); + } + for (const el of document.querySelectorAll("[style]")) { + refs.push(...cssReferences(el.getAttribute("style") ?? "")); + } + for (const template of document.querySelectorAll("template")) { + refs.push(...htmlReferences(template.innerHTML)); + } + return refs; +} + +function localPath(raw: string, fromDir: string): string | null { + if (raw.startsWith("file:")) { + try { + return fileURLToPath(raw); + } catch { + return null; + } + } + const path = raw.replace(/[?#].*$/, ""); + if (isAbsolute(path) && !path.startsWith("//")) return path; + if (path === "" || isNonRelativeUrl(path)) return null; + try { + return resolve(fromDir, decodeURIComponent(path)); + } catch { + return resolve(fromDir, path); + } +} + +function isFile(path: string): boolean { + try { + return statSync(path, { throwIfNoEntry: false })?.isFile() === true; + } catch { + return false; + } +} + +function realOrNull(path: string): string | null { + try { + return realpathSync(path); + } catch { + return null; + } +} + +function readHead(file: string): { text: string; whole: boolean } | null { + let fd: number | undefined; + try { + fd = openSync(file, "r"); + const size = statSync(file).size; + const buffer = Buffer.alloc(Math.min(size, MAX_SCANNED_BYTES)); + const read = readSync(fd, buffer, 0, buffer.length, 0); + return { + text: buffer.toString("utf-8", 0, read), + whole: size <= MAX_SCANNED_BYTES, + }; + } catch { + return null; + } finally { + if (fd !== undefined) closeSync(fd); + } +} + +const isPageOrStylesheet = (path: string) => /\.(?:html?|css)$/i.test(path); + +interface OutsideReferences { + assets: Set; + compositions: Set; + complete: boolean; +} + +// A composition resolves as the render compiler resolves it; one that leaves the project resolves as written. +function referencePath(ref: Reference, file: string, projectDir: string): string | null { + if (!ref.composition) return localPath(ref.raw, dirname(file)); + try { + return resolveCompositionSrc(projectDir, ref.raw) ?? localPath(ref.raw, projectDir); + } catch { + return null; + } +} + +function referencesIn(found: OutsideReferences, file: string): Reference[] { + const read = readHead(file); + if (read === null || !read.whole) found.complete = false; + if (read === null) return []; + return /\.css$/i.test(file) ? cssReferences(read.text) : htmlReferences(read.text); +} + +function noteReference( + found: OutsideReferences, + ref: Reference, + path: string, + projectDir: string, +): string | null { + try { + if (linksOutside(projectDir, path)) { + (ref.composition ? found.compositions : found.assets).add(ref.raw); + return null; + } + } catch { + found.complete = false; + return null; + } + return ref.follow && isPageOrStylesheet(path) ? path : null; +} + +function scanFile(found: OutsideReferences, file: string, projectDir: string): string[] { + const next: string[] = []; + for (const ref of referencesIn(found, file)) { + const path = referencePath(ref, file, projectDir); + if (path === null || !isFile(path)) continue; + const follow = noteReference(found, ref, path, projectDir); + if (follow !== null) next.push(follow); + } + return next; +} + +// Names the references outside the project in the entry and the compositions and stylesheets it pulls in; only the notice reads it. +function outsideReferences(projectDir: string, entryFile: string): OutsideReferences { + const found: OutsideReferences = { + assets: new Set(), + compositions: new Set(), + complete: true, + }; + const visited = new Set(); + const pending = [resolve(projectDir, entryFile)]; + for (let file = pending.pop(); file !== undefined; file = pending.pop()) { + const real = realOrNull(file); + if (real === null || visited.has(real) || !isPathInside(file, projectDir)) continue; + if (visited.size >= MAX_SCANNED_FILES) { + found.complete = false; + break; + } + visited.add(real); + pending.push(...scanFile(found, file, projectDir)); + } + return found; +} + +function refusedReferences(found: OutsideReferences): string { + const parts: string[] = []; + if (found.assets.size > 0) parts.push(` It leaves out ${[...found.assets].join(", ")}.`); + if (found.compositions.size > 0) { + parts.push( + ` It stops on the compositions ${[...found.compositions].join(", ")}; move them into the project to render them.`, + ); + } + if (!found.complete) parts.push(" Some files could not be checked for references."); + return parts.join(""); +} + +const SIGNATURE_BYTES = 16; + +const ascii = (bytes: Buffer, at: number, text: string) => + bytes.toString("latin1", at, at + text.length) === text; +const startsWith = (bytes: Buffer, ...values: number[]) => + values.every((value, index) => bytes[index] === value); +const isoMedia = (bytes: Buffer) => + ["ftyp", "moov", "mdat", "wide", "free", "skip"].some((box) => ascii(bytes, 4, box)); +const riff = (form: string) => (bytes: Buffer) => ascii(bytes, 0, "RIFF") && ascii(bytes, 8, form); +const ebml = (bytes: Buffer) => startsWith(bytes, 0x1a, 0x45, 0xdf, 0xa3); +const mpegAudio = (bytes: Buffer) => + ascii(bytes, 0, "ID3") || (bytes[0] === 0xff && ((bytes[1] ?? 0) & 0xe0) === 0xe0); +const adts = (bytes: Buffer) => + ascii(bytes, 0, "ADIF") || (bytes[0] === 0xff && ((bytes[1] ?? 0) & 0xf6) === 0xf0); +const avif = (bytes: Buffer) => + ascii(bytes, 4, "ftyp") && ["avif", "avis", "mif1"].some((brand) => ascii(bytes, 8, brand)); + +// Each media, font or image extension and the leading bytes a file of that type starts with. +const MEDIA_SIGNATURES: Record boolean> = { + ".mp4": isoMedia, + ".m4v": isoMedia, + ".m4a": isoMedia, + ".mov": isoMedia, + ".webm": ebml, + ".mkv": ebml, + ".mp3": mpegAudio, + ".wav": riff("WAVE"), + ".ogg": (bytes) => ascii(bytes, 0, "OggS"), + ".flac": (bytes) => ascii(bytes, 0, "fLaC"), + ".aac": adts, + ".png": (bytes) => startsWith(bytes, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a), + ".jpg": (bytes) => startsWith(bytes, 0xff, 0xd8, 0xff), + ".jpeg": (bytes) => startsWith(bytes, 0xff, 0xd8, 0xff), + ".gif": (bytes) => ascii(bytes, 0, "GIF87a") || ascii(bytes, 0, "GIF89a"), + ".webp": riff("WEBP"), + ".avif": avif, + ".woff": (bytes) => ascii(bytes, 0, "wOFF"), + ".woff2": (bytes) => ascii(bytes, 0, "wOF2"), + ".ttf": (bytes) => startsWith(bytes, 0x00, 0x01, 0x00, 0x00) || ascii(bytes, 0, "true"), + ".otf": (bytes) => ascii(bytes, 0, "OTTO"), +}; + +function firstBytes(path: string): Buffer | null { + let fd: number | undefined; + try { + fd = openSync(path, "r"); + const head = Buffer.alloc(SIGNATURE_BYTES); + return head.subarray(0, readSync(fd, head, 0, SIGNATURE_BYTES, 0)); + } catch { + return null; + } finally { + if (fd !== undefined) closeSync(fd); + } +} + +function isMediaFontOrImage(path: string): boolean { + const matches = MEDIA_SIGNATURES[extname(path).toLowerCase()]; + const bytes = matches && isFile(path) ? firstBytes(path) : null; + return bytes !== null && matches !== undefined && matches(bytes); +} + +const ALIAS_WALK_MAX_ENTRIES = 20_000; +const ALIAS_WALK_MAX_MS = 2_000; + +interface Target { + identity: string; + isDirectory: boolean; +} + +const NOTHING_THERE = new Set(["ENOENT", "ENOTDIR", "ELOOP"]); + +// What a path reaches with links followed; "missing" when nothing can be read through it, null when its identity is unknown. +function targetOf(path: string): Target | "missing" | null { + try { + const stat = statSync(path, { bigint: true }); + if (stat.ino === 0n) return null; + return { identity: `${stat.dev}:${stat.ino}`, isDirectory: stat.isDirectory() }; + } catch (error) { + return NOTHING_THERE.has((error as NodeJS.ErrnoException).code ?? "") ? "missing" : null; + } +} + +function receivedIdentities(listed: string[]): Set | null { + const identities = new Set(); + for (const path of listed) { + const target = targetOf(path); + if (target === null || target === "missing" || target.isDirectory) return null; + identities.add(target.identity); + } + return identities; +} + +function listDir(dir: string): Dirent[] | null { + try { + return readdirSync(dir, { withFileTypes: true }); + } catch { + return null; + } +} + +const isMediaName = (path: string) => MEDIA_SIGNATURES[extname(path).toLowerCase()] !== undefined; + +// A linked folder is checked when it leads to a folder inside the project, which the walk lists under its own name. +function linkedFolderChecked(path: string, realProjectDir: string): boolean { + const real = realOrNull(path); + return real !== null && isPathInside(real, realProjectDir); +} + +// The entry's path when it reaches a received file under a name that is not media; undefined when that cannot be told. +function receivedAlias( + dir: string, + entry: Dirent, + received: ReadonlySet, + realProjectDir: string, +): string | null | undefined { + const path = join(dir, entry.name); + if (isMediaName(path) && !entry.isSymbolicLink()) return null; + const target = targetOf(path); + if (target === "missing") return null; + if (target === null) return undefined; + if (target.isDirectory) return linkedFolderChecked(path, realProjectDir) ? null : undefined; + return !isMediaName(path) && received.has(target.identity) ? path : null; +} + +function walkBudget(): (entries: number) => boolean { + const deadline = Date.now() + ALIAS_WALK_MAX_MS; + let seen = 0; + return (entries) => { + seen += entries; + return seen <= ALIAS_WALK_MAX_ENTRIES && Date.now() <= deadline; + }; +} + +interface AliasWalk { + received: ReadonlySet; + realProjectDir: string; + pending: string[]; + aliases: string[]; +} + +// Queues the folder's subfolders and records its aliases; false when an entry cannot be checked. +function walkListing(walk: AliasWalk, dir: string, listing: Dirent[]): boolean { + for (const entry of listing) { + if (entry.isDirectory()) { + walk.pending.push(join(dir, entry.name)); + continue; + } + const alias = receivedAlias(dir, entry, walk.received, walk.realProjectDir); + if (alias === undefined) return false; + if (alias !== null) walk.aliases.push(alias); + } + return true; +} + +// Project files that reach a received file, links followed, under a name that is not media; null when the walk could not check every file, including one through a linked folder that leads outside the project. +function nonMediaAliases(projectDir: string, received: ReadonlySet): string[] | null { + const withinBudget = walkBudget(); + const realProjectDir = realOrNull(projectDir) ?? projectDir; + const walk: AliasWalk = { received, realProjectDir, pending: [projectDir], aliases: [] }; + for (let dir = walk.pending.pop(); dir !== undefined; dir = walk.pending.pop()) { + const listing = listDir(dir); + if (listing === null || !withinBudget(listing.length)) return null; + if (!walkListing(walk, dir, listing)) return null; + } + return walk.aliases; +} + +interface Refusal { + reason: string; + alwaysWarn: boolean; +} + +const READS_NOTHING_OUTSIDE = "so the render reads nothing outside the project."; + +function receivedRefusal( + projectDir: string, + record: { path: string; listed: string[] | null }, +): Refusal | null { + const shown = (path: string) => relative(realOrNull(projectDir) ?? projectDir, path); + if (record.listed === null) { + return { reason: `Could not read ${record.path}, ${READS_NOTHING_OUTSIDE}`, alwaysWarn: true }; + } + if (record.listed.length === 0) return null; + const nonMedia = record.listed.find((path) => !isMediaFontOrImage(path)); + if (nonMedia !== undefined) { + const reason = `${shown(nonMedia)} came from someone else and is not a media, font or image file, ${READS_NOTHING_OUTSIDE}`; + return { reason, alwaysWarn: false }; + } + const received = receivedIdentities(record.listed); + const aliases = received === null ? null : nonMediaAliases(projectDir, received); + if (aliases === null) { + const reason = `Could not check every project file for another name of a received file, ${READS_NOTHING_OUTSIDE}`; + return { reason, alwaysWarn: true }; + } + const [alias] = aliases; + if (alias === undefined) return null; + const reason = `${shown(alias)} is another name for a file that came from someone else, ${READS_NOTHING_OUTSIDE}`; + return { reason, alwaysWarn: false }; +} + +/** False when the received-files record cannot be read, lists a received file that is not positively media, a font or an image by extension and leading bytes, or a project file reaches a received file under another kind of name; `warn` then says why and names the references outside the project it refuses. */ +export function outsideAssetsAllowed( + projectDir: string, + entryFile: string | undefined, + warn: (line: string) => void, +): boolean { + const absProjectDir = resolve(projectDir); + const record = receivedRecordStatus(absProjectDir); + const refusal = record === null ? null : receivedRefusal(absProjectDir, record); + if (refusal === null) return true; + const refused = refusedReferences(outsideReferences(absProjectDir, entryFile ?? "index.html")); + if (refusal.alwaysWarn || refused !== "") warn(`${refusal.reason}${refused}`); + return false; +} diff --git a/packages/cli/src/utils/renderArgs.test.ts b/packages/cli/src/utils/renderArgs.test.ts index acad6444f2..768ae3253d 100644 --- a/packages/cli/src/utils/renderArgs.test.ts +++ b/packages/cli/src/utils/renderArgs.test.ts @@ -173,6 +173,8 @@ describe("parseCompositionEntryArg", () => { [resolve(PROJECT, "index.html")]: "file", [resolve(PROJECT, "compositions/intro.html")]: "file", [resolve(PROJECT, "compositions")]: "directory", + [resolve(PROJECT, "assets/scene.png")]: "file", + [resolve(PROJECT, "compositions/old.htm")]: "file", [PROJECT]: "directory", }); @@ -239,6 +241,13 @@ describe("parseCompositionEntryArg", () => { }); }); + it("accepts an .htm entry", () => { + expect(parseCompositionEntryArg("compositions/old.htm", PROJECT, stat)).toEqual({ + ok: true, + value: "compositions/old.htm", + }); + }); + it("rejects an absolute path outside the project", () => { const outsideStat = makeStat({ "/elsewhere/x.html": "file", [PROJECT]: "directory" }); const err = expectCompositionErr( @@ -251,6 +260,7 @@ describe("parseCompositionEntryArg", () => { ["../escape.html", "outside-project", "/escape.html"], ["missing.html", "not-found", "/proj/missing.html"], ["compositions", "not-a-file", "/proj/compositions"], + ["assets/scene.png", "not-html", "/proj/assets/scene.png"], ])("names the resolved entry path and project directory for %s", (raw, kind, entryPath) => { const err = expectCompositionErr(parseCompositionEntryArg(raw, PROJECT, stat)); expect(err).toEqual({ diff --git a/packages/cli/src/utils/staticProjectServer.test.ts b/packages/cli/src/utils/staticProjectServer.test.ts index e4e92eaad5..f63e8b36b0 100644 --- a/packages/cli/src/utils/staticProjectServer.test.ts +++ b/packages/cli/src/utils/staticProjectServer.test.ts @@ -159,6 +159,17 @@ describe("serveStaticProjectHtml colour-tagged PNGs", () => { }); }); +describe("serveStaticProjectHtml content types", () => { + it("tells the browser not to sniff a type, so an image cannot load as a script or stylesheet", async () => { + const { url } = await serveWith(Buffer.from("window.ran = true;"), "scene.png"); + for (const path of ["", "scene.png", "missing.js"]) { + const response = await fetch(`${url}${path}`); + expect(response.headers.get("x-content-type-options"), path).toBe("nosniff"); + } + expect((await fetch(`${url}scene.png`)).headers.get("content-type")).toBe("image/png"); + }); +}); + describe("serveStaticProjectHtml range support", () => { it("answers a Range request with 206 + the requested byte slice", async () => { // Chromium needs byte-range seekability or WAV `.duration` reports Infinity, diff --git a/packages/cli/src/utils/staticProjectServer.ts b/packages/cli/src/utils/staticProjectServer.ts index 177c09ca4a..298ef78c2a 100644 --- a/packages/cli/src/utils/staticProjectServer.ts +++ b/packages/cli/src/utils/staticProjectServer.ts @@ -305,6 +305,7 @@ export async function serveStaticProjectHtml( // fallow-ignore-next-line complexity const server = createServer((req, res) => { + res.setHeader("X-Content-Type-Options", "nosniff"); const url = req.url ?? "/"; if (url === "/" || url === "/index.html") { res.writeHead(200, { "Content-Type": getMimeType("index.html") }); diff --git a/packages/core/src/runtime/compositionLoader.test.ts b/packages/core/src/runtime/compositionLoader.test.ts index 00977798b2..5de13420bd 100644 --- a/packages/core/src/runtime/compositionLoader.test.ts +++ b/packages/core/src/runtime/compositionLoader.test.ts @@ -437,6 +437,25 @@ describe("loadCompositions external hosts", () => { ); }); + it.each(["https://example.com/scene.png", "https://example.com/scene.mp4?x=.html", "scene"])( + "never fetches a data-composition-src that is not an .html file: %s", + async (src) => { + const host = document.createElement("div"); + host.setAttribute("data-composition-src", src); + document.body.appendChild(host); + const fetchSpy = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue(new Response("

media as page

")); + const onDiagnostic = vi.fn(); + await loadCompositions({ ...createLoaderParams(), onDiagnostic }); + expect(fetchSpy).not.toHaveBeenCalled(); + expect(host.innerHTML).not.toContain("media as page"); + expect(onDiagnostic).toHaveBeenCalledWith( + expect.objectContaining({ code: "external_composition_load_failed" }), + ); + }, + ); + it("calls onDiagnostic when HTTP response is not ok", async () => { const host = document.createElement("div"); host.setAttribute("data-composition-src", "https://example.com/404.html"); diff --git a/packages/core/src/runtime/compositionLoader.ts b/packages/core/src/runtime/compositionLoader.ts index d547b61047..62a5524083 100644 --- a/packages/core/src/runtime/compositionLoader.ts +++ b/packages/core/src/runtime/compositionLoader.ts @@ -793,6 +793,9 @@ async function mountExternalCompositions( onDiagnostic: params.onDiagnostic, }); } + if (!compositionUrl || !/\.html?$/i.test(compositionUrl.pathname)) { + throw new Error("data-composition-src must name an .html file"); + } const response = await fetch(src); if (!response.ok) { throw new Error(`HTTP ${response.status}`); diff --git a/packages/producer/src/services/fileServer.test.ts b/packages/producer/src/services/fileServer.test.ts index af9d3a9fc0..dfa1340a03 100644 --- a/packages/producer/src/services/fileServer.test.ts +++ b/packages/producer/src/services/fileServer.test.ts @@ -405,6 +405,33 @@ describe("createFileServer", () => { } }); + it("tells the browser not to sniff a type, so an image cannot load as a script or stylesheet", async () => { + const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-nosniff-")); + try { + writeEmptyIndex(projectDir); + writeFileSync(join(projectDir, "scene.png"), "window.ran = true;"); + writeFileSync(join(projectDir, "scene.js"), "window.ran = true;"); + const server = await createFileServer({ projectDir, preHeadScripts: [], headScripts: [] }); + try { + for (const [name, type] of [ + ["scene.png", "image/png"], + ["scene.js", "application/javascript; charset=utf-8"], + ["index.html", "text/html; charset=utf-8"], + ]) { + const response = await fetch(`${server.url}/${name}`); + expect(response.headers.get("content-type")).toBe(type); + expect(response.headers.get("x-content-type-options")).toBe("nosniff"); + } + const missing = await fetch(`${server.url}/missing.js`); + expect(missing.headers.get("x-content-type-options")).toBe("nosniff"); + } finally { + server.close(); + } + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); + it("serves ES modules with a JavaScript MIME type", async () => { const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-mjs-")); try { diff --git a/packages/producer/src/services/fileServer.ts b/packages/producer/src/services/fileServer.ts index 443a3439d3..ac993bea26 100644 --- a/packages/producer/src/services/fileServer.ts +++ b/packages/producer/src/services/fileServer.ts @@ -99,6 +99,7 @@ const MIME_TYPES: Record = { ".css": "text/css; charset=utf-8", ".js": "application/javascript; charset=utf-8", ".mjs": "application/javascript; charset=utf-8", + ".cjs": "application/javascript; charset=utf-8", ".json": "application/json; charset=utf-8", ".cube": "text/plain; charset=utf-8", ".png": "image/png", @@ -817,6 +818,11 @@ export function createFileServer(options: FileServerOptions): Promise console.warn(`[FileServer] ${line}`); + app.use(async function loadEachFileOnlyAsItsNamedType(c, next) { + await next(); + c.res.headers.set("X-Content-Type-Options", "nosniff"); + }); + app.get("/*", async (c) => { let requestPath = c.req.path; if (requestPath === "/") requestPath = "/index.html"; diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index d9a784a990..342f6edc9c 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -1082,19 +1082,14 @@ describe("detectRenderModeHints", () => { it("compileForRender aborts naming a data-composition-src that points at a folder", async () => { const projectDir = makeSubCompProject( "hf-folder-subcomp-", - [{ id: "intro", src: "compositions/intro" }], + [{ id: "intro", src: "compositions/intro.html" }], {}, ); try { - mkdirSync(join(projectDir, "compositions", "intro")); - writeFileSync( - join(projectDir, "compositions", "intro", "index.html"), - validSubCompHtml("intro", "Intro"), - ); - + mkdirSync(join(projectDir, "compositions", "intro.html")); await expect( compileForRender(projectDir, join(projectDir, "index.html"), projectDir), - ).rejects.toThrow(/compositions\/intro[\s\S]*a folder, not an HTML file/); + ).rejects.toThrow(/compositions\/intro\.html[\s\S]*a folder, not an HTML file/); } finally { rmSync(projectDir, { recursive: true, force: true }); } @@ -1171,6 +1166,61 @@ describe("detectRenderModeHints", () => { } }); + it("compileForRender refuses an entry that is not an .html file, and renders the same page named .html", async () => { + const projectDir = makeSubCompProject("hf-media-entry-", [], {}); + try { + const page = readFileSync(join(projectDir, "index.html"), "utf-8"); + writeFileSync(join(projectDir, "scene.png"), page); + writeFileSync(join(projectDir, "scene.html"), page); + await expect( + compileForRender(projectDir, join(projectDir, "scene.png"), projectDir), + ).rejects.toThrow(/scene\.png is not an \.html file, so it is refused/); + const compiled = await compileForRender( + projectDir, + join(projectDir, "scene.html"), + projectDir, + ); + expect(compiled.html).toContain('data-composition-id="main"'); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }); + + it("compileForRender checks containment before the file type of a data-composition-src", async () => { + const projectDir = makeSubCompProject( + "hf-outside-media-subcomp-", + [{ id: "intro", src: "../outside-scene.png" }], + {}, + ); + const outside = join(dirname(projectDir), "outside-scene.png"); + writeFileSync(outside, validSubCompHtml("intro", "Outside")); + try { + await expect( + compileForRender(projectDir, join(projectDir, "index.html"), projectDir), + ).rejects.toThrow(/outside-scene\.png: it is outside the project folder/); + } finally { + rmSync(outside, { force: true }); + rmSync(projectDir, { recursive: true, force: true }); + } + }); + + it.each(["compositions/scene.png", "/compositions/scene.png", "compositions/scene.mp4"])( + "compileForRender refuses a data-composition-src that is not an .html file: %s", + async (src) => { + const name = src.split("/").pop() ?? ""; + const projectDir = makeSubCompProject("hf-media-subcomp-", [{ id: "intro", src }], { + [name]: validSubCompHtml("intro", "Media named"), + }); + try { + await expect( + compileForRender(projectDir, join(projectDir, "index.html"), projectDir), + ).rejects.toThrow(/not an \.html file/); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }, + ); + it.each([ ["dot-dot segments", "../outside-scene.html"], ["an encoded ../", "%2e%2e%2foutside-scene.html"], diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index be0da6a630..50431ad965 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -249,7 +249,10 @@ function normalizeCompositionSrcs(html: string, projectDir: string): string { }); } -function resolveCompositionFile( +const COMPOSITION_FILE = /\.html?$/i; +const NOT_A_COMPOSITION_FILE = "it is not an .html file, so it is refused"; + +function resolveCompositionPath( projectDir: string, src: string, assetRoot: string | undefined, @@ -258,6 +261,15 @@ function resolveCompositionFile( return file === null || escapesAssetRoot(file, assetRoot) ? null : file; } +function resolveCompositionFile( + projectDir: string, + src: string, + assetRoot: string | undefined, +): string | null { + const file = resolveCompositionPath(projectDir, src, assetRoot); + return file !== null && COMPOSITION_FILE.test(file) ? file : null; +} + // Git metadata and env files are never inlined into a render, as the preview bundler refuses them. const isPrivateCompositionFile = (projectDir: string, filePath: string): boolean => isPrivateFileUnder(projectDir, filePath, isVcsOrSecretPath); @@ -292,7 +304,7 @@ function assertSubCompositionsUsable( if (!srcPath) continue; if (isUnresolvedAssetPlaceholder(srcPath)) continue; // __UPPER__ placeholder or unresolved templating token — not a real reference (shared with lint via @hyperframes/parsers) - const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot); + const filePath = resolveCompositionPath(projectDir, srcPath, assetRoot); if (filePath === null) { problems.push({ srcPath, detail: "it is outside the project folder" }); continue; @@ -305,6 +317,10 @@ function assertSubCompositionsUsable( problems.push({ srcPath, detail: "it is git metadata or an env file" }); continue; } + if (!COMPOSITION_FILE.test(filePath)) { + problems.push({ srcPath, detail: NOT_A_COMPOSITION_FILE }); + continue; + } // Circular reference guard. parseSubCompositions (below) silently // `continue`s on a repeat visit with no reporting at all — mirror that // silence here rather than pretend it surfaces an error somewhere else. @@ -2213,6 +2229,9 @@ export async function compileForRender( if (isPathInside(htmlPath, projectDir) && escapesAssetRoot(htmlPath, assetRoot)) { throw new Error(`Entry file ${htmlPath} leads outside the project folder`); } + if (!COMPOSITION_FILE.test(htmlPath)) { + throw new Error(`Entry file ${htmlPath} is not an .html file, so it is refused`); + } const entryHtml = rebaseDirectEntryAssetPaths(readTextFile(htmlPath), projectDir, htmlPath); const rawHtml = bindVariableMediaSources( normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir), diff --git a/packages/studio-server/src/createStudioApi.originGuard.test.ts b/packages/studio-server/src/createStudioApi.originGuard.test.ts index 077d4bdb51..1c03ad9f41 100644 --- a/packages/studio-server/src/createStudioApi.originGuard.test.ts +++ b/packages/studio-server/src/createStudioApi.originGuard.test.ts @@ -6,10 +6,10 @@ // so it needs Node's fetch classes, which keep them. import { afterEach, describe, expect, it } from "vitest"; import { existsSync, mkdtempSync, rmSync } from "node:fs"; -import { tmpdir } from "node:os"; +import { networkInterfaces, tmpdir } from "node:os"; import { join } from "node:path"; import { createStudioApi } from "./createStudioApi"; -import { previewAllowedHostsFromEnv } from "./helpers/requestIdentity"; +import { previewAllowedHostsFromEnv, wildcardBindHostAllowed } from "./helpers/requestIdentity"; import { stubAdapter } from "./routes/stubAdapter.test-helpers"; const tempDirs: string[] = []; @@ -320,3 +320,32 @@ describe("previewAllowedHostsFromEnv", () => { expect(previewAllowedHostsFromEnv({ HYPERFRAMES_PREVIEW_ALLOWED_HOSTS: " " })).toEqual([]); }); }); + +describe("wildcardBindHostAllowed", () => { + it("allows loopback names and this machine's interface addresses", () => { + expect(wildcardBindHostAllowed("localhost:3003")).toBe(true); + expect(wildcardBindHostAllowed("127.0.0.1")).toBe(true); + const address = Object.values(networkInterfaces()) + .flatMap((entries) => entries ?? []) + .find((entry) => entry.family === "IPv4")?.address; + if (address) expect(wildcardBindHostAllowed(`${address}:3003`)).toBe(true); + }); + + it("refuses other names unless they are listed", () => { + expect(wildcardBindHostAllowed("rebound.example:3003")).toBe(false); + expect(wildcardBindHostAllowed("")).toBe(false); + expect(wildcardBindHostAllowed(undefined)).toBe(false); + expect(wildcardBindHostAllowed("demo.tunnels.example", ["demo.tunnels.example"])).toBe(true); + }); + + it("ignores the preview bind setting", () => { + const saved = process.env["HYPERFRAMES_PREVIEW_HOST"]; + process.env["HYPERFRAMES_PREVIEW_HOST"] = "rebound.example"; + try { + expect(wildcardBindHostAllowed("rebound.example")).toBe(false); + } finally { + if (saved === undefined) delete process.env["HYPERFRAMES_PREVIEW_HOST"]; + else process.env["HYPERFRAMES_PREVIEW_HOST"] = saved; + } + }); +}); diff --git a/packages/studio-server/src/helpers/mime.ts b/packages/studio-server/src/helpers/mime.ts index 2ef35faf3a..8003cd9f5d 100644 --- a/packages/studio-server/src/helpers/mime.ts +++ b/packages/studio-server/src/helpers/mime.ts @@ -3,6 +3,7 @@ export const MIME_TYPES: Record = { ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".mjs": "text/javascript; charset=utf-8", + ".cjs": "text/javascript; charset=utf-8", ".json": "application/json; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", diff --git a/packages/studio-server/src/helpers/requestIdentity.ts b/packages/studio-server/src/helpers/requestIdentity.ts index 7de237146f..4853879383 100644 --- a/packages/studio-server/src/helpers/requestIdentity.ts +++ b/packages/studio-server/src/helpers/requestIdentity.ts @@ -76,13 +76,16 @@ export function identityAllowed(host: string | undefined): boolean { return hostMatchesBind(host, bind); } -function hostAllowed(host: string, allowedHosts: readonly string[] | undefined): boolean { - if (identityAllowed(host)) return true; +function inAllowedHosts(host: string, allowedHosts: readonly string[] | undefined): boolean { if (!allowedHosts) return false; const name = hostnameOf(host); return name !== "" && allowedHosts.some((allowed) => hostnameOf(allowed) === name); } +function hostAllowed(host: string, allowedHosts: readonly string[] | undefined): boolean { + return identityAllowed(host) || inAllowedHosts(host, allowedHosts); +} + // Reads included. Only Host counts: a page can set X-Forwarded-Host, and a read has no Origin to check. export function hostHeaderAllowed( host: string | undefined, @@ -91,6 +94,16 @@ export function hostHeaderAllowed( return host !== undefined && host !== "" && hostAllowed(host, allowedHosts); } +export function wildcardBindHostAllowed( + host: string | undefined, + allowedHosts?: readonly string[], +): boolean { + if (host === undefined || host === "") return false; + return ( + isLoopbackHost(host) || hostMatchesBind(host, "0.0.0.0") || inAllowedHosts(host, allowedHosts) + ); +} + function firstForwarded(value: string | undefined): string | undefined { const first = value?.split(",")[0]?.trim(); return first === "" ? undefined : first; diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index ffc9a0be13..f55d3262f8 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -37,7 +37,7 @@ }, "@hyperframes/producer": { "packed": 901000, - "unpacked": 3245000 + "unpacked": 3290000 }, "@hyperframes/sdk": { "packed": 90662,