fix(engine): keep Chrome's sandbox on for local renders where the host supports it (#7625)

* fix(engine): keep Chrome's sandbox on for local renders where the host supports it

Chrome launched with --no-sandbox on every host. Keep the sandbox on
macOS, Windows and non-root Linux outside containers; drop it as root,
in Docker/Podman/Kubernetes, on Lambda and Cloud Run, and where Linux
restricts unprivileged user namespaces. --no-zygote now ships only with
--no-sandbox, since Chrome refuses it while sandboxed. A sandboxed Linux
launch that fails at startup retries once without the sandbox and warns.
HYPERFRAMES_CHROME_SANDBOX=1|0 overrides the choice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(cli): cover the Linux sandbox retry in managed browser launches

The off-macOS closed-pipe case now passes --no-sandbox, so it still
pins "no seatbelt relaunch" rather than the new Linux sandbox retry,
which gets its own case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(engine): honor HYPERFRAMES_CHROME_SANDBOX=1 and latch the fallback on clear evidence only

With the override set to 1, a sandboxed launch Chrome refuses now fails
instead of retrying unsandboxed. A bare startup exit (TargetCloseError)
retries that launch without the sandbox but only latches the sandbox off
after a second one, so a single unrelated early kill no longer disables
it for the rest of a long-lived process. Chrome naming the sandbox in its
error still latches at once.

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:
James Russo
2026-11-12 23:16:45 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 9c3258ccbf
commit 400a4e2285
7 changed files with 339 additions and 15 deletions
+5 -1
View File
@@ -2,6 +2,7 @@ import type { ChildProcess } from "node:child_process";
import {
inAgentSandbox,
isOneProcessOverPipe,
launchWithChromeSandboxFallback,
machServiceDenied,
oneProcessOverPipe,
pipeLaunchClosedOnMac,
@@ -108,8 +109,11 @@ export async function launchManagedBrowser(
{ diagnose = true }: { diagnose?: boolean } = {},
): Promise<Browser> {
const sandboxed = isOneProcessOverPipe(options) || inAgentSandbox();
const prepared = sandboxed ? oneProcessOverPipe(options) : options;
try {
return await launchOwnedBrowser(puppeteer, sandboxed ? oneProcessOverPipe(options) : options);
return await launchWithChromeSandboxFallback(prepared.args ?? [], (args) =>
launchOwnedBrowser(puppeteer, { ...prepared, args }),
);
} catch (error) {
const unnamedSeatbelt =
!sandboxed &&
+25 -1
View File
@@ -124,9 +124,33 @@ describe("a browser inside an agent's macOS seatbelt", () => {
"Protocol error (Target.setDiscoverTargets): Target closed",
);
const { puppeteer, launches } = recordingPuppeteer([closed]);
await expect(launchManagedBrowser(puppeteer, { args: ["--headless"] })).rejects.toBe(closed);
await expect(
launchManagedBrowser(puppeteer, { args: ["--headless", "--no-sandbox"] }),
).rejects.toBe(closed);
expect(launches).toHaveLength(1);
});
it("retries once without Chrome's sandbox when Linux closes a sandboxed launch at startup", async () => {
vi.stubEnv("CODEX_SANDBOX", "");
Object.defineProperty(process, "platform", { value: "linux" });
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
const { puppeteer, launches } = recordingPuppeteer([
new TargetCloseError("Protocol error (Target.setDiscoverTargets): Target closed"),
]);
await expect(launchManagedBrowser(puppeteer, { args: ["--headless"] })).resolves.toBeDefined();
expect(launches.map((options) => options.args)).toEqual([
["--headless", HYPERFRAMES_BROWSER_MARKER_ARG],
[
"--headless",
"--no-sandbox",
"--disable-setuid-sandbox",
"--no-zygote",
HYPERFRAMES_BROWSER_MARKER_ARG,
],
]);
expect(warn).toHaveBeenCalledOnce();
warn.mockRestore();
});
});
describe("a browser launch that never settles", () => {