mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(inspector): the inspection page reaches only its own server and public hosts (#7903)
* wip: paused before review * test: cover credential files on the preview routes and the deploy notice * refactor: name the inspector's file lookup for what it serves * fix(cli): a link to a private file outside the project keeps its own left-out reason * fix: escape the upload notice and skip private files from old manifests * fix(inspector): the inspection page reaches only its own server and public hosts * fix(inspector): release the page server when the proxy fails to start * fix(inspector): open each source session in its own browser context
This commit is contained in:
@@ -58,6 +58,8 @@
|
||||
"test": "vitest run",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"serve": "bun run build && bun src/server.ts",
|
||||
"test:egress": "bun tests/egress.ts",
|
||||
"test:service-worker": "bun tests/serviceWorker.ts",
|
||||
"test:timeline": "bun tests/timeline.ts",
|
||||
"test:timeline-compositions": "bun tests/timelineCompositions.ts",
|
||||
"test:live": "bun run build && bun tests/live.ts",
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from "./engine";
|
||||
import { playerStatePage, seekPage, type PageRunner } from "./pageQueries";
|
||||
import { assertGeneration, validateInspectRequest, validateTimelineRequest } from "./requests";
|
||||
import { createServer, type Server } from "node:http";
|
||||
import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import {
|
||||
closeSync,
|
||||
@@ -21,10 +21,11 @@ import {
|
||||
readFileSync,
|
||||
realpathSync,
|
||||
readdirSync,
|
||||
rmSync,
|
||||
} from "node:fs";
|
||||
import { join, extname } from "node:path";
|
||||
import { getHyperframeRuntimeScript } from "../../core/src/generated/runtime-inline";
|
||||
import puppeteer, { type Browser, type Page } from "puppeteer-core";
|
||||
import puppeteer, { type Browser, type BrowserContext, type Page } from "puppeteer-core";
|
||||
import {
|
||||
bundleToSingleHtml,
|
||||
type BundleScriptProvenance,
|
||||
@@ -33,6 +34,11 @@ import { closeServer, isLoopbackHost, servableProjectFile } from "./pageServer";
|
||||
import { Sources, fingerprint } from "./sources";
|
||||
import type { InspectRequest, Snippet } from "./types";
|
||||
import { hyperframesCacheDir } from "../../core/src/cacheDir";
|
||||
import {
|
||||
browserProxyArgs,
|
||||
createPublicOnlyProfile,
|
||||
startCaptureProxy,
|
||||
} from "../../engine/src/services/publicNetwork/captureProxy";
|
||||
|
||||
const pageRunner = (page: Page): PageRunner => ({
|
||||
run: <A, R>(fn: (arg: A) => R, arg: A) =>
|
||||
@@ -40,13 +46,32 @@ const pageRunner = (page: Page): PageRunner => ({
|
||||
});
|
||||
|
||||
interface Session {
|
||||
context: BrowserContext;
|
||||
page: Page;
|
||||
server: Server;
|
||||
generation: string;
|
||||
fingerprint: string;
|
||||
sourceContext: () => SourceContext;
|
||||
openMs: number;
|
||||
}
|
||||
interface PublicOnlyHost {
|
||||
browser: Browser;
|
||||
server: Server;
|
||||
port: number;
|
||||
proxy: { close(): Promise<void> };
|
||||
profile: string;
|
||||
}
|
||||
type PageHandler = (req: IncomingMessage, res: ServerResponse) => void;
|
||||
|
||||
async function releaseEach(steps: ReadonlyArray<() => unknown>): Promise<void> {
|
||||
for (const step of steps) {
|
||||
try {
|
||||
await step();
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface InspectorOptions {
|
||||
executablePath?: string;
|
||||
idleMs?: number;
|
||||
@@ -79,7 +104,8 @@ function chromePath() {
|
||||
}
|
||||
export class Inspector {
|
||||
private session?: Session;
|
||||
private browser?: Browser;
|
||||
private host?: PublicOnlyHost;
|
||||
private serve?: PageHandler;
|
||||
private tail: Promise<unknown> = Promise.resolve();
|
||||
private timer?: ReturnType<typeof setTimeout>;
|
||||
private closed = false;
|
||||
@@ -101,11 +127,63 @@ export class Inspector {
|
||||
const session = this.session;
|
||||
this.session = undefined;
|
||||
this.catalog.clear();
|
||||
if (session) {
|
||||
await session.page.close().catch(() => {});
|
||||
await closeServer(session.server);
|
||||
this.serve = undefined;
|
||||
if (session) await session.context.close().catch(() => {});
|
||||
}
|
||||
private async openHost(): Promise<PublicOnlyHost> {
|
||||
if (this.host) return this.host;
|
||||
const server = createServer((req, res) =>
|
||||
this.serve ? this.serve(req, res) : void res.writeHead(404).end(),
|
||||
);
|
||||
let proxy: PublicOnlyHost["proxy"] | undefined;
|
||||
let profile: string | undefined;
|
||||
try {
|
||||
await new Promise<void>((done, fail) => {
|
||||
server.once("error", fail);
|
||||
server.listen(0, "127.0.0.1", done);
|
||||
});
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") throw new Error("preview address unavailable");
|
||||
const started = await startCaptureProxy();
|
||||
proxy = started;
|
||||
profile = await createPublicOnlyProfile("hf-inspect-profile-");
|
||||
const browser = await puppeteer.launch({
|
||||
executablePath: this.options.executablePath ?? chromePath(),
|
||||
env: buildChromeLaunchEnv(),
|
||||
headless: true,
|
||||
userDataDir: profile,
|
||||
args: withHyperframesBrowserMarker([
|
||||
"--disable-dev-shm-usage",
|
||||
"--enable-webgl",
|
||||
"--use-gl=angle",
|
||||
"--use-angle=swiftshader",
|
||||
...browserProxyArgs(started.url, `127.0.0.1:${address.port}`),
|
||||
]),
|
||||
});
|
||||
this.stats.browserStarts++;
|
||||
this.host = { browser, server, port: address.port, proxy: started, profile };
|
||||
return this.host;
|
||||
} catch (error) {
|
||||
const leftover = profile;
|
||||
await releaseEach([
|
||||
() => proxy?.close(),
|
||||
() => closeServer(server),
|
||||
() => leftover && rmSync(leftover, { recursive: true, force: true }),
|
||||
]);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
private async closeHost() {
|
||||
const host = this.host;
|
||||
this.host = undefined;
|
||||
if (!host) return;
|
||||
await releaseEach([
|
||||
() => host.browser.close(),
|
||||
() => closeServer(host.server),
|
||||
() => host.proxy.close(),
|
||||
() => rmSync(host.profile, { recursive: true, force: true }),
|
||||
]);
|
||||
}
|
||||
private async ensure(): Promise<{ session: Session; reused: boolean }> {
|
||||
const digest = fingerprint(this.project);
|
||||
if (this.session?.fingerprint === digest) return { session: this.session, reused: true };
|
||||
@@ -121,7 +199,7 @@ export class Inspector {
|
||||
});
|
||||
if (fingerprint(this.project) !== digest)
|
||||
throw new Error("source_changed_during_load: retry against a stable project");
|
||||
const server = createServer((req, res) => {
|
||||
const serve: PageHandler = (req, res) => {
|
||||
if (!isLoopbackHost(req.headers.host)) return void res.writeHead(403).end();
|
||||
try {
|
||||
const path = decodeURIComponent(new URL(req.url ?? "/", "http://localhost").pathname);
|
||||
@@ -169,27 +247,14 @@ export class Inspector {
|
||||
} catch {
|
||||
res.writeHead(404).end();
|
||||
}
|
||||
});
|
||||
await new Promise<void>((done) => server.listen(0, "127.0.0.1", done));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === "string") throw new Error("preview address unavailable");
|
||||
};
|
||||
let context: BrowserContext | undefined;
|
||||
let page: Page | undefined;
|
||||
try {
|
||||
if (!this.browser) {
|
||||
this.browser = await puppeteer.launch({
|
||||
executablePath: this.options.executablePath ?? chromePath(),
|
||||
env: buildChromeLaunchEnv(),
|
||||
headless: true,
|
||||
args: withHyperframesBrowserMarker([
|
||||
"--disable-dev-shm-usage",
|
||||
"--enable-webgl",
|
||||
"--use-gl=angle",
|
||||
"--use-angle=swiftshader",
|
||||
]),
|
||||
});
|
||||
this.stats.browserStarts++;
|
||||
}
|
||||
page = await this.browser.newPage();
|
||||
const host = await this.openHost();
|
||||
this.serve = serve;
|
||||
context = await host.browser.createBrowserContext();
|
||||
page = await context.newPage();
|
||||
await page.setViewport({ width: 1920, height: 1080, deviceScaleFactor: 1 });
|
||||
if (this.options.instrument !== false) {
|
||||
await page.evaluateOnNewDocument(
|
||||
@@ -199,14 +264,14 @@ export class Inspector {
|
||||
readFileSync(new URL(`../dist/inspection.${"js"}`, import.meta.url), "utf8"),
|
||||
);
|
||||
}
|
||||
await page.goto(`http://127.0.0.1:${address.port}/`, { waitUntil: "load", timeout: 30_000 });
|
||||
await page.goto(`http://127.0.0.1:${host.port}/`, { waitUntil: "load", timeout: 30_000 });
|
||||
await page.waitForFunction("!!window.__renderReady", { timeout: 15_000 });
|
||||
await page.evaluate(() => document.fonts.ready);
|
||||
let sourceContext: SourceContext | undefined;
|
||||
const project = this.project;
|
||||
this.session = {
|
||||
context,
|
||||
page,
|
||||
server,
|
||||
generation,
|
||||
fingerprint: digest,
|
||||
sourceContext: () => {
|
||||
@@ -222,8 +287,8 @@ export class Inspector {
|
||||
this.stats.pageLoads++;
|
||||
return { session: this.session, reused: false };
|
||||
} catch (error) {
|
||||
await page?.close().catch(() => {});
|
||||
await closeServer(server);
|
||||
await context?.close().catch(() => {});
|
||||
this.serve = undefined;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -244,8 +309,7 @@ export class Inspector {
|
||||
this.timer = setTimeout(() => {
|
||||
void this.serialize(async () => {
|
||||
await this.closeSession();
|
||||
await this.browser?.close();
|
||||
this.browser = undefined;
|
||||
await this.closeHost();
|
||||
});
|
||||
}, this.options.idleMs ?? 120_000);
|
||||
this.timer.unref();
|
||||
@@ -333,8 +397,7 @@ export class Inspector {
|
||||
this.closed = true;
|
||||
clearTimeout(this.timer);
|
||||
await this.closeSession();
|
||||
await this.browser?.close();
|
||||
this.browser = undefined;
|
||||
await this.closeHost();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Inspector } from "../src/service";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { createServer } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
let internalHits = 0;
|
||||
const internal = createServer((_req, res) => {
|
||||
internalHits++;
|
||||
res.end("internal");
|
||||
});
|
||||
await new Promise<void>((done) => internal.listen(0, "127.0.0.1", done));
|
||||
const { port } = internal.address() as AddressInfo;
|
||||
const project = mkdtempSync(join(tmpdir(), "hf-inspect-egress-"));
|
||||
writeFileSync(join(project, "asset.txt"), "own asset");
|
||||
writeFileSync(
|
||||
join(project, "index.html"),
|
||||
`<!doctype html><html><body><div data-composition-id="root" data-start="0" data-duration="1" data-width="1920" data-height="1080"></div><script>
|
||||
const read = (url) => fetch(url).then((r) => r.text(), () => "refused");
|
||||
window.__probe = Promise.all([read("asset.txt"), read("http://127.0.0.1:${port}/"), read("http://localhost:${port}/")]);
|
||||
window.__renderReady = true;
|
||||
</script></body></html>`,
|
||||
);
|
||||
const inspector = new Inspector(project, { instrument: false });
|
||||
try {
|
||||
const [own, loopback, localhost] = await inspector.withPage((page) =>
|
||||
page.evaluate(() => (window as Window & { __probe?: Promise<string[]> }).__probe ?? []),
|
||||
);
|
||||
assert.equal(own, "own asset");
|
||||
assert.equal(loopback, "refused");
|
||||
assert.equal(localhost, "refused");
|
||||
assert.equal(internalHits, 0);
|
||||
console.log(JSON.stringify({ ownAssetLoads: true, otherLoopbackServiceRefused: true }));
|
||||
} finally {
|
||||
await inspector.dispose();
|
||||
internal.close();
|
||||
rmSync(project, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { Server } from "node:http";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { Inspector } from "../src/service";
|
||||
|
||||
const listeningServers = (): number =>
|
||||
(process as unknown as { _getActiveHandles(): unknown[] })
|
||||
._getActiveHandles()
|
||||
.filter((handle) => handle instanceof Server && handle.listening).length;
|
||||
|
||||
const projects: string[] = [];
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
for (const dir of projects.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("leaves no page server listening when the network proxy cannot start", async () => {
|
||||
const project = mkdtempSync(join(tmpdir(), "hf-inspect-startup-"));
|
||||
projects.push(project);
|
||||
writeFileSync(join(project, "index.html"), "<html><body></body></html>");
|
||||
vi.stubEnv("HYPERFRAMES_CAPTURE_PROXY", "https://proxy.invalid");
|
||||
const before = listeningServers();
|
||||
const inspector = new Inspector(project, { instrument: false });
|
||||
|
||||
for (let attempt = 0; attempt < 3; attempt++) {
|
||||
await expect(inspector.withPage(async () => undefined)).rejects.toThrow(/http:\/\/ proxy/);
|
||||
}
|
||||
await inspector.dispose();
|
||||
|
||||
expect(listeningServers()).toBe(before);
|
||||
});
|
||||
@@ -11,16 +11,19 @@ vi.mock("puppeteer-core", () => ({
|
||||
default: {
|
||||
launch: async () => ({
|
||||
close: async () => {},
|
||||
newPage: async () => ({
|
||||
setViewport: async () => {},
|
||||
goto: async (url: string) => {
|
||||
pages.push(url);
|
||||
await fetch(url);
|
||||
},
|
||||
waitForFunction: async () => {},
|
||||
evaluate: async () => {},
|
||||
content: async () => "",
|
||||
createBrowserContext: async () => ({
|
||||
close: async () => {},
|
||||
newPage: async () => ({
|
||||
setViewport: async () => {},
|
||||
goto: async (url: string) => {
|
||||
pages.push(url);
|
||||
await fetch(url);
|
||||
},
|
||||
waitForFunction: async () => {},
|
||||
evaluate: async () => {},
|
||||
content: async () => "",
|
||||
close: async () => {},
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
},
|
||||
|
||||
@@ -11,21 +11,22 @@ vi.mock("puppeteer-core", () => ({
|
||||
default: {
|
||||
launch: async (options: { args?: string[] }) => {
|
||||
launches.push(options);
|
||||
const newPage = async () => {
|
||||
let html = "";
|
||||
return {
|
||||
setViewport: async () => {},
|
||||
goto: async (url: string) => {
|
||||
html = await (await fetch(url)).text();
|
||||
},
|
||||
waitForFunction: async () => {},
|
||||
evaluate: async () => {},
|
||||
content: async () => html,
|
||||
close: async () => {},
|
||||
};
|
||||
};
|
||||
return {
|
||||
close: async () => {},
|
||||
newPage: async () => {
|
||||
let html = "";
|
||||
return {
|
||||
setViewport: async () => {},
|
||||
goto: async (url: string) => {
|
||||
html = await (await fetch(url)).text();
|
||||
},
|
||||
waitForFunction: async () => {},
|
||||
evaluate: async () => {},
|
||||
content: async () => html,
|
||||
close: async () => {},
|
||||
};
|
||||
},
|
||||
createBrowserContext: async () => ({ newPage, close: async () => {} }),
|
||||
};
|
||||
},
|
||||
},
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { Inspector } from "../src/service";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const page = (marker: string) =>
|
||||
`<!doctype html><html><body><div data-composition-id="root" data-start="0" data-duration="1" data-width="1920" data-height="1080"><p id="marker">${marker}</p></div><script>
|
||||
window.__registered = navigator.serviceWorker.register("/sw.js").then(() => navigator.serviceWorker.ready);
|
||||
window.__renderReady = true;
|
||||
</script></body></html>`;
|
||||
|
||||
const project = mkdtempSync(join(tmpdir(), "hf-inspect-sw-"));
|
||||
writeFileSync(
|
||||
join(project, "sw.js"),
|
||||
`self.addEventListener("install", () => self.skipWaiting());
|
||||
self.addEventListener("activate", (event) => event.waitUntil(self.clients.claim()));
|
||||
self.addEventListener("fetch", (event) => {
|
||||
if (event.request.mode !== "navigate") return;
|
||||
event.respondWith(new Response(${JSON.stringify(page("STALE"))}, { headers: { "Content-Type": "text/html" } }));
|
||||
});`,
|
||||
);
|
||||
writeFileSync(join(project, "index.html"), page("OLD"));
|
||||
const inspector = new Inspector(project, { instrument: false });
|
||||
const marker = () =>
|
||||
inspector.withPage(async (p) => {
|
||||
await p.evaluate(() => (window as Window & { __registered?: Promise<unknown> }).__registered);
|
||||
return p.evaluate(() => document.getElementById("marker")?.textContent ?? "");
|
||||
});
|
||||
try {
|
||||
assert.equal(await marker(), "OLD");
|
||||
writeFileSync(join(project, "index.html"), page("NEW"));
|
||||
assert.equal(await marker(), "NEW");
|
||||
assert.equal(inspector.stats.pageLoads, 2);
|
||||
console.log(JSON.stringify({ editedSourceIsInspected: true, stats: inspector.stats }));
|
||||
} finally {
|
||||
await inspector.dispose();
|
||||
rmSync(project, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user