@devries-cameron20 / t3code Mirror · read-only / commits / db2cc20264
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

fix(server): t3 browser setup reloads an AppArmor profile that is on disk but not loaded (#36490)

Julius Marminge committed Nov 13, 2026 at 19:39 UTC db2cc2026427c5eb647a08538c12402341db0fab
4 files changed +186 -22
apps/server/src/cli/browser.test.ts new
+139
@@ -0,0 +1,139 @@
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 PlatformError from "effect/PlatformError";
8 +import * as Sink from "effect/Sink";
9 +import * as Stream from "effect/Stream";
10 +import * as TestConsole from "effect/testing/TestConsole";
11 +import { Command } from "effect/cli";
12 +import { ChildProcessSpawner } from "effect/process";
13 +
14 +import packageJson from "../../package.json" with { type: "json" };
15 +import * as PreviewBrowserHost from "../preview/PreviewBrowserHost.ts";
16 +import { browserCommand } from "./browser.ts";
17 +
18 +const USERNS_RESTRICTION = "/proc/sys/kernel/apparmor_restrict_unprivileged_userns";
19 +const BROWSER_ROOT = "/home/me/.t3/tools/chrome-headless-shell";
20 +const BROWSER = `${BROWSER_ROOT}/linux64/154/chrome-headless-shell`;
21 +
22 +const notFound = (method: string, path: string) =>
23 + PlatformError.systemError({
24 + _tag: "NotFound",
25 + module: "FileSystem",
26 + method,
27 + pathOrDescriptor: path,
28 + });
29 +
30 +/** An Ubuntu host with the userns restriction on, the browser installed, and a system font. */
31 +const runSetupAsRoot = (input: { readonly profile: string | undefined }) => {
32 + const files = new Map([
33 + [USERNS_RESTRICTION, "1\n"],
34 + [BROWSER, ""],
35 + ]);
36 + if (input.profile !== undefined) {
37 + files.set(PreviewBrowserHost.APPARMOR_PROFILE_PATH, input.profile);
38 + }
39 + const directories = new Map([
40 + [BROWSER_ROOT, ["linux64"]],
41 + [`${BROWSER_ROOT}/linux64`, ["154"]],
42 + ]);
43 + const commands: Array<string> = [];
44 + const fileSystem = FileSystem.makeNoop({
45 + exists: (path) => Effect.succeed(files.has(path)),
46 + readFileString: (path) => {
47 + const content = files.get(path);
48 + return content === undefined
49 + ? Effect.fail(notFound("readFileString", path))
50 + : Effect.succeed(content);
51 + },
52 + readDirectory: (path) => {
53 + const entries = directories.get(path);
54 + return entries === undefined
55 + ? Effect.fail(notFound("readDirectory", path))
56 + : Effect.succeed(entries);
57 + },
58 + writeFileString: (path, content) => Effect.sync(() => void files.set(path, content)),
59 + });
60 + const spawner = ChildProcessSpawner.make((command) =>
61 + Effect.sync(() => {
62 + const { command: name, args } = command as unknown as {
63 + readonly command: string;
64 + readonly args: ReadonlyArray<string>;
65 + };
66 + commands.push([name, ...args].join(" "));
67 + const output =
68 + name === "fc-match" ? "/usr/share/fonts/sans.ttf" : name === BROWSER ? "Chrome 154\n" : "";
69 + return ChildProcessSpawner.makeHandle({
70 + pid: ChildProcessSpawner.ProcessId(1),
71 + exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
72 + isRunning: Effect.succeed(false),
73 + kill: () => Effect.void,
74 + unref: Effect.succeed(Effect.void),
75 + stdin: Sink.drain,
76 + stdout: Stream.make(new TextEncoder().encode(output)),
77 + stderr: Stream.empty,
78 + all: Stream.make(new TextEncoder().encode(output)),
79 + getInputFd: () => Sink.drain,
80 + getOutputFd: () => Stream.empty,
81 + });
82 + }),
83 + );
84 + return Effect.gen(function* () {
85 + yield* Command.runWith(browserCommand, { version: packageJson.version })([
86 + "setup",
87 + "--base-dir",
88 + "/home/me/.t3",
89 + ]);
90 + return {
91 + output: (yield* TestConsole.logLines).join("\n"),
92 + profile: files.get(PreviewBrowserHost.APPARMOR_PROFILE_PATH),
93 + loads: commands.filter((command) => command.startsWith("apparmor_parser")),
94 + };
95 + }).pipe(
96 + Effect.provideService(HostProcess.Platform, "linux"),
97 + Effect.provideService(HostProcess.UserId, 0),
98 + Effect.provideService(HostProcess.Environment, { SUDO_USER: "me" }),
99 + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
100 + Effect.provideService(FileSystem.FileSystem, fileSystem),
101 + Effect.provide(Layer.mergeAll(NodeServices.layer, Layer.fresh(TestConsole.layer))),
102 + );
103 +};
104 +
105 +const LOAD = `apparmor_parser -r ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`;
106 +
107 +const CUSTOMIZED_PROFILE = PreviewBrowserHost.APPARMOR_PROFILE.replace(
108 + " userns,\n",
109 + " userns,\n deny /tmp/t3-admin-private/** rw,\n",
110 +);
111 +
112 +it.effect.each([
113 + {
114 + label: "reloads a profile that is on disk",
115 + profile: PreviewBrowserHost.APPARMOR_PROFILE,
116 + written: PreviewBrowserHost.APPARMOR_PROFILE,
117 + said: "Reloaded the AppArmor profile",
118 + },
119 + {
120 + label: "writes and loads a missing profile",
121 + profile: undefined,
122 + written: PreviewBrowserHost.APPARMOR_PROFILE,
123 + said: "Wrote and loaded",
124 + },
125 + {
126 + label: "reloads a customized profile without rewriting it",
127 + profile: CUSTOMIZED_PROFILE,
128 + written: CUSTOMIZED_PROFILE,
129 + said: "Reloaded the AppArmor profile",
130 + },
131 +])("t3 browser setup as root $label", (input) =>
132 + Effect.gen(function* () {
133 + const result = yield* runSetupAsRoot({ profile: input.profile });
134 + expect(result.loads).toEqual([LOAD]);
135 + expect(result.profile).toBe(input.written);
136 + expect(result.output).toContain(input.said);
137 + expect(result.output).toContain("This host is ready for T3's browser.");
138 + }),
139 +);
apps/server/src/cli/browser.ts
+35 -17
@@ -127,7 +127,14 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
127 ? new PreviewBrowserHost.PreviewBrowserRootError().message
128 : undefined;
129
130 - const needsProfile = yield* PreviewBrowserHost.sandboxBlocked;
130 + const restricted = yield* PreviewBrowserHost.usernsRestricted;
131 + // The kernel can lack a profile that is on disk, as on a host restored from an image,
132 + // and only root can read the loaded list, so root reloads it on every run.
133 + const needsProfile = isRoot ? restricted : yield* PreviewBrowserHost.sandboxBlocked;
134 + const reloadHint =
135 + restricted && !isRoot
136 + ? `\nIf the browser still reports a blocked sandbox, the AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} is not loaded. To load it, run:\n\n ${setupCommand}\n`
137 + : "";
138 const browser = yield* installedBrowser(baseDirPath);
139 const missing = Option.isSome(browser)
140 ? yield* PreviewBrowserHost.missingLibraries(browser.value)
@@ -140,7 +147,8 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
147 rootWarning ??
148 (Option.isSome(browser)
149 ? "This host is ready for T3's browser."
143 - : "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again."),
150 + : "Chrome's sandbox is allowed here. T3's browser installs on first use; if it then reports missing libraries, run this again.") +
151 + reloadHint,
152 );
153 }
154
@@ -162,25 +170,35 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe
170 }
171
172 if (needsProfile) {
165 - yield* fs
166 - .writeFileString(
167 - PreviewBrowserHost.APPARMOR_PROFILE_PATH,
168 - PreviewBrowserHost.APPARMOR_PROFILE,
169 - )
170 - .pipe(
171 - Effect.mapError(
172 - (cause) =>
173 - new BrowserSetupStepError({
174 - step: `write ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`,
175 - detail: cause.message,
176 - }),
177 - ),
178 - );
173 + // An existing profile may carry an administrator's edits, so it is reloaded as is.
174 + const profileOnDisk = yield* fs
175 + .exists(PreviewBrowserHost.APPARMOR_PROFILE_PATH)
176 + .pipe(Effect.orElseSucceed(() => false));
177 + if (!profileOnDisk) {
178 + yield* fs
179 + .writeFileString(
180 + PreviewBrowserHost.APPARMOR_PROFILE_PATH,
181 + PreviewBrowserHost.APPARMOR_PROFILE,
182 + )
183 + .pipe(
184 + Effect.mapError(
185 + (cause) =>
186 + new BrowserSetupStepError({
187 + step: `write ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}`,
188 + detail: cause.message,
189 + }),
190 + ),
191 + );
192 + }
193 yield* runStep("load the AppArmor profile", "apparmor_parser", [
194 "-r",
195 PreviewBrowserHost.APPARMOR_PROFILE_PATH,
196 ]);
183 - yield* Console.log("Allowed Chrome's sandbox for T3's browser.");
197 + yield* Console.log(
198 + profileOnDisk
199 + ? `Reloaded the AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} that allows Chrome's sandbox for T3's browser.`
200 + : `Wrote and loaded the AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} that allows Chrome's sandbox for T3's browser.`,
201 + );
202 }
203
204 if ((missing.length > 0 || needsFonts) && !hasApt) {
apps/server/src/preview/PreviewBrowserHost.ts
+8 -3
@@ -126,13 +126,18 @@ export const missingLibraries = Effect.fn("PreviewBrowserHost.missingLibraries")
126 return listed.length > 0 ? listed : [loaderError];
127 });
128
129 -export const sandboxBlocked = Effect.gen(function* () {
129 +export const usernsRestricted = Effect.gen(function* () {
130 if ((yield* HostProcess.Platform) !== "linux") return false;
131 const fs = yield* FileSystem.FileSystem;
132 - const restricted = yield* fs.readFileString(USERNS_RESTRICTION).pipe(
132 + return yield* fs.readFileString(USERNS_RESTRICTION).pipe(
133 Effect.map((value) => value.trim() === "1"),
134 Effect.orElseSucceed(() => false),
135 );
136 - if (!restricted) return false;
136 +}).pipe(Effect.withSpan("PreviewBrowserHost.usernsRestricted"));
137 +
138 +/** A profile on disk can still be unloaded; only a failed launch shows that without root. */
139 +export const sandboxBlocked = Effect.gen(function* () {
140 + if (!(yield* usernsRestricted)) return false;
141 + const fs = yield* FileSystem.FileSystem;
142 return !(yield* fs.exists(APPARMOR_PROFILE_PATH).pipe(Effect.orElseSucceed(() => false)));
143 }).pipe(Effect.withSpan("PreviewBrowserHost.sandboxBlocked"));
docs/user/remote-access.md
+4 -2
@@ -316,8 +316,10 @@ The server shows the exact line for how you started it, and keeps your `PATH`
316 when you started it with `npx` and Node is installed only for your user. Where `t3` is not on your `PATH`, such as with only the
317 desktop app installed, it names the full path of the app's own `t3` instead. It allows Chrome's sandbox with an AppArmor profile and installs
318 any missing libraries and system fonts with apt; pages that load without text
319 -can mean the host has no fonts. It is safe to run again. Without `sudo`, it only
320 -reports what it would change.
319 +can mean the host has no fonts. It is safe to run again, and each run reloads
320 +the AppArmor profile: run it again if the browser reports a blocked sandbox on a
321 +host restored from an image or snapshot, where the profile can be on disk
322 +without being loaded. Without `sudo`, it only reports what it would change.
323
324 The browser runs in Chrome's sandbox by default. Chrome cannot start that sandbox
325 as root. If your T3 server runs as root, run it as a nonroot user to keep