Monorepo for the nibling.run platform

Harden the SSH gateway after its adversarial review, and carry agent identity to main #5

Merged nibling.dev
targeting main from nmoo/ssh/hardening-main
Merged opened by and assigned to nibling.dev
targeting main from nmoo/ssh/hardening-main

An adversarial review of the gateway that landed in #3 and #4 (three independent model reviewers, then a lead pass) agreed on two things: a token alone was enough to stand in as one of the account's tabs, and an anonymous SSH client could make the single machine fetch without bound. This PR addresses every finding. It is already deployed to ssh.nibling.dev and verified there.

It also carries the two commits of #4 (agent identity from the SSH env request). That PR merged into the gateway branch after #3 had already landed on main, so its content never reached main; the hardening builds on it.

The answer to "is it safe because it runs in the browser?" #

Partly. The browser bounds what a remote shell can do, which is exactly what the terminal panel can do. It does not bound who may attach as a tab or open a session. That is the gateway's job, and this PR makes it do it.

Routing and the fake-tab problem #

  • A target is <slug> or <slug>:<tab>, where <tab> is a prefix of the id that ssh <handle>@<host> lists. The list prints slug:tab title in attach order.
  • When two attached tabs announce the same slug and no tab is named, the gateway refuses and lists them. Before, the newest hello won, so any second socket holding the account's token could take a session quietly by re-sending hello.
  • The gateway reads the account's public dev.nibling.actor.settings record on attach and closes with 4403 unless sshAccess is true. The Settings switch is now a kill switch for new attachments within a minute. On is cached a minute, off five seconds.
  • Slugs are validated for shell and exec as they already were for git. Tab-announced and client-typed text is stripped of control characters before it reaches a terminal or a log. Exec command lines are no longer logged.

The anonymous surface #

  • Every fetch the username triggers is timed at 5 s. Record listing is capped at 2 pages, 200 records, and a 256 KiB body. The PDS endpoint from the DID document must be https on a public hostname, and the DID document must claim the handle. Key cache TTL is 5 minutes with 1024 entries, so a removed Tangled key stops working within minutes.
  • Tokens: 30 s skew, exp at most 5 minutes out, iat required, jti required and single use. The document cache is bounded and a failing signature forces at most one refetch per DID per minute.
  • WebSocket messages are capped at 64 KiB before auth. A socket has an explicit unauthenticated | authenticating | attached state, so a second auth during verification is a protocol error instead of a ghost registry entry, and the auth deadline runs until ready.

Backpressure #

The per-channel buffer dropped from 64 MiB to 2 MiB on a 256 MB machine. The gateway now sends the tab pause at 512 KiB and resume at 128 KiB. The tab holds up to 4 MiB for a paused channel, then ends it with an error, and frames its output within the 64 KiB message limit. Exec stdin has a byte-length queuing strategy with the same cap.

Tab side #

  • An open for a slug the tab no longer has open is refused with an error naming both, so a command cannot land in the wrong project after navigating.
  • A project with no slug announces null instead of its name.
  • Close codes drive the retry policy: 4401 and 4400 stop until the account or gateway changes, 4403 and 4429 wait the full 30 s, and a 4403 tries again at once when the settings save lands.
  • Turning SSH off in one tab reaches the account's other tabs through a storage event.
  • Closed SSH shells are removed from the sandbox's terminal map with their transcripts.

Operations #

  • The gateway refuses to start without --host-key or SSH_HOST_KEY. --throwaway-host-key is for development.
  • The container runs as node. fly.toml sets explicit connection limits on both services.
  • deploy.yml skips the Fly deploy when nothing under packages/ssh-gateway, the lockfile, or the workflow changed, so a UI-only push no longer drops every live session. The guard assumes a checkout with two commits and fails open to deploying otherwise; it has not run on a spindle yet.
  • D25 and the README state operator trust, what is logged, the host key fingerprint and how to check it, the limits, and the target syntax.

Deliberately not done #

  • Forwarding the SSH client's signature for the tab to verify. A malicious gateway could replay a prior proof, so it buys little; operator trust is documented instead.
  • An in-tab indicator for a live remote session belongs with the presence branch.

Verified #

  • Gateway 99 tests, app 161 node tests, sdk 150, the Playwright attach spec, and the OpenSSH plus git smoke check are green. Typecheck is clean.
  • Deployed to ssh.nibling.dev on 2026-11-22. A real tab attached under the new token rules, which confirms the PDS mints jti and iat and the settings-record check passes. ssh natemoo.re@ssh.nibling.dev listed astro-minimal:<tab> over real OpenSSH. The host key fingerprint is unchanged.
Commits 0