diff --git a/apps/server/src/cli/browser.test.ts b/apps/server/src/cli/browser.test.ts index f652cb58b2104..5c35238d84aa0 100644 --- a/apps/server/src/cli/browser.test.ts +++ b/apps/server/src/cli/browser.test.ts @@ -28,7 +28,11 @@ const notFound = (method: string, path: string) => }); /** An Ubuntu host with the userns restriction on, the browser installed, and a system font. */ -const runSetupAsRoot = (input: { readonly profile: string | undefined }) => { +const runSetupAsRoot = (input: { + readonly profile: string | undefined; + /** An LXC guest whose host still denies userns: the profile loads, Chrome's sandbox does not start. */ + readonly sandboxStillBlocked?: boolean; +}) => { const files = new Map([ [USERNS_RESTRICTION, "1\n"], [BROWSER, ""], @@ -64,11 +68,20 @@ const runSetupAsRoot = (input: { readonly profile: string | undefined }) => { readonly args: ReadonlyArray; }; commands.push([name, ...args].join(" ")); - const output = - name === "fc-match" ? "/usr/share/fonts/sans.ttf" : name === BROWSER ? "Chrome 154\n" : ""; + const blocked = + input.sandboxStillBlocked === true && + !["fc-match", "apparmor_parser", "apt-get", "apt-cache", "ldd"].includes(name) && + !(name === BROWSER && args.includes("--version")); + const output = blocked + ? "FATAL:zygote_host_impl_linux.cc(130)] No usable sandbox!\n" + : name === "fc-match" + ? "/usr/share/fonts/sans.ttf" + : name === BROWSER + ? "Chrome 154\n" + : ""; return ChildProcessSpawner.makeHandle({ pid: ChildProcessSpawner.ProcessId(1), - exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), + exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(blocked ? 1 : 0)), isRunning: Effect.succeed(false), kill: () => Effect.void, unref: Effect.succeed(Effect.void), @@ -137,3 +150,14 @@ it.effect.each([ expect(result.output).toContain("This host is ready for T3's browser."); }), ); + +it.effect("t3 browser setup as root does not report ready while Chrome's sandbox still fails", () => + Effect.gen(function* () { + const result = yield* runSetupAsRoot({ + profile: PreviewBrowserHost.APPARMOR_PROFILE, + sandboxStillBlocked: true, + }); + expect(result.loads).toEqual([LOAD]); + expect(result.output).not.toContain("This host is ready for T3's browser."); + }), +); diff --git a/apps/server/src/cli/browser.ts b/apps/server/src/cli/browser.ts index a82b84ef82d4b..600b36247bfed 100644 --- a/apps/server/src/cli/browser.ts +++ b/apps/server/src/cli/browser.ts @@ -141,8 +141,19 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe : []; const needsFonts = !(yield* fontAvailable); const hasApt = yield* fs.exists("/usr/bin/apt-get").pipe(Effect.orElseSucceed(() => false)); + const sandboxStillBlocked = + restricted && Option.isSome(browser) && rootWarning === undefined + ? PreviewBrowserHost.sandboxFailsToStart( + browser.value, + isRoot ? env.SUDO_USER : undefined, + ) + : Effect.succeed(false); + const stillBlockedMessage = `Chrome's sandbox still cannot start, although the AppArmor profile at ${PreviewBrowserHost.APPARMOR_PROFILE_PATH} is ${isRoot ? "loaded" : "on disk"}. In a container (LXC, Incus, LXD), the container's host restricts user namespaces too: copy that profile to the container's host and load it there with \`apparmor_parser -r ${PreviewBrowserHost.APPARMOR_PROFILE_PATH}\`. Otherwise set \`T3CODE_SERVER_BROWSER_SANDBOX=0\` in the T3 server's environment and restart the server; this disables Chrome's sandbox.`; if (!needsProfile && missing.length === 0 && !needsFonts) { + if (yield* sandboxStillBlocked) { + return yield* Console.log(stillBlockedMessage + reloadHint); + } return yield* Console.log( rootWarning ?? (Option.isSome(browser) @@ -248,6 +259,7 @@ const browserSetupCommand = Command.make("setup", { baseDir: baseDirFlag }).pipe ); } + if (yield* sandboxStillBlocked) return yield* Console.log(stillBlockedMessage); yield* Console.log(rootWarning ?? "This host is ready for T3's browser."); }), ), diff --git a/apps/server/src/preview/PreviewBrowserHost.ts b/apps/server/src/preview/PreviewBrowserHost.ts index 0952dbaf944cd..458a0a272fc30 100644 --- a/apps/server/src/preview/PreviewBrowserHost.ts +++ b/apps/server/src/preview/PreviewBrowserHost.ts @@ -126,6 +126,27 @@ export const missingLibraries = Effect.fn("PreviewBrowserHost.missingLibraries") return listed.length > 0 ? listed : [loaderError]; }); +/** + * A loaded profile does nothing in a container whose host also restricts userns; only a launch + * shows that. Chrome will not sandbox as root, so root launches it as `user`. + */ +export const sandboxFailsToStart = Effect.fn("PreviewBrowserHost.sandboxFailsToStart")(function* ( + executable: string, + user: string | undefined, +) { + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const launch = [executable, "--headless", "--dump-dom", "about:blank"]; + const command = + user === undefined + ? ChildProcess.make(launch[0]!, launch.slice(1), { stdin: "ignore" }) + : ChildProcess.make("runuser", ["-u", user, "--", ...launch], { stdin: "ignore" }); + const output = yield* spawner.string(command, { includeStderr: true }).pipe( + Effect.timeout("15 seconds"), + Effect.orElseSucceed(() => ""), + ); + return output.includes(NO_SANDBOX_SIGNATURE); +}); + export const usernsRestricted = Effect.gen(function* () { if ((yield* HostProcess.Platform) !== "linux") return false; const fs = yield* FileSystem.FileSystem;