Skip to content

Commit 5e7ba0b

Browse files
fix(cli): Chrome starts inside an agent's macOS sandbox as one process over a pipe (#5781)
1 parent b18b3e9 commit 5e7ba0b

4 files changed

Lines changed: 103 additions & 11 deletions

File tree

‎packages/cli/src/browser/launch.ts‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import {
77
import { normalizeErrorMessage } from "../utils/errorMessage.js";
88
import { requestCliExit } from "../utils/commandResult.js";
99
import { processIsAlive } from "../utils/processTree.js";
10-
import { sandboxDeniedRemediation } from "./sandboxDenied.js";
10+
import {
11+
inAgentSandbox,
12+
isOneProcessOverPipe,
13+
machServiceDenied,
14+
oneProcessOverPipe,
15+
sandboxDeniedRemediation,
16+
} from "./sandboxDenied.js";
1117

1218
export class BrowserCleanupError extends AggregateError {}
1319

@@ -56,9 +62,7 @@ export async function resolveManagedGpuMode(
5662
context.signal?.throwIfAborted();
5763
const browser = await launchManagedBrowser(puppeteer, {
5864
...options,
59-
...(context.noListen
60-
? { pipe: true, args: [...(options.args ?? []), "--single-process"] }
61-
: {}),
65+
...(context.noListen ? oneProcessOverPipe(options) : {}),
6266
signal: context.signal,
6367
});
6468
try {
@@ -76,6 +80,29 @@ export async function resolveManagedGpuMode(
7680
export async function launchManagedBrowser(
7781
puppeteer: PuppeteerNode,
7882
options: LaunchOptions,
83+
): Promise<Browser> {
84+
const sandboxed = isOneProcessOverPipe(options) || inAgentSandbox();
85+
try {
86+
return await launchOwnedBrowser(puppeteer, sandboxed ? oneProcessOverPipe(options) : options);
87+
} catch (error) {
88+
const unnamedSeatbelt = !sandboxed && machServiceDenied(normalizeErrorMessage(error));
89+
if (!unnamedSeatbelt) throw withRemediation(error);
90+
return launchOwnedBrowser(puppeteer, oneProcessOverPipe(options)).catch(
91+
(retryError: unknown) => {
92+
throw withRemediation(retryError);
93+
},
94+
);
95+
}
96+
}
97+
98+
function withRemediation(error: unknown): unknown {
99+
const remediation = sandboxDeniedRemediation(normalizeErrorMessage(error));
100+
return remediation ? new Error(remediation, { cause: error }) : error;
101+
}
102+
103+
async function launchOwnedBrowser(
104+
puppeteer: PuppeteerNode,
105+
options: LaunchOptions,
79106
): Promise<Browser> {
80107
if (shutdown) throw new Error("The CLI is stopping; no browser can start.");
81108
ownSignals();
@@ -95,9 +122,6 @@ export async function launchManagedBrowser(
95122
browsers.add(browser);
96123
child.once("exit", () => browsers.delete(browser));
97124
return browser;
98-
} catch (error) {
99-
const remediation = sandboxDeniedRemediation(normalizeErrorMessage(error));
100-
throw remediation ? new Error(remediation, { cause: error }) : error;
101125
} finally {
102126
pending.delete(launch);
103127
}

‎packages/cli/src/browser/sandboxDenied.test.ts‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { LaunchOptions, PuppeteerNode } from "puppeteer-core";
2-
import { describe, expect, it } from "vitest";
1+
import { EventEmitter } from "node:events";
2+
import type { Browser, LaunchOptions, PuppeteerNode } from "puppeteer-core";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
34
import { launchManagedBrowser } from "./launch.js";
45

56
const SEATBELT_LAUNCH_FAILURE = [
@@ -28,3 +29,43 @@ describe("a browser launch a sandbox refuses", () => {
2829
await expect(launchManagedBrowser(failingPuppeteer(other), {})).rejects.toThrow(other);
2930
});
3031
});
32+
33+
const fakeBrowser = () => ({ process: () => new EventEmitter() }) as unknown as Browser;
34+
35+
function recordingPuppeteer(failures: string[]) {
36+
const launches: LaunchOptions[] = [];
37+
const puppeteer = {
38+
launch: async (options: LaunchOptions) => {
39+
launches.push(options);
40+
const failure = failures.shift();
41+
if (failure) throw new Error(failure);
42+
return fakeBrowser();
43+
},
44+
} as unknown as PuppeteerNode;
45+
return { puppeteer, launches };
46+
}
47+
48+
describe("a browser inside an agent's macOS seatbelt", () => {
49+
const platform = process.platform;
50+
afterEach(() => {
51+
vi.unstubAllEnvs();
52+
Object.defineProperty(process, "platform", { value: platform });
53+
});
54+
55+
it("starts as one process over a pipe when Codex names its seatbelt", async () => {
56+
vi.stubEnv("CODEX_SANDBOX", "seatbelt");
57+
Object.defineProperty(process, "platform", { value: "darwin" });
58+
const { puppeteer, launches } = recordingPuppeteer([]);
59+
await launchManagedBrowser(puppeteer, { args: ["--headless"] });
60+
expect(launches).toHaveLength(1);
61+
expect(launches[0]).toMatchObject({ pipe: true, args: ["--headless", "--single-process"] });
62+
});
63+
64+
it("relaunches as one process over a pipe when an unnamed sandbox refuses the mach service", async () => {
65+
vi.stubEnv("CODEX_SANDBOX", "");
66+
const { puppeteer, launches } = recordingPuppeteer([SEATBELT_LAUNCH_FAILURE]);
67+
await expect(launchManagedBrowser(puppeteer, { args: ["--headless"] })).resolves.toBeDefined();
68+
expect(launches.map((options) => options.pipe ?? false)).toEqual([false, true]);
69+
expect(launches[1]!.args).toEqual(["--headless", "--single-process"]);
70+
});
71+
});

‎packages/cli/src/browser/sandboxDenied.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1+
import type { LaunchOptions } from "puppeteer-core";
2+
13
const MACH_SERVICE_DENIED = /bootstrap_check_in\b[^\n]*Permission denied/;
4+
const SINGLE_PROCESS = "--single-process";
5+
6+
export function machServiceDenied(errorMessage: string): boolean {
7+
return MACH_SERVICE_DENIED.test(errorMessage);
8+
}
9+
10+
/** Codex runs agent commands in a macOS seatbelt, which names itself in the environment. */
11+
export function inAgentSandbox(env = process.env, platform = process.platform): boolean {
12+
return platform === "darwin" && env.CODEX_SANDBOX === "seatbelt";
13+
}
14+
15+
/** The one launch a seatbelt allows: no child process can register its mach port and nothing may listen. */
16+
export function oneProcessOverPipe(options: LaunchOptions): LaunchOptions {
17+
const args = options.args ?? [];
18+
return {
19+
...options,
20+
pipe: true,
21+
args: args.includes(SINGLE_PROCESS) ? args : [...args, SINGLE_PROCESS],
22+
};
23+
}
24+
25+
export function isOneProcessOverPipe(options: LaunchOptions): boolean {
26+
return options.pipe === true && (options.args ?? []).includes(SINGLE_PROCESS);
27+
}
228

329
/** A process sandbox (an agent's macOS seatbelt) refused Chrome its mach service, so no browser starts here. */
430
export function sandboxDeniedRemediation(errorMessage: string): string | undefined {
5-
if (!MACH_SERVICE_DENIED.test(errorMessage)) return undefined;
31+
if (!machServiceDenied(errorMessage)) return undefined;
632
return [
733
"The browser can't start inside this sandbox: macOS refused Chrome's mach service (bootstrap_check_in: Permission denied).",
834
"Rerun this command with escalation, outside the sandbox.",

‎packages/cli/src/capture/captureCompositionFrame.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
launchManagedBrowser,
44
resolveManagedGpuMode,
55
} from "../browser/launch.js";
6+
import { oneProcessOverPipe } from "../browser/sandboxDenied.js";
67
import { spawn } from "node:child_process";
78
import type { Browser, Page } from "puppeteer-core";
89
import type { InProcessRequest, InProcessResponse } from "../utils/staticProjectServer.js";
@@ -230,7 +231,7 @@ export async function openSettledCompositionPage(
230231
headless: true,
231232
signal: options.signal,
232233
executablePath,
233-
...(answer ? { pipe: true, args: [...args, "--single-process"] } : { args }),
234+
...(answer ? oneProcessOverPipe({ args }) : { args }),
234235
});
235236
};
236237

0 commit comments

Comments
 (0)