Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(termux): isolate Linux runtime inside PRoot

Rahman986237d7419b
diff --git a/scripts/install-termux.sh b/scripts/install-termux.sh
index 39f6c9bb..c2f3e51a 100755
--- a/scripts/install-termux.sh
+++ b/scripts/install-termux.sh
@@ -1,12 +1,5 @@
#!/data/data/com.termux/files/usr/bin/bash
-# MSO bootstrap for Termux/Android.
-#
-# Supported architecture:
-# Termux host -> Ubuntu PRoot -> normal MSO Linux installer (--no-service)
-#
-# Native Android/Termux is deliberately not used for the MSO runtime because
-# MSO depends on Bun and native Node modules. Keeping the runtime in a normal
-# Linux userspace also avoids Android-specific libc/platform mismatches.
+# Termux is only the launcher: all MSO tools run in Ubuntu's Linux userspace.
set -Eeuo pipefail
umask 077
@@ -15,6 +8,7 @@ GUEST_USER="${MSO_TERMUX_USER:-mso}"
IMAGE="${MSO_TERMUX_IMAGE:-ubuntu:24.04}"
INSTALL_URL="${MSO_TERMUX_INSTALL_URL:-https://raw.githubusercontent.com/rahmanef63/mso/main/scripts/install.sh}"
TERMUX_BIN="${PREFIX:-/data/data/com.termux/files/usr}/bin"
+GUEST_SYSTEM_PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
info() { printf '· %s\n' "$*"; }
ok() { printf '✓ %s\n' "$*"; }
@@ -24,20 +18,19 @@ case "${PREFIX:-}" in
*/com.termux/files/usr) ;;
*) fail 'run this installer from Termux on Android.' ;;
esac
+# These identities also appear in the generated launcher. Never interpolate
+# arbitrary shell source (the download URL is passed as a positional argument).
+[[ "$DISTRO" =~ ^[a-zA-Z0-9][a-zA-Z0-9_-]*$ ]] || fail 'invalid distro name.'
+[[ "$GUEST_USER" =~ ^[a-z_][a-z0-9_-]*$ ]] && [ "$GUEST_USER" != root ] || fail 'invalid non-root guest user.'
command -v apt >/dev/null 2>&1 || fail 'Termux apt is missing.'
-
info 'repairing/updating Termux packages'
if ! apt update; then
printf '\nRepository update failed. Run `termux-change-repo`, choose a working Main repository, then retry.\n' >&2
exit 1
fi
DEBIAN_FRONTEND=noninteractive apt full-upgrade -y
-
-# Use apt directly here: when a partial Termux upgrade breaks curl, `pkg` can
-# fail before it gets a chance to repair the affected libraries.
DEBIAN_FRONTEND=noninteractive apt install -y curl ca-certificates proot-distro
-
command -v curl >/dev/null 2>&1 || fail 'curl is still unavailable after package repair.'
command -v proot-distro >/dev/null 2>&1 || fail 'proot-distro installation failed.'
curl --version >/dev/null 2>&1 || fail 'curl is installed but cannot start; run `apt update && apt full-upgrade -y` again.'
@@ -49,45 +42,70 @@ else
proot-distro install "$IMAGE" --name "$DISTRO"
fi
+# Reset at the guest boundary, BEFORE starting bash. Unsetting PREFIX alone
+# leaves Android PATH, BUN_INSTALL, npm_config_nodedir, LD_PRELOAD, BASH_ENV,
+# compiler flags and exported shell functions behind. Login profiles can also
+# reintroduce them, so neither installation nor the launcher uses bash -lc.
info 'preparing Ubuntu guest dependencies and non-root MSO owner'
-proot-distro login "$DISTRO" -- bash -lc "
+proot-distro login "$DISTRO" -- /usr/bin/env -i \
+ HOME=/root USER=root LOGNAME=root SHELL=/bin/bash \
+ PATH="$GUEST_SYSTEM_PATH" LANG=C.UTF-8 TERM="${TERM:-xterm-256color}" \
+ /bin/bash --noprofile --norc -c '
set -Eeuo pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get update
- apt-get install -y sudo curl ca-certificates git jq coreutils util-linux build-essential python3 passwd
- if ! id -u '$GUEST_USER' >/dev/null 2>&1; then
- useradd -m -s /bin/bash '$GUEST_USER'
+ apt-get install -y sudo curl ca-certificates git jq coreutils util-linux build-essential python3 passwd unzip
+ if ! id -u "$1" >/dev/null 2>&1; then
+ useradd -m -d "/home/$1" -s /bin/bash "$1"
fi
- printf '%s ALL=(ALL) NOPASSWD:ALL\\n' '$GUEST_USER' > '/etc/sudoers.d/$GUEST_USER'
- chmod 0440 '/etc/sudoers.d/$GUEST_USER'
-"
+ [ "$(getent passwd "$1" | cut -d: -f6)" = "/home/$1" ] || {
+ echo "Existing guest user must have home /home/$1; choose another MSO_TERMUX_USER." >&2
+ exit 1
+ }
+ printf "%s ALL=(ALL) NOPASSWD:ALL\n" "$1" > "/etc/sudoers.d/$1"
+ chmod 0440 "/etc/sudoers.d/$1"
+' bash "$GUEST_USER"
info 'installing/updating MSO inside Ubuntu'
-proot-distro login "$DISTRO" --user "$GUEST_USER" -- bash -lc "
+proot-distro login "$DISTRO" --user "$GUEST_USER" -- /usr/bin/env -i \
+ HOME="/home/$GUEST_USER" USER="$GUEST_USER" LOGNAME="$GUEST_USER" SHELL=/bin/bash \
+ PATH="/home/$GUEST_USER/.local/bin:/home/$GUEST_USER/.bun/bin:$GUEST_SYSTEM_PATH" \
+ BUN_INSTALL="/home/$GUEST_USER/.bun" LANG=C.UTF-8 TERM="${TERM:-xterm-256color}" \
+ /bin/bash --noprofile --norc -c '
set -Eeuo pipefail
- unset PREFIX TERMUX_VERSION
- export PATH=\"\$HOME/.local/bin:\$HOME/.bun/bin:\$PATH\"
- curl -fsSL '$INSTALL_URL' | bash -s -- --no-service --no-onboard
+ cd "$HOME"
+ # Refuse manually linked Android runtimes even on an otherwise clean PATH.
+ # Missing tools are installed by the normal Linux installer (NodeSource/Bun).
+ for runtime in node bun; do
+ if command -v "$runtime" >/dev/null 2>&1; then
+ [ "$("$runtime" -p "process.platform")" = linux ] || {
+ echo "Non-Linux $runtime in guest PATH; remove the guest Android runtime link and retry." >&2
+ exit 1
+ }
+ fi
+ done
+ curl -fsSL "$1" | /bin/bash -s -- --no-service --no-onboard
+ [ "$(node -p "process.platform")" = linux ]
+ [ "$(bun -p "process.platform")" = linux ]
command -v mso >/dev/null 2>&1
mso --version
mso -h >/dev/null
-"
+' bash "$INSTALL_URL"
LAUNCHER="$TERMUX_BIN/mso"
info "installing Termux launcher: $LAUNCHER"
cat > "$LAUNCHER" <<EOF
#!$TERMUX_BIN/bash
set -Eeuo pipefail
-exec proot-distro login "$DISTRO" --user "$GUEST_USER" -- bash -lc 'export PATH="\$HOME/.local/bin:\$HOME/.bun/bin:\$PATH"; exec mso "\$@"' bash "\$@"
+exec proot-distro login "$DISTRO" --user "$GUEST_USER" -- /usr/bin/env -i \\
+ HOME="/home/$GUEST_USER" USER="$GUEST_USER" LOGNAME="$GUEST_USER" SHELL=/bin/bash \\
+ PATH="/home/$GUEST_USER/.local/bin:/home/$GUEST_USER/.bun/bin:$GUEST_SYSTEM_PATH" \\
+ BUN_INSTALL="/home/$GUEST_USER/.bun" LANG=C.UTF-8 TERM="\${TERM:-xterm-256color}" \\
+ /bin/bash --noprofile --norc -c 'cd "\$HOME"; exec mso "\$@"' bash "\$@"
EOF
chmod 0755 "$LAUNCHER"
ok 'MSO is installed for Termux through Ubuntu PRoot.'
-printf '\nVerify:\n'
-printf ' mso --version\n'
-printf ' mso doctor\n\n'
-printf 'Start:\n'
-printf ' mso\n'
-printf ' mso web\n\n'
+printf '\nVerify:\n mso --version\n mso doctor\n\nStart:\n mso\n mso web\n\n'
printf 'The PRoot environment has no systemd service. The launcher enters the Ubuntu guest automatically.\n'
printf 'YOLO mode is intentionally not enabled by this installer; use `mso --yolo` only when you explicitly want automatic write/exec approval.\n'
diff --git a/scripts/install-termux.test.ts b/scripts/install-termux.test.ts
new file mode 100644
index 00000000..3e4b08d6
--- /dev/null
+++ b/scripts/install-termux.test.ts
@@ -0,0 +1,110 @@
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { spawnSync } from "node:child_process";
+import { afterEach, describe, expect, it } from "vitest";
+
+const installer = path.resolve("scripts/install-termux.sh");
+const roots: string[] = [];
+const systemPath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
+afterEach(() => { for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true }); });
+const quote = (s: string) => `'${s.replaceAll("'", "'\\''")}'`;
+function fixture() {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), "mso-termux-isolation-")); roots.push(root);
+ const prefix = path.join(root, "com.termux/files/usr");
+ fs.mkdirSync(path.join(prefix, "bin"), { recursive: true });
+ const poison = path.join(root, "poison.sh");
+ fs.writeFileSync(poison, "echo HOST_STARTUP_LEAK >&2; exit 91\n");
+ for (const name of ["node", "bun"]) fs.writeFileSync(path.join(prefix, "bin", name), "#!/bin/sh\necho android\n", { mode: 0o755 });
+ // No packages, downloads, user creation or real PRoot operations are performed.
+ // Capture the real installer argv, including its generated launcher heredoc.
+ const mock = `
+apt() { :; }
+curl() { :; }
+proot-distro() {
+ local arg user=root
+ for arg in "$@"; do [ "$arg" != --user ] || user=owner; done
+ while [ "$#" -gt 0 ] && [ "$1" != -- ]; do shift; done
+ [ "$#" -gt 0 ] || return 0
+ shift
+ [ "$1" != /bin/true ] || return 0
+ printf '%s\\0' "$@" > ${quote(root)}/"$user.args"
+}
+`;
+ const env: NodeJS.ProcessEnv = { PATH: `/usr/bin:/bin`, HOME: root, PREFIX: prefix, TERM: "xterm-256color", NODE_ENV: "test" };
+ const run = spawnSync("/bin/bash", ["--noprofile", "--norc", "-c", `${mock}\nsource ${quote(installer)}`], { env, encoding: "utf8", timeout: 10000 });
+ expect(run.status, run.stderr).toBe(0);
+ const readArgs = (name: string) => fs.readFileSync(path.join(root, `${name}.args`), "utf8").split("\0").slice(0, -1);
+ const dirty = {
+ ...env, PATH: `${prefix}/bin:/usr/bin:/bin`, BUN_INSTALL: `${prefix}/bun`,
+ TERMUX_VERSION: "test", NODE_PATH: `${prefix}/node_modules`, NODE_OPTIONS: "--require=/android-only.js",
+ npm_config_nodedir: prefix, npm_config_prefix: prefix, LD_LIBRARY_PATH: `${prefix}/lib`,
+ LD_PRELOAD: `${prefix}/lib/nonexistent-test-preload.so`, CFLAGS: "-I/android", LDFLAGS: "-L/android",
+ BASH_ENV: poison, ENV: poison, "BASH_FUNC_node%%": "() { echo android; }",
+ };
+ return { root, prefix, mock, env, dirty, readArgs, launcher: path.join(prefix, "bin/mso") };
+}
+function probe(args: string[], env: NodeJS.ProcessEnv) {
+ const copy = [...args], body = copy.indexOf("-c") + 1;
+ expect(body).toBeGreaterThan(0);
+ // Execute the exact clean boundary, but never execute privileged guest setup.
+ copy[body] = 'printf "PATH=%s\\nHOME=%s\\nBUN_INSTALL=%s\\n" "$PATH" "$HOME" "${BUN_INSTALL:-}"; /usr/bin/env; type -t node; type -t bun; :';
+ return spawnSync(copy[0], copy.slice(1), { env, encoding: "utf8", timeout: 10000 });
+}
+
+describe("Termux PRoot Linux environment boundary", () => {
+ it("keeps the adapter executable and valid Bash", () => {
+ expect(fs.statSync(installer).mode & 0o111).toBe(0o111);
+ const result = spawnSync("/bin/bash", ["-n", installer], { encoding: "utf8" });
+ expect(result.status, result.stderr).toBe(0);
+ });
+ it("clears Android PATH, Node/Bun configuration, loader flags and shell startup hooks before either guest shell", () => {
+ const f = fixture();
+ for (const name of ["root", "owner"]) {
+ const args = f.readArgs(name);
+ expect(args.slice(0, 2)).toEqual(["/usr/bin/env", "-i"]);
+ expect(args.slice(args.indexOf("/bin/bash"), args.indexOf("-c") + 1)).toEqual(["/bin/bash", "--noprofile", "--norc", "-c"]);
+ const result = probe(args, f.dirty);
+ expect(result.status, result.stderr).toBe(0);
+ expect(result.stdout).not.toContain(f.prefix);
+ expect(result.stdout).not.toMatch(/TERMUX_VERSION=|NODE_PATH=|NODE_OPTIONS=|npm_config_|LD_PRELOAD=|LD_LIBRARY_PATH=|CFLAGS=|LDFLAGS=|BASH_ENV=|ENV=|BASH_FUNC_|function/);
+ expect(result.stderr).not.toContain("HOST_STARTUP_LEAK");
+ expect(result.stdout).toContain(`PATH=${name === "root" ? "" : "/home/mso/.local/bin:/home/mso/.bun/bin:"}${systemPath}\n`);
+ expect(result.stdout).toContain(`HOME=${name === "root" ? "/root" : "/home/mso"}\n`);
+ if (name === "owner") expect(result.stdout).toContain("BUN_INSTALL=/home/mso/.bun\n");
+ }
+ });
+ it("generates a syntax-valid launcher with the same clean boundary and lossless argument forwarding", () => {
+ const f = fixture();
+ expect(fs.statSync(f.launcher).mode & 0o777).toBe(0o755);
+ expect(spawnSync("/bin/bash", ["-n", f.launcher]).status).toBe(0);
+ // exec cannot call the shell mock; a test-only copy removes just that exec.
+ const source = fs.readFileSync(f.launcher, "utf8").replace("exec proot-distro", "proot-distro");
+ const argv = ["web", "two words", "", "$(touch NOT_EXECUTED)", "a'b"];
+ const result = spawnSync("/bin/bash", ["--noprofile", "--norc", "-c", `${f.mock}\n${source}`, "bash", ...argv], { env: f.env, encoding: "utf8", timeout: 10000 });
+ expect(result.status, result.stderr).toBe(0);
+ const args = f.readArgs("owner");
+ expect(args.slice(-argv.length)).toEqual(argv);
+ expect(args[args.indexOf("-c") + 1]).toBe('cd "$HOME"; exec mso "$@"');
+ const checked = probe(args, f.dirty);
+ expect(checked.status, checked.stderr).toBe(0);
+ expect(checked.stdout).toContain(`PATH=/home/mso/.local/bin:/home/mso/.bun/bin:${systemPath}\n`);
+ expect(checked.stdout).toContain("BUN_INSTALL=/home/mso/.bun\n");
+ expect(checked.stdout).not.toContain(f.prefix);
+ expect(checked.stdout).not.toMatch(/NODE_OPTIONS=|npm_config_|BASH_ENV=|TERMUX_VERSION=|LD_PRELOAD=/);
+ expect(checked.stderr).not.toContain("HOST_STARTUP_LEAK");
+ });
+ it.each(["node", "bun"])("rejects a guest-visible Android %s before downloading the installer", (android) => {
+ const f = fixture(), bin = path.join(f.root, "guest-bin"); fs.mkdirSync(bin);
+ for (const runtime of ["node", "bun"]) fs.writeFileSync(path.join(bin, runtime), `#!/bin/sh\necho ${runtime === android ? "android" : "linux"}\n`, { mode: 0o755 });
+ const args = f.readArgs("owner"), body = args[args.indexOf("-c") + 1];
+ const guard = body.slice(0, body.indexOf(' curl -fsSL "$1"'));
+ expect(guard).toContain('for runtime in node bun; do');
+ const result = spawnSync("/bin/bash", ["--noprofile", "--norc", "-c", `${guard}\necho DOWNLOAD_REACHED`], { env: { HOME: f.root, PATH: `${bin}:/usr/bin:/bin`, NODE_ENV: "test" }, encoding: "utf8", timeout: 10000 });
+ expect(result.status).not.toBe(0);
+ expect(result.stderr).toContain(`Non-Linux ${android}`);
+ expect(result.stdout).not.toContain("DOWNLOAD_REACHED");
+ expect(body).toContain('[ "$(node -p "process.platform")" = linux ]');
+ expect(body).toContain('[ "$(bun -p "process.platform")" = linux ]');
+ });
+});