diff --git a/packages/cli/src/utils/staticProjectServer.test.ts b/packages/cli/src/utils/staticProjectServer.test.ts
index 3a8f662a95..dd15a9e5dd 100644
--- a/packages/cli/src/utils/staticProjectServer.test.ts
+++ b/packages/cli/src/utils/staticProjectServer.test.ts
@@ -729,6 +729,46 @@ describe("serveStaticProjectHtml private project files", () => {
);
});
+describe("serveStaticProjectHtml page another site loads", () => {
+ const MARK = '';
+ const SANDBOX = "sandbox allow-scripts";
+
+ async function serveProject(): Promise {
+ dir = mkdtempSync(join(tmpdir(), "hf-static-site-"));
+ writeFileSync(join(dir, "scene.html"), "scene");
+ server = await serveStaticProjectHtml(
+ dir,
+ "composition",
+ );
+ return server.url;
+ }
+
+ it.each(["", "scene.html"])(
+ "marks and sandboxes /%s when another site frames it",
+ async (path) => {
+ const url = await serveProject();
+ const res = await fetch(`${url}${path}`, {
+ headers: { "Sec-Fetch-Site": "cross-site", "Sec-Fetch-Dest": "iframe" },
+ });
+ expect(res.status).toBe(200);
+ expect(await res.text()).toContain(MARK);
+ expect(res.headers.get("content-security-policy")).toContain(SANDBOX);
+ },
+ );
+
+ it.each(["none", "same-origin"])(
+ "leaves the capture page unmarked for Sec-Fetch-Site %s",
+ async (site) => {
+ const url = await serveProject();
+ for (const path of ["", "scene.html"]) {
+ const res = await fetch(`${url}${path}`, { headers: { "Sec-Fetch-Site": site } });
+ expect(await res.text(), path).not.toContain(MARK);
+ expect(res.headers.get("content-security-policy"), path).not.toContain(SANDBOX);
+ }
+ },
+ );
+});
+
function writeToolRecords(projectDir: string): void {
mkdirSync(join(projectDir, ".hyperframes", "prepared-assets"), { recursive: true });
for (const name of ["app-history.jsonl", "agent-handoff.json", "comments.jsonl"]) {
diff --git a/packages/cli/src/utils/staticProjectServer.ts b/packages/cli/src/utils/staticProjectServer.ts
index dea30b9165..56775aeb3e 100644
--- a/packages/cli/src/utils/staticProjectServer.ts
+++ b/packages/cli/src/utils/staticProjectServer.ts
@@ -1,7 +1,15 @@
-import { createServer, request as httpRequest, type Server, type ServerResponse } from "node:http";
+import {
+ createServer,
+ request as httpRequest,
+ type IncomingMessage,
+ type Server,
+ type ServerResponse,
+} from "node:http";
import { Duplex } from "node:stream";
import { createReadStream, existsSync, readFileSync, statSync } from "node:fs";
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
+import { injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document";
+import { PREVIEW_FOR_ANOTHER_SITE_META } from "@hyperframes/core/studio-preview-mark";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { isAbsolute, relative, resolve } from "node:path";
import {
@@ -11,6 +19,7 @@ import {
} from "@hyperframes/core";
import { getMimeType } from "@hyperframes/core/studio-api";
import { decodeWellFormedEscapes, openRangeEnd } from "@hyperframes/studio-server";
+import { requestFromAnotherSite } from "@hyperframes/studio-server/request-identity";
import { findStageProxyBox } from "./compositionViewport.js";
import { resolveAutoProxy } from "./projectConfig.js";
import { injectMediaCodecMap } from "./compositionServer.js";
@@ -281,6 +290,30 @@ async function serveProxyRequest(
}
}
+function markForAnotherSite(html: string): string {
+ return injectTagsAtHeadStart(html, ``);
+}
+
+/** A load by another site is sandboxed, and the documents it gets are marked so their runtime offers it no editing port. */
+function loadedByAnotherSite(req: IncomingMessage, res: ServerResponse): boolean {
+ const anotherSite = requestFromAnotherSite({
+ fetchSite: req.headers["sec-fetch-site"] as string | undefined,
+ host: req.headers.host,
+ referer: req.headers.referer,
+ });
+ if (anotherSite) {
+ const policies = [res.getHeader("Content-Security-Policy"), "sandbox allow-scripts"];
+ res.setHeader("Content-Security-Policy", policies.filter((p) => p !== undefined).map(String));
+ }
+ return anotherSite;
+}
+
+function serveMarkedDocument(filePath: string, res: ServerResponse): void {
+ const body = markForAnotherSite(readFileSync(filePath, "utf-8"));
+ res.writeHead(200, { "Content-Type": getMimeType(filePath) });
+ res.end(body);
+}
+
export async function serveStaticProjectHtml(
projectDir: string,
html: string,
@@ -310,6 +343,7 @@ export async function serveStaticProjectHtml(
findStageProxyBox(html),
)
: html;
+ const markedHtml = markForAnotherSite(servedHtml);
let listeningPort = -1;
let inProcessHost: string | undefined;
@@ -328,10 +362,11 @@ export async function serveStaticProjectHtml(
for (const [name, value] of Object.entries(SAME_ORIGIN_FRAMING_HEADERS)) {
res.setHeader(name, value);
}
+ const anotherSite = loadedByAnotherSite(req, res);
const url = req.url ?? "/";
if (url === "/" || url === "/index.html") {
res.writeHead(200, { "Content-Type": getMimeType("index.html") });
- res.end(servedHtml);
+ res.end(anotherSite ? markedHtml : servedHtml);
return;
}
@@ -368,6 +403,8 @@ export async function serveStaticProjectHtml(
copies.signal,
proxyLimits.get(filePath),
);
+ } else if (anotherSite && getMimeType(filePath).startsWith("text/html")) {
+ serveMarkedDocument(filePath, res);
} else {
if (!serveUntaggedPng(filePath, res))
serveFileWithRange(filePath, req.headers.range, res);
diff --git a/packages/engine/src/services/fileServer.file-race.test.ts b/packages/engine/src/services/fileServer.file-race.test.ts
index c934e2292e..055401b756 100644
--- a/packages/engine/src/services/fileServer.file-race.test.ts
+++ b/packages/engine/src/services/fileServer.file-race.test.ts
@@ -70,6 +70,9 @@ describe("file server checked reads", () => {
fs.rmSync(root, { recursive: true, force: true });
});
+ const unmarked = (text: string) =>
+ text.replace(/\n?\n?/, "");
+
function expectClosed() {
// HTTP sockets can reuse an already-closed fd before fetch resolves.
// Track successful real closes instead of probing stale descriptor numbers.
@@ -96,7 +99,7 @@ describe("file server checked reads", () => {
server = await createFileServer({ projectDir, compiledDir });
const response = await fetch(`${server.url}/${name}`);
expect(response.status).toBe(200);
- expect(await response.text()).toBe("checked bytes");
+ expect(unmarked(await response.text())).toBe("checked bytes");
expectClosed();
});
@@ -193,7 +196,7 @@ describe("file server checked reads", () => {
expect(text).toContain("compiled");
expect(text).toContain("window.headTest = 1;");
expect(text).toContain("window.bodyTest = 1;");
- expect(await (await fetch(`${server.url}/other.html`)).text()).toBe(html);
+ expect(unmarked(await (await fetch(`${server.url}/other.html`)).text())).toBe(html);
expectClosed();
});
diff --git a/packages/engine/src/services/fileServer.test.ts b/packages/engine/src/services/fileServer.test.ts
index c097228f0f..c561c5e6ed 100644
--- a/packages/engine/src/services/fileServer.test.ts
+++ b/packages/engine/src/services/fileServer.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { networkInterfaces, tmpdir } from "node:os";
import { join } from "node:path";
+import { get as httpGet } from "node:http";
import { createFileServer } from "./fileServer.js";
describe("file server media responses", () => {
@@ -61,3 +62,118 @@ describe("file server listener", () => {
}
});
});
+
+function getWithHost(url: string, host: string): Promise<{ status: number; body: string }> {
+ return new Promise((resolveGet, rejectGet) => {
+ httpGet(url, { headers: { host } }, (res) => {
+ let body = "";
+ res.setEncoding("utf-8");
+ res.on("data", (chunk: string) => (body += chunk));
+ res.on("end", () => resolveGet({ status: res.statusCode ?? 0, body }));
+ }).on("error", rejectGet);
+ });
+}
+
+async function withProject(
+ options: { hostname?: string },
+ run: (server: Awaited>) => Promise,
+): Promise {
+ const projectDir = mkdtempSync(join(tmpdir(), "hf-engine-host-"));
+ try {
+ writeFileSync(
+ join(projectDir, "index.html"),
+ "composition",
+ );
+ writeFileSync(join(projectDir, "scene.html"), "scene");
+ writeFileSync(join(projectDir, "asset.txt"), "project-file");
+ const server = await createFileServer({ projectDir, ...options });
+ try {
+ await run(server);
+ } finally {
+ server.close();
+ }
+ } finally {
+ rmSync(projectDir, { recursive: true, force: true });
+ }
+}
+
+describe("file server request Host", () => {
+ it.each(["/", "/index.html", "/asset.txt"])(
+ "refuses %s to a page name that is not loopback",
+ async (path) => {
+ await withProject({}, async (server) => {
+ const res = await getWithHost(`${server.url}${path}`, `rebound.example:${server.port}`);
+ expect(res.status).toBe(403);
+ expect(res.body).not.toContain("composition");
+ expect(res.body).not.toContain("project-file");
+ });
+ },
+ );
+
+ it.each(["localhost", "127.0.0.1", "[::1]"])(
+ "answers the capture browser on %s",
+ async (name) => {
+ await withProject({}, async (server) => {
+ const res = await getWithHost(`${server.url}/asset.txt`, `${name}:${server.port}`);
+ expect(res.status).toBe(200);
+ expect(res.body).toBe("project-file");
+ });
+ },
+ );
+
+ it("answers the hostname it was bound to and still refuses other names", async () => {
+ const external = firstExternalIpv4();
+ if (!external) return;
+ await withProject({ hostname: external }, async (server) => {
+ const own = await fetch(`http://${external}:${server.port}/asset.txt`);
+ expect(await own.text()).toBe("project-file");
+ const rebound = await getWithHost(
+ `http://${external}:${server.port}/asset.txt`,
+ `rebound.example:${server.port}`,
+ );
+ expect(rebound.status).toBe(403);
+ });
+ });
+
+ it("answers this machine's own address on a wildcard bind and refuses other names", async () => {
+ const external = firstExternalIpv4();
+ if (!external) return;
+ await withProject({ hostname: "0.0.0.0" }, async (server) => {
+ const own = await fetch(`http://${external}:${server.port}/asset.txt`);
+ expect(await own.text()).toBe("project-file");
+ const rebound = await getWithHost(
+ `${server.url}/asset.txt`,
+ `rebound.example:${server.port}`,
+ );
+ expect(rebound.status).toBe(403);
+ });
+ });
+});
+
+describe("file server page editing agent", () => {
+ it.each(["index.html", "scene.html"])(
+ "marks %s so its runtime offers a framing site no editing port",
+ async (name) => {
+ await withProject({}, async (server) => {
+ const res = await fetch(`${server.url}/${name}`);
+ expect(res.status).toBe(200);
+ expect(await res.text()).toContain('');
+ });
+ },
+ );
+});
+
+describe("file server IPv6 wildcard bind", () => {
+ it("answers loopback and refuses other names when bound to ::", async () => {
+ await withProject({ hostname: "::" }, async (server) => {
+ expect((await getWithHost(`${server.url}/asset.txt`, `[::1]:${server.port}`)).status).toBe(
+ 200,
+ );
+ const rebound = await getWithHost(
+ `${server.url}/asset.txt`,
+ `rebound.example:${server.port}`,
+ );
+ expect(rebound.status).toBe(403);
+ });
+ });
+});
diff --git a/packages/engine/src/services/fileServer.ts b/packages/engine/src/services/fileServer.ts
index aba5a74af8..b5b215c5a4 100644
--- a/packages/engine/src/services/fileServer.ts
+++ b/packages/engine/src/services/fileServer.ts
@@ -10,7 +10,10 @@ import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { readFileSync, openSync, fstatSync, closeSync, statSync, constants } from "node:fs";
import { join, extname } from "node:path";
+import { hostname as machineName, networkInterfaces } from "node:os";
import { injectScriptsIntoHtml } from "@hyperframes/core/compiler";
+import { injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document";
+import { PREVIEW_FOR_ANOTHER_SITE_META } from "@hyperframes/core/studio-preview-mark";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution";
import { answerOwnLoopbackHostOnly } from "../utils/loopbackOnly.js";
@@ -108,6 +111,37 @@ function resolveContainedPaths(
return { projectPath, compiledPath };
}
+const OFFER_NO_FRAMING_SITE_AN_EDITING_PORT = ``;
+
+function nameInHost(host: string): string {
+ const bracketed = /^\[([^\]]+)\]/.exec(host);
+ return (bracketed ? bracketed[1] : host.split(":")[0])?.toLowerCase() ?? "";
+}
+
+function isLoopbackName(name: string): boolean {
+ return name === "localhost" || name === "::1" || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(name);
+}
+
+function thisMachinesNames(): Set {
+ const names = new Set([machineName().toLowerCase()]);
+ for (const entries of Object.values(networkInterfaces())) {
+ for (const entry of entries ?? []) names.add(entry.address.toLowerCase());
+ }
+ return names;
+}
+
+/** Loopback, the bound name, or on a wildcard bind this machine's own addresses: never a name a page can point here. */
+function hostAllowedFor(bound: string): (host: string | undefined) => boolean {
+ const boundName = bound.replace(/^\[|\]$/g, "").toLowerCase();
+ const wildcard = boundName === "0.0.0.0" || boundName === "::";
+ return (host) => {
+ const name = host ? nameInHost(host) : "";
+ if (name === "") return false;
+ if (isLoopbackName(name) || name === boundName) return true;
+ return wildcard && thisMachinesNames().has(name);
+ };
+}
+
export function createFileServer(options: FileServerOptions): Promise {
const {
projectDir,
@@ -124,6 +158,13 @@ export function createFileServer(options: FileServerOptions): Promise listeningPort);
+ if (!boundToLoopbackOnly) {
+ const hostAllowed = hostAllowedFor(hostname);
+ app.use(async function refuseHostNotServedHere(c, next) {
+ if (hostAllowed(c.req.header("host") ?? new URL(c.req.url).host)) return next();
+ return c.text("Forbidden: unknown host", 403);
+ });
+ }
app.get("/*", (c) => {
let requestPath = c.req.path;
@@ -150,7 +191,9 @@ export function createFileServer(options: FileServerOptions): Promise {
);
});
+function getWithHost(url: string, host: string): Promise<{ status: number; body: string }> {
+ return new Promise((resolveGet, rejectGet) => {
+ httpGet(url, { headers: { host } }, (res) => {
+ let body = "";
+ res.setEncoding("utf-8");
+ res.on("data", (chunk: string) => (body += chunk));
+ res.on("end", () => resolveGet({ status: res.statusCode ?? 0, body }));
+ }).on("error", rejectGet);
+ });
+}
+
+describe("createFileServer request Host", () => {
+ it.each(["/", "/index.html", "/asset.txt"])(
+ "refuses %s to a page name that is not loopback",
+ async (path) => {
+ const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-host-"));
+ try {
+ writeFileSync(join(projectDir, "index.html"), "composition");
+ writeFileSync(join(projectDir, "asset.txt"), "project-file");
+ await withFileServer(projectDir, async (server) => {
+ const res = await getWithHost(`${server.url}${path}`, `rebound.example:${server.port}`);
+ expect(res.status).toBe(403);
+ expect(res.body).not.toContain("composition");
+ expect(res.body).not.toContain("project-file");
+ });
+ } finally {
+ rmSync(projectDir, { recursive: true, force: true });
+ }
+ },
+ );
+
+ it.each(["localhost", "127.0.0.1", "[::1]"])(
+ "answers the capture browser on %s",
+ async (name) => {
+ const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-host-"));
+ try {
+ writeFileSync(join(projectDir, "asset.txt"), "project-file");
+ await withFileServer(projectDir, async (server) => {
+ const res = await getWithHost(`${server.url}/asset.txt`, `${name}:${server.port}`);
+ expect(res.status).toBe(200);
+ expect(res.body).toBe("project-file");
+ });
+ } finally {
+ rmSync(projectDir, { recursive: true, force: true });
+ }
+ },
+ );
+});
+
+describe("createFileServer page editing agent", () => {
+ it.each(["index.html", "scene.html"])(
+ "marks %s so its runtime offers a framing site no editing port",
+ async (name) => {
+ const projectDir = mkdtempSync(join(tmpdir(), "hf-file-server-mark-"));
+ try {
+ writeFileSync(join(projectDir, name), "");
+ await withFileServer(projectDir, async (server) => {
+ const res = await fetch(`${server.url}/${name}`);
+ expect(res.status).toBe(200);
+ expect(await res.text()).toContain('');
+ });
+ } finally {
+ rmSync(projectDir, { recursive: true, force: true });
+ }
+ },
+ );
+});
+
describe("createFileServer tool records", () => {
it.each([
".hyperframes/app-history.jsonl",
diff --git a/packages/producer/src/services/fileServer.ts b/packages/producer/src/services/fileServer.ts
index 62e115f1c8..7388001b5e 100644
--- a/packages/producer/src/services/fileServer.ts
+++ b/packages/producer/src/services/fileServer.ts
@@ -28,6 +28,8 @@ import { promisify } from "node:util";
import { Readable } from "node:stream";
import { join, extname, resolve, sep } from "node:path";
import { injectScriptsAtHeadStart, injectScriptsIntoHtml } from "@hyperframes/core/compiler";
+import { injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document";
+import { PREVIEW_FOR_ANOTHER_SITE_META } from "@hyperframes/core/studio-preview-mark";
import { fpsToNumber, type Fps } from "@hyperframes/core";
import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path";
import { stripSdrPngColourTags } from "@hyperframes/core/compiler/png-colour-tags";
@@ -731,6 +733,8 @@ export interface FileServerHandle {
addPreHeadScript: (script: string) => void;
}
+const OFFER_NO_FRAMING_SITE_AN_EDITING_PORT = ``;
+
/**
* Set before the Hyperframes runtime executes so render/probe pages can avoid
* preview-only initialization work that mutates the live visual timeline.
@@ -874,6 +878,7 @@ export function createFileServer(options: FileServerOptions): Promise