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.
14 KiB
Files — a relay route for encrypted chunks, and a file payload (PROTOCOL.md §4, §5, §7, §8)
Amicitia F29, step 4 of its full-chat plan (Amicitia/.agents/features/full-chat.md §2, §4, §5.5).
Unlike F27 and F28 this changes what the relay stores: two tables and four routes. Everything
readable about a file — its name, its type, its size to the byte, its thumbnail and its key — travels
inside MLS, in a file payload. The relay holds same-size ciphertext chunks under a random id.
1. Why and what
A chat people move to sends a photo of the menu, a short video of the kids, a PDF of the tickets. A blob is at most 256 KiB, and a photo is several MiB, so a file cannot ride in a message. It goes to the relay in chunks, encrypted on the device with a key that only the message carries.
So this change adds:
- The relay's
filesroute: an uploader creates a file (a random id, the hashes of an upload token and a fetch token it made, the number of chunks), puts its chunks with the upload token, and may delete it early; the recipient gets each chunk with the fetch token. Chunks go after 30 days, like blobs. A per-file and a relay-wide quota answer 507. - The client's chunked encryption: a fresh key per file, the file padded to whole chunks, every
chunk exactly the same size on the wire, each sealed with ChaCha20-Poly1305 in the STREAM
construction (the
aeadcrate'sStreamBE32), so a chunk tampered with, swapped, dropped from the end or taken from another file is refused. - A
filepayload, a message that shares the sender'sseqwithtextandpoll: the file's id, fetch token and key, its name, type and size, and a small thumbnail.
2. Design
sequenceDiagram
participant A as Anna's app
participant C as Client (Anna)
participant R as Relay
participant D as Client (Ben)
participant B as Ben's app
A->>C: start_upload(size) → FileUpload (id, tokens, key; no network)
A->>A: saves the FileUpload in its outbox
C->>R: POST /v0/files {id, uploadTokenHash, fetchTokenHash, chunks}
loop every chunk i
C->>R: PUT /v0/files/{id}/chunks/{i} (256 000 bytes) — upload token
end
A->>C: compose_file(ben, &upload, name, mime, thumb) → seq 9
C->>R: MLS blob {"kind":"file","seq":9,"file":…,"fetchToken":…,"key":…,"name":…,"mime":…,"size":…,"thumb":…}
R->>D: fetch
D->>B: Event::File{seq 9, file: FileRef, name, mime, size, thumb}
loop every chunk i
D->>R: GET /v0/files/{id}/chunks/{i} — fetch token
D->>B: open(i) → plaintext, or FileDamaged
end
2.1 Sizes
| Constant | Value | Why |
|---|---|---|
FILE_CHUNK_BYTES |
256 000 ciphertext bytes (250 KiB) | Amicitia's plan (§5.5) and G41: every chunk is this size on the wire, the last one too |
FILE_CHUNK_PLAIN |
255 984 | the chunk minus the 16-byte Poly1305 tag |
MAX_FILE_BYTES |
100 MiB (104 857 600) | Amicitia's plan (§5.5); via Arca for bigger ones later |
MAX_FILE_CHUNKS |
410 | ⌈100 MiB / 255 984⌉ |
MAX_THUMB_BYTES |
32 KiB | a JPEG; it rides in the MLS blob (base64url ≈ 43 KiB, well under 256 KiB) |
MAX_FILE_NAME_CHARS |
255 | what file systems allow |
MAX_MIME_BYTES |
127 | RFC 6838's limit on each half, roughly |
A file of size bytes has chunks = ⌈size / 255 984⌉ chunks. The relay learns chunks, i.e. the
size rounded up to 250 KiB, and nothing finer (G41).
2.2 The encryption
- Key: 32 random bytes from the provider's randomness, made for this one file and never reused.
- AEAD: ChaCha20-Poly1305 (RustCrypto
chacha20poly13050.10, already in the tree throughopenmls_rust_crypto), in STREAM (aead::stream::StreamBE32, Hoang–Reyhanitabar–Rogaway–Vizár): the nonce is a 7-byte prefix (zeros: the key is fresh per file), the chunk's index as a 32-bit big-endian counter, and a byte that is 1 for the last chunk only. So chunk i opens only at position i, and only the true last chunk opens as last: a reordered, repeated or missing chunk, or a file cut short, fails to open. No associated data: a key belongs to one file. - Padding: the file is padded with zero bytes to
chunks × 255 984; the padding is inside the AEAD. The receiver truncates tosizeand checks the padding is zeros. - Deterministic: the same key, index and bytes give the same chunk. An upload interrupted by a crash resumes by putting chunks again; the relay accepts a chunk it already holds when the bytes are the same, and refuses different bytes (409), so a file never changes once a chunk is up.
- No cryptography is written here:
StreamBE32is theaeadcrate's, the cipher RustCrypto's.
2.3 The relay: what it stores (§5) and the routes (§8)
| Table | Columns |
|---|---|
files |
id, hash(upload token), hash(fetch token), chunks, uploaded hour (rounded) |
chunks |
file id, index, ciphertext |
No size column (every chunk is FILE_CHUNK_BYTES), no name, no type, no sender, no recipient, no
fetch count: the relay sees each GET as it serves it, and keeps no count of them.
| Method and path | Auth | Effect |
|---|---|---|
POST /v0/files {id, uploadTokenHash, fetchTokenHash, chunks} |
none | creates a file of chunks (1–410) chunks. 201; 409 if the id is taken; 400 if chunks is out of range; 507 if the relay's file quota would be passed |
PUT /v0/files/{id}/chunks/{index} (exactly 256 000 bytes) |
upload token | stores chunk index. 201; again with the same bytes 201; with other bytes 409; an index at or past chunks 507 (the file's quota, after the token); another size 413 |
GET /v0/files/{id}/chunks/{index} |
fetch token | the chunk's bytes. A chunk not (yet) there is 404, like a wrong token |
DELETE /v0/files/{id} |
upload token | the uploader deletes the file and its chunks early. 204 |
- Uploads only with a token the uploader made: the relay stores only the hashes it was given at
POST; a stranger who learns a file id can neither add, replace nor delete a chunk. - Quotas: per file, at most its declared chunks, each exactly 256 000 bytes, and at most 410;
relay-wide, at most
--max-filesfiles (default 10 000) and--max-file-bytesreserved (default 4 GiB), counting each file aschunks × 256 000from the moment it is created, so an upload under way is never cut off half-way by another. Separate from the blob quota, so files cannot starve messages. Over a limit, a bare 507. Nothing new is stored to count:chunksand the rows are enough. - Purge (hourly, with blobs): a file whose uploaded hour is more than 30 days old goes with its chunks; so does a file still incomplete after 24 hours (an upload abandoned, or a stranger reserving the quota with files never filled).
- 404 for a wrong token, an unknown id, a malformed id or index, as everywhere (§8).
- A refusal has no body (§8), the 413 of a body over a route's limit included. (Found while writing this: axum's own 413 carried a text, on the blob and JSON routes too; every route now answers a bare 413, with a test.)
2.4 The file payload (§4)
{"kind":"file","seq":9,"file":"<22 chars>","fetchToken":"<43>","key":"<43>","name":"Menu.jpg","mime":"image/jpeg","size":2483022,"thumb":"<base64url of a JPEG>"}
filethe relay's file id;fetchTokenthe capability to get its chunks;keythe file key.name1–255 characters, no control character, no/or\;mimetype/subtype, 3–127 printable ASCII bytes with one/and no space;size1 toMAX_FILE_BYTES.thumb(optional) at most 32 KiB, and it must start like a JPEG (FF D8 FF); otherwise the receiver keeps the file and drops the thumbnail, as a text keeps its words and loses a bad reply.- It shares the sender's one
seqcounter; a receipt covers it; a reply and a reaction can name it. - The relay sees a blob of the usual kind, larger by up to the thumbnail.
2.5 What the receiving client checks
| Received | Dropped when | Otherwise |
|---|---|---|
file |
seq 0; a name, type or size outside §2.4; the sender is blocked |
Event::File, its seq counts like a text's; a thumbnail that is too big or not a JPEG is left out |
| a chunk | it is not 256 000 bytes, does not open at its index (or as the last), or the padding is not zeros | ClientError::FileDamaged: the app says the file could not be opened and offers to try again |
Sending checks the same and refuses with ClientError::BadFile (a name, type or size outside |
||
| §2.4, a chunk index outside the file, or plaintext of the wrong length). |
2.6 The client's API (Amicitia pins it)
start_upload(size) -> FileUpload: id, upload token, fetch token, key; no network.FileUploadisSerialize/Deserialize(an app keeps it in its outbox to resume) and itsDebugshows only the chunk count.create_file(&FileUpload),put_chunk(&FileUpload, index, plain),upload(&FileUpload, bytes)(all chunks),delete_file(&FileUpload): the network calls.create_fileafter a crash may meet its own file: 409 is taken as done (the id is 16 random bytes; nobody else made it).FileUpload::seal(index, plain)andFileRef::open(index, chunk): encryption without network.compose_file(contact, &FileUpload, name, mime, thumb) -> Outgoing;sent_seqnames it. The app delivers it after the upload: a recipient who fetches a chunk too early gets 404 and tries again later, but there is no reason to make them.get_chunk(&FileRef, index) -> plainanddownload(&FileRef) -> bytes;FileRef(id, fetch token, key, size) isSerialize/Deserializefor the app's encrypted database;Debughides the token and the key.Event::File { contact, seq, file: FileRef, name, mime, thumb }(the size isfile.size()).Transportgainscreate_file,put_chunk,get_chunk,delete_file;MemoryTransportkeeps files with the relay's rules.- The client keeps no state for files: nothing new in the snapshot (
SNAPSHOT_VERSIONstays 1).
2.7 An older client
A client from F28 knows no file: it has a seq, so it shows Update Amicitia to see this message
(F27's rule). An older relay has no /v0/files: POST is 404, the app says the relay cannot take
files yet and keeps the message unsent.
3. Risks
- Privacy — the relay learns a file id, its chunk count (the size rounded up to 250 KiB, G41), the hour it was created, and — as it serves them — each chunk's fetch and its timing. It does not learn the name, the type, the exact size, the thumbnail or the content, nor who sent it to whom: the file has no mailbox. It can link the upload to the sender's next post, and the fetches to the recipient's, by timing and address (§1's honest limit, already stated for blobs).
- Privacy — members learn the name, type, size and thumbnail, and the content once downloaded. The app strips photo metadata (Amicitia's side; videos keep theirs, G43).
- The fetch token in the message: anyone who holds the MLS message holds the file. That is the recipient — by design. A file forwarded later would need a fresh upload or the same token, a choice for that feature.
- Abuse: anyone can create files, as anyone can create mailboxes. The quotas bound it, the 24-hour
purge of incomplete files frees what was reserved and never filled, and the 30-day purge the rest.
A relay facing the internet is still a resource strangers can fill up to its operator's limits
(
home-relay.md's stated limit). - Integrity: the MLS message authenticates the sender, the key and the size; STREAM authenticates each chunk's content and position and the end of the file. A relay can withhold a chunk (the download fails, retried later), never change one.
- Memory:
uploadanddownloadhold the whole file (up to 100 MiB).put_chunkandget_chunkwork one chunk at a time, for an app that streams to disk. - Offline: the
FileUploadis saved before any network; every step can be repeated.
4. Gaps and designed solutions
| Gap | Design |
|---|---|
| The size reaches the relay, rounded up to 250 KiB (G41) | Same-size chunks (this change); later, padding the chunk count to powers of two |
| A stranger can reserve the file quota with files never filled | Incomplete after 24 h = purged; quotas; the operator's limits |
| The relay sees when a file is fetched, and how often | Not stored; stated in §1/§5. A 1:1 file is fetched once per device |
| Files bigger than 100 MiB | Via Arca (Amicitia's side, when Arca's share capability exists) |
| A group (F32): one upload, many recipients | The same file payload sealed into each member's group; one upload serves all, the relay sees several fetch series of one file |
| Two people on different relays | Not today: a client has one transport, so both sides of a conversation use one relay |
5. Verification
| Case | Test |
|---|---|
| Create, put, get, delete; the routes' status codes | relay/tests/files.rs |
| A put without the upload token, with the fetch token, or with another file's token is 404 | same |
| A get without the fetch token, or with the upload token, is 404 | same |
A chunk of the wrong size is 413; an index past chunks is 507; chunks 0 or 411 is 400 |
same |
| The same chunk again: 201 with the same bytes, 409 with other bytes | same |
| The relay-wide quota (files, reserved bytes) answers 507, and frees on delete and purge | same |
| A file is gone after 30 days; an incomplete one after 24 h; a complete one is kept | same, clock injected |
| The live schema is §5's, the two new tables included | relay/tests/relay.rs |
| Every chunk on the wire is 256 000 bytes, whatever the file's size | client/tests/files.rs |
| A file goes both ways and comes back byte for byte (1 byte, exactly one chunk, one chunk + 1, 3 chunks) | same |
A chunk tampered with, swapped with another, the last one missing or opened as not-last, or from another file: FileDamaged |
same + unit |
The file event carries name, type, size, thumbnail; its seq counts; a receipt covers it; a reaction can name it |
same |
| A malformed name, type or size is refused when sending and dropped when received; a bad thumbnail is dropped and the file kept | unit through receive_payload |
| A blocked contact's file is dropped | unit |
| An F28 client shows a file as Unsupported | F27's rule: a_newer_apps_message_is_unsupported_and_counts_for_receipts (now with sticker) |
| The relay's database holds no file name, type, thumbnail or content (canary, real relay) | client/tests/real_relay.rs |
| Planted bugs | the review |