TypeScript
fix(server): clarify browser setup when running as root (#22892)
Jaron Kramerc78712e84bcd
diff --git a/apps/server/src/mcp/PreviewAutomationBroker.test.ts b/apps/server/src/mcp/PreviewAutomationBroker.test.tsindex f9d618f0520..ae54eca6f98 100644--- a/apps/server/src/mcp/PreviewAutomationBroker.test.ts+++ b/apps/server/src/mcp/PreviewAutomationBroker.test.ts@@ -19,6 +19,7 @@ import * as Fiber from "effect/Fiber";import * as Result from "effect/Result";import * as Stream from "effect/Stream";+import { PreviewBrowserRootError } from "../preview/PreviewBrowserHost.ts";import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";const makeBroker = PreviewAutomationBroker.make.pipe(Effect.provide(NodeServices.layer));@@ -288,9 +289,22 @@ it.effect("announces a live replacement stream before delivering requests", () =);it.effect.each([- { clientId: SERVER_BROWSER_AUTOMATION_CLIENT_ID, shown: true },- { clientId: "client-1", shown: false },-])("tells the agent why its own server browser failed ($clientId)", ({ clientId, shown }) =>+ {+ clientId: SERVER_BROWSER_AUTOMATION_CLIENT_ID,+ shown: true,+ message: "page.goto: net::ERR_CONNECTION_REFUSED at http://localhost:4719/",+ },+ {+ clientId: "client-1",+ shown: false,+ message: "page.goto: net::ERR_CONNECTION_REFUSED at http://localhost:4719/",+ },+ {+ clientId: SERVER_BROWSER_AUTOMATION_CLIENT_ID,+ shown: true,+ message: new PreviewBrowserRootError().message,+ },+])("preserves server browser failure guidance ($clientId)", ({ clientId, shown, message }) =>Effect.scoped(Effect.gen(function* () {const broker = yield* makeBroker;@@ -303,7 +317,7 @@ it.effect.each([ok: false,error: {_tag: "PreviewAutomationExecutionError",- message: "page.goto: net::ERR_CONNECTION_REFUSED at http://localhost:4719/",+ message,},}),).pipe(Effect.forkScoped);@@ -312,7 +326,7 @@ it.effect.each([.invoke<void>({ scope, operation: "open", input: {} }).pipe(Effect.flip);expect(error._tag).toBe("PreviewAutomationExecutionError");- expect(error.message.includes("ERR_CONNECTION_REFUSED")).toBe(shown);+ expect(error.message.includes(message)).toBe(shown);}),),);diff --git a/apps/server/src/preview/PreviewBrowserHost.test.ts b/apps/server/src/preview/PreviewBrowserHost.test.tsindex 2a90d000b26..02ea6bedc42 100644--- a/apps/server/src/preview/PreviewBrowserHost.test.ts+++ b/apps/server/src/preview/PreviewBrowserHost.test.ts@@ -72,6 +72,36 @@ const diagnose = (input: {};describe("diagnoseLaunchFailure", () => {+ it.effect("explains how to restart a root server without Chrome's sandbox", () =>+ Effect.gen(function* () {+ const { error, commands } = yield* diagnose({+ platform: "linux",+ uid: 0,+ output: PreviewBrowserHost.NO_SANDBOX_SIGNATURE,+ ldd: "",+ });+ expect(error).toBeInstanceOf(PreviewBrowserHost.PreviewBrowserRootError);+ expect(error?.message).toContain("T3 server is running as root");+ expect(error?.message).toContain("T3 server's environment and restart the server");+ expect(error?.message).toContain("T3CODE_SERVER_BROWSER_SANDBOX=0 t3 serve");+ expect(error?.message).toContain("Environment=T3CODE_SERVER_BROWSER_SANDBOX=0");+ expect(error?.message).toContain("this disables Chrome's sandbox");+ expect(commands).toEqual([]);+ }),+ );++ it.effect("keeps host setup guidance for a nonroot sandbox failure", () =>+ Effect.gen(function* () {+ const { error } = yield* diagnose({+ platform: "linux",+ output: PreviewBrowserHost.NO_SANDBOX_SIGNATURE,+ ldd: "",+ });+ expect(error).toBeInstanceOf(PreviewBrowserHost.PreviewBrowserSandboxError);+ expect(error?.message).toContain("Run `sudo t3 browser setup` on the host");+ }),+ );+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.tsindex 896f51b308e..1847e5f2320 100644--- a/apps/server/src/preview/PreviewBrowserHost.ts+++ b/apps/server/src/preview/PreviewBrowserHost.ts@@ -73,7 +73,7 @@ export class PreviewBrowserRootError extends Schema.TaggedError<PreviewBrowserRo{},) {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.";+ return "T3 server is running as root; Chrome's sandbox cannot start. Run T3 as a nonroot user. Otherwise set `T3CODE_SERVER_BROWSER_SANDBOX=0` in the T3 server's environment and restart the server; this disables Chrome's sandbox. Manual: `T3CODE_SERVER_BROWSER_SANDBOX=0 t3 serve`. Systemd: add `Environment=T3CODE_SERVER_BROWSER_SANDBOX=0` under `[Service]`, reload systemd, then restart the service. `t3 browser setup` cannot fix root sandboxing.";}}diff --git a/docs/user/remote-access.md b/docs/user/remote-access.mdindex a01febe9188..14fd7f75d3f 100644--- a/docs/user/remote-access.md+++ b/docs/user/remote-access.md@@ -259,8 +259,22 @@ any missing libraries and system fonts with apt; pages that load without textcan mean the host has no fonts. It is safe to run again. Without `sudo`, it onlyreports what it would change.-The browser always runs in Chrome's sandbox. Where you cannot change the host,-set `T3CODE_SERVER_BROWSER_SANDBOX=0` for the environment to run without it.+The browser runs in Chrome's sandbox by default. Chrome cannot start that sandbox+as root. If your T3 server runs as root, run it as a nonroot user to keep+sandboxing enabled. `t3 browser setup` does not fix this root restriction.++If you need to run as root, or cannot enable the sandbox on your host, set+`T3CODE_SERVER_BROWSER_SANDBOX=0` in the **T3 server's environment** and restart+the server. This disables Chrome's sandbox. For a manual launch:++```sh+T3CODE_SERVER_BROWSER_SANDBOX=0 t3 serve+```++For a systemd service, add `Environment=T3CODE_SERVER_BROWSER_SANDBOX=0` under+`[Service]` in its configuration, run `sudo systemctl daemon-reload`, then restart+your T3 service. Setting the variable in an agent's terminal does not change the+environment of an already running T3 server.## Connect an outside agent