sessions/README.md
agent 08636ddc25 Document setup, security model and conventions
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 11:55:38 +00:00

178 lines
8.5 KiB
Markdown

# sessions
Start, stop and resume **Claude Code sessions on the dev box from a web page**,
without SSHing in yourself. Each session runs `claude` with **Remote Control**, so
you then talk to it from the Claude app or the "Open in Claude" link.
```
phone/browser ──HTTPS──▶ prod: reverse proxy ──▶ sessions-web (container, 127.0.0.1:8470)
│ ssh, key locked to one command
▼
dev box: sessions-ctl --remote ──▶ systemd: claude-session@<name>
└─ tmux -L claude-<name>
└─ claude --remote-control
```
| Part | Where | What |
|---|---|---|
| [`dev/sessions_ctl.py`](dev/sessions_ctl.py) | dev box, `~/.local/bin/sessions-ctl` | CLI + SSH forced command. Stdlib Python ≥ 3.11 |
| [`dev/systemd/`](dev/systemd) | dev box, `~/.config/systemd/user/` | one unit per session; hourly key-IP refresh |
| [`web/app.py`](web/app.py) | prod, container | login page + session list; stdlib, no JS |
| [`web/sessions-web.container`](web/sessions-web.container) | prod, Quadlet | runs the container under systemd, restarts at boot |
## Security model
The prod server holds a key that can reach the dev box, so a compromised prod
must not mean a compromised dev box. The line in `~/.ssh/authorized_keys` is:
```
from="<IP of reudy.net>",restrict,command="…/sessions-ctl --remote" ssh-ed25519 AAAA… sessions-manager
```
- **`command=`**: whatever prod asks for, sshd runs `sessions-ctl --remote`. It
reads `$SSH_ORIGINAL_COMMAND`, splits it without a shell, and accepts only
`list repos status create start stop health`. Names and branches are
checked against strict patterns; repos are picked by key from an allowlist
in the config, so no URL or path crosses the link.
- **`restrict`**: no PTY, no port/agent/X11 forwarding. (Tested: `-L` is refused.)
- **`from=`**: the key only works from the address `reudy.net` resolves to.
sshd runs with `UseDNS no`, so a hostname there would never match; instead
`sessions-access-refresh.timer` re-resolves it hourly and rewrites the line.
If DNS fails it keeps the old line rather than writing an empty one.
- **No free text crosses the link.** There is deliberately no "initial
prompt". Instructions go through Remote Control, which is guarded by your
claude.ai account, not by prod. So the worst a hijacked prod can do is start
sessions up to `max_running`, or stop them.
- The web app: scrypt password hash, HMAC-signed `HttpOnly; Secure;
SameSite=Strict` cookie, CSRF token on every POST, 5 failed logins per IP /
20 overall per 15 min, strict CSP, no JavaScript. It pins the dev box host
key (`StrictHostKeyChecking yes`).
- The container is rootless, has a read-only root filesystem and drops all
capabilities. It publishes only to `127.0.0.1`. Secrets are Podman secrets,
not files beside the unit.
- Every remote call is logged on the dev box: `journalctl -t sessions-ctl`.
**Folder trust:** Claude Code asks "do you trust this folder?" on first launch
in a new directory, which would block an unattended session. There is no CLI
flag for that in interactive mode, so `sessions-ctl` records the trust in
`~/.claude.json` (`projects[<dir>].hasTrustDialogAccepted`) for checkouts it
creates. The repo allowlist is the trust decision. This relies on an internal
Claude Code field. If a Claude Code update breaks it, the session shows
*"stuck on the folder-trust prompt"* on the web page. See Troubleshooting.
## How a session lives
- `create NAME REPO [BASE]`: clones the repo into `~/sessions/NAME`, creates
branch `session/NAME`, writes `~/.local/state/sessions/NAME.json` (with a
fixed conversation UUID) and runs `systemctl --user enable --now
claude-session@NAME`.
- The unit starts a **dedicated tmux server** (`tmux -L claude-NAME`) running
`claude --remote-control dev-box-NAME`. The first launch uses `--session-id
<uuid>` and later launches use `--resume <uuid>`, so a restart continues the
same conversation and keeps the same Remote Control link.
- **Crash:** `Restart=always`, 10 s delay, at most 5 restarts in 10 min.
- **Reboot:** enabled units start with the user manager, because `agent` has
linger on.
- **Stop:** `disable --now`, so the session stays stopped across reboots until you
start it again.
- **Limits:** `max_running` in the config (default 2). Each unit has
`MemoryHigh=1200M` and `MemoryMax=1600M`, which apply to claude and everything
it runs.
- Attach locally: `tmux -L claude-NAME attach`.
- Delete (local only, never over SSH): `sessions-ctl delete NAME --yes`. This
refuses if the checkout has uncommitted or unpushed work unless you pass `--force`.
## Install: dev box
```sh
git clone ssh://git@git.reudy.net:14922/reudy-net/sessions.git && cd sessions
./dev/install.sh # idempotent; re-run after every pull
$EDITOR ~/.config/sessions/config.toml # [repos] allowlist + [ssh_access]
./dev/install.sh # writes the authorized_keys line
sessions-ctl health
```
Requires linger (`sudo loginctl enable-linger agent`); `install.sh` warns if
it's off.
## Install: prod
As the rootless user that will run the container. Replace `~/.ssh/<key>` with
the private half of the key that is in the dev box's config.
```sh
git clone ssh://git@git.reudy.net:14922/reudy-net/sessions.git && cd sessions
podman build -t localhost/sessions-web:latest web/
# 1. Secrets (nothing is echoed; each command reads from a file or a hidden prompt)
podman secret create sessions-ssh-key ~/.ssh/<key>
head -c 48 /dev/urandom | base64 -w0 | podman secret create sessions-web-secret -
read -rsp 'Web password (12+ chars): ' PW; echo
printf '%s\n' "$PW" | podman run --rm -i localhost/sessions-web:latest python3 /app/app.py hash-password --stdin \
| podman secret create sessions-password-hash - ; unset PW
# 2. Non-secret config + the dev box's pinned host key
install -d -m 700 ~/.config/sessions-web
install -m 600 web/env.example ~/.config/sessions-web/env
install -m 644 web/known_hosts.dev-box ~/.config/sessions-web/known_hosts
# 3. Check that the key works from this machine before involving the container
ssh -i ~/.ssh/<key> -p 14922 -o UserKnownHostsFile=web/known_hosts.dev-box agent@2.28.100.223 health
# 4. Run under systemd (starts at boot only with linger on for this user)
install -D -m 644 web/sessions-web.container ~/.config/containers/systemd/sessions-web.container
systemctl --user daemon-reload
systemctl --user start sessions-web
curl -s http://127.0.0.1:8470/healthz # → ok
```
### Reverse proxy
The container listens on `127.0.0.1:8470` only. Put it behind the proxy that already
terminates TLS on prod, for example at `sessions.reudy.net`. The proxy must set
`X-Forwarded-For` for the rate limiter (`SESSIONS_TRUST_PROXY=1`).
Caddy:
```
sessions.reudy.net {
reverse_proxy 127.0.0.1:8470
}
```
nginx:
```
location / {
proxy_pass http://127.0.0.1:8470;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
```
### Updating prod
```sh
git pull && podman build -t localhost/sessions-web:latest web/ && systemctl --user restart sessions-web
```
## Troubleshooting
| Symptom | Check |
|---|---|
| Page says "dev box unreachable: Permission denied" | Prod's outgoing IP isn't the one `reudy.net` resolves to. On the dev box, `sudo journalctl -u ssh -n 20` shows the source IP. `sessions-ctl refresh-access` shows what `from=` holds |
| "Host key verification failed" | The dev box's host key changed. Update `known_hosts.dev-box` deliberately; never turn off checking |
| Session `failed` | `journalctl --user -u claude-session@NAME -n 50`. After 5 crashes in 10 min systemd gives up; fix the cause, then Start again |
| "stuck on the folder-trust prompt" | `tmux -L claude-NAME attach`, accept, detach. Then the trust field has moved in a Claude Code update, so fix `trust_workspace()` |
| No "Open in Claude" link | The link is read from the pane once Remote Control is up; refresh after a few seconds |
## Relation to `sess`
The dev box's own `sess` tool (in the dev-box repo) manages the box's role,
test and project agents in the **default** tmux server. This manager is
separate: its own tmux sockets (`-L claude-NAME`), its own `~/sessions/`, and
one systemd unit per session. The two don't see or affect each other, so `sess
ls` doesn't list these sessions. Use `sessions-ctl list` for those.
## Tests
```sh
python3 -m unittest discover -s dev/tests
python3 -m unittest discover -s web/tests
```