mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(engine): renders reach only public addresses from every part of the browser (#7758)
* fix(engine): renders reach only public addresses from every part of the browser * fix(engine): renders reach only public addresses from every part of the browser * fix(engine): renders reach only public addresses from every part of the browser * fix(engine): renders reach only public addresses from every part of the browser
This commit is contained in:
@@ -193,11 +193,12 @@ three rendering activities—`plan`, `renderChunk`, and `assemble`. Your
|
||||
orchestrator remains responsible for dispatch, retries, storage, and networking.
|
||||
|
||||
A service that renders projects from other people should set
|
||||
`PRODUCER_RESTRICT_PAGE_NETWORK=true`. The render page can then reach only its own
|
||||
project server and public addresses, checked by host and by every address a name
|
||||
resolves to: the same rule asset downloads follow. A refused request fails like
|
||||
any other failed asset and shows in the render's browser diagnostics. Leave it
|
||||
unset for local renders that load assets from your own machine or network.
|
||||
`PRODUCER_RESTRICT_PAGE_NETWORK=true`. The render browser can then reach only its own
|
||||
project server and public IPv4 addresses: everything else it sends, from pages, frames,
|
||||
workers, WebSockets and WebRTC alike, goes through a local proxy that checks every
|
||||
address a name resolves to and connects to the address it checked. A refused request
|
||||
fails like any other unreachable asset. Leave it unset for local renders that load
|
||||
assets from your own machine or network.
|
||||
|
||||
## Related topics
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { runInNewContext } from "node:vm";
|
||||
import { Window } from "happy-dom";
|
||||
import type { Page } from "puppeteer-core";
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
|
||||
vi.mock("./publicNetwork.js", () => ({ publicRequest: vi.fn() }));
|
||||
vi.mock("@hyperframes/engine/public-network", () => ({ publicRequest: vi.fn() }));
|
||||
afterEach(() => vi.mocked(publicRequest).mockReset());
|
||||
import { annotateGifAssetMetadata, catalogAssets, type CatalogedAsset } from "./assetCataloger.js";
|
||||
|
||||
|
||||
@@ -22,10 +22,10 @@ import type { DesignTokens } from "./types.js";
|
||||
import type { IconCandidate } from "./faviconRanker.js";
|
||||
import { CAPTURE_USER_AGENT } from "./userAgent.js";
|
||||
import sharp from "sharp";
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
import { loadSharp } from "../utils/optionalPackages.js";
|
||||
|
||||
vi.mock("./publicNetwork.js", () => ({ publicRequest: vi.fn() }));
|
||||
vi.mock("@hyperframes/engine/public-network", () => ({ publicRequest: vi.fn() }));
|
||||
vi.mock("../utils/optionalPackages.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../utils/optionalPackages.js")>();
|
||||
return { ...actual, loadSharp: vi.fn(actual.loadSharp) };
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
import { isBlockedNetworkHost } from "@hyperframes/engine";
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
import { fetchLocalPageUrl } from "./localPage.js";
|
||||
import { ensureCaptureDirSync, writeCaptureFileSync } from "./captureFile.js";
|
||||
import { join, extname } from "node:path";
|
||||
|
||||
@@ -7,9 +7,9 @@ import { Window } from "happy-dom";
|
||||
import type { Page } from "puppeteer-core";
|
||||
import { downloadAndRewriteFonts } from "./assetDownloader.js";
|
||||
import { extractHtml } from "./htmlExtractor.js";
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
|
||||
vi.mock("./publicNetwork.js", () => ({ publicRequest: vi.fn() }));
|
||||
vi.mock("@hyperframes/engine/public-network", () => ({ publicRequest: vi.fn() }));
|
||||
|
||||
const FONT = readFileSync(
|
||||
new URL("../../../../docs/public/catalog/assets/a634cb9e7783af7e.woff2", import.meta.url),
|
||||
|
||||
@@ -5,7 +5,8 @@ import { afterEach, expect, it, vi } from "vitest";
|
||||
|
||||
const mocks = vi.hoisted(() => ({ launch: vi.fn(), proxyClose: vi.fn(), exit: vi.fn() }));
|
||||
vi.mock("puppeteer-core", () => ({ default: { launch: mocks.launch } }));
|
||||
vi.mock("./captureProxy.js", () => ({
|
||||
vi.mock("@hyperframes/engine/public-network", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@hyperframes/engine/public-network")>()),
|
||||
startCaptureProxy: async () => ({ url: "http://127.0.0.1:4567", close: mocks.proxyClose }),
|
||||
}));
|
||||
vi.mock("../utils/commandResult.js", () => ({ requestCliExit: mocks.exit }));
|
||||
|
||||
@@ -7,7 +7,8 @@ const mocks = vi.hoisted(() => ({
|
||||
once: vi.fn(),
|
||||
launch: vi.fn(),
|
||||
}));
|
||||
vi.mock("./captureProxy.js", () => ({
|
||||
vi.mock("@hyperframes/engine/public-network", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@hyperframes/engine/public-network")>()),
|
||||
startCaptureProxy: async () => ({ url: "http://127.0.0.1:4567", close: mocks.proxyClose }),
|
||||
}));
|
||||
vi.mock("../browser/launch.js", () => ({
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import type { Browser } from "puppeteer-core";
|
||||
import { mkdtemp, mkdir, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
launchManagedBrowser,
|
||||
closeManagedBrowser,
|
||||
@@ -9,7 +6,11 @@ import {
|
||||
} from "../browser/launch.js";
|
||||
import { captureBrowserArgs } from "./browserLaunchArgs.js";
|
||||
import { settleAcrossNavigation } from "./navigateForCapture.js";
|
||||
import { startCaptureProxy } from "./captureProxy.js";
|
||||
import {
|
||||
browserProxyArgs,
|
||||
createPublicOnlyProfile,
|
||||
startCaptureProxy,
|
||||
} from "@hyperframes/engine/public-network";
|
||||
import { removeTreeBestEffortAsync } from "@hyperframes/core/remove-tree";
|
||||
|
||||
/** The launch-wide proxy covers pages, popups, workers and both extraction passes. */
|
||||
@@ -42,15 +43,7 @@ export async function launchCaptureBrowser(options: {
|
||||
return closing;
|
||||
};
|
||||
try {
|
||||
const profilePath = await mkdtemp(join(tmpdir(), "hf-capture-profile-"));
|
||||
profile = profilePath;
|
||||
await mkdir(join(profile, "Default"));
|
||||
await writeFile(
|
||||
join(profile, "Default", "Preferences"),
|
||||
JSON.stringify({
|
||||
webrtc: { ip_handling_policy: "disable_non_proxied_udp" },
|
||||
}),
|
||||
);
|
||||
profile = await createPublicOnlyProfile("hf-capture-profile-");
|
||||
unregister = registerBrowserSessionCleanup(close);
|
||||
const puppeteer = await import("puppeteer-core");
|
||||
browser = await launchManagedBrowser(puppeteer.default, {
|
||||
@@ -61,10 +54,10 @@ export async function launchCaptureBrowser(options: {
|
||||
// Linux containers may lack the user namespaces Chromium's sandbox requires.
|
||||
args: [
|
||||
...captureBrowserArgs(options.disableWebgl, options.width, options.height),
|
||||
`--proxy-server=${proxy.url}`,
|
||||
`--proxy-bypass-list=<-loopback>${options.localOrigin ? `;${new URL(options.localOrigin).host}` : ""}`,
|
||||
"--disable-quic",
|
||||
"--force-webrtc-ip-handling-policy=disable_non_proxied_udp",
|
||||
...browserProxyArgs(
|
||||
proxy.url,
|
||||
options.localOrigin ? new URL(options.localOrigin).host : undefined,
|
||||
),
|
||||
],
|
||||
});
|
||||
const newPage = browser.newPage.bind(browser);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
vi.mock("./publicNetwork.js", () => ({ publicRequest: vi.fn() }));
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
vi.mock("@hyperframes/engine/public-network", () => ({ publicRequest: vi.fn() }));
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { discoverLottieResponse, LottieDiscovery } from "./lottieDiscovery.js";
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { saveLottieAnimations } from "./mediaCapture.js";
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
|
||||
vi.mock("./publicNetwork.js", () => ({ publicRequest: vi.fn() }));
|
||||
vi.mock("@hyperframes/engine/public-network", () => ({ publicRequest: vi.fn() }));
|
||||
|
||||
function iconAnimation(color: number[]) {
|
||||
return {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
vi.mock("./publicNetwork.js", () => ({ publicRequest: vi.fn() }));
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
vi.mock("@hyperframes/engine/public-network", () => ({ publicRequest: vi.fn() }));
|
||||
import {
|
||||
existsSync,
|
||||
lstatSync,
|
||||
|
||||
@@ -42,7 +42,7 @@ if (exitAfterMs) {
|
||||
process.exit(0);
|
||||
}, exitAfterMs);
|
||||
}
|
||||
const { resolvePublicAddress } = await import("./publicNetwork.js");
|
||||
const { resolvePublicAddress } = await import("@hyperframes/engine/public-network");
|
||||
const outcome = await resolvePublicAddress("assets.capture-fixture.com").catch(
|
||||
(error: Error) => error.message,
|
||||
);
|
||||
|
||||
@@ -63,7 +63,7 @@ vi.mock("node:https", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
import { createCaptureTlsFixture } from "./fixtures/captureTls.js";
|
||||
|
||||
type RequestReceipt = {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { fileURLToPath } from "node:url";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
import { startTestDnsServer } from "../utils/dnsTestServer.js";
|
||||
import { testProcessIsAlive, waitForTestCondition } from "../utils/processTestUtils.js";
|
||||
import { systemLookupInKillableChild } from "./publicNetwork.js";
|
||||
import { systemLookupInKillableChild } from "@hyperframes/engine/public-network";
|
||||
|
||||
const fixture = join(dirname(fileURLToPath(import.meta.url)), "publicNetwork.fixture.ts");
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ vi.mock("node:tls", async (importOriginal) => {
|
||||
});
|
||||
|
||||
import { safeFetch } from "./assetDownloader.js";
|
||||
import { publicRequest } from "./publicNetwork.js";
|
||||
import { startCaptureProxy } from "./captureProxy.js";
|
||||
import { publicRequest } from "@hyperframes/engine/public-network";
|
||||
import { startCaptureProxy } from "@hyperframes/engine/public-network";
|
||||
import { createCaptureTlsFixture } from "./fixtures/captureTls.js";
|
||||
|
||||
const DESTINATION = "assets.capture-fixture.com";
|
||||
|
||||
@@ -130,7 +130,7 @@ export default defineCommand({
|
||||
},
|
||||
// fallow-ignore-next-line complexity
|
||||
async run({ args }) {
|
||||
const { captureUpstreamProxy } = await import("../capture/upstreamProxy.js");
|
||||
const { captureUpstreamProxy } = await import("@hyperframes/engine/public-network");
|
||||
try {
|
||||
captureUpstreamProxy();
|
||||
} catch (error) {
|
||||
|
||||
@@ -107,6 +107,7 @@ var __dirname = __hf_dirname(__filename);`,
|
||||
"./chrome-host-ceiling",
|
||||
"./chrome-startup",
|
||||
"./network-policy",
|
||||
"./public-network",
|
||||
"./shader-transitions",
|
||||
"./system-memory",
|
||||
"./wsl-host",
|
||||
|
||||
@@ -54,6 +54,12 @@
|
||||
"runtime": "./dist/utils/networkPolicy.js",
|
||||
"types": "./dist/utils/networkPolicy.d.ts",
|
||||
"environments": ["bun", "node"]
|
||||
},
|
||||
"./public-network": {
|
||||
"source": "./src/services/publicNetwork/index.ts",
|
||||
"runtime": "./dist/services/publicNetwork/index.js",
|
||||
"types": "./dist/services/publicNetwork/index.d.ts",
|
||||
"environments": ["bun", "node"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,11 @@
|
||||
"bun": "./src/utils/networkPolicy.ts",
|
||||
"import": "./src/utils/networkPolicy.ts",
|
||||
"types": "./src/utils/networkPolicy.ts"
|
||||
},
|
||||
"./public-network": {
|
||||
"bun": "./src/services/publicNetwork/index.ts",
|
||||
"import": "./src/services/publicNetwork/index.ts",
|
||||
"types": "./src/services/publicNetwork/index.ts"
|
||||
}
|
||||
},
|
||||
"publishConfig": {
|
||||
@@ -94,6 +99,10 @@
|
||||
"./network-policy": {
|
||||
"import": "./dist/utils/networkPolicy.js",
|
||||
"types": "./dist/utils/networkPolicy.d.ts"
|
||||
},
|
||||
"./public-network": {
|
||||
"import": "./dist/services/publicNetwork/index.js",
|
||||
"types": "./dist/services/publicNetwork/index.d.ts"
|
||||
}
|
||||
},
|
||||
"main": "./dist/index.js",
|
||||
|
||||
@@ -10,6 +10,7 @@ export interface BrowserLaunchFingerprint {
|
||||
readonly browserTimeoutMs: number;
|
||||
readonly protocolTimeoutMs: number;
|
||||
readonly requestedCaptureMode: CaptureMode;
|
||||
readonly publicOnly?: { readonly pageServerHost?: string };
|
||||
}
|
||||
|
||||
export interface BrowserLaunchResult {
|
||||
@@ -60,6 +61,7 @@ function fingerprintKey(fingerprint: Readonly<BrowserLaunchFingerprint>): string
|
||||
fingerprint.browserTimeoutMs,
|
||||
fingerprint.protocolTimeoutMs,
|
||||
fingerprint.requestedCaptureMode,
|
||||
fingerprint.publicOnly ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -680,6 +680,16 @@ describe("browser launch capture-mode contract", () => {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it("puts the browser behind the public-only proxy only on a restricted host", () => {
|
||||
const fingerprint = () =>
|
||||
_createBrowserLaunchFingerprintForTests([], { pageServerUrl: "http://localhost:4321" });
|
||||
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "");
|
||||
expect(fingerprint().publicOnly).toBeUndefined();
|
||||
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "true");
|
||||
expect(fingerprint().publicOnly).toEqual({ pageServerHost: "localhost:4321" });
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveBrowserGpuMode", () => {
|
||||
|
||||
@@ -30,7 +30,13 @@ import {
|
||||
isManagedChromeDir,
|
||||
pickSharedShell,
|
||||
} from "./chromeHostCeiling.js";
|
||||
import { DEFAULT_CONFIG, type EngineConfig } from "../config.js";
|
||||
import { DEFAULT_CONFIG, resolveRestrictPageNetwork, type EngineConfig } from "../config.js";
|
||||
import {
|
||||
browserProxyArgs,
|
||||
createPublicOnlyProfile,
|
||||
startCaptureProxy,
|
||||
} from "./publicNetwork/captureProxy.js";
|
||||
import { removeTreeBestEffortAsync } from "@hyperframes/core/remove-tree";
|
||||
import { getSystemTotalMb, LOW_MEMORY_TOTAL_MB_THRESHOLD } from "./systemMemory.js";
|
||||
import { chromeSandboxArgs, launchWithChromeSandboxFallback } from "./chromeSandbox.js";
|
||||
import { describeWslGpuAccess, detectWsl, type WslHostFacts } from "./wslHost.js";
|
||||
@@ -854,7 +860,7 @@ function logResolvedBrowserGpuMode(resolved: "hardware" | "software", reason: st
|
||||
|
||||
type BrowserLaunchConfig = Partial<
|
||||
Pick<EngineConfig, "browserTimeout" | "protocolTimeout" | "chromePath" | "forceScreenshot">
|
||||
> & { withoutWaylandDisplay?: boolean };
|
||||
> & { withoutWaylandDisplay?: boolean; pageServerUrl?: string };
|
||||
|
||||
function createBrowserLaunchFingerprint(
|
||||
chromeArgs: string[],
|
||||
@@ -887,6 +893,11 @@ function createBrowserLaunchFingerprint(
|
||||
browserTimeoutMs: launchConfig.browserTimeout,
|
||||
protocolTimeoutMs: launchConfig.protocolTimeout,
|
||||
requestedCaptureMode,
|
||||
publicOnly: resolveRestrictPageNetwork()
|
||||
? {
|
||||
pageServerHost: launchConfig.pageServerUrl && new URL(launchConfig.pageServerUrl).host,
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -914,6 +925,24 @@ async function launchInsideSandbox(ppt: PuppeteerNode, options: LaunchOptions):
|
||||
}
|
||||
}
|
||||
|
||||
async function publicOnlyNetwork(pageServerHost: string | undefined) {
|
||||
const proxy = await startCaptureProxy(null);
|
||||
try {
|
||||
const userDataDir = await createPublicOnlyProfile("hf-render-profile-");
|
||||
return {
|
||||
args: browserProxyArgs(proxy.url, pageServerHost),
|
||||
userDataDir,
|
||||
async close() {
|
||||
await proxy.close();
|
||||
await removeTreeBestEffortAsync(userDataDir);
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
await proxy.close();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// fallow-ignore-next-line complexity
|
||||
async function launchBrowser(
|
||||
fingerprint: Readonly<BrowserLaunchFingerprint>,
|
||||
@@ -922,6 +951,10 @@ async function launchBrowser(
|
||||
const env = launchEnv(fingerprint);
|
||||
let captureMode = fingerprint.requestedCaptureMode;
|
||||
let browser: Browser | undefined;
|
||||
const network = fingerprint.publicOnly
|
||||
? await publicOnlyNetwork(fingerprint.publicOnly.pageServerHost)
|
||||
: undefined;
|
||||
const browserArgs = network ? [...fingerprint.args, ...network.args] : fingerprint.args;
|
||||
const launch = (args: readonly string[]) => {
|
||||
let attempted = args;
|
||||
return launchWithChromeSandboxFallback(args, (launchArgs) => {
|
||||
@@ -931,6 +964,7 @@ async function launchBrowser(
|
||||
headless: true,
|
||||
args: withHyperframesBrowserMarker(launchArgs),
|
||||
defaultViewport: null,
|
||||
userDataDir: network?.userDataDir,
|
||||
executablePath: fingerprint.executablePath,
|
||||
env,
|
||||
timeout: fingerprint.browserTimeoutMs,
|
||||
@@ -956,7 +990,7 @@ async function launchBrowser(
|
||||
timedOut: false,
|
||||
}));
|
||||
try {
|
||||
browser = await launch(fingerprint.args);
|
||||
browser = await launch(browserArgs);
|
||||
|
||||
const browserVersion = await browser.version().catch(() => "unknown");
|
||||
const gpuFlags = fingerprint.args.filter(
|
||||
@@ -975,7 +1009,7 @@ async function launchBrowser(
|
||||
`${result.detail}; retrying once with a longer budget.`,
|
||||
);
|
||||
await closeBrowserAfterFailedProbe(browser);
|
||||
browser = await launch(fingerprint.args);
|
||||
browser = await launch(browserArgs);
|
||||
result = await probe(browser, BEGINFRAME_PROBE_RETRY_TIMEOUT_MS);
|
||||
}
|
||||
if (!result.supported) {
|
||||
@@ -986,13 +1020,15 @@ async function launchBrowser(
|
||||
`${result.detail}; falling back to screenshot mode.`,
|
||||
);
|
||||
captureMode = "screenshot";
|
||||
browser = await launch([...stripBeginFrameFlags(fingerprint.args), WHOLE_TILE_RASTER_FLAG]);
|
||||
browser = await launch([...stripBeginFrameFlags(browserArgs), WHOLE_TILE_RASTER_FLAG]);
|
||||
}
|
||||
}
|
||||
|
||||
if (network) browser.once("disconnected", () => void network.close());
|
||||
return { browser, captureMode };
|
||||
} catch (error) {
|
||||
await browser?.close().catch(() => {});
|
||||
await network?.close();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { createCaptureSession, createStaticVerificationPage } from "./frameCapture.js";
|
||||
|
||||
// Each page records what it was asked to do; `goto` stops the setup there.
|
||||
const calls: string[] = [];
|
||||
const fakePage = () => ({
|
||||
mainFrame: () => ({}),
|
||||
on: () => {},
|
||||
close: async () => {},
|
||||
goto: async () => {
|
||||
calls.push("goto");
|
||||
throw new Error("stop at navigation");
|
||||
},
|
||||
createCDPSession: async () => ({
|
||||
on: () => {},
|
||||
send: async (method: string, params?: { patterns?: { urlPattern: string }[] }) => {
|
||||
if (method === "Fetch.enable" && params?.patterns?.[0]?.urlPattern === "*") {
|
||||
calls.push("filter");
|
||||
}
|
||||
return {};
|
||||
},
|
||||
}),
|
||||
evaluateOnNewDocument: async () => {},
|
||||
setViewport: async () => {},
|
||||
evaluate: async () => undefined,
|
||||
});
|
||||
|
||||
vi.mock("./browserManager.js", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("./browserManager.js")>()),
|
||||
resolveBrowserGpuMode: async () => "software",
|
||||
acquireBrowser: async () => ({
|
||||
captureMode: "screenshot",
|
||||
browser: { version: async () => "Chrome/150.0.0.0", newPage: async () => fakePage() },
|
||||
}),
|
||||
}));
|
||||
|
||||
const dirs: string[] = [];
|
||||
afterEach(() => {
|
||||
calls.length = 0;
|
||||
vi.unstubAllEnvs();
|
||||
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
async function openPages(): Promise<void> {
|
||||
const dir = mkdtempSync(join(tmpdir(), "hf-page-network-"));
|
||||
dirs.push(dir);
|
||||
const session = await createCaptureSession(
|
||||
"http://localhost:4321",
|
||||
dir,
|
||||
{ width: 320, height: 180, fps: { num: 30, den: 1 }, format: "jpeg" },
|
||||
null,
|
||||
{ forceScreenshot: true },
|
||||
);
|
||||
calls.push("session page ready");
|
||||
await expect(createStaticVerificationPage(session)).rejects.toThrow("stop at navigation");
|
||||
}
|
||||
|
||||
describe("render page address rules", () => {
|
||||
it("guard every composition page before it navigates on a restricted host", async () => {
|
||||
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "true");
|
||||
await openPages();
|
||||
expect(calls).toEqual(["filter", "session page ready", "filter", "goto"]);
|
||||
});
|
||||
|
||||
it("leave local renders alone", async () => {
|
||||
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "");
|
||||
await openPages();
|
||||
expect(calls).toEqual(["session page ready", "goto"]);
|
||||
});
|
||||
});
|
||||
@@ -107,13 +107,7 @@ import {
|
||||
} from "./riskFrames.js";
|
||||
import { getAvailableMemoryMb } from "./systemMemory.js";
|
||||
import { isPsnrFilterAvailable } from "../utils/psnrFilterAvailability.js";
|
||||
import {
|
||||
DEFAULT_CONFIG,
|
||||
applyConcreteGpuScreenshotClamp,
|
||||
resolveRestrictPageNetwork,
|
||||
type EngineConfig,
|
||||
} from "../config.js";
|
||||
import { restrictPageNetwork } from "./pageNetworkPolicy.js";
|
||||
import { DEFAULT_CONFIG, applyConcreteGpuScreenshotClamp, type EngineConfig } from "../config.js";
|
||||
import type {
|
||||
CaptureOptions,
|
||||
CaptureVideoMetadataHint,
|
||||
@@ -1589,7 +1583,11 @@ export async function createCaptureSession(
|
||||
);
|
||||
|
||||
const withoutWaylandDisplay = await captureWithoutWaylandDisplay(resolvedGpuMode);
|
||||
const browserLease = await acquireBrowser(chromeArgs, { ...config, withoutWaylandDisplay });
|
||||
const browserLease = await acquireBrowser(chromeArgs, {
|
||||
...config,
|
||||
withoutWaylandDisplay,
|
||||
pageServerUrl: serverUrl,
|
||||
});
|
||||
return constructCaptureSessionWithRollback({
|
||||
browserLease,
|
||||
serverUrl,
|
||||
@@ -1655,11 +1653,6 @@ async function constructCaptureSessionWithRollback(
|
||||
}
|
||||
}
|
||||
|
||||
/** Every page that loads the composition goes through here, before it navigates. */
|
||||
async function guardPageNetwork(page: Page, serverUrl: string): Promise<void> {
|
||||
if (resolveRestrictPageNetwork()) await restrictPageNetwork(page, serverUrl);
|
||||
}
|
||||
|
||||
async function constructCaptureSession(
|
||||
input: CaptureSessionConstructionInput & { onPageCreated(page: Page): void },
|
||||
): Promise<CaptureSession> {
|
||||
@@ -1679,7 +1672,6 @@ async function constructCaptureSession(
|
||||
|
||||
const page = await browser.newPage();
|
||||
onPageCreated(page);
|
||||
await guardPageNetwork(page, serverUrl);
|
||||
// Cap the existing session's inspection buffer; new sessions have separate buffers.
|
||||
const frame = page.mainFrame();
|
||||
// Frame.client is runtime-only, so keep capture working when it is absent.
|
||||
@@ -4140,7 +4132,6 @@ export async function createStaticVerificationPage(session: CaptureSession): Pro
|
||||
const pageReadyTimeout = session.config?.playerReadyTimeout ?? DEFAULT_CONFIG.playerReadyTimeout;
|
||||
|
||||
try {
|
||||
await guardPageNetwork(page, session.serverUrl);
|
||||
await page.evaluateOnNewDocument(() => {
|
||||
const w = window as unknown as { __name?: <T>(fn: T, _name: string) => T };
|
||||
if (typeof w.__name !== "function") {
|
||||
|
||||
@@ -1,194 +0,0 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createServer, type Server } from "node:http";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
||||
import type { Browser, Page } from "puppeteer-core";
|
||||
import { resolveHeadlessShellPath } from "./browserManager.js";
|
||||
import { restrictPageNetwork } from "./pageNetworkPolicy.js";
|
||||
import { closeTestChrome, launchTestChrome, listenOnLoopback } from "./__fixtures__/loopback.js";
|
||||
|
||||
const chromePath = resolveHeadlessShellPath();
|
||||
const hasBrowser = chromePath !== undefined && spawnSync(chromePath, ["--version"]).status === 0;
|
||||
|
||||
// Chrome dials these names where the rules say; the policy decides from `answers`, its own resolver.
|
||||
const answers: Record<string, string[]> = {
|
||||
"private.hf-test": ["127.0.0.1"],
|
||||
"meta.hf-test": ["169.254.169.254"],
|
||||
"public.hf-test": ["8.8.8.8"],
|
||||
};
|
||||
let flakyLookups = 0;
|
||||
const resolveHost = async (host: string) => {
|
||||
// Fails its first lookup only, like a passing DNS blip.
|
||||
if (host === "flaky.hf-test" && flakyLookups++ === 0) throw new Error(`EAI_AGAIN ${host}`);
|
||||
if (host === "flaky.hf-test") return ["8.8.4.4"];
|
||||
const found = answers[host];
|
||||
if (!found) throw new Error(`ENOTFOUND ${host}`);
|
||||
return found;
|
||||
};
|
||||
|
||||
function serve(handler: (path: string) => { type: string; body: string }): Server {
|
||||
return createServer((req, res) => {
|
||||
const path = new URL(req.url ?? "/", "http://site").pathname;
|
||||
if (path.startsWith("/redirect/")) {
|
||||
res.writeHead(302, { location: decodeURIComponent(path.slice("/redirect/".length)) });
|
||||
return void res.end();
|
||||
}
|
||||
const { type, body } = handler(path);
|
||||
res.writeHead(200, { "content-type": type, "access-control-allow-origin": "*" });
|
||||
res.end(body);
|
||||
});
|
||||
}
|
||||
|
||||
describe.skipIf(!hasBrowser)("restrictPageNetwork in Chrome", () => {
|
||||
let browser: Browser;
|
||||
let site: Server;
|
||||
let other: Server;
|
||||
let siteOrigin: string;
|
||||
let otherPort: string;
|
||||
const otherHits: string[] = [];
|
||||
let page: Page | undefined;
|
||||
|
||||
beforeAll(async () => {
|
||||
site = serve((path) =>
|
||||
path === "/worker.js"
|
||||
? {
|
||||
type: "text/javascript",
|
||||
body: "fetch(self.location.search.slice(1)).then(() => postMessage('ok'), () => postMessage('refused'));",
|
||||
}
|
||||
: path === "/asset.txt"
|
||||
? { type: "text/plain", body: "asset" }
|
||||
: { type: "text/html", body: "<!doctype html><title>site</title>" },
|
||||
);
|
||||
other = createServer((req, res) => {
|
||||
otherHits.push(req.url ?? "/");
|
||||
res.writeHead(200, { "content-type": "text/plain", "access-control-allow-origin": "*" });
|
||||
res.end("secret");
|
||||
});
|
||||
siteOrigin = await listenOnLoopback(site);
|
||||
otherPort = new URL(await listenOnLoopback(other)).port;
|
||||
// The render browser runs frames in-process (no site isolation); mirror that.
|
||||
browser = await launchTestChrome(chromePath, [
|
||||
"--disable-features=IsolateOrigins,site-per-process",
|
||||
"--host-resolver-rules=MAP private.hf-test 127.0.0.1, MAP public.hf-test 127.0.0.1, MAP flaky.hf-test 127.0.0.1, MAP meta.hf-test 169.254.169.254",
|
||||
]);
|
||||
});
|
||||
afterEach(async () => {
|
||||
await page?.close();
|
||||
page = undefined;
|
||||
otherHits.length = 0;
|
||||
});
|
||||
afterAll(async () => {
|
||||
await closeTestChrome(browser);
|
||||
site?.close();
|
||||
other?.close();
|
||||
});
|
||||
|
||||
async function openRestricted(refused: string[]): Promise<Page> {
|
||||
page = await browser.newPage();
|
||||
page.on("requestfailed", (request) => {
|
||||
if (request.failure()?.errorText.startsWith("net::ERR_BLOCKED_BY_CLIENT"))
|
||||
refused.push(request.url());
|
||||
});
|
||||
await restrictPageNetwork(page, siteOrigin, resolveHost);
|
||||
await page.goto(`${siteOrigin}/`, { waitUntil: "load" });
|
||||
return page;
|
||||
}
|
||||
|
||||
const fetchText = (target: Page, url: string) =>
|
||||
target.evaluate(
|
||||
(u) =>
|
||||
fetch(u).then(
|
||||
(r) => r.text(),
|
||||
() => "refused",
|
||||
),
|
||||
url,
|
||||
);
|
||||
|
||||
it("lets the page load from its own server", async () => {
|
||||
const restricted = await openRestricted([]);
|
||||
expect(await fetchText(restricted, `${siteOrigin}/asset.txt`)).toBe("asset");
|
||||
});
|
||||
|
||||
it("refuses another local port, by address and by a name that resolves to it", async () => {
|
||||
const refused: string[] = [];
|
||||
const restricted = await openRestricted(refused);
|
||||
expect(await fetchText(restricted, `http://127.0.0.1:${otherPort}/by-address`)).toBe("refused");
|
||||
expect(await fetchText(restricted, `http://private.hf-test:${otherPort}/by-name`)).toBe(
|
||||
"refused",
|
||||
);
|
||||
expect(otherHits).toEqual([]);
|
||||
expect(refused).toEqual([
|
||||
`http://127.0.0.1:${otherPort}/by-address`,
|
||||
`http://private.hf-test:${otherPort}/by-name`,
|
||||
]);
|
||||
});
|
||||
|
||||
it("refuses link-local addresses, by address and by name, without waiting on them", async () => {
|
||||
const refused: string[] = [];
|
||||
const restricted = await openRestricted(refused);
|
||||
const started = Date.now();
|
||||
expect(await fetchText(restricted, "http://169.254.169.254/")).toBe("refused");
|
||||
expect(await fetchText(restricted, "http://meta.hf-test/")).toBe("refused");
|
||||
expect(await fetchText(restricted, "http://2852039166/")).toBe("refused");
|
||||
expect(Date.now() - started).toBeLessThan(5_000);
|
||||
expect(refused).toEqual([
|
||||
"http://169.254.169.254/",
|
||||
"http://meta.hf-test/",
|
||||
"http://169.254.169.254/",
|
||||
]);
|
||||
});
|
||||
|
||||
it("refuses frames and workers too", async () => {
|
||||
const restricted = await openRestricted([]);
|
||||
await restricted.evaluate(
|
||||
(src) =>
|
||||
new Promise<void>((resolve) => {
|
||||
const frame = document.createElement("iframe");
|
||||
frame.onload = frame.onerror = () => resolve();
|
||||
frame.src = src;
|
||||
document.body.append(frame);
|
||||
setTimeout(resolve, 2_000);
|
||||
}),
|
||||
`http://127.0.0.1:${otherPort}/frame`,
|
||||
);
|
||||
const fromWorker = await restricted.evaluate(
|
||||
(src) =>
|
||||
new Promise<string>((resolve) => {
|
||||
const worker = new Worker(src);
|
||||
worker.onmessage = (event) => resolve(String(event.data));
|
||||
worker.onerror = () => resolve("worker failed to start");
|
||||
}),
|
||||
`${siteOrigin}/worker.js?http://127.0.0.1:${otherPort}/worker`,
|
||||
);
|
||||
expect(fromWorker).toBe("refused");
|
||||
expect(otherHits).toEqual([]);
|
||||
});
|
||||
|
||||
it("refuses a redirect from its own server to another local port", async () => {
|
||||
const restricted = await openRestricted([]);
|
||||
const target = `http://127.0.0.1:${otherPort}/redirected`;
|
||||
expect(
|
||||
await fetchText(restricted, `${siteOrigin}/redirect/${encodeURIComponent(target)}`),
|
||||
).toBe("refused");
|
||||
expect(otherHits).toEqual([]);
|
||||
});
|
||||
|
||||
it("allows a name that resolves to a public address", async () => {
|
||||
const restricted = await openRestricted([]);
|
||||
expect(await fetchText(restricted, `http://public.hf-test:${otherPort}/public`)).toBe("secret");
|
||||
expect(otherHits).toEqual(["/public"]);
|
||||
});
|
||||
|
||||
it("asks again after a lookup fails, rather than refusing the host for the rest of the page", async () => {
|
||||
const restricted = await openRestricted([]);
|
||||
expect(await fetchText(restricted, `http://flaky.hf-test:${otherPort}/first`)).toBe("refused");
|
||||
expect(await fetchText(restricted, `http://flaky.hf-test:${otherPort}/second`)).toBe("secret");
|
||||
expect(otherHits).toEqual(["/second"]);
|
||||
});
|
||||
|
||||
it("refuses a name it cannot resolve", async () => {
|
||||
const refused: string[] = [];
|
||||
const restricted = await openRestricted(refused);
|
||||
expect(await fetchText(restricted, `http://unknown.hf-test:${otherPort}/x`)).toBe("refused");
|
||||
expect(refused).toEqual([`http://unknown.hf-test:${otherPort}/x`]);
|
||||
});
|
||||
});
|
||||
@@ -1,75 +0,0 @@
|
||||
import { lookup } from "node:dns/promises";
|
||||
import { isIP } from "node:net";
|
||||
import type { CDPSession, Page, Protocol } from "puppeteer-core";
|
||||
import { isBlockedNetworkHost } from "../utils/networkPolicy.js";
|
||||
|
||||
type ResolveHost = (hostname: string) => Promise<string[]>;
|
||||
|
||||
const systemResolveHost: ResolveHost = async (hostname) =>
|
||||
(await lookup(hostname, { all: true, verbatim: true })).map(({ address }) => address);
|
||||
|
||||
type HostVerdict = "public" | "non-public" | "unresolved";
|
||||
|
||||
async function hostVerdict(hostname: string, resolveHost: ResolveHost): Promise<HostVerdict> {
|
||||
const host = hostname.replace(/^\[|\]$/g, "");
|
||||
if (isBlockedNetworkHost(host)) return "non-public";
|
||||
if (isIP(host)) return "public";
|
||||
let addresses: string[];
|
||||
try {
|
||||
addresses = await resolveHost(host);
|
||||
} catch {
|
||||
return "unresolved";
|
||||
}
|
||||
if (addresses.length === 0) return "unresolved";
|
||||
return addresses.some((a) => isBlockedNetworkHost(a.split("%")[0]!)) ? "non-public" : "public";
|
||||
}
|
||||
|
||||
/**
|
||||
* The page reaches only its own server and public addresses (the asset downloader's rule, by host and by
|
||||
* resolved address). Install before navigation; a refused request fails with net::ERR_BLOCKED_BY_CLIENT.
|
||||
*/
|
||||
export async function restrictPageNetwork(
|
||||
page: Page,
|
||||
allowedOrigin: string,
|
||||
resolveHost: ResolveHost = systemResolveHost,
|
||||
): Promise<void> {
|
||||
const own = new URL(allowedOrigin).origin;
|
||||
const verdicts = new Map<string, Promise<HostVerdict>>();
|
||||
const forgetFailedLookup = (host: string, asked: Promise<HostVerdict>, answer: HostVerdict) => {
|
||||
if (answer === "unresolved" && verdicts.get(host) === asked) verdicts.delete(host);
|
||||
};
|
||||
const allows = (url: string): Promise<boolean> => {
|
||||
let target: URL;
|
||||
try {
|
||||
target = new URL(url);
|
||||
} catch {
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
if (target.origin === own || !/^(https?|wss?):$/.test(target.protocol)) {
|
||||
return Promise.resolve(true);
|
||||
}
|
||||
const host = target.hostname;
|
||||
let verdict = verdicts.get(host);
|
||||
if (!verdict) {
|
||||
const asked = hostVerdict(host, resolveHost);
|
||||
verdicts.set(host, asked);
|
||||
void asked.then((answer) => forgetFailedLookup(host, asked, answer));
|
||||
verdict = asked;
|
||||
}
|
||||
return verdict.then((answer) => answer === "public");
|
||||
};
|
||||
const client: CDPSession = await page.createCDPSession();
|
||||
client.on("Fetch.requestPaused", (event: Protocol.Fetch.RequestPausedEvent) => {
|
||||
void allows(event.request.url).then((allowed) => {
|
||||
const settled = allowed
|
||||
? client.send("Fetch.continueRequest", { requestId: event.requestId })
|
||||
: client.send("Fetch.failRequest", {
|
||||
requestId: event.requestId,
|
||||
errorReason: "BlockedByClient",
|
||||
});
|
||||
// A page closed mid-request rejects the reply; there is nothing left to settle.
|
||||
return settled.catch(() => undefined);
|
||||
});
|
||||
});
|
||||
await client.send("Fetch.enable", { patterns: [{ urlPattern: "*", requestStage: "Request" }] });
|
||||
}
|
||||
+28
-4
@@ -1,5 +1,8 @@
|
||||
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
|
||||
import { createServer, request, type IncomingMessage, type ServerResponse } from "node:http";
|
||||
import { connect, type Socket } from "node:net";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { Duplex } from "node:stream";
|
||||
import { assertPublicHostname, resolvePublicAddress } from "./publicNetwork.js";
|
||||
import {
|
||||
@@ -8,7 +11,7 @@ import {
|
||||
openUpstreamTunnel,
|
||||
type UpstreamProxy,
|
||||
} from "./upstreamProxy.js";
|
||||
import { listenOnBrowserSafePort } from "@hyperframes/engine";
|
||||
import { listenOnBrowserSafePort } from "../../utils/browserSafePort.js";
|
||||
|
||||
const CONNECTION_TIMEOUT_MS = 15_000;
|
||||
const HOP_HEADERS = new Set([
|
||||
@@ -173,9 +176,11 @@ async function forwardTunnel(
|
||||
}
|
||||
}
|
||||
|
||||
/** Every browser target uses this proxy; it dials a checked numeric IPv4 or the opt-in upstream proxy. */
|
||||
export async function startCaptureProxy(): Promise<{ url: string; close(): Promise<void> }> {
|
||||
const proxy = captureUpstreamProxy();
|
||||
/** Every browser target uses this proxy; it dials a checked numeric IPv4, or (unless null) the upstream proxy. */
|
||||
export async function startCaptureProxy(
|
||||
upstream: UpstreamProxy | null = captureUpstreamProxy() ?? null,
|
||||
): Promise<{ url: string; close(): Promise<void> }> {
|
||||
const proxy = upstream ?? undefined;
|
||||
const sockets = new Set<Socket>();
|
||||
const server = createServer(
|
||||
(incoming, response) => void forwardHttp(incoming, response, sockets, proxy),
|
||||
@@ -205,3 +210,22 @@ export async function startCaptureProxy(): Promise<{ url: string; close(): Promi
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function browserProxyArgs(proxyUrl: string, directHost?: string): string[] {
|
||||
return [
|
||||
`--proxy-server=${proxyUrl}`,
|
||||
`--proxy-bypass-list=<-loopback>${directHost ? `;${directHost}` : ""}`,
|
||||
"--disable-quic",
|
||||
"--force-webrtc-ip-handling-policy=disable_non_proxied_udp",
|
||||
];
|
||||
}
|
||||
|
||||
export async function createPublicOnlyProfile(prefix: string): Promise<string> {
|
||||
const profile = await mkdtemp(join(tmpdir(), prefix));
|
||||
await mkdir(join(profile, "Default"));
|
||||
await writeFile(
|
||||
join(profile, "Default", "Preferences"),
|
||||
JSON.stringify({ webrtc: { ip_handling_policy: "disable_non_proxied_udp" } }),
|
||||
);
|
||||
return profile;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from "./publicNetwork.js";
|
||||
export * from "./captureProxy.js";
|
||||
export * from "./upstreamProxy.js";
|
||||
+1
-1
@@ -5,7 +5,7 @@ import { isIP, type Socket } from "node:net";
|
||||
import { request as httpRequest, type IncomingMessage, type ClientRequest } from "node:http";
|
||||
import { request as httpsRequest, type RequestOptions } from "node:https";
|
||||
import { connect as tlsConnect } from "node:tls";
|
||||
import { isBlockedNetworkHost } from "@hyperframes/engine/network-policy";
|
||||
import { isBlockedNetworkHost } from "../../utils/networkPolicy.js";
|
||||
import {
|
||||
captureUpstreamProxy,
|
||||
connectAuthority,
|
||||
@@ -0,0 +1,334 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createSocket, type Socket as UdpSocket } from "node:dgram";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { createServer, type IncomingMessage, type RequestOptions, type Server } from "node:http";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import type { Page } from "puppeteer-core";
|
||||
import { resolveHeadlessShellPath } from "./browserManager.js";
|
||||
import { closeCaptureSession, createCaptureSession, type CaptureSession } from "./frameCapture.js";
|
||||
|
||||
const { REBINDING, dialed } = vi.hoisted(() => ({
|
||||
REBINDING: "rebind.hf-test",
|
||||
dialed: [] as string[],
|
||||
}));
|
||||
|
||||
// A rebinding name: every address check hears the public 8.8.8.8, a connection made by name lands on 127.0.0.1.
|
||||
vi.mock("node:dns/promises", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("node:dns/promises")>();
|
||||
const lookup = ((host: string, options?: object) =>
|
||||
host === REBINDING
|
||||
? Promise.resolve([{ address: "8.8.8.8", family: 4 }])
|
||||
: real.lookup(host, options as never)) as typeof real.lookup;
|
||||
return { ...real, lookup, default: { ...real, lookup } };
|
||||
});
|
||||
vi.mock("node:dns", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("node:dns")>();
|
||||
class Resolver extends real.promises.Resolver {
|
||||
override resolve4(host: string): Promise<string[]> {
|
||||
return host === REBINDING ? Promise.resolve(["8.8.8.8"]) : super.resolve4(host);
|
||||
}
|
||||
}
|
||||
const promises = { ...real.promises, Resolver };
|
||||
return { ...real, promises, default: { ...real, promises } };
|
||||
});
|
||||
vi.mock("node:http", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("node:http")>();
|
||||
const request = ((options: RequestOptions, callback?: (res: IncomingMessage) => void) => {
|
||||
const host = options.hostname ?? "";
|
||||
if (host !== REBINDING && host !== "8.8.8.8") return real.request(options, callback);
|
||||
dialed.push(host);
|
||||
// The public peer is offline here; a dial by name is what the rebinding answer reaches.
|
||||
return host === "8.8.8.8"
|
||||
? real.request(
|
||||
{
|
||||
...options,
|
||||
hostname: "offline.invalid",
|
||||
lookup: (_h, _o, cb) => cb(new Error("offline"), "", 4),
|
||||
},
|
||||
callback,
|
||||
)
|
||||
: real.request({ ...options, hostname: "127.0.0.1" }, callback);
|
||||
}) as typeof real.request;
|
||||
return { ...real, request, default: { ...real, request } };
|
||||
});
|
||||
// Chrome's own resolver agrees with the connection-time answer.
|
||||
vi.mock("./browserManager.js", async (importOriginal) => {
|
||||
const real = await importOriginal<typeof import("./browserManager.js")>();
|
||||
return {
|
||||
...real,
|
||||
buildChromeArgs: (...args: Parameters<typeof real.buildChromeArgs>) => [
|
||||
...real.buildChromeArgs(...args),
|
||||
`--host-resolver-rules=MAP ${REBINDING} 127.0.0.1`,
|
||||
],
|
||||
};
|
||||
});
|
||||
|
||||
const chromePath = resolveHeadlessShellPath();
|
||||
const hasBrowser = chromePath !== undefined && spawnSync(chromePath, ["--version"]).status === 0;
|
||||
|
||||
const SHARED_WORKER = `onconnect = (e) => { const port = e.ports[0];
|
||||
port.onmessage = (m) => fetch(m.data, { method: "POST", body: "x" }).then(() => port.postMessage("reached"), () => port.postMessage("refused")); };`;
|
||||
const SERVICE_WORKER = `self.addEventListener("install", () => self.skipWaiting());
|
||||
self.addEventListener("activate", (e) => e.waitUntil(self.clients.claim()));
|
||||
self.addEventListener("message", (m) => fetch(m.data).then(() => m.source.postMessage("reached"), () => m.source.postMessage("refused")));`;
|
||||
const DEDICATED_WORKER = `onmessage = (m) => fetch(m.data).then(() => postMessage("reached"), () => postMessage("refused"));`;
|
||||
|
||||
function listen(server: Server | UdpSocket): Promise<number> {
|
||||
return new Promise((resolve) => {
|
||||
const bound = () => {
|
||||
const address = server.address();
|
||||
resolve(typeof address === "object" && address ? address.port : 0);
|
||||
};
|
||||
if ("bind" in server) server.bind(0, "127.0.0.1", bound);
|
||||
else server.listen(0, "127.0.0.1", bound);
|
||||
});
|
||||
}
|
||||
|
||||
describe.skipIf(!hasBrowser)("a render page on a restricted host", () => {
|
||||
let site: Server;
|
||||
let stash: Server;
|
||||
let stun: UdpSocket;
|
||||
let siteOrigin: string;
|
||||
let stashPort: number;
|
||||
let stunPort: number;
|
||||
let session: CaptureSession;
|
||||
let outputDir: string;
|
||||
// Every TCP connection and UDP packet the stand-in private host receives.
|
||||
const reached: string[] = [];
|
||||
|
||||
beforeAll(async () => {
|
||||
vi.stubEnv("PRODUCER_RESTRICT_PAGE_NETWORK", "true");
|
||||
site = createServer((req, res) => {
|
||||
const path = new URL(req.url ?? "/", "http://site").pathname;
|
||||
if (path.startsWith("/redirect/")) {
|
||||
res.writeHead(302, { location: decodeURIComponent(path.slice("/redirect/".length)) });
|
||||
return void res.end();
|
||||
}
|
||||
const scripts: Record<string, string> = {
|
||||
"/shared-worker.js": SHARED_WORKER,
|
||||
"/sw.js": SERVICE_WORKER,
|
||||
"/worker.js": DEDICATED_WORKER,
|
||||
};
|
||||
const script = scripts[path];
|
||||
res.writeHead(200, {
|
||||
"content-type": script ? "text/javascript" : path === "/" ? "text/html" : "text/plain",
|
||||
});
|
||||
res.end(script ?? (path === "/" ? "<!doctype html><title>render</title>" : "asset"));
|
||||
});
|
||||
stash = createServer((_req, res) => {
|
||||
reached.push("http");
|
||||
res.end("secret");
|
||||
});
|
||||
stash.on("connection", () => reached.push("tcp"));
|
||||
stash.on("upgrade", (_req, socket) => socket.destroy());
|
||||
stun = createSocket("udp4");
|
||||
stun.on("message", () => reached.push("udp"));
|
||||
// The render file server's own address form.
|
||||
siteOrigin = `http://localhost:${await listen(site)}`;
|
||||
stashPort = await listen(stash);
|
||||
stunPort = await listen(stun);
|
||||
outputDir = mkdtempSync(join(tmpdir(), "hf-render-network-"));
|
||||
session = await createCaptureSession(
|
||||
siteOrigin,
|
||||
outputDir,
|
||||
{ width: 320, height: 180, fps: { num: 30, den: 1 }, format: "jpeg" },
|
||||
null,
|
||||
{ forceScreenshot: true, enableBrowserPool: false },
|
||||
);
|
||||
});
|
||||
afterEach(() => {
|
||||
reached.length = 0;
|
||||
dialed.length = 0;
|
||||
stash.closeAllConnections();
|
||||
});
|
||||
afterAll(async () => {
|
||||
if (session) await closeCaptureSession(session);
|
||||
site?.close();
|
||||
stash?.close();
|
||||
stun?.close();
|
||||
if (outputDir) rmSync(outputDir, { recursive: true, force: true });
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
async function page(): Promise<Page> {
|
||||
await session.page.goto(`${siteOrigin}/`, { waitUntil: "load" });
|
||||
return session.page;
|
||||
}
|
||||
const stashUrl = (path: string, host = "127.0.0.1") => `http://${host}:${stashPort}${path}`;
|
||||
|
||||
it("loads from its own server", async () => {
|
||||
const p = await page();
|
||||
expect(await p.evaluate((u) => fetch(u).then((r) => r.text()), `${siteOrigin}/asset.txt`)).toBe(
|
||||
"asset",
|
||||
);
|
||||
});
|
||||
|
||||
it("cannot POST to another local port from the page", async () => {
|
||||
const p = await page();
|
||||
const answer = await p.evaluate(
|
||||
(u) =>
|
||||
fetch(u, { method: "POST", body: "x" }).then(
|
||||
() => "reached",
|
||||
() => "refused",
|
||||
),
|
||||
stashUrl("/page"),
|
||||
);
|
||||
expect({ answer, reached }).toEqual({ answer: "refused", reached: [] });
|
||||
});
|
||||
|
||||
it("cannot reach it from an iframe", async () => {
|
||||
const p = await page();
|
||||
await p.evaluate(
|
||||
(src) =>
|
||||
new Promise<void>((resolve) => {
|
||||
const frame = document.createElement("iframe");
|
||||
frame.onload = () => resolve();
|
||||
frame.onerror = () => resolve();
|
||||
frame.src = src;
|
||||
document.body.append(frame);
|
||||
}),
|
||||
stashUrl("/frame"),
|
||||
);
|
||||
expect(reached).toEqual([]);
|
||||
});
|
||||
|
||||
it("cannot reach it from a dedicated worker", async () => {
|
||||
const p = await page();
|
||||
const answer = await p.evaluate(
|
||||
([src, target]) =>
|
||||
new Promise<string>((resolve) => {
|
||||
const worker = new Worker(src!);
|
||||
worker.onmessage = (m) => resolve(String(m.data));
|
||||
worker.onerror = () => resolve("worker failed");
|
||||
worker.postMessage(target);
|
||||
}),
|
||||
[`${siteOrigin}/worker.js`, stashUrl("/worker")],
|
||||
);
|
||||
expect({ answer, reached }).toEqual({ answer: "refused", reached: [] });
|
||||
});
|
||||
|
||||
it("cannot POST to it from a shared worker", async () => {
|
||||
const p = await page();
|
||||
const answer = await p.evaluate(
|
||||
([src, target]) =>
|
||||
new Promise<string>((resolve) => {
|
||||
const worker = new SharedWorker(src!);
|
||||
worker.port.onmessage = (m) => resolve(String(m.data));
|
||||
worker.onerror = () => resolve("worker failed");
|
||||
worker.port.postMessage(target);
|
||||
}),
|
||||
[`${siteOrigin}/shared-worker.js`, stashUrl("/shared-worker")],
|
||||
);
|
||||
expect({ answer, reached }).toEqual({ answer: "refused", reached: [] });
|
||||
});
|
||||
|
||||
it("cannot reach it from a service worker", async () => {
|
||||
const p = await page();
|
||||
const answer = await p.evaluate(
|
||||
async ([src, target]) => {
|
||||
await navigator.serviceWorker.register(src!);
|
||||
const registration = await navigator.serviceWorker.ready;
|
||||
return new Promise<string>((resolve) => {
|
||||
navigator.serviceWorker.onmessage = (m) => resolve(String(m.data));
|
||||
registration.active!.postMessage(target);
|
||||
});
|
||||
},
|
||||
[`${siteOrigin}/sw.js`, stashUrl("/service-worker")],
|
||||
);
|
||||
expect({ answer, reached }).toEqual({ answer: "refused", reached: [] });
|
||||
});
|
||||
|
||||
it("cannot open a WebSocket to it", async () => {
|
||||
const p = await page();
|
||||
await p.evaluate(
|
||||
(u) =>
|
||||
new Promise<void>((resolve) => {
|
||||
const socket = new WebSocket(u);
|
||||
socket.onopen = socket.onerror = socket.onclose = () => resolve();
|
||||
}),
|
||||
`ws://127.0.0.1:${stashPort}/ws`,
|
||||
);
|
||||
expect(reached).toEqual([]);
|
||||
});
|
||||
|
||||
it("cannot send WebRTC traffic to it, over UDP or TCP", async () => {
|
||||
const p = await page();
|
||||
const gathered = p.evaluate(
|
||||
async ([stunUrl, turnUrl]) => {
|
||||
const pc = new RTCPeerConnection({
|
||||
iceServers: [{ urls: stunUrl! }, { urls: turnUrl!, username: "u", credential: "p" }],
|
||||
});
|
||||
pc.createDataChannel("x");
|
||||
await pc.setLocalDescription(await pc.createOffer());
|
||||
await new Promise<void>((resolve) => {
|
||||
if (pc.iceGatheringState === "complete") resolve();
|
||||
pc.onicegatheringstatechange = () => pc.iceGatheringState === "complete" && resolve();
|
||||
});
|
||||
pc.close();
|
||||
},
|
||||
[`stun:127.0.0.1:${stunPort}`, `turn:127.0.0.1:${stashPort}?transport=tcp`],
|
||||
);
|
||||
// A packet that does arrive ends the wait early; gathering completing is the clean end.
|
||||
const firstPacket = new Promise<void>((resolve) => {
|
||||
stun.once("message", () => resolve());
|
||||
stash.once("connection", () => resolve());
|
||||
});
|
||||
await Promise.race([gathered, firstPacket]);
|
||||
expect(reached).toEqual([]);
|
||||
});
|
||||
|
||||
it("cannot prefetch or preload from it", async () => {
|
||||
const p = await page();
|
||||
await p.evaluate(
|
||||
(urls) =>
|
||||
Promise.all(
|
||||
urls.map(
|
||||
([rel, href]) =>
|
||||
new Promise<void>((resolve) => {
|
||||
const link = document.createElement("link");
|
||||
link.rel = rel!;
|
||||
if (rel === "preload") link.as = "fetch";
|
||||
link.crossOrigin = "anonymous";
|
||||
link.onload = link.onerror = () => resolve();
|
||||
link.href = href!;
|
||||
document.head.append(link);
|
||||
}),
|
||||
),
|
||||
),
|
||||
[
|
||||
["prefetch", stashUrl("/prefetch")],
|
||||
["preload", stashUrl("/preload")],
|
||||
],
|
||||
);
|
||||
expect(reached).toEqual([]);
|
||||
});
|
||||
|
||||
it("cannot be redirected to it by its own server", async () => {
|
||||
const p = await page();
|
||||
const answer = await p.evaluate(
|
||||
(u) =>
|
||||
fetch(u).then(
|
||||
() => "reached",
|
||||
() => "refused",
|
||||
),
|
||||
`${siteOrigin}/redirect/${encodeURIComponent(stashUrl("/redirected"))}`,
|
||||
);
|
||||
expect({ answer, reached }).toEqual({ answer: "refused", reached: [] });
|
||||
});
|
||||
|
||||
it("cannot reach it through a name that rebinds from public to private", async () => {
|
||||
const p = await page();
|
||||
const answer = await p.evaluate(
|
||||
(u) =>
|
||||
fetch(u).then(
|
||||
() => "reached",
|
||||
() => "refused",
|
||||
),
|
||||
stashUrl("/rebound", REBINDING),
|
||||
);
|
||||
expect({ answer, reached }).toEqual({ answer: "refused", reached: [] });
|
||||
expect(dialed).toEqual(["8.8.8.8"]);
|
||||
});
|
||||
});
|
||||
@@ -2135,6 +2135,18 @@ export const ALLOWED_DELETIONS = new Map([
|
||||
"packages/cli/src/commands/cloud/render.pageMotionBlur.test.ts",
|
||||
"tests for the removed cloud render --page-motion-blur flag; the hosted render API never accepted the field",
|
||||
],
|
||||
[
|
||||
"packages/engine/src/services/pageNetworkPolicy.ts",
|
||||
"replaced by the browser-wide public-only proxy every render browser launches behind on a restricted host",
|
||||
],
|
||||
[
|
||||
"packages/engine/src/services/pageNetworkPolicy.browser.test.ts",
|
||||
"its cases moved to renderNetwork.browser.test.ts, which drives the browser-wide proxy through a real capture session",
|
||||
],
|
||||
[
|
||||
"packages/engine/src/services/frameCapture-pageNetwork.test.ts",
|
||||
"pinned the removed per-page filter; browserManager.test.ts pins when a render browser goes behind the proxy",
|
||||
],
|
||||
]);
|
||||
|
||||
export function parseBase(argv, fallback = "origin/main") {
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
},
|
||||
"@hyperframes/engine": {
|
||||
"packed": 490452,
|
||||
"unpacked": 1620913
|
||||
"unpacked": 1643000
|
||||
},
|
||||
"@hyperframes/gcp-cloud-run": {
|
||||
"packed": 49412,
|
||||
|
||||
Reference in New Issue
Block a user