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 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 28

Keep an off answer for seconds, not a minute, and let a forbidden tab try again on the long backoff
Mint a token per fake tab and expect list lines as slug:tab
Skip the SSH gateway Fly deploy when nothing under it changed
Document targets, the kill switch, host key checking, and limits
Update D25 for the 5-minute key cache, target syntax, and trust boundaries
Try a forbidden attach again once the account's settings save lands
Follow an SSH access change made in another tab of the account
Cut a long project title in hello to the gateway's field limit
Hold a paused SSH channel's output, cap what a tab buffers, and frame within messageBytes
Stop reattaching after a refused token, SSH access off, or a protocol error
Forget an SSH shell's terminal session and transcript when it closes
Refuse an SSH session for a project other than the one the tab has open
Announce a project without a slug as null, not by its name
Exercise slug:tab targets and slug refusals in the OpenSSH smoke check
Tell the tab to pause and resume a channel as its SSH client falls behind and catches up
Stop logging exec command lines, which can carry secrets
Strip control characters from tab and client text before it reaches a terminal or the log
Refuse a slug two tabs announce, and route slug:tab to one of them
Run the gateway as the node user and set Fly connection limits
Refuse to start without a host key unless --throwaway-host-key is passed
Attach a tab only when its account has sshAccess on, and close the auth race
Accept an attach token once, within five minutes, with a bounded document cache
Bound and time every lookup the SSH username triggers before auth
Name the close code for an account whose sshAccess is off
Give the tab protocol pause/resume, tighter limits, and a slug:tab target
Take the agent's model in OpenRouter's vendor/model form, and send env in one SetEnv option
Let an SSH client name the AI agent driving it, and label the session with it
Warm a file's editor while the pointer or focus rests on it in the explorer
Sign up or Sign in to add to the discussion.