mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
fix(cli): keep Chrome's sandbox on for website capture where the host supports it (#7774)
* fix(cli): keep Chrome's sandbox on for website capture where the host supports it `hyperframes capture` added --no-sandbox on every Linux host. It now uses the engine's host check, the same one rendering uses: the sandbox stays on unless the process runs as root, in a container or serverless runtime, or with user namespaces restricted. HYPERFRAMES_CHROME_SANDBOX=0/1 still overrides, and a launch Chrome refuses with the sandbox is still retried without it. When the host check turns the sandbox off, capture prints a one-time warning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cli): drop the new comment block from the capture launch args Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e4e93d6ed9
commit
27b195febc
@@ -1,12 +1,71 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { captureBrowserArgs } from "./browserLaunchArgs.js";
|
||||
import type { ChromeSandboxHost } from "@hyperframes/engine";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
_resetCaptureSandboxWarningForTests,
|
||||
captureBrowserArgs,
|
||||
captureSandboxEnabled,
|
||||
} from "./browserLaunchArgs.js";
|
||||
|
||||
const NO_SANDBOX = ["--no-sandbox", "--disable-setuid-sandbox", "--no-zygote"];
|
||||
|
||||
function host(overrides: Partial<ChromeSandboxHost> = {}): ChromeSandboxHost {
|
||||
return {
|
||||
platform: "linux",
|
||||
env: {},
|
||||
uid: 1000,
|
||||
exists: () => false,
|
||||
read: () => undefined,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("capture browser sandbox", () => {
|
||||
it.each(["darwin", "win32"] as const)("keeps Chromium's sandbox on %s", (platform) => {
|
||||
expect(captureBrowserArgs(false, 800, 600, platform)).not.toContain("--no-sandbox");
|
||||
afterEach(() => {
|
||||
_resetCaptureSandboxWarningForTests();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("retains the Linux container workaround", () => {
|
||||
expect(captureBrowserArgs(false, 800, 600, "linux")).toContain("--no-sandbox");
|
||||
it("keeps Chrome's sandbox on a Linux host that can give it one", () => {
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
expect(captureSandboxEnabled(host())).toBe(true);
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each(["darwin", "win32"] as const)("keeps Chrome's sandbox on %s", (platform) => {
|
||||
expect(captureSandboxEnabled(host({ platform, uid: 0 }))).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["root", host({ uid: 0 })],
|
||||
["a container", host({ exists: (path) => path === "/.dockerenv" })],
|
||||
["serverless", host({ env: { AWS_LAMBDA_FUNCTION_NAME: "fn" } })],
|
||||
[
|
||||
"restricted user namespaces",
|
||||
host({
|
||||
read: (path) =>
|
||||
path === "/proc/sys/kernel/apparmor_restrict_unprivileged_userns" ? "1" : undefined,
|
||||
}),
|
||||
],
|
||||
])("turns the sandbox off on %s and says so once", (_label, sandboxHost) => {
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
expect(captureSandboxEnabled(sandboxHost)).toBe(false);
|
||||
expect(captureSandboxEnabled(sandboxHost)).toBe(false);
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
expect(warn.mock.calls[0]![0]).toContain("sandbox is off for website capture");
|
||||
});
|
||||
|
||||
it("follows HYPERFRAMES_CHROME_SANDBOX, and an explicit 0 stays quiet", () => {
|
||||
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
expect(captureSandboxEnabled(host({ env: { HYPERFRAMES_CHROME_SANDBOX: "0" } }))).toBe(false);
|
||||
expect(captureSandboxEnabled(host({ uid: 0, env: { HYPERFRAMES_CHROME_SANDBOX: "1" } }))).toBe(
|
||||
true,
|
||||
);
|
||||
expect(warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("adds the no-sandbox flags only when the sandbox is off", () => {
|
||||
const on = captureBrowserArgs(false, 800, 600, true);
|
||||
for (const flag of NO_SANDBOX) expect(on).not.toContain(flag);
|
||||
expect(captureBrowserArgs(false, 800, 600, false)).toEqual(expect.arrayContaining(NO_SANDBOX));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,12 +1,35 @@
|
||||
import {
|
||||
chromeSandboxArgs,
|
||||
chromeSandboxEnabled,
|
||||
type ChromeSandboxHost,
|
||||
} from "@hyperframes/engine";
|
||||
|
||||
let warnedSandboxOff = false;
|
||||
|
||||
export function _resetCaptureSandboxWarningForTests(): void {
|
||||
warnedSandboxOff = false;
|
||||
}
|
||||
|
||||
export function captureSandboxEnabled(host?: ChromeSandboxHost): boolean {
|
||||
const enabled = chromeSandboxEnabled(host);
|
||||
const env = host?.env ?? process.env;
|
||||
if (!enabled && env.HYPERFRAMES_CHROME_SANDBOX !== "0" && !warnedSandboxOff) {
|
||||
warnedSandboxOff = true;
|
||||
console.warn(
|
||||
"[hyperframes] Chrome's sandbox is off for website capture on this host (running as root, in a container or serverless runtime, or with user namespaces restricted). Capture only sites you trust here, or set HYPERFRAMES_CHROME_SANDBOX=1 to require the sandbox.",
|
||||
);
|
||||
}
|
||||
return enabled;
|
||||
}
|
||||
|
||||
export function captureBrowserArgs(
|
||||
disableWebgl: boolean,
|
||||
viewportWidth: number,
|
||||
viewportHeight: number,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
sandbox: boolean = captureSandboxEnabled(),
|
||||
): string[] {
|
||||
const needsLinuxSandboxException = platform === "linux";
|
||||
return [
|
||||
...(needsLinuxSandboxException ? ["--no-sandbox"] : []),
|
||||
...chromeSandboxArgs(sandbox),
|
||||
"--disable-dev-shm-usage",
|
||||
...(disableWebgl
|
||||
? ["--disable-gpu"]
|
||||
|
||||
@@ -51,7 +51,6 @@ export async function launchCaptureBrowser(options: {
|
||||
userDataDir: profile,
|
||||
executablePath: options.executablePath,
|
||||
protocolTimeout: options.protocolTimeout,
|
||||
// Linux containers may lack the user namespaces Chromium's sandbox requires.
|
||||
args: [
|
||||
...captureBrowserArgs(options.disableWebgl, options.width, options.height),
|
||||
...browserProxyArgs(
|
||||
|
||||
@@ -563,7 +563,12 @@ export {
|
||||
pipeLaunchClosedOnMac,
|
||||
sandboxDeniedRemediation,
|
||||
} from "./services/sandboxLaunch.js";
|
||||
export { launchWithChromeSandboxFallback } from "./services/chromeSandbox.js";
|
||||
export {
|
||||
chromeSandboxArgs,
|
||||
chromeSandboxEnabled,
|
||||
launchWithChromeSandboxFallback,
|
||||
type ChromeSandboxHost,
|
||||
} from "./services/chromeSandbox.js";
|
||||
export {
|
||||
VariablesInvalidError,
|
||||
type VariablesIssue,
|
||||
|
||||
Reference in New Issue
Block a user