Skip to content

Commit c387779

Browse files
authored
fix(server): explain root browser launch failures instead of pointing to setup (pingdotgg#19846)
1 parent 4de78cd commit c387779

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
@@ -96,6 +96,12 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
9696
const fs = yield* FileSystem.FileSystem;
9797
const isRoot = (yield* HostProcess.UserId) === 0;
9898
const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND);
99+
// Root, unless sudo came from another user, is likely the server's own account.
100+
const env = yield* HostProcess.Environment;
101+
const rootWarning =
102+
isRoot && (env.SUDO_USER ?? "root") === "root" && !PreviewBrowserHost.sandboxDisabled(env)
103+
? new PreviewBrowserHost.PreviewBrowserRootError().message
104+
: undefined;
99105

100106
const needsProfile = yield* PreviewBrowserHost.sandboxBlocked;
101107
const browser = yield* installedBrowser(yield* setupBaseDir(baseDir));
@@ -106,9 +112,10 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
106112

107113
if (!needsProfile && missing.length === 0) {
108114
return yield* Console.log(
109-
Option.isSome(browser)
110-
? "This host is ready for T3's browser."
111-
: "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again.",
115+
rootWarning ??
116+
(Option.isSome(browser)
117+
? "This host is ready for T3's browser."
118+
: "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again."),
112119
);
113120
}
114121

@@ -165,7 +172,7 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
165172
yield* Console.log("Installed the browser's libraries.");
166173
}
167174

168-
yield* Console.log("This host is ready for T3's browser.");
175+
yield* Console.log(rootWarning ?? "This host is ready for T3's browser.");
169176
}),
170177
),
171178
);

‎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
@@ -73,7 +73,20 @@ export class PreviewBrowserLibrariesError extends Schema.TaggedError<PreviewBrow
7373
}
7474
}
7575

76-
export type PreviewBrowserHostError = PreviewBrowserSandboxError | PreviewBrowserLibrariesError;
76+
/** Chrome will not start its sandbox as root (crbug.com/638180), which no host setup fixes. */
77+
export class PreviewBrowserRootError extends Schema.TaggedError<PreviewBrowserRootError>()(
78+
"PreviewBrowserRootError",
79+
{},
80+
) {
81+
override get message(): string {
82+
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.";
83+
}
84+
}
85+
86+
export type PreviewBrowserHostError =
87+
| PreviewBrowserSandboxError
88+
| PreviewBrowserLibrariesError
89+
| PreviewBrowserRootError;
7790

7891
const MISSING_LIBRARY = /^\s*(\S+) => not found$/gm;
7992
const LOADER_ERROR = /error while loading shared libraries: ([^:\s]+)/;
@@ -86,7 +99,10 @@ export const diagnoseLaunchFailure = Effect.fn("PreviewBrowserHost.diagnoseLaunc
8699
}) {
87100
const { setupCommand } = input;
88101
if (input.output.includes(NO_SANDBOX_SIGNATURE)) {
89-
return new PreviewBrowserSandboxError({ setupCommand });
102+
// Playwright reports Chrome's refusal to sandbox as root as a sandbox failure too.
103+
return (yield* HostProcess.UserId) === 0
104+
? new PreviewBrowserRootError()
105+
: new PreviewBrowserSandboxError({ setupCommand });
90106
}
91107
if ((yield* HostProcess.Platform) !== "linux") return undefined;
92108
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
@@ -137,6 +137,7 @@ const isHostSetupError = Schema.is(
137137
Schema.Union([
138138
PreviewBrowserHost.PreviewBrowserSandboxError,
139139
PreviewBrowserHost.PreviewBrowserLibrariesError,
140+
PreviewBrowserHost.PreviewBrowserRootError,
140141
]),
141142
);
142143

0 commit comments

Comments
 (0)