diff --git a/.agents/features/relay.md b/.agents/features/relay.md new file mode 100644 index 0000000..05d6aec --- /dev/null +++ b/.agents/features/relay.md @@ -0,0 +1,122 @@ +# F20 — Wire types and the relay, on loopback first + +Analysis, 2026-11-11. Issues #4 (protocol crate) and #5 (relay). It builds on `docs/PROTOCOL.md` v0, +approved 2026-11-08. The home-server deploy (#8) and the client (#6) come later. + +## 1. Why and what +Chat needs somewhere for a message to wait until the other person's device fetches it. The relay is +that place, and it is the part of the system most open to abuse of trust: whoever runs it sees every +connection. So it is built to `PROTOCOL.md` §5 exactly, and every claim in §7 that this feature can +prove is proved by a test. + +After F20 a developer can run `tabellarius-relay` on `127.0.0.1` and: +- create a mailbox; +- post an opaque blob to it with a post token; +- fetch the mailbox's blobs with the fetch token; +- acknowledge a blob, which deletes it; +- rotate the post token (block); +- leave an invite with a KeyPackage, and fetch it once with the invite's secret. + +The relay never sees a plaintext message: it receives bytes and stores them. Encryption is the +client's job (F21). + +## 2. Architecture +```mermaid +flowchart LR + client[client, F21] -- HTTP/1.1 JSON + octets, /v0/... --> api[relay: axum router] + api --> core[relay::Store — every rule, clock passed in] + core --> db[(SQLite: mailboxes, blobs, invites)] + proto[protocol crate: ids, tokens, limits, request/response types, parsers] --> api + proto --> client +``` +### Wire types (`tabellarius-protocol`, no I/O) +- `MailboxId`, `InviteId`: 16 random bytes. `Token` (post, fetch, invite secret): 32 random bytes. + Each travels as unpadded base64url. Parsing is strict: an exact length, the alphabet only, and the + canonical form only, so one value has exactly one spelling. +- `TokenHash`: SHA-256 of a token, 32 bytes. The client sends hashes when it creates a mailbox or an + invite. The relay therefore never sees a fetch token until it is used, and stores only what it was + given. +- Limits: a blob is at most 256 KiB, and a KeyPackage at most 16 KiB. +- Request and response bodies are `serde` structs with `deny_unknown_fields`. The version is in the + path (`/v0/`). + +### Endpoints (`/v0`) +| Method and path | Auth (`Authorization: Bearer `) | Effect | +|:--|:--|:--| +| `POST /v0/mailboxes` `{id, postTokenHash, fetchTokenHash}` | none | creates the mailbox; `created_day` = today; 409 if the id exists | +| `POST /v0/mailboxes/{id}/blobs` (octets) | post token | stores the blob with `size` and the hour it arrived | +| `GET /v0/mailboxes/{id}/blobs` | fetch token | `[{blob: base64url, receivedHour}]`, oldest first, at most 100 | +| `DELETE /v0/mailboxes/{id}/blobs/{sha256}` | fetch token | acknowledges: deletes that mailbox's blob whose SHA-256 is given | +| `PUT /v0/mailboxes/{id}/post-token` `{postTokenHash}` | fetch token | rotates the post token; the old one is refused from now on | +| `POST /v0/invites` `{id, secretHash, keyPackage}` | none | stores the invite; it expires after 7 days | +| `GET /v0/invites/{id}` | invite secret | returns the KeyPackage once, then deletes the invite | +| `GET /version` | none | crate version and build commit | + +- **Acknowledging by the blob's SHA-256**, not by a relay-assigned id. §5 has no id column. A + sequential row id would also tell clients how much traffic the relay carries. The client knows the + hash of what it fetched; the relay computes it over that mailbox's blobs only. +- **A wrong token, an unknown mailbox and an unknown invite give the same 404.** Nobody can probe + which mailboxes exist. Hashes are compared in constant time (`subtle`). +- **The clock is passed in.** The store's methods take `now`. The server reads the system clock once + per request, and tests pass their own. +- **Purge:** `Store::purge(now)` deletes blobs older than 30 days and invites past their expiry. The + server runs it every hour and at start. +- **No logs of requests.** No `tracing` subscriber. The server prints one line at start: the + version and the bind address. There is no per-request log of IPs, user agents or bodies. Counts + are not built yet. +- **Bind address:** `127.0.0.1:8470` by default. Another address must be given explicitly + (`--listen`). TLS is the reverse proxy's job (F23). + +### Storage — exactly §5 +```sql +mailboxes(id BLOB PRIMARY KEY, post_hash BLOB, fetch_hash BLOB, created_day INTEGER) +blobs(mailbox_id BLOB, ciphertext BLOB, size INTEGER, received_hour INTEGER) +invites(id BLOB PRIMARY KEY, secret_hash BLOB, key_package BLOB, expiry INTEGER) +``` +- `received_hour` is the arrival time rounded down to the hour (unix seconds / 3600), and + `created_day` the day (/ 86400). `expiry` is rounded up to the hour. +- `secure_delete` is on, so deleted blobs are overwritten in the file and not left in free pages. + WAL is off (`journal_mode=DELETE`), so there is no second copy in a `-wal` file. +- A test compares the live schema (`sqlite_master`, `PRAGMA table_info`) with this list. A column + added in passing fails it. The SQLite row id stays internal and is never sent. + +## 3. Risks +- **Privacy, by the operator's own logs:** the relay keeps none. A reverse proxy might, and that is + F23's NixOS module. Timing and IP at connection time stay visible to an operator who logs at the + network layer (§1, stated). +- **Privacy, metadata in the database:** `size` and `received_hour` are in §5 on purpose, coarse by + design. Nothing links two mailboxes to one person. +- **Abuse:** creating a mailbox and an invite needs no account, so anyone who can reach the relay can + fill its disk. v0 caps sizes, not counts. On a home relay behind a private address that is + acceptable. Quotas without identity (proof of work, per-mailbox caps) are a gap, before any public + relay (§4). +- **Crypto:** none is written here. SHA-256 comes from `sha2`, the constant-time compare from + `subtle`, and randomness only in tests and the client. +- **Offline:** the relay is only the post office. The app works offline for everything else. + +## 4. Gaps and designed solutions +| Gap | Solution | +|:--|:--| +| No per-mailbox or global quota | a blob count and a byte cap per mailbox, refused with 507; before any public relay | +| `cargo fuzz` needs nightly and Linux, and the CI runners are Windows | parsers are hammered with `proptest` (random and mutated input, 10,000 cases each) in `cargo test`; `cargo fuzz` targets are added with the Linux runner | +| Deleting a whole mailbox (leaving the app, erase everything) | not in §5's operations; proposed for v1 of the protocol, with the client | +| Counts for the operator (requests, blobs) | an in-memory counter at `/metrics` on loopback; later | +| The canary in a *message* (§7, first test) | needs MLS, so it is proved end to end in F21. F20 proves the canary for tokens and addresses | + +## 5. Verification matrix +| Case | Kind | Test | +|:--|:--|:--| +| Ids and tokens round-trip; wrong length, alphabet, padding or non-canonical spelling refused | unit + property | `protocol` | +| Any byte string given to any parser: an error, never a panic | property (10,000 cases) | `protocol` | +| Unknown JSON field refused | unit | `protocol` | +| The live database schema is exactly §5 | integration | `relay` `schema_is_exactly_protocol_section_5` | +| Post with no, a wrong or the fetch token: 404; with the post token: 201 | integration (HTTP) | `relay` | +| After rotation the old post token is refused and the new one accepted | integration | `relay` | +| An acknowledged blob is gone; another mailbox's blob with the same bytes is not | integration | `relay` | +| A blob is purged after 30 days and kept at 29 (clock injected) | integration | `relay` | +| An invite is fetched once, with the right secret only; it is gone after 7 days | integration | `relay` | +| Tokens, invite secrets and the client address never appear in the database file (byte search) | integration | `relay` `no_token_or_address_reaches_the_database` | +| A deleted blob's bytes are not left in the file (`secure_delete`) | integration | `relay` | +| A blob over 256 KiB is refused with 413 | integration | `relay` | +| The binary binds loopback by default and serves `/version` | integration (spawned binary) | `relay` | +| `relay` does not depend on `client`; `protocol` depends on nothing here | CI | `scripts/check-deps.sh` |