Files
Amicitia-Tabellarius/.agents/features/home-relay.md
T

110 lines
7.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# F23 — The relay on a home server: quotas, a NixOS module, TLS
Analysis, 2026-11-16. Issues T#8 (deploy) and Amicitia #51. The container image of T#8 is left for later: the NixOS module is what the home server runs. It builds on F20 (the relay) and
`relay.md` §3–4, which put two things before any relay that faces the internet: **quotas**, and a
**reverse proxy that logs no IP address**.
## 1. Why and what
Two people on two machines can only chat through a relay both can reach. Mark's home server is the
first one. After F23:
- the relay refuses to fill its disk: per-mailbox and relay-wide caps, answered with a bare **507**;
- `deploy/nixos/module.nix` runs it on NixOS, listening on loopback only, behind **Caddy** with TLS
from Let's Encrypt, and **no access log, no error log with an address in it**;
- a NixOS VM test boots that configuration, sends requests through the proxy from a known address,
and fails if that address appears anywhere in the journal or under `/var/log`;
- Amicitia accepts an `https://` relay address (its own PR).
## 2. Design
```mermaid
flowchart LR
A[app] -->|TLS 443| C[Caddy: TLS, no logs]
C -->|HTTP 127.0.0.1:8470| R[tabellarius-relay: systemd, DynamicUser, loopback only]
R --> D[(SQLite: PROTOCOL.md section 5)]
```
### Quotas *(protocol change, §8: a new status)*
| Limit | Default | Set by |
|:--|:--|:--|
| waiting blobs in one mailbox | 1 000 | the protocol |
| waiting bytes in one mailbox | 32 MiB | the protocol |
| waiting bytes on the relay | 1 GiB | `--max-bytes` |
| mailboxes on the relay | 100 000 | `--max-mailboxes` |
| unclaimed invites on the relay | 10 000 | `--max-invites` |
- Over a limit, the relay answers **507 Insufficient Storage**, bare, like every refusal. The
per-mailbox caps are checked **after** the post token, so 507 tells nothing to someone without
the token. The relay-wide caps say only that the relay is full.
- **No identity is needed to count**: nothing new is stored; the counts come from §5's own columns
(`size`, the rows). No IP address is kept, not even in memory: rate limits by address would need
one. A flood can fill the relay up to its caps; then it refuses, and the operator sees it.
- The client already handles it: 507 is a failure, so the message waits in the outbox.
- **Mailboxes are never deleted yet** (`relay.md` §4): an abandoned mailbox counts against the
100 000 until a protocol version can delete one. Stated.
### The NixOS module (`deploy/nixos/module.nix`)
- `services.tabellarius.enable`, `.domain`, `.maxBytes`, `.maxMailboxes`, `.maxInvites`, `.package`
(the flake's own build).
- **The relay:** a systemd service with `DynamicUser`, `StateDirectory=tabellarius` (the database),
`--listen 127.0.0.1:8470`. Hardened: `ProtectSystem=strict`, `ProtectHome`, `PrivateTmp`,
`PrivateDevices`, `NoNewPrivileges`, `MemoryDenyWriteExecute`, `RestrictAddressFamilies=AF_INET
AF_INET6`, `SystemCallFilter=@system-service`, and **`IPAddressDeny=any` with
`IPAddressAllow=localhost`**: even a bug cannot make it talk to anything but the proxy.
- **The proxy:** `services.caddy`, one virtual host, `reverse_proxy 127.0.0.1:8470`. The NixOS
module writes an access log per host by default; the host's `logFormat` is set to
`output discard`, and Caddy's default logger too. Caddy's own TLS-handshake and proxy errors can
name a client address, so no log is the rule; an operator who needs one turns it on and knows
why. Headers: none added. *(2026-12-03)* The default logger is every site's on the machine, so the module sets
it with `lib.mkDefault`: a host that serves other sites may keep a global error log (.217 sets
`"level ERROR"`). Such a log can then hold a client's address in a rare TLS-handshake error for the relay's name.
- **journald:** the relay prints one line at start. Nothing per request.
### The VM test (`deploy/nixos/test.nix`, `nix flake check`)
Two machines: `server` with the module (TLS from Caddy's internal CA instead of Let's Encrypt), and
`client` at a fixed address. The client creates a mailbox, posts, fetches, acknowledges, makes a
mistake (a wrong token, a malformed request), and fills a mailbox to its 507. Then the test greps
the server's whole journal and `/var/log` for the client's address and fails if it appears.
**Run on the hub (2026-12-02).** `.fabrica/artifex/nix-check.yaml` builds the package (with the crates' tests) and
runs this VM test on Fabrica's NixOS runner. First run r54ed0gn5tcqs at f1e1f6e: package 11 min, VM test 10 min,
both exit 0. Run it again on every change to `deploy/`, `crates/relay` or `flake.nix`, and before every deploy.
### Deploying on the home server (2026-12-02)
The relay is deployed by the server's operator from a **tag** of this repository, never from a branch: the flake
input pins the tag, so the code that runs is the code tagged and published (`docs/VERIFY.md`).
- **The flake input:** `tabellarius.url = "git+https://gitea.marek-novak.cz/mnovak/Amicitia-Tabellarius.git?ref=refs/tags/vX.Y.Z";`
and `imports = [ tabellarius.nixosModules.default ];`.
- **The service:** `services.tabellarius = { enable = true; domain = "<name>"; };` The caps keep their defaults
(1 GiB of waiting blobs, 100 000 mailboxes, 10 000 invites); `wake = true` (UnifiedPush wake-ups, F25b).
- **The proxy:** the module turns on `services.caddy` with one virtual host. On a server whose Caddy is already
the NixOS module, it is one more host in the same Caddy; a server with another proxy on 443 must proxy the
name to `127.0.0.1:8470` itself, with no access log and without `X-Forwarded-For`, `X-Real-IP` and `Forwarded`.
- **DNS and TLS:** one A/AAAA name for the relay. Caddy gets its certificate by HTTP-01, so port 80 must reach it.
- **After deploying:** `curl -s https://<name>/version` answers the relay's version, and `https://<name>/join` serves the join page; `journalctl -u
tabellarius` shows the one start line and nothing per request; no file under `/var/log` names a client.
- **Calls (v0.3.0, `call-kinds.md`):** a secret file outside the store, written by Custos (32 random bytes, base64,
one line), then `turn.secretFile = "<path>"`, `turn.urls = [ "turn:<name>:3478?transport=udp"
"turn:<name>:3478?transport=tcp" ]` and `turn.coturn.enable = true`. coturn runs on the same machine with the
same secret and no log. Behind NAT: `turn.coturn.externalIp = "<public>/<this machine's LAN address>"`, and the
router forwards 3478 (UDP and TCP) and 49152–49999 (UDP) to it. The relay answers `POST /v0/mailboxes/{id}/turn`
only once `secretFile` is set. Check: `systemctl status coturn`, `ss -lun 'sport = :3478'`, and from outside
`turnutils_uclient` with a pair from the relay.
- **Rollback:** point the input at the previous tag and switch. The database (`/var/lib/tabellarius/relay.db`)
keeps only waiting ciphertext; it stays across versions unless `docs/PROTOCOL.md` says otherwise.
## 3. Risks
- **Privacy:** the logs are the risk this feature exists to close. The VM test is its proof, once
it runs. The network layer (a router, the ISP) still sees addresses and timing (§1).
- **Abuse:** caps bound the disk; they do not stop someone from filling them. Proof of work or
invite-only mailbox creation is for a later protocol version.
- **Reproducibility:** the module uses the flake's package, so the relay that runs is the one built
from this commit (`docs/VERIFY.md`).
## 4. Tests — written first
1. Each cap: the post or create that would cross it gets 507; one under it succeeds; after an
acknowledgement (or a purge) there is room again.
2. The per-mailbox cap answers 404, not 507, to a wrong token.
3. The CLI reads `--max-bytes`, `--max-mailboxes`, `--max-invites`; a bad value is refused.
4. The VM test (§2), on Linux.
Planted bugs: the mailbox cap checked before the token; an acknowledgement not freeing space; the
global byte cap ignored; the invite cap counting claimed invites; the CLI option ignored.