diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4d8d43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +*.pyc +.env +*.key diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..b0b021a --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,42 @@ +# AGENTS.md — sessions + +A remote session manager: a web page on prod that starts Claude Code sessions +on the dev box through one locked-down SSH key. Read the README's "Security +model" before changing anything that touches the SSH boundary. + +## Layout +- `dev/` runs on the **dev box**: `sessions_ctl.py` (installed as + `~/.local/bin/sessions-ctl`), systemd user units, `install.sh`. +- `web/` runs on **prod** in a rootless Podman container: `app.py`, + `Containerfile`, Quadlet unit `sessions-web.container`. + +## Stack and conventions +- Python ≥ 3.11, **standard library only** on both sides. No pip install, so + there's no supply chain to audit. Keep it that way unless there's a strong reason. +- The web page has **no JavaScript**. Plain forms, POST, then a redirect. +- `sessions-ctl` always prints exactly one JSON object; exit 0/1/2. +- The dev port for local work on the web app is **8470**, bound to 127.0.0.1. + Preview via `ssh -p 14922 -L 8470:localhost:8470 `. + +## Invariants — do not break +1. Nothing received over SSH reaches a shell or Claude. No "initial prompt" + feature, no free-text arguments. New remote verbs need validation *and* a + test in `dev/tests/test_sessions_ctl.py`. +2. Local-only verbs (`delete`, `refresh-access`, `_launch`) stay out of + `REMOTE_VERBS`. +3. `refresh-access` never writes an empty `from=`, and never touches lines + other than the one ending in ` sessions-manager`. +4. The session unit keeps `DBUS_SESSION_BUS_ADDRESS` pointed at nothing. + Otherwise tmux moves claude into its own `tmux-spawn-*.scope` and it escapes + `MemoryMax` and clean stops (checked 2026-09-30 with tmux 3.6). +5. The container publishes to `127.0.0.1` only. + +## Test +```sh +python3 -m unittest discover -s dev/tests +python3 -m unittest discover -s web/tests +``` +To test end to end on the dev box, add a throwaway key with +`from="127.0.0.1",restrict,command=".../sessions-ctl --remote" … sessions-selftest`, +run the container with `--network host` and `SESSIONS_DEV_HOST=127.0.0.1`, +then remove the key line again. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7257274 --- /dev/null +++ b/README.md @@ -0,0 +1,178 @@ +# 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@ + └─ tmux -L claude- + └─ 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="",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[].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 + ` and later launches use `--resume `, 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/` 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/ +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/ -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 +```