Skip to content
TypeScript

fix(server): explain root browser launch failures instead of pointing to setup (#19846)

Madhav Gautamc38777968b8d
diff --git a/apps/server/src/cli/browser.test.ts b/apps/server/src/cli/browser.test.ts
new file mode 100644
index 00000000000..ef775b9c184
--- /dev/null
+++ b/apps/server/src/cli/browser.test.ts
@@ -0,0 +1,30 @@
+import * as NodeServices from "@effect/platform-node/NodeServices";
+import { expect, it } from "@effect/vitest";
+import * as HostProcess from "@t3tools/shared/HostProcess";
+import * as Effect from "effect/Effect";
+import * as FileSystem from "effect/FileSystem";
+import * as Layer from "effect/Layer";
+import * as TestConsole from "effect/testing/TestConsole";
+import { Command } from "effect/cli";
+
+import { browserCommand } from "./browser.ts";
+
+it.effect("setup in a root shell warns that the server cannot run as root", () =>
+ Effect.gen(function* () {
+ // Every file read fails, so setup finds nothing to install and changes nothing.
+ yield* Command.runWith(browserCommand, { version: "0.0.0" })([
+ "setup",
+ "--base-dir",
+ "/nonexistent",
+ ]).pipe(
+ Effect.provide(FileSystem.layerNoop({})),
+ Effect.provideService(HostProcess.Platform, "linux"),
+ Effect.provideService(HostProcess.UserId, 0),
+ // A root user who follows the old advice runs setup through sudo.
+ Effect.provideService(HostProcess.Environment, { SUDO_USER: "root" }),
+ );
+ const output = (yield* TestConsole.logLines).join("\n");
+ expect(output).toContain("Chrome will not start its sandbox as root");
+ expect(output).not.toContain("ready");
+ }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, TestConsole.layer))),
+);
diff --git a/apps/server/src/cli/browser.ts b/apps/server/src/cli/browser.ts
index a15507c4726..f741458241d 100644
--- a/apps/server/src/cli/browser.ts
+++ b/apps/server/src/cli/browser.ts
@@ -96,6 +96,12 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
const fs = yield* FileSystem.FileSystem;
const isRoot = (yield* HostProcess.UserId) === 0;
const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND);
+ // Root, unless sudo came from another user, is likely the server's own account.
+ const env = yield* HostProcess.Environment;
+ const rootWarning =
+ isRoot && (env.SUDO_USER ?? "root") === "root" && !PreviewBrowserHost.sandboxDisabled(env)
+ ? new PreviewBrowserHost.PreviewBrowserRootError().message
+ : undefined;
const needsProfile = yield* PreviewBrowserHost.sandboxBlocked;
const browser = yield* installedBrowser(yield* setupBaseDir(baseDir));
@@ -106,9 +112,10 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
if (!needsProfile && missing.length === 0) {
return yield* Console.log(
- Option.isSome(browser)
- ? "This host is ready for T3's browser."
- : "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again.",
+ rootWarning ??
+ (Option.isSome(browser)
+ ? "This host is ready for T3's browser."
+ : "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again."),
);
}
@@ -165,7 +172,7 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
yield* Console.log("Installed the browser's libraries.");
}
- yield* Console.log("This host is ready for T3's browser.");
+ yield* Console.log(rootWarning ?? "This host is ready for T3's browser.");
}),
),
);
diff --git a/apps/server/src/preview/PreviewBrowserHost.test.ts b/apps/server/src/preview/PreviewBrowserHost.test.ts
index 5ae0a70752c..8c6b8b47865 100644
--- a/apps/server/src/preview/PreviewBrowserHost.test.ts
+++ b/apps/server/src/preview/PreviewBrowserHost.test.ts
@@ -56,6 +56,7 @@ const diagnose = (input: {
output: string;
ldd: string;
version?: string;
+ uid?: number;
}) => {
const { spawner, commands } = hostReporting({
ldd: input.ldd,
@@ -68,6 +69,7 @@ const diagnose = (input: {
}).pipe(
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
Effect.provideService(HostProcess.Platform, input.platform),
+ Effect.provideService(HostProcess.UserId, input.uid ?? 1000),
Effect.map((error) => ({ error, commands })),
);
};
@@ -86,6 +88,22 @@ describe("diagnoseLaunchFailure", () => {
}),
);
+ it.effect("tells a root server to drop root, not to run setup", () =>
+ Effect.gen(function* () {
+ const { error, commands } = yield* diagnose({
+ platform: "linux",
+ output: PreviewBrowserHost.NO_SANDBOX_SIGNATURE,
+ ldd: "",
+ uid: 0,
+ });
+ expect(error?._tag).toBe("PreviewBrowserRootError");
+ expect(error?.message).toContain("`User=`");
+ expect(error?.message).toContain("`T3CODE_SERVER_BROWSER_SANDBOX=0`");
+ expect(error?.message).not.toMatch(/AppArmor|browser setup/);
+ expect(commands).toEqual([]);
+ }),
+ );
+
it.effect("names every library the loader cannot find", () =>
Effect.gen(function* () {
const { error } = yield* diagnose({
diff --git a/apps/server/src/preview/PreviewBrowserHost.ts b/apps/server/src/preview/PreviewBrowserHost.ts
index 9fcd30a664b..179ba9a1d2d 100644
--- a/apps/server/src/preview/PreviewBrowserHost.ts
+++ b/apps/server/src/preview/PreviewBrowserHost.ts
@@ -73,7 +73,20 @@ export class PreviewBrowserLibrariesError extends Schema.TaggedError<PreviewBrow
}
}
-export type PreviewBrowserHostError = PreviewBrowserSandboxError | PreviewBrowserLibrariesError;
+/** Chrome will not start its sandbox as root (crbug.com/638180), which no host setup fixes. */
+export class PreviewBrowserRootError extends Schema.TaggedError<PreviewBrowserRootError>()(
+ "PreviewBrowserRootError",
+ {},
+) {
+ override get message(): string {
+ 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.";
+ }
+}
+
+export type PreviewBrowserHostError =
+ | PreviewBrowserSandboxError
+ | PreviewBrowserLibrariesError
+ | PreviewBrowserRootError;
const MISSING_LIBRARY = /^\s*(\S+) => not found$/gm;
const LOADER_ERROR = /error while loading shared libraries: ([^:\s]+)/;
@@ -86,7 +99,10 @@ export const diagnoseLaunchFailure = Effect.fn("PreviewBrowserHost.diagnoseLaunc
}) {
const { setupCommand } = input;
if (input.output.includes(NO_SANDBOX_SIGNATURE)) {
- return new PreviewBrowserSandboxError({ setupCommand });
+ // Playwright reports Chrome's refusal to sandbox as root as a sandbox failure too.
+ return (yield* HostProcess.UserId) === 0
+ ? new PreviewBrowserRootError()
+ : new PreviewBrowserSandboxError({ setupCommand });
}
if ((yield* HostProcess.Platform) !== "linux") return undefined;
const libraries = yield* missingLibraries(input.executable);
diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts
index 9d822361d64..0f134133acc 100644
--- a/apps/server/src/preview/ServerBrowser.ts
+++ b/apps/server/src/preview/ServerBrowser.ts
@@ -137,6 +137,7 @@ const isHostSetupError = Schema.is(
Schema.Union([
PreviewBrowserHost.PreviewBrowserSandboxError,
PreviewBrowserHost.PreviewBrowserLibrariesError,
+ PreviewBrowserHost.PreviewBrowserRootError,
]),
);