Skip to content

Commit bdf1fd8

Browse files
committed
fix(server): report a root preview browser as root, not AppArmor
Chrome refuses to start its sandbox as uid 0, and Playwright rewrites that abort so it was diagnosed as an AppArmor failure. Name the root workarounds instead, and make browser setup warn rather than report the host ready.
1 parent bbd98a5 commit bdf1fd8

7 files changed

Lines changed: 431 additions & 87 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import * as NodePath from "@effect/platform-node/NodePath";
2+
import { describe, 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 Option from "effect/Option";
8+
import * as Sink from "effect/Sink";
9+
import * as Stream from "effect/Stream";
10+
import { ChildProcessSpawner } from "effect/process";
11+
import * as TestConsole from "effect/testing/TestConsole";
12+
13+
import * as PreviewBrowserHost from "../preview/PreviewBrowserHost.ts";
14+
import { runBrowserSetup } from "./browser.ts";
15+
16+
const EXECUTABLE = "/data/t3/tools/chrome-headless-shell/linux64/154/chrome-headless-shell";
17+
18+
const versionSpawner = ChildProcessSpawner.make(() =>
19+
Effect.sync(() =>
20+
ChildProcessSpawner.makeHandle({
21+
pid: ChildProcessSpawner.ProcessId(1),
22+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
23+
isRunning: Effect.succeed(false),
24+
kill: () => Effect.void,
25+
unref: Effect.succeed(Effect.void),
26+
stdin: Sink.drain,
27+
stdout: Stream.empty,
28+
stderr: Stream.empty,
29+
all: Stream.make(new TextEncoder().encode("Google Chrome for Testing 154.0.8037.92\n")),
30+
getInputFd: () => Sink.drain,
31+
getOutputFd: () => Stream.empty,
32+
}),
33+
),
34+
);
35+
36+
const fileSystemFor = (browserInstalled: boolean) => {
37+
const directories: Record<string, ReadonlyArray<string>> = browserInstalled
38+
? {
39+
"/data/t3/tools/chrome-headless-shell": ["linux64"],
40+
"/data/t3/tools/chrome-headless-shell/linux64": ["154"],
41+
}
42+
: {};
43+
return FileSystem.layerNoop({
44+
readDirectory: (path) => Effect.succeed([...(directories[path] ?? [])]),
45+
exists: (path) => Effect.succeed(browserInstalled && path === EXECUTABLE),
46+
});
47+
};
48+
49+
const setupOutput = (input: {
50+
readonly platform?: NodeJS.Platform;
51+
readonly uid?: number;
52+
readonly env?: NodeJS.ProcessEnv;
53+
readonly browserInstalled?: boolean;
54+
}) =>
55+
Effect.gen(function* () {
56+
yield* runBrowserSetup(Option.some("/data/t3"));
57+
return (yield* TestConsole.logLines)
58+
.filter((line): line is string => typeof line === "string")
59+
.join("\n");
60+
}).pipe(
61+
Effect.provide(
62+
Layer.mergeAll(
63+
TestConsole.layer,
64+
fileSystemFor(input.browserInstalled ?? true),
65+
NodePath.layerPosix,
66+
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, versionSpawner),
67+
),
68+
),
69+
Effect.provideService(HostProcess.Platform, input.platform ?? "linux"),
70+
Effect.provideService(HostProcess.UserId, input.uid ?? 1000),
71+
Effect.provideService(HostProcess.Environment, input.env ?? {}),
72+
);
73+
74+
describe("t3 browser setup", () => {
75+
it.effect("warns that a root service cannot sandbox Chrome instead of saying ready", () =>
76+
Effect.gen(function* () {
77+
const output = yield* setupOutput({ uid: 0 });
78+
expect(output).toBe(PreviewBrowserHost.ROOT_SANDBOX_MESSAGE);
79+
expect(output).toContain("nonroot `User=`");
80+
expect(output).toContain("T3CODE_SERVER_BROWSER_SANDBOX=0");
81+
expect(output).not.toContain("ready");
82+
expect(output).not.toContain("AppArmor");
83+
expect(output).not.toContain("browser setup");
84+
}),
85+
);
86+
87+
it.effect("still says ready for a non-root user", () =>
88+
Effect.gen(function* () {
89+
const output = yield* setupOutput({ uid: 1000 });
90+
expect(output).toBe("This host is ready for T3's browser.");
91+
}),
92+
);
93+
94+
it.effect("still says ready when sudo is only elevating setup for a non-root user", () =>
95+
Effect.gen(function* () {
96+
const output = yield* setupOutput({
97+
uid: 0,
98+
env: { SUDO_UID: "1000", SUDO_USER: "ada" },
99+
});
100+
expect(output).toBe("This host is ready for T3's browser.");
101+
}),
102+
);
103+
104+
it.effect("says ready when a root service has turned the sandbox off", () =>
105+
Effect.gen(function* () {
106+
const output = yield* setupOutput({
107+
uid: 0,
108+
env: { T3CODE_SERVER_BROWSER_SANDBOX: "0" },
109+
});
110+
expect(output).toBe("This host is ready for T3's browser.");
111+
}),
112+
);
113+
114+
it.effect(
115+
"does not claim the sandbox is allowed for a root service before the browser is installed",
116+
() =>
117+
Effect.gen(function* () {
118+
const output = yield* setupOutput({ uid: 0, browserInstalled: false });
119+
expect(output).toBe(PreviewBrowserHost.ROOT_SANDBOX_MESSAGE);
120+
expect(output).not.toContain("allowed here");
121+
}),
122+
);
123+
124+
it.effect("leaves other systems alone", () =>
125+
Effect.gen(function* () {
126+
const output = yield* setupOutput({ platform: "darwin", uid: 0 });
127+
expect(output).toBe("Nothing to set up: T3's browser runs as is on this system.");
128+
}),
129+
);
130+
});

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

