提交 Haven Android 仓库源码 v5.87.79

This commit is contained in:
ali
2026-09-10 13:45:36 +08:00
parent 87b452c4dc
commit 22f4ca5830
2524 changed files with 599455 additions and 1 deletions
+237
View File
@@ -0,0 +1,237 @@
---
layout: default
title: "Design: Bridges — a unified device/capability manager"
---
# Bridges — one manager for everything Haven brokers
> **Status:** design / forward-looking. Not built. This is the binoculars view of
> where the "broker an Android capability and re-expose it" pattern is heading,
> with a grounded phased path from today's sprawl to the dream.
## 1. The pattern, named
Almost everything Haven does is one move, stated in [VISION](https://github.com/GlassHaven/Haven/blob/main/VISION.md):
**take a capability Android holds, that some downstream can't reach on its own,
broker it, and re-expose it.** A YubiKey to a server. A GPU to a proot cage. A
USB drive to a real kernel. Audio out of a headless Linux app.
We've built each as a *point solution* with its own UI, its own lifecycle, its
own MCP verbs. The capability is the same shape every time — only the endpoints
change. **Bridges** makes that shape first-class: one registry, one surface, one
consent model, for every brokered capability.
The user-facing name is the **Devices** screen. The internal concept is a
**Bridge** — and a Bridge is precisely *an instance of the host-broker the Vision
already describes*.
### Where this sits in the Vision (so we don't trip over the primitives)
This is **not a fifth primitive.** VISION.md is deliberate: the four primitives
(Namespace, Runtime, Gateway, Presence) reach *outward* to other machines'
files, shells, networks, and screens; the broker — *"The host as a privileged
peer"* — is *"the build-vs-delegate discipline pointed inward,"* reaching the
phone's *own* capabilities and feeding them to those same four (a brokered USB
device becomes usable via Gateway/USB-IP, Runtime/guest-or-VM, Presence/agent).
**Bridges is the registry and surface for that inward layer** — it names and
makes manageable a pattern the Vision already commits to, it does not invent a
new one. The sinks below are just the four primitives wearing work gloves.
The **Devices** screen earns its place by the Vision's own build-vs-delegate
test: it's a *composition* surface (you route a source *into* a runtime or out a
gateway), so Haven owns it — and, being a Presence surface, it inherits the three
agent-sharing requirements for free: observable state (`list_bridges`),
actionable API (`create_bridge`/`drop_bridge`), reachable surface (the
auto-detect notification). Humans tap, agents call, both observe.
## 2. The model
A **Bridge** is a tuple:
```
Bridge = (source, sink, transport, state, consent, lifecycle)
```
- **Source** — a capability the phone holds: a USB device, the camera, the mic,
the speaker, motion/position/environment **sensors**, **GPS**, the screen, the
clipboard, NFC, the secure element, the GPU, location, network, battery.
- **Sink** — who receives it: the **AI agent** (MCP), the **proot guest**, a
**local VM** (real kernel), a **remote host**, another **app**, the **LAN**.
- **Transport** — the mechanism that carries it: the `UsbDeviceConnection`
broker, the `haven-usb` libusb/hidraw/serial shim, **USB/IP**, a virgl/venus
socket, PulseAudio-over-TCP, a Wayland socket, a **v4l2 camera shim**, an
**iio/evdev sensor shim**, **gpsd/NMEA**, a reverse-adb tunnel.
- **State** — `available → bridging → active → error`, plus health: throughput,
client count, last error.
- **Consent** — *not a parallel model; the Vision's existing tiers.* A **per-use**
bridge (open this drive now) is **Tier 2** (act once) — its own consent, no
pre-enable toggle (the #287 lesson). A **standing** bridge (a guest can reach
the camera until revoked) is **Tier 3** — a `create_standing_policy` grant with
scope, rate ceiling, expiry, and the kill-switch. Anything irreversible or
physical is **Tier 4**, always per-action. The registry just records which tier
a given bridge sits at; the consent rail is the one Haven already ships.
The **Bridge Registry** is the single source of truth. It *generalises*
`list_usb_exports` (which already lists USB bridges) to every capability, and the
**Devices** screen + MCP are thin views over it.
## 3. The capability matrix
The whole point is to make "what can reach what" legible. This is the
[USB device-class matrix](../features/usb.md#device-class-support) grown up to
cover all capabilities. (✅ shipped · ⚠️ feasible, unbuilt · 🔬 research · ❌ wall.)
| Source (phone holds) | → Agent (MCP) | → proot guest | → local VM | → remote host |
|---|:---:|:---:|:---:|:---:|
| USB HID / CDC-serial | ✅ | ✅ `haven-usb` | ✅ via USB/IP | ✅ USB/IP |
| USB mass storage / block | ⚠️ | ❌ no kernel | ✅ **VM (#287)** | ✅ USB/IP |
| USB other bulk (printer/SDR/MTP) | ✅ raw | ⚠️ libusb shim | ✅ VM | ✅ |
| USB webcam/audio (isochronous) | ❌ | ❌ | ❌ | ❌ *(Android API wall)* |
| Camera (phone's own, Camera2) | ✅ capture | 🔬 v4l2 shim | 🔬 v4l2→USB/IP | 🔬 |
| Microphone | ⚠️ | ⚠️ PA source | ⚠️ | ⚠️ |
| Speaker / audio out | ✅ | ✅ **PulseAudio (#257)** | ✅ | ⚠️ |
| GPS / location | ⚠️ | ⚠️ **gpsd/NMEA** | ⚠️ | ⚠️ |
| Motion/environment sensors | ⚠️ | 🔬 iio/evdev shim | 🔬 | 🔬 |
| GPU | — | ✅ **virgl/venus** | 🔬 | — |
| Display / screen | ✅ capture | n/a | n/a | ⚠️ |
| Clipboard | ✅ | ✅ | ⚠️ | ✅ |
| NFC / secure element | ✅ FIDO | ⚠️ | ⚠️ | ✅ USB/IP (key) |
| Network / adb | — | — | — | ✅ reverse tunnel |
Reading the grid is the feature: it shows what's shipped, what's one shim away,
and where Android's API walls (isochronous USB, no `/dev/kvm`, no kernel modules)
genuinely stop us — so nobody re-discovers a wall by hitting it.
## 4. The surfaces
**Devices screen** (the user-facing manager). A live list grouped by source type:
```
USB
• YubiKey FIDO+CCID → [Agent ✓] ⋮ attach to guest / export USB/IP
• SanDisk 64GB (ext4) → [VM ▸ booting 60%] ⋮ eject
Camera / Mic
• Back camera → not bridged ⋮ to guest / to agent
Sensors
• GPS → [guest gpsd ✓] ⋮ stop
• Motion (accel/gyro) → not bridged ⋮ to guest / to agent
GPU → [desktops: virgl ✓]
Audio → [guest PulseAudio ✓]
```
Each row: what it is, where it's bridged (with live state/health), and a menu of
*other* sinks it could go to. The auto-detect notification ("USB drive detected —
open it?") is just this surface reaching out when a source appears.
**MCP** (the registry, agent-drivable) — generalising `list_usb_exports`:
- `list_bridges` — every source, its current bridge(s), and what's possible.
- `create_bridge(source, sink, transport?)` — e.g. `(gps, guest)`, `(usb:1-2, vm)`.
- `drop_bridge(id)` — tear one down.
- `inspect_bridge(id)` — health, throughput, the exact transport details.
The existing `open_usb_drive` / `usb_attach_to_guest` / `start_usbip_export`
become **thin façades** over `create_bridge`, kept for ergonomics.
## 5. The walls (so the dream stays honest)
- **Isochronous USB** — UVC webcams / UAC audio over an *attached* USB device
can't pass on *any* sink: `UsbDeviceConnection` has no isochronous API. (The
phone's *own* camera via Camera2 is a different path that sidesteps this.)
- **No `/dev/kvm`** unrooted — local VMs are TCG (emulated, slow). Fine for files
and peripherals, not for performance.
- **No kernel modules** in proot — kernel-object classes (block, netdev, v4l2,
ALSA) need either a shim (userspace) or a real VM kernel.
- **Per-source Android permissions** — camera, location, sensors, USB each carry
their own runtime grant *on top of* Haven's consent rail.
- **Exclusivity & contention** — some sources can't fan out to two sinks at once;
`UsbAccessGate` (today's FIDO-vs-export coordinator) generalises to a
per-source lease in the registry.
- **A bridge is a privilege path** — guest-reaches-camera is an escalation.
Standing exposures gate hard (Tier 3); per-use actions self-consent (Tier 2).
- **Brokers access, never the device's app** (Vision boundary). A camera bridge
feeds frames to a sink; it does not become a camera app or reimplement a
vendor's tool. The bridge ends at the `/dev`-node-or-socket; what the sink does
with it is the sink's business.
- **Non-root is the common case; root is a gated power-user path** (Vision
boundary). The matrix marks root-only transports (raw bus access, udev, kernel
modules) as the exception, never the default — the point is to work on the
phone the user actually has.
## 6. Roadmap — from legible to dreaming
**Phase 0 — make the sprawl legible (cheap, no refactor).**
- Read-only `list_bridges` MCP that *reflects* today's bridges (USB exports,
guest-USB proxy, audio bridge, virgl, the USB-drive VM, adb tunnel).
- A read-only **Devices** list mirroring it.
- Land the two interactive slices already scoped: **auto-detect-on-plug
notification** + a **Files-tab "Open USB drive"** entry, built as the *seed* of
the Devices list (a row model, not a one-off link).
**Phase 1 — the registry becomes the source of truth (USB first).**
- A `BridgeRegistry` + `Bridge` abstraction in `core`. Route the existing USB
bridges through it; the Devices screen drives create/drop.
- **Multi-device USB/IP on `:3240`** (the hub case) + implement `OP_REQ_DEVLIST`
(the gap the spike found) — so a hub can fan N devices to N different sinks at
once (YubiKey→agent, stick→VM, serial→guest, simultaneously).
- Generalise `UsbAccessGate` into a per-source lease.
**Phase 2 — first new source: sensors.**
- **GPS → `gpsd`/NMEA** into the guest/VM — Linux mapping & nav apps "just see" a
GPS. Highest value, cleanest shim.
- Motion/environment sensors via an **iio or evdev** userspace shim (same
`LD_PRELOAD`-+-socket trick as `haven-usb`).
- `(sensor, agent)` over MCP — the agent can read the phone's real-world state.
**Phase 3 — camera + mic.**
- A **v4l2 userspace shim** fed by **Camera2** — the guest gets a `/dev/video0`
(proot has no kernel for `v4l2loopback`, so interpose the v4l2 ioctls). The
phone's own camera, not a USB webcam, so the isochronous wall doesn't apply.
- Mic as a PulseAudio *source* (the audio bridge in reverse).
**Phase 4 — symmetry & the network.**
- Bridges **to a remote host** and **from the LAN in**: forward the phone's GPS,
camera, sensors to a desktop; or accept a remote device into the phone's guest.
- The screen/display as a source; clipboard both ways. The full matrix lit up.
**Phase 5 — the dream: a composable virtual device bus.**
- A guest or VM boots seeing a **synthetic device tree** assembled à la carte from
the phone: its GPS, cameras, sensors, attached USB peripherals, GPU, audio — all
as *native Linux devices*. You hand a Linux environment (local cage, on-device
VM, or a remote box) exactly the slice of the phone you choose, and it behaves
like a laptop that happens to have a phone's sensors bolted on.
- The phone becomes a **peripheral/sensor host** for *any* Linux, anywhere — the
natural endgame of the broker thesis. "Plug my phone's reality into this Linux."
## 7. Migration map (today → bridges)
Nothing is thrown away; each existing feature *becomes* a Bridge:
| Today | Becomes |
|---|---|
| `start_usbip_export` / `usb_attach_to_guest` | `create_bridge(usb:N-M, remote\|guest)` façade |
| `open_usb_drive` (#287) | `create_bridge(usb:N-M, vm)` façade |
| Audio bridge (#257) | `create_bridge(audio-out, guest)` |
| virgl/venus GPU | `create_bridge(gpu, guest)` |
| `expose_adb` | `create_bridge(adb, workstation)` |
| FIDO/CTAPHID over NFC/USB | `create_bridge(secure-element, agent\|remote)` |
| `list_usb_exports` | `list_bridges` (superset) |
## 8. Ponytail discipline (so this isn't a cathedral)
- The abstraction has *already* earned itself: 3+ sources, 4+ sinks, scattered
UIs. But grow it, don't big-bang it.
- **Phase 0 is pure reflection** — no behaviour change, just legibility. Ship it
first; it pays for itself immediately.
- **Add a transport only when a source needs it.** No speculative plugin system.
- **Per-use consent beats standing toggles** wherever the action is deliberate
(the #287 lesson) — fewer gates, less Settings sprawl.
- **Stop at the first rung that holds.** A read-only Devices list + the auto-detect
notification may be 80% of the felt value; the registry refactor and new sources
earn their place one real use-case at a time.
---
[← Features](../FEATURES.md) · [USB device forwarding](../features/usb.md) ·
[Reading USB drives](../features/usb-drives.md) ·
[Vision](https://github.com/GlassHaven/Haven/blob/main/VISION.md)
+519
View File
@@ -0,0 +1,519 @@
# The MCP backbone — re-architecture
Status: **design / forward-looking.** This plans a rebuild of Haven's agent
transport. It is written to the depth [VISION.md](../../VISION.md) demands
because the MCP endpoint is not a feature — it is, per the vision's own words,
*"the moat"* (§Face, §1a/§1b). A moat that is less reliable and less secure than
the SSH terminal sitting beside it is a liability, not a moat. This document is
the plan to make the agent transport a first-class carrier: **equal to SSH in
reliability, and stronger than it is today in security**, without losing the
consent model that is the one part already built right.
The companion inward-broker design is [bridges.md](bridges.md); this is its
transport-layer sibling.
---
## 1. Thesis
VISION §1a describes what shipped: *"MCP / JSON-RPC over HTTP loopback (port
range 8730–8739), Streamable HTTP stateless transport."* That sentence is
accurate and it is also the whole problem. The endpoint is:
- a **hand-rolled HTTP/1.1 server** on raw `ServerSocket`s and daemon threads,
living inside the same 1,537-line class as the transport and the same process
as the UI;
- **started from exactly one place** — a preference collector in
`HavenApp.onCreate` — and never health-checked or restarted on foreground,
network change, or a timer;
- trusted on the basis of **network position and a self-asserted name**, with
**no cryptographic authentication anywhere in the path**;
- **plaintext at the MCP layer** on every carrier, borrowing whatever security
the carrier happens to provide.
The SSH terminal — the thing the user rightly compares it to — is none of these.
It is a mutually authenticated (host-key TOFU + user key/FIDO), encrypted,
integrity-protected, persistent channel owned by a `specialUse` foreground
service with active reconnect. MCP has *no* equivalent at its own layer. It is a
tool surface bolted onto a socket, defended entirely by the carrier under it —
and its default carrier trust assumptions are weaker than the ones SSH makes.
The security rent VISION demands of *"every feature"* has not been paid on the
backbone. This plan pays it.
### Every claim here is grounded
This design follows three full read-throughs of the backbone (transport,
security/consent, dispatch/tool/client). Where a risk rests on Android platform
behavior rather than a line of Haven code — cross-app loopback reachability,
browser DNS-rebinding to loopback, SQLCipher-at-rest — it is flagged as
**platform-inferred, not PoC'd**. Under-claiming is the house style.
---
## 2. The backbone as it is today
### 2.1 Transports — five carriers, one gate, one-shot framing
Five paths reach the server; all funnel into one `handleConnection(input,
output, isLoopback)` so pairing/consent/audit are identical regardless of path
(a genuine strength worth preserving):
| # | Carrier | Setup | Default | Origin seen |
|---|---------|-------|---------|-------------|
| 1 | **Loopback** | `ServerSocket(127.0.0.1)`, first free of 8730–8739 | on | `isLoopback=true` |
| 2 | **LAN** | kernel socket on the Wi-Fi IPv4 | off | `isLoopback=false` |
| 3 | **WireGuard** | userspace `TunneledServerSocket` on the WG IP | on | `isLoopback=false` |
| 4 | **near SSH `-R`** | `McpNearCarrier` rides the user's interactive session, `-R 127.0.0.1:8730→127.0.0.1:8730` | — | **`isLoopback=true`** |
| 5 | **headless SSH `-R`** | `McpTunnelManager`'s own reconnecting session, carries adb + guest-service forwards | — | per-forward |
Clients do not connect to these directly — they point at a **local failover
proxy** (`~/.local/bin/haven-mcp-failover.py`) on `127.0.0.1:8788`, which picks a
backend per request (near `-R` first, then LAN via `ip neigh`). The proxy exists
because the server **closes the TCP connection after every response** — no
keep-alive — and a naive byte-splice propagated that close and broke the MCP
client's second keep-alive request with `ECONNRESET`. The proxy terminates
client keep-alive locally and opens a fresh backend per request. That contract —
one request per connection, `Content-Length`-framed, ports 8730–8739 — is load
bearing and any rewrite must honor or retire it in lockstep.
The framing itself (`handleConnection`) is hand-rolled: request-line split,
header loop that extracts only `content-length` and `mcp-session-id`, and a body
read of:
```kotlin
val buf = CharArray(contentLength) // no upper bound
while (read < contentLength) { … reader.read(buf, …) … } // chars vs a byte length
```
Two defects live in those two lines (§2.3).
### 2.2 Lifecycle — why it dies backgrounded and does not revive
`mcpServer.start()` has **one caller**: the `mcpAgentEndpointEnabled` collector
in `HavenApp.onCreate`. `MainActivity.onResume` never (re)starts or health-checks
it. The accept loops are **daemon threads in the Application process** (WG uses a
`Dispatchers.IO` coroutine). When Android reclaims the process — LMK, task
swipe, Doze, `specialUse` FGS pressure — every daemon thread dies with it. The
`specialUse` FGS makes the process *harder* to reclaim; it does not keep a dead
thread alive.
The `McpForegroundParticipantModule` shim is **passive**. It contributes:
- `activeSessions` — reads `server.isRunning`; `disconnectAll()` can `stop()` but
**never `start()`**;
- `ForegroundKeepAlive.isActive = server.isRunning` — keeps the FGS alive *while*
the server runs, but can't resurrect it;
- `ForegroundReviveHook.reviveNow()` — kicks the **`McpTunnelManager` tunnel**,
**not** the MCP accept loop.
So on return-to-foreground / network-available, SSH sessions and the headless
tunnel revive — but nothing re-checks the MCP accept loop. `start()` even has an
`isHealthy()` guard that heals a "process-suspend zombie" (`isRunning==true` but
socket/thread dead) — but only if `start()` is *called*, and it is not, outside
the pref collector. **A zombie server in a surviving process stays wedged until
the user toggles the endpoint.** This is exactly the outage observed in the
field: the near SSH tunnel stayed up, the phone's MCP server behind it was dead,
and `/mcp reconnect` couldn't fix it because that only re-dials the client→proxy
leg. The failover proxy papers over the *process-restart* case; it cannot paper
over a live-process zombie.
### 2.3 Security — the weak half
The consent *state machine* is carefully built and well-tested (§4). The
*authentication and transport-trust* layer is where the moat leaks.
**No cryptographic client authentication exists.** Trust is decided by two
mechanisms, both spoofable:
1. **Network position.** `trusted = isLoopback && trustLoopbackEnabled`, and
`trustLoopbackMcpClients` **defaults to true**. A trusted request skips *both*
pairing *and* all per-action consent. So by default, anything arriving on
127.0.0.1 gets consent-free access to every tool — including destructive ones.
2. **A self-asserted name.** Off-loopback, pairing checks `clientName in
allowlist`, where `clientName` is `clientInfo.name` — chosen by the client. It
is not a credential. A LAN/WG peer that guesses a paired name (`"claude-code"`)
passes the gate. Worse, **every grant** — session-allow, persistent bypass,
Tier-3 standing policy, the notification-Allow window — is keyed on this same
spoofable name, so a client that names itself after a trusted one **inherits
its grants**.
**The `isLoopback` inference is the root flaw**, and it fails in two directions:
- **Co-resident apps (platform-inferred).** On stock Android, 127.0.0.1 is not
per-app-namespaced; any app with `INTERNET` can connect to another app's
loopback listener. The server's own kdoc defends auto-trust with *"all local
processes on Android already have at least as much access to this app's
data"* — which is **false** for the app sandbox. A co-resident malicious app,
or a **web page** the user opens (the endpoint answers `OPTIONS` and every
response with `Access-Control-Allow-Origin: *`, validates no `Host`/`Origin`,
and auto-trusts loopback → DNS-rebinding-class drive-by), reaches the full tool
surface with no consent.
- **Reverse-tunneled remote traffic — highest impact.** Both the near carrier
and the tunnel manager forward with `targetHost=127.0.0.1`, so traffic from the
*remote SSH host's* localhost is delivered to the **phone's** loopback →
`isLoopbackAddress==true` → auto-trusted. `isLoopback` cannot distinguish an
on-device process from tunneled-remote traffic. **Any process or user on the
workstation the phone tunnels to reaches Haven consent-free.** This is the
default agent-driving path.
**Plaintext at the MCP layer on every carrier.** No TLS. WG and SSH encrypt the
*carrier*, so the bytes are protected in transit there; the **LAN binder**
(off by default) exposes plaintext MCP on the Wi-Fi IP, and write-tool arguments
carry secrets (`create_connection` passwords, `set_ssh_key_option`,
`create_totp_secret`) in cleartext to any LAN sniffer. Redaction is audit-DB-only
and never touches the wire.
**Parser DoS.** `CharArray(contentLength)` with no cap → a `Content-Length:
2000000000` forces a ~4 GB allocation → `OutOfMemoryError`, reachable from any
carrier. Header count/length is unbounded (Slowloris, bounded only by the 70 s
`soTimeout`). Thread-per-connection with no pool. The byte-vs-char body read also
truncates multibyte UTF-8 bodies → the read loop never completes → 70 s hang
(ASCII JSON dodges it today).
### 2.4 The tool + client layers
- **`McpTools.kt` is an 11,667-line God file**: one class, ~50 constructor deps,
**182 tools in a single `linkedMapOf`**, schemas hand-built inline with an
`org.json` DSL, no compile-time arg/schema validation. Every new tool and every
domain (mail/usb/desktop/rclone/tunnels) edits one file — a permanent
merge-conflict choke point. Capability gates (`serve_file`,
`queue_terminal_input`) are special-cased **by tool name inside the transport
layer**, and a reserved-key content channel (`__mcpContent`/`__imageBase64`)
is an implicit handler↔server contract.
- **Two independent hand-rolled JSON-RPC stacks.** The server and
`GuestMcpClient` each re-implement framing/session/SSE on `org.json` + raw
sockets. No batch, no server→client push (`GET /mcp` → 405), `listChanged`
hardwired false, no protocol-version negotiation (the client's requested
version is read then ignored).
- **§1b (Haven as MCP client of other apps) is essentially unbuilt.**
`GuestMcpClient` only proxies an in-proot server the agent itself launched and
registered by port. The vision's Android `<intent-filter>`/`meta-data` app
discovery, Settings capability bundles, and unified one-surface proxying do not
exist.
### 2.5 SSH terminal vs MCP today — the gap in one table
| Property | SSH terminal | MCP backbone today |
|---|---|---|
| Client auth | user key / FIDO2, host-key TOFU | **none** (position + spoofable name) |
| Encryption/integrity | always (SSH transport) | **none at MCP layer**; borrowed from carrier; plaintext on LAN |
| Persistence | one warm channel, keep-alive | **connection-per-request**, one-shot |
| Lifecycle owner | `specialUse` FGS + active reconnect | Application-process daemon threads, **single-caller start** |
| Self-revive | yes (reconnect, revive hooks) | **no** — zombie until pref toggle |
| Trust boundary | possession of a private key | *"can deliver bytes to the socket"* |
The consent gate is the one column where MCP is genuinely ahead of a raw shell —
and the plan keeps it.
---
## 3. Design principles (the invariants)
A re-architecture earns the right to touch this code only if it preserves what is
already correct. From the tests and VISION:
1. **UI is ground truth; no hidden channel.** Every agent action and reach-out
lands on a surface the user can see (VISION §4). The transport is addressable
from outside the UI process precisely so the agent is *another caller on the
same surfaces* — never a bypass layer.
2. **The tier is a property of the verb, not the caller** (VISION §Consent
tiers). A human tapping a Tier-4 action *is* the consent; an agent calling it
prompts. Reflexes never self-escalate.
3. **Fail-closed when no foreground activity can render the prompt.** Consent
returns DENY immediately when backgrounded; it never silently waits or
auto-allows. This is the strongest property in the current design.
4. **DENY is never cached; only ALLOW is.** A misclick can't lock the agent out;
a denial can't be replayed into an allow.
5. **The consent sheet is non-dismissable; back = Deny.**
6. **Standing policies can only *remove* a prompt the user pre-authorized, never
widen scope.** Denylist of self-escalating tools enforced at create *and*
eval; server-side rate/expiry clamps.
7. **Pairing is add-only via an on-device human tap.** No MCP tool can self-add a
client or grant auto-approval.
8. **Everything is audited, including DENY, with redaction owned by the
recorder.**
9. **Deterministic ports (8730–8739)** so cached clients, the `-R` forwards, and
the proxy agree; **MCP-over-SSH forwards are non-critical** so a bind failure
never tears down the user's terminal.
10. **MCP is the moat — keep it Haven-specific.** Do not genericize the backbone
into a shared cross-project spec; composition over SSH/RFB is the product.
Everything below changes *how trust is established and how the server stays
alive*; none of it weakens 1–10.
---
## 4. Target architecture
Six layers, each a clean seam. The current code collapses transport, protocol,
tools, auth, and lifecycle into two God classes; the target separates them so
each can be tested, hardened, and reused (host *and* client) independently.
### Layer A — one MCP protocol core (shared by host and client)
Extract a single `mcp-core` module owning framing, JSON-RPC 2.0 (with batch),
the streamable-HTTP session lifecycle, protocol-version negotiation, typed
request/response/notification/content models, and error mapping. Both the server
and `GuestMcpClient` sit on it — the second hand-rolled stack disappears.
**Framing decision.** Replace the hand-rolled parser with a real embedded HTTP
engine so the parser-DoS *class* is gone (bounded bodies, correct byte framing,
header caps, a bounded worker pool, optional keep-alive). Two candidates:
- **Ktor CIO server engine** — coroutine-native, already the concurrency model
the WG path uses; proven on Android. Adds Ktor to the graph.
- **Official Kotlin MCP SDK** (`io.modelcontextprotocol:kotlin-sdk`) — spec
framing + client/server for free, but drags Ktor + `kotlinx-io` and its own
opinions, and must clear F-Droid reproducible-build + Gradle
dependency-verification.
Recommendation: adopt a minimal real HTTP engine (Ktor CIO) for framing and keep
a **thin, Haven-owned JSON-RPC/MCP layer** on top, rather than the full SDK — the
SDK's surface is larger than Haven needs and the consent/audit wiring is
bespoke. This is the ladder's rung-4 answer (use an installed-class dependency
for the hard part — HTTP framing — and hand-write only the thin part that is
genuinely Haven-specific). Keep-alive then becomes possible, which lets the
failover proxy be **retired** rather than perpetually maintained (Stage 4).
### Layer B — carriers with *explicit* origin trust
Kill the `isLoopback ⇒ device-trust` inference. Each carrier tags the true trust
origin of the sockets it accepts, set at bind time, not read from the peer
address:
- `ORIGIN_DEVICE` — a genuine on-device local process.
- `ORIGIN_TUNNELED` — a reverse-tunnel (`-R`) carrier. **Never** device-trusted,
because the far end is a remote host.
- `ORIGIN_LAN` / `ORIGIN_WG` — networked peers, always full gate.
The near and headless `-R` carriers must **not** be routed through the
device-trusted binder. The cleanest way to make `ORIGIN_DEVICE` mean what it
says is a **Unix-domain socket with `SO_PEERCRED`** for the on-device path:
peer-uid is checked, so a co-resident app is a *different uid* and is refused
device-trust; a `-R` tunnel can't forge a uid. TCP loopback stays available for
clients that need it (adb-forward), but at `ORIGIN_LAN`-equivalent trust (full
gate + token), not auto-trust. This single change closes the co-resident-app,
DNS-rebinding, and reverse-tunnel holes at once.
### Layer C — real per-client authentication
At pairing time, mint a **per-client secret** (random token) bound to the
authenticated identity and returned once to the client. Every subsequent request
carries it (`Authorization: Bearer …` / bound to the `Mcp-Session-Id`). The
allowlist, bypass set, and standing policies key on the **authenticated
identity**, not `clientInfo.name`. Consequences:
- Name-spoofing dies (identity is a secret, not a string).
- Grant inheritance dies (grants bind to identity).
- A tunneled or LAN client with no token can `initialize` and prompt for
pairing, but cannot inherit another client's trust.
- DNS-rebinding dies (a browser page has no token).
`ORIGIN_DEVICE` (uid-checked Unix socket) may skip the token as today's loopback
does — but that is now a *real* device-local guarantee, not a spoofable one.
### Layer D — service-owned, actively supervised lifecycle
Move server ownership out of the `HavenApp` pref collector and into the FGS
domain (`SshConnectionService` / `SessionManagerRegistry`), as an **active**
participant:
- A real `ForegroundReviveHook` for the MCP server (not just the tunnel) that
calls `isHealthy()` and `start()` on return-to-foreground, on
network-available, and on a heartbeat — the same triggers SSH reconnect uses.
- Restart the accept loop on death; treat a zombie (`isRunning && !healthy`) as
dead.
Net: **MCP availability becomes `FGS-alive`, the same bar as SSH** — the
outage that needed a manual endpoint toggle becomes a self-heal. The failover
proxy's role shrinks to carrier-selection (near vs LAN vs WG) and, once
keep-alive lands (Layer A), can be retired.
### Layer E — the tool layer as a registry of providers
Split `McpTools.kt` into per-domain `ToolProvider`s (namespace, runtime, gateway,
presence, brokers, …) contributed via Hilt multibinding into a `ToolRegistry`.
Make the cross-cutting concerns **declarative on the tool**, not special-cased in
the transport:
- `capability` gate (feature on/off) as a field, not a name check in
`handleToolsCall`;
- `consentLevel` + `summary` as today, but co-located with the handler;
- a **typed result model** (text / image / file-ref / guest-passthrough) that
retires the reserved `__` keys;
- a schema DSL (or codegen from Kotlin types) to kill the inline `org.json`
boilerplate and give compile-time validation.
This is also what makes §1b tractable: external tools are just another provider.
### Layer F — bidirectional (§1b) on the shared core
With Layer A, Haven-as-client is first-class. A `ClientRegistry` + pluggable
discovery: the in-proot port registration that exists today, and — later — the
Android `<intent-filter>` + `meta-data` enumeration VISION §1b specifies (no
external bytecode; each plugin a separately source-built APK, so F-Droid
reproducibility holds). External tools fold into the one surface through the same
consent + audit stack, indistinguishable to the agent. Server→client
notifications (SSE, now that the core supports it) deliver `listChanged` so a
newly-discovered capability appears without a poll.
---
## 5. Security hardening — the concrete checklist
Ordered by impact. Items 1–5 are the ones that make MCP *safe*; several are
small enough to ship ahead of the full re-arch (Stage 0).
1. **Explicit per-carrier origin trust; tunneled carriers are never
device-trusted** (Layer B). Closes the reverse-tunnel hole.
2. **`ORIGIN_DEVICE` = uid-checked Unix socket** (or, minimally, default
`trustLoopbackMcpClients` to **off**). Closes co-resident-app trust.
3. **Real pairing token; identity-keyed allowlist and grants** (Layer C). Closes
name-spoofing, grant inheritance, DNS-rebinding.
4. **No plaintext on LAN** — require an encrypting carrier or add TLS to the LAN
binder. Stops secret-argument leakage.
5. **Parser caps** — bound `Content-Length` (cap the buffer), cap header
count/size, read the body as **bytes** against the byte length, bounded worker
pool, validate `Host`/`Origin`, and **drop `Access-Control-Allow-Origin: *`**
on the state-changing endpoint. Closes the OOM, the multibyte hang, Slowloris,
and browser CSRF. *(Stage-0 candidate — no protocol change.)*
6. **Notification-Allow window** (`armRetryWindow`) → operation-scoped and
single-use, matching the `EVERY_CALL` `retryGrant` semantics (today it is a
120 s client::tool window that admits differing arguments).
7. **Redaction beyond key-name and beyond the audit DB** — content-based secret
detection (so a password in `send_terminal_input.text` or a `url`/`command`
arg is caught), scrub client names from Logcat, and confirm/enable
**SQLCipher at-rest** for the audit table *(at-rest encryption unverified —
confirm during Stage 0)*.
---
## 6. Migration — staged by leverage, not effort
Big-bang rewrites of a load-bearing transport are how you ship a broken v5.59.3.
Each stage is independently shippable and ordered so the reliability + security
wins land first, protocol churn last.
- **Stage 0 — safe hardening (days).** Parser caps + byte-accurate body +
bounded pool + `Host`/`Origin` validation + drop `ACAO:*` on POST (§5.5);
tighten the notification-Allow window (§5.6); confirm audit at-rest encryption
(§5.7). No wire-protocol change, no client change. Closes the OOM, the
multibyte hang, and browser CSRF immediately. **Shipped `b864cce2`.**
- **Stage 1 — reliability (the outage fix).** Active MCP revive hook +
service-owned supervision (Layer D). MCP self-heals to SSH's bar. No protocol
change; the failover proxy stays. **Shipped `d41d5cf5`.**
- **Stage 2 — trust origin.** Explicit per-carrier origin tags; tunneled
carriers off the trusted binder; default `trustLoopback` off or the
uid-checked device socket (Layers B). Highest-impact security change; carrier
wiring only, tools untouched. **Shipped `a6b89f94`** — bind-time `McpOrigin`
tags, a dedicated tunneled-origin listener (8740–8749) the `-R` carriers
target, and `trustLoopback` defaulting off (the uid-checked Unix socket
remains future work; TCP loopback at full gate covers the client-compat
case).
- **Stage 3 — authentication.** Pairing token; identity-keyed allowlist/grants
(Layer C). Requires a one-time re-pair of existing clients (surfaced as a
prompt). **Shipped `cd8e024d`** — 256-bit per-client token minted on pairing
approval (hash-only at rest), `Authorization: Bearer` or an
authenticated-session id required off-device; the `lastClientHint` name
fallback is gone.
- **Stage 4 — protocol core + keep-alive.** Extract the shared core (Layer A);
retire the second hand-rolled stack; keep-alive lands → the failover proxy
loses its reason to exist. **Shipped (this change)** as `core:mcp` — framing
parser, JSON-RPC builders, and a keep-alive connection loop shared by the
server and `GuestMcpClient` — plus a per-server connection cap. Deliberate
divergences from the sketch above: **no HTTP engine dependency** (§8 Q3 —
Stage 0's bounded byte-accurate parser already closed the DoS class, so Ktor
would be a dependency without a job); **no batch** (MCP revision 2025-06-18
*removed* JSON-RPC batching, so single-frame bodies are the spec shape);
**no SSE yet** (deferred to Stage 5 where `listChanged` gets its first real
consumer); **version negotiation is moot** while exactly one protocol
revision is supported — responding with the server's latest when the request
names another IS the spec's negotiation. The WireGuard netstack path stays
one-request-per-connection: its tunneled streams have no read timeout, so an
idle keep-alive peer would pin an IO thread.
- **Stage 5 — tool registry + §1b plugin bus.** Split `McpTools` into providers
(Layer E); build Android-app MCP discovery on the shared client core (Layer F).
**In progress** — the *declarative tool contract* landed first (the part of
Layer E that removes special-casing from the transport): the two capability
gates (`serve_file`, `queue_terminal_input`) are now a `ToolHandler.capability`
field the dispatcher reads via `McpTools.capabilityDenial(name)` instead of
matching tool names in `handleToolsCall`; and tool results are a typed
`ToolResult` (`Structured` / `Image` / `Passthrough`) that the transport
renders, so the reserved `__mcpContent` / `__imageBase64` keys are a
McpTools-internal handler↔`callTyped` detail, no longer a handler↔server
contract. Then the *provider split* began: a `ToolProvider` seam plus the
first extraction — the key-store domain (TOTP + age identities) moved out of
the God file into `KeyStoreToolProvider`, which `McpTools` aggregates. The
seam is deliberately an *internal* decomposition: `McpTools`'s public surface
(`definitions`/`consentFor`/`capabilityDenial`/`call`/`callTyped`) is
unchanged, so the transport and every server test stay untouched — no
Hilt-multibinding rewire of `McpServer`'s constructor (which would cascade
into every server test builder). Each future domain is a mechanical repeat.
All seven domains (key-store, tunnels, ssh-keys, rclone, usb, desktop, mail)
are now extracted; the `__` keys are deleted (handlers return `ToolResult`
directly); and every `inputSchema` is built by the `SchemaDsl.kt` builder
(`objectSchema { string("path", …, required = true) }`) instead of inline
`org.json` — the conversion was verified by a canonical-JSON snapshot diff of
all 182 tool schemas (identical modulo `required: []` vs omitted, which are
equivalent in JSON Schema). **Still to do:** the whole of Layer F (§1b
Android-app MCP discovery via `<intent-filter>`/`meta-data`, a `ClientRegistry`,
and SSE/`listChanged` server→client push) — deferred until a consumer exists.
Stages 0–2 are the ones that make the moat defensible and are worth prioritizing;
3–5 are the ones that make it *architecturally* first-class and unlock the
bidirectional vision.
**Status (2026-07-06):** Stages 0–4 shipped; Stage 5 Layer E complete
(declarative contract, all 7 provider extractions, typed `ToolResult`,
schema DSL). Only Layer F (§1b plugin bus + SSE push) remains, deferred
until a consumer exists.
---
## 7. Scope boundaries
- **Not an SDK adoption for its own sake.** Bring in a real HTTP engine for
framing; keep the MCP/consent/audit layer Haven-owned. No external bytecode —
F-Droid reproducibility is non-negotiable (VISION §1b).
- **MCP stays the moat.** No generic cross-project transport spec; the backbone
is Haven-specific by design.
- **Don't break the failover-proxy contract** (one-shot, `Content-Length`, ports
8730–8739) except by retiring the proxy in the same change (Stage 4).
- **The consent model is not up for redesign** — it is the part already right.
This plan changes authentication and lifecycle *underneath* it, not the tiers,
the fail-closed rule, or the verb-not-caller principle.
---
## 8. Open questions
1. **Unix socket vs TCP loopback for `ORIGIN_DEVICE`** — a Unix socket gives
`SO_PEERCRED` uid auth but some MCP clients only speak TCP; may need both,
with TCP loopback demoted to full-gate + token. *(Still open; Stage 2
shipped the trustLoopback-off default instead.)*
2. **Failover proxy: retire or keep?** *(Settled with Stage 4.)* Keep-alive
removed its reason; the proxy is now optional workstation infra. Its
carrier-selection job (near vs LAN) belongs in the client's MCP config —
point the client at the `-R` endpoint directly. Nothing in-repo depends on
the proxy.
3. **Kotlin MCP SDK vs Ktor-CIO-plus-thin-layer** — *(settled with Stage 4:
neither.)* Stage 0's bounded, byte-accurate parser closed the framing-DoS
class the engine was meant to solve, and keep-alive fit in a ~90-line loop
in `core:mcp` — so an engine would be transitive weight (plus F-Droid
dependency-verification and reproducibility surface) without a job.
Revisit only if Stage 5's SSE/plugin bus outgrows the hand-rolled loop.
4. **Audit at-rest encryption** — is the Room DB already SQLCipher-backed? If
not, Stage 0.
5. **Token delivery to headless/cron agents** — how a non-interactive client
obtains its pairing token without a human tap (a scoped provisioning flow?).
*(Partially eased by Stage 3: the token is returned in the pairing
initialize's `_meta` + instructions, so the agent behind the client can
capture and store it for its headless siblings.)*
---
*This document is the transport-layer half of "every feature must pay its
security rent" (VISION §Architectural direction, layer 3). The tool surface is
wide and the consent model is sound; the backbone underneath them is the debt.
This is the plan to retire it.*