mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli,engine): pin the page server's direct host to an explicit port and the Studio's own origin (#7945)
The bypass entry for a restricted browser's page server now always names its port, so a default port no longer admits every port on the host. Studio thumbnails take the server's own origin, not a request's Host header. A stop signal that lands while the temp profile is being made removes it and launches no Chrome. The wiring test follows renamed and namespaced calls, requires the callback to pass on the network's args and profile, and refuses an unset pageServerUrl. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
df698a0290
commit
dbc9c18262
@@ -0,0 +1,55 @@
|
||||
import { existsSync } from "node:fs";
|
||||
import type { Browser } from "puppeteer-core";
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
|
||||
const stop = vi.hoisted(() => ({
|
||||
cleanups: [] as (() => Promise<void>)[],
|
||||
profileStarted: undefined as (() => void) | undefined,
|
||||
finishProfile: undefined as (() => void) | undefined,
|
||||
made: [] as string[],
|
||||
}));
|
||||
|
||||
vi.mock("./launch.js", () => ({
|
||||
registerBrowserSessionCleanup: (cleanup: () => Promise<void>) => {
|
||||
stop.cleanups.push(cleanup);
|
||||
return () => void stop.cleanups.splice(stop.cleanups.indexOf(cleanup), 1);
|
||||
},
|
||||
}));
|
||||
|
||||
// The profile is made only once the test lets it finish, so a stop can land while it is being made.
|
||||
vi.mock("@hyperframes/engine/public-network", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("@hyperframes/engine/public-network")>();
|
||||
return {
|
||||
...real,
|
||||
createPublicOnlyProfile: async (prefix: string) => {
|
||||
stop.profileStarted?.();
|
||||
await new Promise<void>((resolve) => (stop.finishProfile = resolve));
|
||||
const made = await real.createPublicOnlyProfile(prefix);
|
||||
stop.made.push(made);
|
||||
return made;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const { launchWithPageNetwork } = await import("./pageNetwork.js");
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
// fails without the fix: a stop that landed while the profile was being made ran before the profile had a path, so
|
||||
// the profile made a moment later stayed in the temp folder.
|
||||
it("removes a profile still being made when the CLI stops, and launches no Chrome", async () => {
|
||||
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "true");
|
||||
const started = new Promise<void>((resolve) => (stop.profileStarted = resolve));
|
||||
const launch = vi.fn(async () => ({ connected: true, once: () => {} }) as unknown as Browser);
|
||||
const launching = launchWithPageNetwork("http://127.0.0.1:4000/", launch);
|
||||
await started;
|
||||
const stopping = Promise.all(stop.cleanups.map((cleanup) => cleanup()));
|
||||
stop.finishProfile?.();
|
||||
await stopping;
|
||||
await expect(launching).rejects.toThrow(/stopping/);
|
||||
expect(launch).not.toHaveBeenCalled();
|
||||
expect(stop.made).toHaveLength(1);
|
||||
expect(existsSync(stop.made[0]!)).toBe(false);
|
||||
});
|
||||
@@ -75,6 +75,21 @@ describe("launchWithPageNetwork", () => {
|
||||
await expect.poll(() => existsSync(seen!.userDataDir!)).toBe(false);
|
||||
});
|
||||
|
||||
// fails without the fix: a page server on the default port gave a bypass entry with no port, which admits
|
||||
// every port on that host.
|
||||
it("names the page server's port in the bypass entry even when the URL leaves it out", async () => {
|
||||
process.env.PRODUCER_RESTRICT_PAGE_NETWORK = "true";
|
||||
let seen: PageNetworkLaunch | undefined;
|
||||
const fake = fakeBrowser();
|
||||
await launchWithPageNetwork("http://localhost/index.html", async (network) => {
|
||||
seen = network;
|
||||
return fake.browser;
|
||||
});
|
||||
expect(seen?.args).toContain("--proxy-bypass-list=<-loopback>;localhost:80");
|
||||
fake.disconnect();
|
||||
await expect.poll(() => existsSync(seen!.userDataDir!)).toBe(false);
|
||||
});
|
||||
|
||||
it("refuses to launch a restricted browser before the page has a URL", async () => {
|
||||
process.env.PRODUCER_RESTRICT_PAGE_NETWORK = "true";
|
||||
let launched = false;
|
||||
|
||||
@@ -16,24 +16,32 @@ export async function launchWithPageNetwork(
|
||||
if (!resolveRestrictPageNetwork()) return launch({ args: [] });
|
||||
if (!pageUrl)
|
||||
throw new Error("A restricted page network needs the page's URL before Chrome launches.");
|
||||
const { browserProxyArgs, createPublicOnlyProfile, logRefusal, startCaptureProxy } =
|
||||
await import("@hyperframes/engine/public-network");
|
||||
const {
|
||||
browserProxyArgs,
|
||||
createPublicOnlyProfile,
|
||||
logRefusal,
|
||||
pageServerDirectHost,
|
||||
startCaptureProxy,
|
||||
} = await import("@hyperframes/engine/public-network");
|
||||
const { rm } = await import("node:fs/promises");
|
||||
const proxy = await startCaptureProxy(null, logRefusal);
|
||||
let userDataDir: string | undefined;
|
||||
let profile: Promise<string> | undefined;
|
||||
let released: Promise<void> | undefined;
|
||||
let stopWatching = () => {};
|
||||
const release = () =>
|
||||
(released ??= (async () => {
|
||||
stopWatching();
|
||||
await proxy.close();
|
||||
const userDataDir = await profile?.catch(() => undefined);
|
||||
if (userDataDir) await rm(userDataDir, { recursive: true, force: true }).catch(() => {});
|
||||
})());
|
||||
try {
|
||||
stopWatching = registerBrowserSessionCleanup(release);
|
||||
userDataDir = await createPublicOnlyProfile("hf-cli-profile-");
|
||||
profile = createPublicOnlyProfile("hf-cli-profile-");
|
||||
const userDataDir = await profile;
|
||||
if (released) throw new Error("The CLI is stopping; Chrome was not launched.");
|
||||
const browser = await launch({
|
||||
args: browserProxyArgs(proxy.url, new URL(pageUrl).host),
|
||||
args: browserProxyArgs(proxy.url, pageServerDirectHost(pageUrl)),
|
||||
userDataDir,
|
||||
});
|
||||
if (browser.connected) browser.once("disconnected", () => void release());
|
||||
|
||||
@@ -12,6 +12,8 @@ const OPENS_NO_COMPOSITION: Record<string, string> = {
|
||||
"capture/launchCaptureBrowser.ts": "`capture` opens a website, not a composition",
|
||||
};
|
||||
|
||||
const WATCHED = new Set(["launchManagedBrowser", "acquireBrowser", "launchWithPageNetwork"]);
|
||||
|
||||
function sources(dir: string): string[] {
|
||||
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
||||
const at = join(dir, entry.name);
|
||||
@@ -20,36 +22,77 @@ function sources(dir: string): string[] {
|
||||
});
|
||||
}
|
||||
|
||||
const callee = (call: ts.CallExpression) => call.expression.getText();
|
||||
/** The called function's own name, so `engine.acquireBrowser(…)` reads as `acquireBrowser`. */
|
||||
const calleeName = (call: ts.CallExpression) =>
|
||||
ts.isPropertyAccessExpression(call.expression)
|
||||
? call.expression.name.text
|
||||
: call.expression.getText();
|
||||
|
||||
const usesProperty = (body: ts.Node, object: string, property: string): boolean =>
|
||||
(ts.isPropertyAccessExpression(body) &&
|
||||
body.expression.getText() === object &&
|
||||
body.name.text === property) ||
|
||||
!!ts.forEachChild(body, (child) => usesProperty(child, object, property) || undefined);
|
||||
|
||||
/** Whether `node` is inside a `launchWithPageNetwork` callback that passes on both its args and its profile. */
|
||||
function fenced(node: ts.Node): boolean {
|
||||
for (let at = node.parent; at; at = at.parent) {
|
||||
const call = at.parent;
|
||||
if (!ts.isArrowFunction(at) && !ts.isFunctionExpression(at)) continue;
|
||||
if (!call || !ts.isCallExpression(call) || calleeName(call) !== "launchWithPageNetwork")
|
||||
continue;
|
||||
const network = at.parameters[0]?.name.getText();
|
||||
return (
|
||||
!!network &&
|
||||
usesProperty(at.body, network, "args") &&
|
||||
usesProperty(at.body, network, "userDataDir")
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const unsetValue = (value: ts.Expression) =>
|
||||
value.kind === ts.SyntaxKind.NullKeyword ||
|
||||
ts.isVoidExpression(value) ||
|
||||
(ts.isIdentifier(value) && value.text === "undefined");
|
||||
|
||||
const renamesWatched = (node: ts.Node): node is ts.ImportSpecifier | ts.BindingElement =>
|
||||
(ts.isImportSpecifier(node) || ts.isBindingElement(node)) &&
|
||||
!!node.propertyName &&
|
||||
WATCHED.has(node.propertyName.getText()) &&
|
||||
node.name.getText() !== node.propertyName.getText();
|
||||
|
||||
const setsPageServer = (property: ts.ObjectLiteralElementLike) =>
|
||||
property.name?.getText() === "pageServerUrl" &&
|
||||
(ts.isShorthandPropertyAssignment(property) ||
|
||||
(ts.isPropertyAssignment(property) && !unsetValue(property.initializer)));
|
||||
|
||||
/** A launch outside the fence: Chrome started directly, or an engine browser with no page server. */
|
||||
function launchesUnfenced(call: ts.CallExpression): boolean {
|
||||
const called = calleeName(call);
|
||||
if (called === "launchManagedBrowser" || /puppeteer.*\.launch$/i.test(call.expression.getText()))
|
||||
return !fenced(call);
|
||||
return (
|
||||
called === "acquireBrowser" &&
|
||||
!call.arguments.some(
|
||||
(arg) => ts.isObjectLiteralExpression(arg) && arg.properties.some(setsPageServer),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The Chrome launches in `text` that skip the page-network fence: a `launchManagedBrowser` or puppeteer `.launch`
|
||||
* not inside a `launchWithPageNetwork` call, or an engine `acquireBrowser` given no `pageServerUrl`.
|
||||
* not inside a `launchWithPageNetwork` callback that uses its network, an `acquireBrowser` without a set
|
||||
* `pageServerUrl`, or any of these under another name, which this check couldn't follow.
|
||||
*/
|
||||
function unfencedLaunches(name: string, text: string): string[] {
|
||||
const file = ts.createSourceFile(name, text, ts.ScriptTarget.Latest, true);
|
||||
const found: string[] = [];
|
||||
const at = (node: ts.Node) =>
|
||||
`${name}:${file.getLineAndCharacterOfPosition(node.getStart()).line + 1}`;
|
||||
const fenced = (node: ts.Node): boolean =>
|
||||
!!node.parent &&
|
||||
((ts.isCallExpression(node.parent) && callee(node.parent) === "launchWithPageNetwork") ||
|
||||
fenced(node.parent));
|
||||
const visit = (node: ts.Node) => {
|
||||
if (ts.isCallExpression(node)) {
|
||||
const name = callee(node);
|
||||
if ((name === "launchManagedBrowser" || /puppeteer.*\.launch$/i.test(name)) && !fenced(node))
|
||||
found.push(at(node));
|
||||
if (
|
||||
name === "acquireBrowser" &&
|
||||
!node.arguments.some(
|
||||
(arg) =>
|
||||
ts.isObjectLiteralExpression(arg) &&
|
||||
arg.properties.some((property) => property.name?.getText() === "pageServerUrl"),
|
||||
)
|
||||
)
|
||||
found.push(at(node));
|
||||
}
|
||||
if (renamesWatched(node)) found.push(`${at(node)} renames ${node.propertyName!.getText()}`);
|
||||
if (ts.isCallExpression(node) && launchesUnfenced(node)) found.push(at(node));
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(file);
|
||||
@@ -57,22 +100,41 @@ function unfencedLaunches(name: string, text: string): string[] {
|
||||
}
|
||||
|
||||
describe("the page-network fence", () => {
|
||||
it("is found missing on a bare launch, and found on a fenced one", () => {
|
||||
it("is found missing on a bare, renamed or unused launch, and found on a fenced one", () => {
|
||||
expect(
|
||||
unfencedLaunches(
|
||||
"bare.ts",
|
||||
`const a = await launchManagedBrowser(puppeteer.default, { args });
|
||||
const b = await puppeteer.default.launch({ args });
|
||||
const c = await acquireBrowser(args, { forceScreenshot: true });`,
|
||||
const c = await acquireBrowser(args, { forceScreenshot: true });
|
||||
const d = await engine.acquireBrowser(args, { pageServerUrl: undefined });
|
||||
import { launchManagedBrowser as launchIt } from "./launch.js";
|
||||
const { acquireBrowser: acquire } = await import("@hyperframes/engine");
|
||||
const e = await launchWithPageNetwork(url, (network) =>
|
||||
launchManagedBrowser(puppeteer.default, { args, userDataDir: network.userDataDir }),
|
||||
);`,
|
||||
),
|
||||
).toEqual(["bare.ts:1", "bare.ts:2", "bare.ts:3"]);
|
||||
).toEqual([
|
||||
"bare.ts:1",
|
||||
"bare.ts:2",
|
||||
"bare.ts:3",
|
||||
"bare.ts:4",
|
||||
"bare.ts:5 renames launchManagedBrowser",
|
||||
"bare.ts:6 renames acquireBrowser",
|
||||
"bare.ts:8",
|
||||
]);
|
||||
expect(
|
||||
unfencedLaunches(
|
||||
"fenced.ts",
|
||||
`const a = await launchWithPageNetwork(url, (network) =>
|
||||
launchManagedBrowser(puppeteer.default, { args: [...args, ...network.args] }),
|
||||
launchManagedBrowser(puppeteer.default, { args: [...args, ...network.args], userDataDir: network.userDataDir }),
|
||||
);
|
||||
const c = await acquireBrowser(args, { forceScreenshot: true, pageServerUrl: url });`,
|
||||
const b = await launchWithPageNetwork(url, (network) => {
|
||||
const allArgs = [...args, ...network.args];
|
||||
return launchManagedBrowser(puppeteer.default, { userDataDir: network.userDataDir, args: allArgs });
|
||||
});
|
||||
const c = await acquireBrowser(args, { forceScreenshot: true, pageServerUrl: url });
|
||||
const d = await acquireBrowser(args, { pageServerUrl });`,
|
||||
),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -766,6 +766,23 @@ describe("Studio thumbnail capture", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
// fails without the fix: the direct host came from each preview URL, which a request's loopback Host header
|
||||
// can name, so a request could open another local port to the thumbnail page and two spellings of one server
|
||||
// closed each other's browser.
|
||||
it("gives the thumbnail browser the server's own origin, whatever host a request named", async () => {
|
||||
fakePageBrowser();
|
||||
const launch = vi.spyOn(engineState, "acquireBrowser");
|
||||
const dir = tmpProject();
|
||||
server = createStudioServer({ projectDir: dir, origin: () => "http://127.0.0.1:4100" });
|
||||
const at = (previewUrl: string) => ({ ...opts(dir), previewUrl });
|
||||
await server.adapter.generateThumbnail?.(at("http://127.0.0.1:4100/api/projects/demo/preview"));
|
||||
await server.adapter.generateThumbnail?.(at("http://localhost:4100/api/projects/demo/preview"));
|
||||
await server.adapter.generateThumbnail?.(at("http://localhost:9999/api/projects/demo/preview"));
|
||||
expect(
|
||||
launch.mock.calls.map((call) => (call[1] as { pageServerUrl?: string }).pageServerUrl),
|
||||
).toEqual(["http://127.0.0.1:4100"]);
|
||||
});
|
||||
|
||||
it("undoes a row's isolation after its screenshot, since the page is reused", async () => {
|
||||
const { screenshot, evaluate } = fakePageBrowser();
|
||||
const dir = tmpProject();
|
||||
|
||||
@@ -268,7 +268,7 @@ let _thumbnailBrowserModes: {
|
||||
requested: BrowserGpuMode;
|
||||
resolved: ResolvedBrowserGpuMode;
|
||||
softwareWebGpu: boolean;
|
||||
pageServerHost: string;
|
||||
pageServerOrigin: string;
|
||||
} | null = null;
|
||||
|
||||
interface ThumbnailBrowserSession {
|
||||
@@ -276,34 +276,37 @@ interface ThumbnailBrowserSession {
|
||||
requestedGpuMode: BrowserGpuMode;
|
||||
resolvedGpuMode: ResolvedBrowserGpuMode;
|
||||
softwareWebGpu: boolean;
|
||||
pageServerHost: string;
|
||||
pageServerOrigin: string;
|
||||
}
|
||||
|
||||
// The browser gets the preview's server as its page server: with PRODUCER_RESTRICT_PAGE_NETWORK on, that is the
|
||||
// one host it reaches directly, so a preview on another host needs a new browser.
|
||||
// The page server is the Studio's own origin when known, never a request's Host: with PRODUCER_RESTRICT_PAGE_NETWORK
|
||||
// on, it is the one host and port the browser reaches directly, so another page server needs a new browser.
|
||||
async function getThumbnailBrowser(
|
||||
requestedGpuMode: BrowserGpuMode,
|
||||
previewUrl: string,
|
||||
pageServerUrl: string,
|
||||
isShuttingDown: () => boolean,
|
||||
): Promise<ThumbnailBrowserSession | null> {
|
||||
if (isShuttingDown()) return null;
|
||||
const pageServerHost = new URL(previewUrl).host;
|
||||
const pageServerOrigin = new URL(pageServerUrl).origin;
|
||||
if (
|
||||
_thumbnailBrowserLease?.browser.connected &&
|
||||
_thumbnailBrowserModes?.requested === requestedGpuMode &&
|
||||
_thumbnailBrowserModes.pageServerHost === pageServerHost
|
||||
_thumbnailBrowserModes.pageServerOrigin === pageServerOrigin
|
||||
) {
|
||||
return {
|
||||
browser: _thumbnailBrowserLease.browser,
|
||||
requestedGpuMode: _thumbnailBrowserModes.requested,
|
||||
resolvedGpuMode: _thumbnailBrowserModes.resolved,
|
||||
softwareWebGpu: _thumbnailBrowserModes.softwareWebGpu,
|
||||
pageServerHost,
|
||||
pageServerOrigin,
|
||||
};
|
||||
}
|
||||
if (_thumbnailBrowserInitializing) {
|
||||
const session = await _thumbnailBrowserInitializing;
|
||||
if (session?.requestedGpuMode === requestedGpuMode && session.pageServerHost === pageServerHost)
|
||||
if (
|
||||
session?.requestedGpuMode === requestedGpuMode &&
|
||||
session.pageServerOrigin === pageServerOrigin
|
||||
)
|
||||
return session;
|
||||
}
|
||||
if (_thumbnailBrowserLease) await closeThumbnailBrowser();
|
||||
@@ -337,14 +340,14 @@ async function getThumbnailBrowser(
|
||||
},
|
||||
gpuConfig,
|
||||
),
|
||||
{ forceScreenshot: true, pageServerUrl: previewUrl },
|
||||
{ forceScreenshot: true, pageServerUrl },
|
||||
);
|
||||
_thumbnailBrowserLease = acquired;
|
||||
_thumbnailBrowserModes = {
|
||||
requested: requestedGpuMode,
|
||||
resolved: resolvedGpuMode,
|
||||
softwareWebGpu,
|
||||
pageServerHost,
|
||||
pageServerOrigin,
|
||||
};
|
||||
acquired.browser.on("disconnected", () => {
|
||||
if (_thumbnailBrowserLease !== acquired) return;
|
||||
@@ -357,7 +360,7 @@ async function getThumbnailBrowser(
|
||||
requestedGpuMode,
|
||||
resolvedGpuMode,
|
||||
softwareWebGpu,
|
||||
pageServerHost,
|
||||
pageServerOrigin,
|
||||
};
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
@@ -806,7 +809,7 @@ export function createStudioServer(options: StudioServerOptions): StudioServer {
|
||||
async generateThumbnail(opts): Promise<Buffer | null> {
|
||||
const session = await getThumbnailBrowser(
|
||||
browserGpuMode,
|
||||
opts.previewUrl,
|
||||
options.origin?.() ?? opts.previewUrl,
|
||||
() => shuttingDown,
|
||||
);
|
||||
if (!session) {
|
||||
|
||||
@@ -696,6 +696,18 @@ describe("browser launch capture-mode contract", () => {
|
||||
expect(fingerprint().publicOnly).toEqual({ pageServerHost: "localhost:4321" });
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
// fails without the fix: a page server on its scheme's default port gave a direct host with no port, which
|
||||
// Chrome's bypass list takes as every port on that host.
|
||||
it("names the page server's port even when the URL leaves out the default", () => {
|
||||
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "true");
|
||||
const host = (pageServerUrl: string) =>
|
||||
_createBrowserLaunchFingerprintForTests([], { pageServerUrl }).publicOnly?.pageServerHost;
|
||||
expect(host("http://localhost/")).toBe("localhost:80");
|
||||
expect(host("https://127.0.0.1/")).toBe("127.0.0.1:443");
|
||||
expect(host("http://[::1]:4321/")).toBe("[::1]:4321");
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveBrowserGpuMode", () => {
|
||||
|
||||
@@ -36,6 +36,7 @@ import {
|
||||
browserProxyArgs,
|
||||
createPublicOnlyProfile,
|
||||
logRefusal,
|
||||
pageServerDirectHost,
|
||||
startCaptureProxy,
|
||||
} from "./publicNetwork/captureProxy.js";
|
||||
import { removeTreeBestEffortAsync } from "@hyperframes/core/remove-tree";
|
||||
@@ -858,7 +859,8 @@ function createBrowserLaunchFingerprint(
|
||||
requestedCaptureMode,
|
||||
publicOnly: resolveRestrictPageNetwork()
|
||||
? {
|
||||
pageServerHost: launchConfig.pageServerUrl && new URL(launchConfig.pageServerUrl).host,
|
||||
pageServerHost:
|
||||
launchConfig.pageServerUrl && pageServerDirectHost(launchConfig.pageServerUrl),
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
|
||||
@@ -240,6 +240,11 @@ export async function startCaptureProxy(
|
||||
};
|
||||
}
|
||||
|
||||
export function pageServerDirectHost(pageServerUrl: string): string {
|
||||
const url = new URL(pageServerUrl);
|
||||
return `${url.hostname}:${url.port || (url.protocol === "https:" ? "443" : "80")}`;
|
||||
}
|
||||
|
||||
export function browserProxyArgs(proxyUrl: string, directHost?: string): string[] {
|
||||
return [
|
||||
`--proxy-server=${proxyUrl}`,
|
||||
|
||||
Reference in New Issue
Block a user