Lines changed: 90 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
*
1010
* Both need root. Without it, the command prints what it would change and the
1111
* `sudo` line to run. It is safe to run again; it skips what is already done.
12+
*
13+
* It cannot fix a server that is itself running as root. Chrome refuses its
14+
* sandbox for uid 0, so that case warns instead of reporting the host ready.
15+
* `sudo` from a non-root user is only root for this command and still reports
16+
* ready when the checks pass.
1217
*/
1318
import * as HostProcess from "@t3tools/shared/HostProcess";
1419
import * as Console from "effect/Console";
@@ -99,90 +104,98 @@ const installedBrowser = Effect.fn("browserSetup.installedBrowser")(function* (b
99104
return Option.none<string>();
100105
});
101106

102-
const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe(
103-
Command.withDescription(
104-
"Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries.",
105-
),
106-
Command.withHandler(({ baseDir }) =>
107-
Effect.gen(function* () {
108-
if ((yield* HostProcess.Platform) !== "linux") {
109-
return yield* Console.log("Nothing to set up: T3's browser runs as is on this system.");
110-
}
111-
const fs = yield* FileSystem.FileSystem;
112-
const isRoot = (yield* HostProcess.UserId) === 0;
113-
const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND);
107+
/** Runs `t3 browser setup` for an explicit or default T3 home. */
108+
export const runBrowserSetup = Effect.fn("browserSetup")(function* (
109+
baseDir: Option.Option<string>,
110+
) {
111+
if ((yield* HostProcess.Platform) !== "linux") {
112+
return yield* Console.log("Nothing to set up: T3's browser runs as is on this system.");
113+
}
114+
const fs = yield* FileSystem.FileSystem;
115+
const env = yield* HostProcess.Environment;
116+
const uid = yield* HostProcess.UserId;
117+
const isRoot = uid === 0;
118+
const warnsAboutRoot = PreviewBrowserHost.setupWarnsAboutRoot({ uid, env });
114119

115-
const needsProfile = yield* PreviewBrowserHost.sandboxBlocked;
116-
const browser = yield* installedBrowser(yield* setupBaseDir(baseDir));
117-
const missing = Option.isSome(browser)
118-
? yield* PreviewBrowserHost.missingLibraries(browser.value)
119-
: [];
120-
const hasApt = yield* fs.exists("/usr/bin/apt-get").pipe(Effect.orElseSucceed(() => false));
120+
const needsProfile = yield* PreviewBrowserHost.sandboxBlocked;
121+
const browser = yield* installedBrowser(yield* setupBaseDir(baseDir));
122+
const missing = Option.isSome(browser)
123+
? yield* PreviewBrowserHost.missingLibraries(browser.value)
124+
: [];
125+
const hasApt = yield* fs.exists("/usr/bin/apt-get").pipe(Effect.orElseSucceed(() => false));
126+
const reportReady = (message: string) =>
127+
Console.log(warnsAboutRoot ? PreviewBrowserHost.ROOT_SANDBOX_MESSAGE : message);
121128

122-
if (!needsProfile && missing.length === 0) {
123-
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.",
127-
);
128-
}
129+
if (!needsProfile && missing.length === 0) {
130+
return yield* reportReady(
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.",
134+
);
135+
}
129136

130-
if (!isRoot) {
131-
if (needsProfile) {
132-
yield* Console.log(
133-
`This host blocks the sandbox T3's browser runs in. Setup installs an AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} that allows it.`,
134-
);
135-
}
136-
if (missing.length > 0) {
137-
yield* Console.log(`T3's browser is missing ${missing.join(", ")}; setup installs them.`);
138-
}
139-
return yield* Console.log(`\nThis needs root. Run:\n\n ${setupCommand}\n`);
140-
}
137+
if (!isRoot) {
138+
const setupCommand = yield* resolveRootCliCommand(PreviewBrowserHost.SETUP_SUBCOMMAND);
139+
if (needsProfile) {
140+
yield* Console.log(
141+
`This host blocks the sandbox T3's browser runs in. Setup installs an AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} that allows it.`,
142+
);
143+
}
144+
if (missing.length > 0) {
145+
yield* Console.log(`T3's browser is missing ${missing.join(", ")}; setup installs them.`);
146+
}
147+
return yield* Console.log(`\nThis needs root. Run:\n\n ${setupCommand}\n`);
148+
}
141149

142-
if (needsProfile) {
143-
yield* fs
144-
.writeFileString(
145-
PreviewBrowserHost.APPARMOR_PROFILE_PATH,
146-
PreviewBrowserHost.APPARMOR_PROFILE,
147-
)
148-
.pipe(
149-
Effect.mapError(
150-
(cause) =>
151-
new BrowserSetupStepError({
152-
step: `write ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`,
153-
detail: cause.message,
154-
}),
155-
),
156-
);
157-
yield* runStep("load the AppArmor profile", "apparmor_parser", [
158-
"-r",
159-
PreviewBrowserHost.APPARMOR_PROFILE_PATH,
160-
]);
161-
yield* Console.log("Allowed Chrome's sandbox for T3's browser.");
162-
}
150+
if (needsProfile) {
151+
yield* fs
152+
.writeFileString(
153+
PreviewBrowserHost.APPARMOR_PROFILE_PATH,
154+
PreviewBrowserHost.APPARMOR_PROFILE,
155+
)
156+
.pipe(
157+
Effect.mapError(
158+
(cause) =>
159+
new BrowserSetupStepError({
160+
step: `write ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`,
161+
detail: cause.message,
162+
}),
163+
),
164+
);
165+
yield* runStep("load the AppArmor profile", "apparmor_parser", [
166+
"-r",
167+
PreviewBrowserHost.APPARMOR_PROFILE_PATH,
168+
]);
169+
yield* Console.log("Allowed Chrome's sandbox for T3's browser.");
170+
}
163171

164-
if (missing.length > 0) {
165-
if (!hasApt) {
166-
return yield* Console.log(
167-
`T3's browser is missing ${missing.join(", ")}. Install them with your package manager, then run this again.`,
168-
);
169-
}
170-
yield* runStep("refresh the package lists", "apt-get", ["update"]);
171-
const packages = yield* Effect.forEach(PreviewBrowserHost.DEBIAN_PACKAGES, (names) =>
172-
Effect.findFirst(names, aptOffers).pipe(Effect.map(Option.getOrElse(() => names[0]!))),
173-
);
174-
yield* runStep("install the browser's libraries", "apt-get", [
175-
"install",
176-
"-y",
177-
"--no-install-recommends",
178-
...packages,
179-
]);
180-
yield* Console.log("Installed the browser's libraries.");
181-
}
172+
if (missing.length > 0) {
173+
if (!hasApt) {
174+
return yield* Console.log(
175+
`T3's browser is missing ${missing.join(", ")}. Install them with your package manager, then run this again.`,
176+
);
177+
}
178+
yield* runStep("refresh the package lists", "apt-get", ["update"]);
179+
const packages = yield* Effect.forEach(PreviewBrowserHost.DEBIAN_PACKAGES, (names) =>
180+
Effect.findFirst(names, aptOffers).pipe(Effect.map(Option.getOrElse(() => names[0]!))),
181+
);
182+
yield* runStep("install the browser's libraries", "apt-get", [
183+
"install",
184+
"-y",
185+
"--no-install-recommends",
186+
...packages,
187+
]);
188+
yield* Console.log("Installed the browser's libraries.");
189+
}
190+
191+
yield* reportReady("This host is ready for T3's browser.");
192+
});
182193

183-
yield* Console.log("This host is ready for T3's browser.");
184-
}),
194+
const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe(
195+
Command.withDescription(
196+
"Set up this Linux host for T3's browser: allow Chrome's sandbox and install its libraries.",
185197
),
198+
Command.withHandler(({ baseDir }) => runBrowserSetup(baseDir)),
186199
);
187200

188201
export const browserCommand = Command.make("browser").pipe(

0 commit comments

Comments
 (0)