diff --git a/packages/cli/src/capture/browserLaunchArgs.test.ts b/packages/cli/src/capture/browserLaunchArgs.test.ts index 68c79ffde8..cc5c510704 100644 --- a/packages/cli/src/capture/browserLaunchArgs.test.ts +++ b/packages/cli/src/capture/browserLaunchArgs.test.ts @@ -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 { + 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)); }); }); diff --git a/packages/cli/src/capture/browserLaunchArgs.ts b/packages/cli/src/capture/browserLaunchArgs.ts index 29aba9bfaf..085bb08264 100644 --- a/packages/cli/src/capture/browserLaunchArgs.ts +++ b/packages/cli/src/capture/browserLaunchArgs.ts @@ -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"] diff --git a/packages/cli/src/capture/launchCaptureBrowser.ts b/packages/cli/src/capture/launchCaptureBrowser.ts index 7f394af7c7..fe6a9c2a8d 100644 --- a/packages/cli/src/capture/launchCaptureBrowser.ts +++ b/packages/cli/src/capture/launchCaptureBrowser.ts @@ -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( diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index c8c6e95a9a..cb6e6a0462 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -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,