Run sessions-web behind Authelia, configured only by env vars

Drop the app's own login (password hash, signed cookies, CSRF tokens, rate
limiting): Authelia via Caddy forward_auth is the login. The app refuses any
request without the Remote-User header (optionally limited to
SESSIONS_ALLOWED_USERS) and any cross-site POST (Sec-Fetch-Site / Origin).

The SSH key and the dev box host key now come from environment variables
printed by `sessions-ctl keygen`, so the container needs no secrets store or
mounts. Add compose.yaml so prod's panel can build and deploy it; remove the
Quadlet unit and the files it needed. README covers the panel deploy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KKYfRST9cTJYae3f7Jf3ac
This commit is contained in:
agent 2026-09-30 14:39:56 +00:00
parent ca66db3aeb
commit b55ea07c96
9 changed files with 254 additions and 332 deletions

124
README.md
View file

@ -5,7 +5,7 @@ without SSHing in yourself. Each session runs `claude` with **Remote Control**,
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)
phone/browser ──HTTPS──▶ prod: Caddy + Authelia ──▶ sessions-web (container, 127.0.0.1:<port>)
│ ssh, key locked to one command
▼
dev box: sessions-ctl --remote ──▶ systemd: claude-session@<name>
@ -17,8 +17,8 @@ you then talk to it from the Claude app or the "Open in Claude" link.
|---|---|---|
| [`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 |
| [`web/app.py`](web/app.py) | prod, container | session list + buttons; stdlib, no JS, no login of its own |
| [`compose.yaml`](compose.yaml) | prod, panel | how the panel builds and runs the container |
## Security model
@ -43,13 +43,20 @@ from="<IP of reudy.net>",restrict,command="…/sessions-ctl --remote" ssh-ed2551
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.
- **The web app has no login of its own; Authelia is the login.** It serves
only requests carrying `Remote-User`, which Caddy's `forward_auth` sets from
Authelia (overwriting anything the client sent). With the header missing it
answers 403. That's a tripwire in case the route ever loses forward_auth,
not the protection itself. `SESSIONS_ALLOWED_USERS` can narrow it to named
users. Authelia's policy for the domain should be `two_factor`. It is by
default on prod.
- Cross-site POSTs are refused by checking `Sec-Fetch-Site`/`Origin`, so no
CSRF secret is needed. The page has a strict CSP and no JavaScript. The dev
box host key is pinned (`StrictHostKeyChecking yes`).
- The container is configured only by environment variables, including the
SSH key (one base64 line). At start it writes the key to a 0600 file on its
`/tmp` tmpfs. It runs rootless with a read-only root filesystem and all
capabilities dropped, published only on `127.0.0.1`.
- 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
@ -87,77 +94,74 @@ Claude Code field. If a Claude Code update breaks it, the session shows
```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
$EDITOR ~/.config/sessions/config.toml # [repos] allowlist, [ssh_access] allow_from_host
sessions-ctl keygen # add --rotate to replace an existing key
```
`keygen` makes the key pair prod uses. It generates it in RAM, writes the
public half to the config and `authorized_keys` (replacing the old
`sessions-manager` line), and prints the five environment variables for prod
to stdout, with a note on stderr. The private key is then deleted from this
box, so the printed copy is the only one. Paste it, then clear the terminal.
If it's lost, run `keygen --rotate` and paste again.
Requires linger (`sudo loginctl enable-linger agent`); `install.sh` warns if
it's off.
## Install: prod
## Deploy on prod (panel)
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.
The repo root has a `compose.yaml`, so the panel can build and run it:
1. **New app** in the panel from the Forgejo repo `reudy-net/sessions`, branch `main`.
2. **Environment**: paste the five lines `sessions-ctl keygen` printed. Optional:
`SESSIONS_ALLOWED_USERS=reudy`.
3. **Route**: e.g. `sessions.reudy.net` → service `web`, port `8470`, with
**Authelia on**. From the CLI:
`panelctl init sessions "sessions.reudy.net|127.0.0.1:18470||web:8470" true`
4. **Deploy.** The container log should say
`sessions-web on 0.0.0.0:8470 → agent@<dev box>:14922 …`.
5. Open `https://sessions.reudy.net`, sign in through Authelia, and you should
see the session list. An error there comes from the dev box (see
Troubleshooting).
**Reboots:** the container has `restart: unless-stopped`. Rootless Podman only
honours that at boot if the user's `podman-restart.service` is enabled
(`systemctl --user is-enabled podman-restart` as the panel user). That applies
to every panel app, so it belongs in the NixOS config.
### Without the panel
Any container runtime works. Put the five variables in a `chmod 600` env
file and run:
```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
podman build -t localhost/sessions-web web/
podman run -d --name sessions-web --restart unless-stopped \
--env-file ~/.config/sessions-web/env -p 127.0.0.1:8470:8470 \
--read-only --tmpfs /tmp --cap-drop ALL --security-opt no-new-privileges \
localhost/sessions-web
```
### Reverse proxy
Then put it behind `forward_auth` in Caddy:
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 {
forward_auth 127.0.0.1:9091 {
uri /api/authz/forward-auth
copy_headers Remote-User Remote-Groups Remote-Email Remote-Name
}
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 |
| "forbidden: this page must be served behind Authelia" | The route has no forward_auth, or the user isn't in `SESSIONS_ALLOWED_USERS` |
| Container exits with "configuration error" | One of the env values was pasted incompletely. Run `sessions-ctl keygen --rotate` again and paste all five lines |
| Page says "dev box unreachable: Permission denied" | Prod's outgoing IP isn't the one `reudy.net` resolves to, or the env holds an old key. 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. Check that the change is expected, then update `SESSIONS_DEV_HOST_KEY`. 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 |