Files
mnovak 3a0944ebff
ci / check (pull_request) Has been cancelled
[relay] Files: a file route for same-size encrypted chunks with its own quotas; the client seals chunks in STREAM and sends a file payload inside MLS
The relay stores files (id, upload and fetch token hashes, chunk count, uploaded hour) and chunks
(file id, index, ciphertext): every chunk is 256 000 bytes, so it learns the size rounded up to
250 KiB and nothing finer, and it keeps no fetch count. Only the uploader's token puts or deletes,
only the fetch token gets, a chunk never changes; per-file and relay-wide quotas answer 507; files
go after 30 days, incomplete ones after 24 hours. The client pads a file to whole chunks and seals
each with ChaCha20-Poly1305 in the aead crate's STREAM, under a key made for that file; the key,
name, type, size and thumbnail travel only in the new `file` payload, which shares `seq` with text.
Also: every route now answers a bare 413 (axum's own carried a text). PROTOCOL.md §1, §4, §5, §7, §8.
2026-11-22 19:29:37 +01:00

192 lines
17 KiB
Markdown
Raw Permalink 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.
# Tabellarius protocol v0 — what crosses the wire, and what the relay learns
**Approved 2026-11-08 (Mark)** as v0, before any relay code. Every claim below must have a test. A change
is a protocol version bump: documented here with its date and reason, and reviewed in its PR
(`CLAUDE.md` §2.2, §3).
## 1. Threat model
| Adversary | Protected | Not protected |
|:--|:--|:--|
| **Relay operator** (honest but curious, including us) | message content, names, contact lists, which mailboxes belong to the same person *by content*; a file's name, type, exact size and content | IP addresses at connection time and timing — an operator who logs at the network layer can correlate. Reduced by not logging (§5), not prevented. A file's size rounded up to 250 KiB, when it was uploaded, and each fetch of its chunks as it serves them (§8, files) |
| **Network observer** | content (TLS + MLS) | that a device talks to a relay, and when |
| **Stranger** | cannot post: no capability token | — |
| **Contact who turns hostile** | block = rotate the token; their posts are refused | what they already received |
| **Stolen phone** | database encrypted; key in the OS keystore | an unlocked phone |
| **Compromised device** | — | everything on it. Out of scope, stated plainly. |
## 2. Building blocks
- **MLS** via OpenMLS *(licence and Tauri mobile build to verify)*. One MLS group per pair of people.
The Orator fans out into these private groups, so replies stay private.
- **Credentials:** an Ed25519 key made on the device. The display name travels **inside** MLS
messages only.
## 3. Mailboxes and tokens
- A client creates **one mailbox per contact** (random 128-bit id), so the relay cannot tell by
addressing that two conversations involve the same person.
- Each mailbox has a **post token** (random 256-bit) given only to that contact, and a **fetch token**
kept by the owner. The relay stores **hashes** of both.
- **Block** = the owner rotates the post token; the old one is refused. The blocked person is not told.
## 4. Invitations
A QR code (in person) or a link carries: relay URL, an invite id, and a one-time secret. Unclaimed
invites expire after **7 days** *(decided 2026-11-08)*.
1. **The inviter** leaves its MLS KeyPackage on the relay under the invite id and the secret's hash.
It also creates a **welcome mailbox**, whose id and post token are derived from the secret with
HKDF-SHA256: empty salt, info `tabellarius v0 welcome mailbox` (16 bytes) and
`tabellarius v0 welcome mailbox post` (32 bytes).
2. **The invitee** claims the KeyPackage with the secret, which deletes the invite. It creates the
MLS group with the inviter in it, and posts to the welcome mailbox:
- the Welcome;
- an MLS message carrying its own mailbox id, a post token for it, and its name.
3. **The inviter** joins from the Welcome and answers inside MLS with its own mailbox, post token and
name.
From then on each side posts to the other's mailbox. Names and post tokens travel **only inside
MLS**. To the relay, the welcome mailbox is an ordinary mailbox.
*(Changed 2026-11-14, approved by Mark, from "the invitee replies with their own KeyPackage": the
invitee needs no KeyPackage, because it creates the group itself.)*
### Payloads inside MLS *(added 2026-11-17, F24; `.agents/features/block-and-receipts.md`)*
Every MLS application message carries one JSON object. The relay sees none of it.
| `kind` | Fields | Meaning |
|:--|:--|:--|
| `hello` | `mailbox`, `postToken`, `name` | where and how to post to the sender; once per conversation |
| `text` | `body`, `seq`, `replyTo`? | a message; `seq` counts the sender's messages to this contact from 1. `replyTo` *(2026-11-21)*, when there, names the message it answers |
| `reaction` *(2026-11-21)* | `to`, `emoji`? | the sender's reaction to the message `to`: `emoji` (1–32 bytes of UTF-8, no control characters) sets it, replacing their earlier one; absent removes it. No `seq` |
| `poll` *(2026-11-22)* | `seq`, `question`, `options`, `multiple` | a message (it shares `seq` with `text`): a question of 1–300 characters and 2–10 different options of 1–100, none blank, no control characters; `multiple` allows several choices in a vote |
| `vote` *(2026-11-22)* | `poll`, `choices` | the sender's choices in the poll `poll` names, replacing their earlier ones: indexes into `options`, ascending, each once, at most one unless `multiple`; `[]` takes the vote back. Refused once the poll is closed. No `seq` |
| `pollClose` *(2026-11-22)* | `poll`, `ballots` | sent by the poll's author only: the poll is closed, and `ballots` (`{author, choices}`, one per member with a non-empty vote) are **the final votes**, as the author's client had accepted them, so a vote that crossed the close counts nowhere. No `seq` |
| `file` *(2026-11-22)* | `seq`, `file`, `fetchToken`, `key`, `name`, `mime`, `size`, `thumb`? | a message (it shares `seq` with `text`): a file on the relay (§8, files). `file` its id, `fetchToken` the capability to get its chunks, `key` its 32-byte key; `name` 1–255 characters without control characters, `/` or `\`; `mime` `type/subtype`, 3–127 printable ASCII bytes; `size` 1 byte to 100 MiB; `thumb`, when there, a JPEG of at most 32 KiB (a receiver drops a thumbnail that is not, and keeps the file). The key, the name, the type and the exact size travel **only here** |
| `receipt` | `upto` | the receiver has saved the sender's texts up to `upto`. Sent only by a client with receipts on; a client with them off also ignores them |
| `inbox` | `postToken` | a new post token for the mailbox the receiver already knows (after an unblock). It cannot name another mailbox |
**Naming a message** *(added 2026-11-21, Amicitia F27; `.agents/features/reactions-replies.md`)*:
`{"author", "seq"}`, where `author` is the 16 random bytes in the author's MLS credential (unpadded
base64url, 22 characters; made on the device, never a name) and `seq` the author's own. A receiver
accepts a reference only to a message that exists in that conversation (an author who is one of its
two members, a `seq` from 1 up to the last one sent or received); a reaction that names anything
else is dropped, and a text keeps its words but loses the reference.
**Kinds a client does not know** *(2026-11-21)*: one with a `seq` of at least 1 is shown as *update
the app to see this message* and counts for receipts; one without is dropped. A known kind with an
unknown field is dropped: **new fields go into new kinds**, so that older clients say so instead of
dropping a message without a trace.
**A file's chunks** *(added 2026-11-22, Amicitia F29; `.agents/features/files.md`)*: the file is
padded with zeros to whole chunks of 255 984 bytes and each is sealed with ChaCha20-Poly1305 under
the file's key in the STREAM construction (the `aead` crate's `StreamBE32`: a zero 7-byte nonce
prefix, the chunk's index as a 32-bit big-endian counter, and a last-chunk flag), so every chunk is
256 000 bytes and opens only at its own index, and only the last opens as last. A key is made for
one file and never reused. A receiver refuses a chunk that does not open, and padding that is not
zeros.
**Block** (§3) is a rotate of the post token to the hash of a token nobody gets. Nothing is sent to
the blocked person: their posts get §8's ordinary 404. What they posted before is fetched and
dropped. **Unblock** rotates again and sends the new token in an `inbox` payload.
## 5. What the relay stores — the complete list
| Table | Columns |
|:--|:--|
| `mailboxes` | id, hash(post token), hash(fetch token), created day |
| `blobs` | mailbox id, ciphertext, size, received hour (rounded) |
| `invites` | invite id, hash(fetch secret), KeyPackage, expiry |
| `push` *(only if the user opts in, G03; made concrete 2026-11-20)* | mailbox id, push endpoint |
| `files` *(2026-11-22, F29)* | file id, hash(upload token), hash(fetch token), chunk count, uploaded hour (rounded) |
| `chunks` *(2026-11-22, F29)* | file id, index, ciphertext (always 256 000 bytes) |
Nothing else. *(2026-11-22:)* a file has no mailbox, no sender and no recipient, and the relay
keeps **no count of fetches**: it sees each one as it serves it. A file is deleted when its
uploader deletes it, after **30 days**, or after **24 hours** if not all its chunks have arrived. No IP, no user agent, no request log beyond counts. *(2026-11-20:)* the relay also
remembers **in memory only**, never on disk, when it last woke each mailbox (§6), to wake it at most
once a minute. A blob is deleted on acknowledged
delivery and after **30 days** at the latest *(decided 2026-11-08)*. The reverse proxy in front must also not
log; the NixOS module ships configured that way.
## 6. Waking a closed app
A notification for a closed app needs a push service. The wake-up carries **no content and no sender**
— only "something arrived".
- **Android:** UnifiedPush lets the user choose their own distributor; FCM as a fallback.
*(Made concrete 2026-11-20, F25b; `.agents/features/wake-up.md`.)* The owner gives the relay one
**push endpoint per mailbox** (§8, `PUT …/push`), so the relay cannot link a person's mailboxes by
endpoint — it does see the endpoint's **host**, which on a person's own push server links them.
When a blob is stored under a valid post token, the relay POSTs the constant body `wake` to the
endpoint, at most once a minute per mailbox, with nothing of the mailbox, the blob or its sender.
It connects only to **global** addresses (it resolves the host itself and drops loopback,
private, link-local, CGNAT, multicast and reserved addresses, IPv4-mapped forms included), follows
no redirect, gives up after 10 s, and has at most 16 wakes on their way. A push server's 404 or
410 removes the endpoint. The push server learns that the relay woke one of its capability URLs,
and when. Not done: Web Push encryption and VAPID (RFC 8291/8292) — the body is a public
constant, but a distributor that requires VAPID cannot be used (gap W1).
- **iOS:** only the app's publisher can send through Apple's push service, so a self-hosted relay
cannot wake an iOS app directly. It would go through a small push gateway run by the publisher,
which learns a device token and timing.
**Decided:** iOS uses a small push gateway run by the publisher, carrying no content and no sender.
It learns a device token and timing, and **users are told so plainly** in *How your messages travel*.
The platform facts above are Claude's understanding, to be verified in `.agents/features/wake-up.md`.
## 7. Tests that prove this document
- A canary string sent in a message never appears anywhere in the relay's database or logs.
- A reaction's emoji and a reply's text never appear in the relay's database (`client/tests/real_relay.rs`, `client/tests/reactions.rs`); a reaction or reply to a message that does not exist in the conversation is refused (`client/tests/reactions.rs`, the client's unit tests).
- A poll's question and options never appear in the relay's database (`client/tests/real_relay.rs`); a vote or close that does not fit its poll, a close by anyone but the poll's author, and anything after the close are refused and dropped (`client/tests/polls.rs`, the client's unit tests).
- A file's name, type, thumbnail and content never appear in the relay's database, and every chunk it holds is 256 000 bytes (`client/tests/real_relay.rs`, `client/tests/files.rs`); a chunk tampered with, swapped, repeated, cut or from another file is refused (`client/tests/files.rs`, the client's unit tests); only the upload token puts or deletes and only the fetch token gets, a chunk never changes, the quotas answer 507, and files go after 30 days or 24 hours incomplete (`relay/tests/files.rs`).
- A post without a valid token is refused; after rotation the old token is refused.
- A delivered blob is gone; an undelivered blob is gone after the TTL (clock injected).
- A wake carries the constant body and nothing of the mailbox, its tokens or the blob; a mailbox is
woken at most once a minute; a non-global address is never connected to (`relay/tests/wake.rs`).
- Parsers are fuzzed (`cargo fuzz`) for every message type.
- Every check in `VERIFY.md` §1 has a CI job or a documented manual procedure.
## 8. The wire (v0) — approved 2026-11-08, routes added 2026-11-14, quotas 2026-11-16, join page 2026-11-18, push 2026-11-20, files 2026-11-22
HTTP/1.1 behind TLS (the reverse proxy, F23). Ids and tokens are unpadded base64url of a fixed length
with one canonical spelling: ids are 16 bytes, and tokens and hashes are 32. JSON bodies refuse
unknown fields. A blob is at most 256 KiB, and a KeyPackage at most 16 KiB.
| Method and path | Auth (`Authorization: Bearer`) | Effect |
|:--|:--|:--|
| `POST /v0/mailboxes` `{id, postTokenHash, fetchTokenHash}` | none | creates a mailbox. The owner sends **hashes**, so the relay never holds a token |
| `POST /v0/mailboxes/{id}/blobs` (bytes) | post token | stores the blob with its size and the hour it arrived |
| `GET /v0/mailboxes/{id}/blobs` | fetch token | the waiting blobs, oldest first, at most 100. Fetching deletes nothing |
| `DELETE /v0/mailboxes/{id}/blobs/{sha256}` | fetch token | acknowledges: deletes that mailbox's blob with this SHA-256. §5 has no blob id, and a sequential id would leak how busy the relay is |
| `PUT /v0/mailboxes/{id}/post-token` `{postTokenHash}` | fetch token | blocks: the old post token is refused from now on |
| `PUT /v0/mailboxes/{id}/push` `{endpoint}` *(2026-11-20)* | fetch token | where to wake the owner (§6): `https://`, a host, no user info or fragment, at most 1 000 bytes. 204 |
| `DELETE /v0/mailboxes/{id}/push` *(2026-11-20)* | fetch token | no more wakes for this mailbox. 204, also when none was set |
| `POST /v0/invites` `{id, secretHash, keyPackage}` | none | stores an invite. It expires after 7 days, rounded up to the hour |
| `GET /v0/invites/{id}` | invite secret | returns the KeyPackage once, then deletes the invite |
| `POST /v0/files` `{id, uploadTokenHash, fetchTokenHash, chunks}` *(2026-11-22)* | none | creates a file of `chunks` (1–410) chunks. The uploader sends **hashes** of two tokens it made. 201; 409 if the id is taken; 400 if `chunks` is out of range |
| `PUT /v0/files/{id}/chunks/{index}` (exactly 256 000 bytes) *(2026-11-22)* | upload token | stores chunk `index` (decimal, from 0). The same bytes again: 201; other bytes: 409, a chunk never changes; another size: 413 |
| `GET /v0/files/{id}/chunks/{index}` *(2026-11-22)* | fetch token | the chunk's bytes; a chunk not there (yet) is 404 |
| `DELETE /v0/files/{id}` *(2026-11-22)* | upload token | deletes the file and its chunks. 204 |
| `GET /version` | none | the protocol and relay versions |
| `GET /join` | none | a static page for an invite link opened in a browser *(added 2026-11-18)* |
- A wrong token, an unknown id and a malformed id all give the same bare **404**, so nobody can probe
what exists.
- **Quotas** *(added 2026-11-16, F23, so a relay can face the internet; `.agents/features/home-relay.md`)*:
a mailbox holds at most **1 000 waiting blobs** and **32 MiB**, and a relay at most what its
operator sets (by default 1 GiB of blobs, 100 000 mailboxes, 10 000 unclaimed invites). Over a
limit the answer is a bare **507**. The per-mailbox caps are checked only after the post token,
so a 507 tells a stranger nothing about a mailbox. Nothing new is stored to count: the counts
come from §5's own rows and `size`. A client treats 507 like any failure and tries again later.
- **Files** *(added 2026-11-22, Amicitia F29, so a chat can carry photos, videos and documents;
`.agents/features/files.md`)*: a file holds at most its declared chunks (an index at or past them
is 507, checked after the upload token), at most 410 (100 MiB of plaintext). A relay holds at most
`--max-files` files (default 10 000) and `--max-file-bytes` (default 4 GiB), each file counted at
its full size from its creation so an upload under way is never cut off; over either, `POST` is
507. Files have their own quota, apart from blobs, so they cannot starve messages. Only the token
whose hash came with `POST` can add, or delete; a stranger who learns a file id can do neither.
A relay from before this answers 404 to `POST /v0/files`.
- **The join page** *(added 2026-11-18; `.agents/features/join-page.md`)*: an invite can travel as
`https://<relay>/join#<invite id>.<secret>`, which messengers make tappable. The browser sends
only `GET /join`: the fragment, and so the secret, never reaches the relay, a link preview or a
proxy. The page is static (one inline script and style, named by hash in a
`Content-Security-Policy` of `default-src 'none'`; `no-store`; `no-referrer`; no cookie) and
points its one button at `amicitia://invite?relay=<its origin>#<id>.<secret>`. The relay stores
nothing for it and learns only that some address opened the page.
- A refusal has no body.
- The relay prints no request.
- **Push** *(added 2026-11-20, Amicitia F25b, so a closed Android app hears that something arrived)*:
a wrong token or unknown mailbox is §8's bare 404; an endpoint that breaks the rules is 400, and so
is `http://` to a loopback host, which only a relay started with `--push-loopback` (tests) accepts.
- Tests: `crates/relay/tests/relay.rs`, `crates/relay/tests/wake.rs`, `crates/relay/tests/files.rs`,
`crates/protocol/tests/wire.rs`.