fix: static, producer and engine file servers check Host and mark runtime pages (#7852)

* fix(cli): the static project server answers only loopback and marks pages another site loads

check, snapshot, validate, layout, compare, grade-compare and motion-shot serve the
project from a loopback server. It now refuses any Host other than loopback or its
in-process name before reading a file. A page or file that another site loads comes
back sandboxed, and its documents carry the another-site mark, so the runtime offers
that site no editing port. The capture browser's own loads are unchanged.

* fix(producer): the render file server answers only loopback and offers no editing port

The render file server now refuses any Host other than loopback before it opens a
file; the capture browser asks for localhost. Every HTML page it serves carries the
another-site mark, so its runtime answers no editing connect. A render never sends
one, so capture is unchanged.

* fix(engine): the exported file server answers only its own host and offers no editing port

createFileServer from @hyperframes/engine now refuses any Host other than loopback or
the hostname it was bound to (on a wildcard bind, this machine's own addresses too)
before it reads a file. Every HTML page it serves carries the another-site mark, so
its runtime answers no editing connect.
This commit is contained in:
Michael Wang
2026-11-15 20:32:40 +00:00
committed by GitHub
parent 1f17b3dfeb
commit 490199cef8
7 changed files with 318 additions and 5 deletions
@@ -729,6 +729,46 @@ describe("serveStaticProjectHtml private project files", () => {
);
});
describe("serveStaticProjectHtml page another site loads", () => {
const MARK = '<meta name="hyperframes-preview-for-another-site">';
const SANDBOX = "sandbox allow-scripts";
async function serveProject(): Promise<string> {
dir = mkdtempSync(join(tmpdir(), "hf-static-site-"));
writeFileSync(join(dir, "scene.html"), "<html><head></head><body>scene</body></html>");
server = await serveStaticProjectHtml(
dir,
"<html><head></head><body>composition</body></html>",
);
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"]) {
+39 -2
View File
@@ -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, `<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`);
}
/** 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);
@@ -70,6 +70,9 @@ describe("file server checked reads", () => {
fs.rmSync(root, { recursive: true, force: true });
});
const unmarked = (text: string) =>
text.replace(/\n?<meta name="hyperframes-preview-for-another-site">\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();
});
@@ -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<ReturnType<typeof createFileServer>>) => Promise<void>,
): Promise<void> {
const projectDir = mkdtempSync(join(tmpdir(), "hf-engine-host-"));
try {
writeFileSync(
join(projectDir, "index.html"),
"<html><head></head><body>composition</body></html>",
);
writeFileSync(join(projectDir, "scene.html"), "<html><head></head><body>scene</body></html>");
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('<meta name="hyperframes-preview-for-another-site">');
});
},
);
});
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);
});
});
});
+44 -1
View File
@@ -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 = `<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`;
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<string> {
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<FileServerHandle> {
const {
projectDir,
@@ -124,6 +158,13 @@ export function createFileServer(options: FileServerOptions): Promise<FileServer
let listeningPort = -1;
const boundToLoopbackOnly = ["127.0.0.1", "localhost", "::1"].includes(hostname);
if (boundToLoopbackOnly) answerOwnLoopbackHostOnly(app, () => 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<FileServer
relativePath === "index.html"
? injectScriptsIntoHtml(rawHtml, headScripts, bodyScripts, stripEmbeddedRuntime)
: rawHtml;
return c.text(html, 200, { "Content-Type": contentType });
return c.text(injectTagsAtHeadStart(html, OFFER_NO_FRAMING_SITE_AN_EDITING_PORT), 200, {
"Content-Type": contentType,
});
}
return new Response(content, {
@@ -2,6 +2,7 @@ import { describe, expect, it } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import path, { join } from "node:path";
import { tmpdir } from "node:os";
import { get as httpGet } from "node:http";
import { MAX_OPEN_MEDIA_RANGE_BYTES } from "@hyperframes/studio-server/media-range";
import {
closeFileServerSafely,
@@ -1191,6 +1192,74 @@ describe("createFileServer private project files", () => {
);
});
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"), "<html><body>composition</body></html>");
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), "<html><head></head><body></body></html>");
await withFileServer(projectDir, async (server) => {
const res = await fetch(`${server.url}/${name}`);
expect(res.status).toBe(200);
expect(await res.text()).toContain('<meta name="hyperframes-preview-for-another-site">');
});
} finally {
rmSync(projectDir, { recursive: true, force: true });
}
},
);
});
describe("createFileServer tool records", () => {
it.each([
".hyperframes/app-history.jsonl",
@@ -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 = `<meta name="${PREVIEW_FOR_ANOTHER_SITE_META}">`;
/**
* 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<FileServer
html = isIndex
? injectScriptsIntoHtml(html, headScripts, bodyScripts, stripEmbeddedRuntime)
: html;
html = injectTagsAtHeadStart(html, OFFER_NO_FRAMING_SITE_AN_EDITING_PORT);
return c.text(html, 200, { "Content-Type": contentType });
}