Skip to content

Commit a66bd16

Browse files
committed
fix(server): explain root browser launch failures instead of pointing to setup
Chrome refuses to sandbox as root, which the server reported as an AppArmor problem while `t3 browser setup` said the host was ready.
1 parent 339179f commit a66bd16

5 files changed

Lines changed: 78 additions & 6 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { expect, it } from "@effect/vitest";
3+
import * as HostProcess from "@t3tools/shared/HostProcess";
4+
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
6+
import * as Layer from "effect/Layer";
7+
import * as TestConsole from "effect/testing/TestConsole";
8+
import { Command } from "effect/cli";
9+
10+
import { browserCommand } from "./browser.ts";
11+
12+
it.effect("setup in a root shell warns that the server cannot run as root", () =>
13+
Effect.gen(function* () {
14+
// Every file read fails, so setup finds nothing to install and changes nothing.
15+
yield* Command.runWith(browserCommand, { version: "0.0.0" })([
16+
"setup",
17+
"--base-dir",
18+
"/nonexistent",
19+
]).pipe(
20+
Effect.provide(FileSystem.layerNoop({})),
21+
Effect.provideService(HostProcess.Platform, "linux"),
22+
Effect.provideService(HostProcess.UserId, 0),
23+
// A root user who follows the old advice runs setup through sudo.
24+
Effect.provideService(HostProcess.Environment, { SUDO_USER: "root" }),
25+
);
26+
const output = (yield* TestConsole.logLines).join("\n");
27+
expect(output).toContain("Chrome will not start its sandbox as root");
28+
expect(output).not.toContain("ready");
29+
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, TestConsole.layer))),
30+
);

‎apps/server/src/cli/browser.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
111111
const fs = yield* FileSystem.FileSystem;
112112
const isRoot = (yield* HostProcess.UserId) === 0;
113113
const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND);
114+
// Root, unless sudo came from another user, is likely the server's own account.
115+
const env = yield* HostProcess.Environment;
116+
const rootWarning =
117+
isRoot && (env.SUDO_USER ?? "root") === "root" && !PreviewBrowserHost.sandboxDisabled(env)
118+
? new PreviewBrowserHost.PreviewBrowserRootError().message
119+
: undefined;
114120

115121
const needsProfile = yield* PreviewBrowserHost.sandboxBlocked;
116122
const browser = yield* installedBrowser(yield* setupBaseDir(baseDir));
@@ -121,9 +127,10 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
121127

122128
if (!needsProfile && missing.length === 0) {
123129
return yield* Console.log(
124-
Option.isSome(browser)
125-
? "This host is ready for T3's browser."
126-
: "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again.",
130+
rootWarning ??
131+
(Option.isSome(browser)
132+
? "This host is ready for T3's browser."
133+
: "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again."),
127134
);
128135
}
129136

@@ -180,7 +187,7 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
180187
yield* Console.log("Installed the browser's libraries.");
181188
}
182189

183-
yield* Console.log("This host is ready for T3's browser.");
190+
yield* Console.log(rootWarning ?? "This host is ready for T3's browser.");
184191
}),
185192
),
186193
);

‎apps/server/src/preview/PreviewBrowserHost.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const diagnose = (input: {
5656
output: string;
5757
ldd: string;
5858
version?: string;
59+
uid?: number;
5960
}) => {
6061
const { spawner, commands } = hostReporting({
6162
ldd: input.ldd,
@@ -68,6 +69,7 @@ const diagnose = (input: {
6869
}).pipe(
6970
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
7071
Effect.provideService(HostProcess.Platform, input.platform),
72+
Effect.provideService(HostProcess.UserId, input.uid ?? 1000),
7173
Effect.map((error) => ({ error, commands })),
7274
);
7375
};
@@ -86,6 +88,22 @@ describe("diagnoseLaunchFailure", () => {
8688
}),
8789
);
8890

91+
it.effect("tells a root server to drop root, not to run setup", () =>
92+
Effect.gen(function* () {
93+
const { error, commands } = yield* diagnose({
94+
platform: "linux",
95+
output: PreviewBrowserHost.NO_SANDBOX_SIGNATURE,
96+
ldd: "",
97+
uid: 0,
98+
});
99+
expect(error?._tag).toBe("PreviewBrowserRootError");
100+
expect(error?.message).toContain("`User=`");
101+
expect(error?.message).toContain("`T3CODE_SERVER_BROWSER_SANDBOX=0`");
102+
expect(error?.message).not.toMatch(/AppArmor|browser setup/);
103+
expect(commands).toEqual([]);
104+
}),
105+
);
106+
89107
it.effect("names every library the loader cannot find", () =>
90108
Effect.gen(function* () {
91109
const { error } = yield* diagnose({

‎apps/server/src/preview/PreviewBrowserHost.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,20 @@ export class PreviewBrowserLibrariesError extends Schema.TaggedError<PreviewBrow
8787
}
8888
}
8989

90-
export type PreviewBrowserHostError = PreviewBrowserSandboxError | PreviewBrowserLibrariesError;
90+
/** Chrome will not start its sandbox as root (crbug.com/638180), which no host setup fixes. */
91+
export class PreviewBrowserRootError extends Schema.TaggedError<PreviewBrowserRootError>()(
92+
"PreviewBrowserRootError",
93+
{},
94+
) {
95+
override get message(): string {
96+
return "Chrome will not start its sandbox as root, so T3's browser cannot run while the T3 server runs as root. Run the server as a nonroot user (for example `User=` in its systemd service), or set `T3CODE_SERVER_BROWSER_SANDBOX=0` to run the browser unsandboxed as root.";
97+
}
98+
}
99+
100+
export type PreviewBrowserHostError =
101+
| PreviewBrowserSandboxError
102+
| PreviewBrowserLibrariesError
103+
| PreviewBrowserRootError;
91104

92105
const MISSING_LIBRARY = /^\s*(\S+) => not found$/gm;
93106
/** glibc's loader abort, naming the first library it could not load. */
@@ -106,7 +119,10 @@ export const diagnoseLaunchFailure = Effect.fn("PreviewBrowserHost.diagnoseLaunc
106119
}) {
107120
const { setupCommand } = input;
108121
if (input.output.includes(NO_SANDBOX_SIGNATURE)) {
109-
return new PreviewBrowserSandboxError({ setupCommand });
122+
// Playwright reports Chrome's refusal to sandbox as root as a sandbox failure too.
123+
return (yield* HostProcess.UserId) === 0
124+
? new PreviewBrowserRootError()
125+
: new PreviewBrowserSandboxError({ setupCommand });
110126
}
111127
if ((yield* HostProcess.Platform) !== "linux") return undefined;
112128
const libraries = yield* missingLibraries(input.executable);

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ const isHostSetupError = Schema.is(
141141
Schema.Union([
142142
PreviewBrowserHost.PreviewBrowserSandboxError,
143143
PreviewBrowserHost.PreviewBrowserLibrariesError,
144+
PreviewBrowserHost.PreviewBrowserRootError,
144145
]),
145146
);
146147

0 commit comments

Comments
 (0)