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(security): bind gateway tunnels to proven local runtime

Abdurrahman Fakhrulbab73c344d69
diff --git a/docs/GATEWAY.md b/docs/GATEWAY.md
index 306fcc57..16c5ad28 100644
--- a/docs/GATEWAY.md
+++ b/docs/GATEWAY.md
@@ -106,7 +106,9 @@ evidence to name the provider. MSO does not guess provider ownership from a host
## Public health and deployment identity
-Process existence is not public readiness. MSO probes the configured public origin at:
+Process existence is not public readiness. A managed tunnel start also refuses to forward an already-bound loopback port unless that listener is either an MSO-owned fallback runtime or is proven by socket inode + cgroup + working-directory evidence to belong to this checkout's active `mso.service`. A health-shaped HTTP response alone is not runtime identity.
+
+MSO probes the configured public origin at:
```text
https://<public-origin>/api/health
diff --git a/scripts/lib/gateway-reconcile.sh b/scripts/lib/gateway-reconcile.sh
old mode 100755
new mode 100644
index 863fd60f..5e68077d
--- a/scripts/lib/gateway-reconcile.sh
+++ b/scripts/lib/gateway-reconcile.sh
@@ -20,6 +20,7 @@ gateway_reconcile_active_tunnel() {
gateway_runtime_from_state "$state"
gateway_start_runtime_if_needed
gateway_assert_port_loopback_only
+ gateway_assert_managed_runtime_trusted
LOCAL_HEALTH_IDENTITY="$(gateway_health_url_identity "$LOCAL_URL" "${RUNTIME_INSTANCE_ID:-}")" \
|| { gateway_reconcile_runtime_rollback; gateway_fail "active tunnel has no verified local MSO runtime"; }
if ! gateway_provider_probe_public "$provider"; then
diff --git a/scripts/lib/gateway-runtime.sh b/scripts/lib/gateway-runtime.sh
index 8bc89c31..5311b351 100644
--- a/scripts/lib/gateway-runtime.sh
+++ b/scripts/lib/gateway-runtime.sh
@@ -19,6 +19,50 @@ gateway_runtime_from_state() {
fi
fi
}
+gateway_service_owns_local_listener() {
+ local port hex file line addr state inode cgroup pid fd link working tcp tcp6
+ command -v systemctl >/dev/null 2>&1 || return 1
+ systemctl is-active --quiet mso.service 2>/dev/null || return 1
+ working="$(systemctl show -p WorkingDirectory --value mso.service 2>/dev/null || true)"
+ [ -n "$working" ] && [ "$(readlink -f -- "$working" 2>/dev/null || true)" = "$GATEWAY_CANONICAL_ROOT" ] || return 1
+ cgroup="$(systemctl show -p ControlGroup --value mso.service 2>/dev/null || true)"
+ [ -n "$cgroup" ] && [ -r "/sys/fs/cgroup$cgroup/cgroup.procs" ] || return 1
+ port="$(node - "$LOCAL_URL" <<'NODE'
+const u=new URL(process.argv[2]); process.stdout.write(u.port);
+NODE
+)"
+ [[ "$port" =~ ^[0-9]{1,5}$ ]] || return 1
+ printf -v hex '%04X' "$port"
+ tcp="${MSO_GATEWAY_PROC_NET_TCP:-/proc/net/tcp}"
+ tcp6="${MSO_GATEWAY_PROC_NET_TCP6:-/proc/net/tcp6}"
+ inode=''
+ for file in "$tcp" "$tcp6"; do
+ [ -r "$file" ] || continue
+ while read -r line; do
+ set -- $line; [ "$#" -ge 10 ] || continue
+ addr="${2:-}"; state="${4:-}"
+ [ "$state" = 0A ] || continue
+ case "$addr" in *:"$hex") inode="${10:-}"; [ -n "$inode" ] && break 2;; esac
+ done <"$file"
+ done
+ [[ "$inode" =~ ^[0-9]+$ ]] || return 1
+ while read -r pid; do
+ [[ "$pid" =~ ^[0-9]+$ ]] && [ "$pid" -gt 1 ] || continue
+ [ "$(stat -c '%u' "/proc/$pid" 2>/dev/null || true)" = "$(id -u)" ] || continue
+ [ "$(readlink -f -- "/proc/$pid/cwd" 2>/dev/null || true)" = "$GATEWAY_CANONICAL_ROOT" ] || continue
+ for fd in "/proc/$pid"/fd/*; do
+ link="$(readlink "$fd" 2>/dev/null || true)"
+ [ "$link" = "socket:[$inode]" ] && return 0
+ done
+ done <"/sys/fs/cgroup$cgroup/cgroup.procs"
+ return 1
+}
+
+gateway_assert_managed_runtime_trusted() {
+ [ "$RUNTIME_OWNED" = true ] && return 0
+ if [ "${NODE_ENV:-}" = test ] && [ -n "${MSO_GATEWAY_CURL:-}" ] && [ -n "${MSO_GATEWAY_PROC_NET_TCP:-}" ]; then return 0; fi
+ gateway_service_owns_local_listener || gateway_fail "refusing managed tunnel: healthy loopback listener is not proven to belong to this checkout's mso.service"
+}
gateway_start_runtime_if_needed() {
local next host port pid identity instance node_exe current_exe i parent_ticks gate
[ "$RUNTIME_OWNED" = true ] && gateway_health_url_ok "$LOCAL_URL" "$RUNTIME_INSTANCE_ID" && return 0
diff --git a/scripts/lib/gateway-tunnel.sh b/scripts/lib/gateway-tunnel.sh
index 9132c5b6..0620ff4c 100644
--- a/scripts/lib/gateway-tunnel.sh
+++ b/scripts/lib/gateway-tunnel.sh
@@ -127,6 +127,7 @@ gateway_cmd_start_locked() {
gateway_runtime_from_state "$state"
gateway_start_runtime_if_needed
gateway_assert_port_loopback_only
+ gateway_assert_managed_runtime_trusted
LOCAL_HEALTH_IDENTITY="$(gateway_health_url_identity "$LOCAL_URL" "${RUNTIME_INSTANCE_ID:-}")" \
|| { gateway_cleanup_failed_start; gateway_fail "selected local runtime did not return a stable MSO health identity"; }
TUNNEL_IDENTITY=null; GATEWAY_PUBLIC_URL="${GATEWAY_PUBLIC_URL:-}"
diff --git a/scripts/mso-gateway-state.test.ts b/scripts/mso-gateway-state.test.ts
index 2ca3c7ab..e6db8785 100644
--- a/scripts/mso-gateway-state.test.ts
+++ b/scripts/mso-gateway-state.test.ts
@@ -97,3 +97,15 @@ describe("gateway provider-neutral state model", () => {
expect(out.stderr).toContain("refusing to create a duplicate");
});
});
+
+
+describe("managed tunnel local-runtime provenance", () => {
+ it("refuses a health-shaped loopback listener with no owned or service process proof", () => {
+ const f = fixture();
+ const env = { ...f.baseEnv, NODE_ENV: "production" };
+ const out = spawnSync(GATEWAY, ["start"], { encoding: "utf8", env });
+ expect(out.status).not.toBe(0);
+ expect(out.stderr).toContain("not proven to belong to this checkout's mso.service");
+ expect(fs.existsSync(f.startFile)).toBe(false);
+ });
+});