Open History to see the last commit
1/**
2 * Host setup the shared headless browser needs, and the one command that does
3 * it. Every failure here names that command, so the server log, an agent's tool
4 * error, and the viewer all offer the same fix: `sudo t3 browser setup`.
5 *
6 * T3 never turns Chrome's sandbox off by itself. A host that cannot give it one
7 * gets the command instead, and only the operator's explicit
8 * `T3CODE_SERVER_BROWSER_SANDBOX=0` launches without it.
9 */
10import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
11import * as Effect from "effect/Effect";
12import * as FileSystem from "effect/FileSystem";
13import * as Schema from "effect/Schema";
14import * as ChildProcess from "effect/process/ChildProcess";
15import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
16
17/** The subcommand that sets the host up; callers render it, with `sudo`, for how T3 was launched. */
18export const SETUP_SUBCOMMAND = "browser setup";
19
20/** What Chrome prints before aborting when it cannot sandbox itself. */
21export const NO_SANDBOX_SIGNATURE = "No usable sandbox";
22
23/** Whether an operator explicitly allowed Chrome to run without its sandbox. */
24export const sandboxDisabled = (env: Readonly<Record<string, string | undefined>>) =>
25 env.T3CODE_SERVER_BROWSER_SANDBOX === "0";
26
27/** Set on Ubuntu 23.10+: unprivileged user namespaces need an AppArmor profile. */
28const USERNS_RESTRICTION = "/proc/sys/kernel/apparmor_restrict_unprivileged_userns";
29export const APPARMOR_PROFILE_PATH = "/etc/apparmor.d/t3-chrome-headless-shell";
30
31/**
32 * Lets T3's headless browser, in any T3 home and at any pinned version, create
33 * the user namespace Chrome's sandbox runs in. Modelled on the profile Ubuntu
34 * ships for Google Chrome; `unconfined` adds nothing beyond `userns`.
35 */
36export const APPARMOR_PROFILE = `# Written by \`t3 browser setup\`: lets T3 Code's headless browser use Chrome's sandbox.
37abi <abi/4.0>,
38include <tunables/global>
39
40profile t3-chrome-headless-shell /**/tools/chrome-headless-shell/*/*/chrome-headless-shell flags=(unconfined) {
41 userns,
42
43 include if exists <local/t3-chrome-headless-shell>
44}
45`;
46
47/**
48 * Chrome's Debian dependencies that minimal images leave out, from the headless
49 * shell's deb.deps. Ubuntu 24.04 and Debian 13 renamed some with a `t64`
50 * suffix; setup installs whichever name the host's apt offers.
51 */
52export const DEBIAN_PACKAGES: ReadonlyArray<ReadonlyArray<string>> = [
53 ["libnss3"],
54 ["libglib2.0-0t64", "libglib2.0-0"],
55 ["libatk1.0-0t64", "libatk1.0-0"],
56 ["libatk-bridge2.0-0t64", "libatk-bridge2.0-0"],
57 ["libatspi2.0-0t64", "libatspi2.0-0"],
58 ["libdbus-1-3"],
59 ["libx11-6"],
60 ["libxcb1"],
61 ["libxcomposite1"],
62 ["libxdamage1"],
63 ["libxext6"],
64 ["libxfixes3"],
65 ["libxrandr2"],
66 ["libxkbcommon0"],
67 ["libgbm1"],
68 ["libasound2t64", "libasound2"],
69 ["libexpat1"],
70];
71
72export class PreviewBrowserSandboxError extends Schema.TaggedError<PreviewBrowserSandboxError>()(
73 "PreviewBrowserSandboxError",
74 { setupCommand: Schema.String },
75) {
76 override get message(): string {
77 return `This host blocks the sandbox T3's browser runs in (AppArmor on Ubuntu 23.10+). Run \`${this.setupCommand}\` on the host once to allow it, then try again.`;
78 }
79}
80
81export class PreviewBrowserLibrariesError extends Schema.TaggedError<PreviewBrowserLibrariesError>()(
82 "PreviewBrowserLibrariesError",
83 { setupCommand: Schema.String, libraries: Schema.Array(Schema.String) },
84) {
85 override get message(): string {
86 return `This host is missing libraries T3's browser needs (${this.libraries.join(", ")}). Run \`${this.setupCommand}\` on the host to install them, then try again.`;
87 }
88}
89
90export type PreviewBrowserHostError = PreviewBrowserSandboxError | PreviewBrowserLibrariesError;
91
92const MISSING_LIBRARY = /^\s*(\S+) => not found$/gm;
93
94/**
95 * After a launch fails, names the host setup it is missing: the sandbox, from
96 * Chrome's own abort message, or shared libraries, from `ldd`. Undefined when
97 * neither explains it. Linux only; other hosts never need either.
98 */
99export const diagnoseLaunchFailure = Effect.fn("PreviewBrowserHost.diagnoseLaunchFailure")(
100 function* (input: {
101 readonly executable: string;
102 readonly output: string;
103 readonly setupCommand: string;
104 }) {
105 const { setupCommand } = input;
106 if (input.output.includes(NO_SANDBOX_SIGNATURE)) {
107 return new PreviewBrowserSandboxError({ setupCommand });
108 }
109 if ((yield* HostProcessPlatform) !== "linux") return undefined;
110 const libraries = yield* missingLibraries(input.executable);
111 return libraries.length === 0
112 ? undefined
113 : new PreviewBrowserLibrariesError({ setupCommand, libraries });
114 },
115);
116
117/** Shared libraries the loader cannot find for `executable`, by `ldd`. */
118export const missingLibraries = Effect.fn("PreviewBrowserHost.missingLibraries")(function* (
119 executable: string,
120) {
121 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
122 const report = yield* spawner
123 .string(ChildProcess.make("ldd", [executable], { stdin: "ignore", stderr: "ignore" }))
124 .pipe(
125 Effect.timeout("5 seconds"),
126 Effect.orElseSucceed(() => ""),
127 );
128 return [...report.matchAll(MISSING_LIBRARY)].map((match) => match[1]!);
129});
130
131/**
132 * Whether Chrome's sandbox will be blocked here: the host restricts user
133 * namespaces and T3's AppArmor profile is not installed. Readable without
134 * root, so the server checks it at startup.
135 */
136export const sandboxBlocked = Effect.gen(function* () {
137 if ((yield* HostProcessPlatform) !== "linux") return false;
138 const fs = yield* FileSystem.FileSystem;
139 const restricted = yield* fs.readFileString(USERNS_RESTRICTION).pipe(
140 Effect.map((value) => value.trim() === "1"),
141 Effect.orElseSucceed(() => false),
142 );
143 if (!restricted) return false;
144 return !(yield* fs.exists(APPARMOR_PROFILE_PATH).pipe(Effect.orElseSucceed(() => false)));
145}).pipe(Effect.withSpan("PreviewBrowserHost.sandboxBlocked"));