[relay] Files: same-size encrypted chunks through the relay, a file payload inside MLS (Amicitia F29) #23
Reference in New Issue
Block a user
Delete Branch "feature/files"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Amicitia F29, step 4 of its full-chat plan: photos, videos and files, end-to-end encrypted. A protocol change: the relay stores two new tables (
docs/PROTOCOL.md§5) behind four new routes (§8). Analysis:.agents/features/files.md.What changes
filesroute.POST /v0/files{id, uploadTokenHash, fetchTokenHash, chunks};PUT /v0/files/{id}/chunks/{index}(exactly 256 000 bytes, upload token);GET …/chunks/{index}(fetch token);DELETE /v0/files/{id}(upload token). Tablesfiles (id, upload_hash, fetch_hash, chunks, uploaded_hour)andchunks (file_id, idx, ciphertext): no size column (every chunk is the same size), no name, type, sender, recipient, and no fetch count.--max-files(10 000) and--max-file-bytes(4 GiB), each file counted at full size from creation so an upload is never cut off; apart from the blob quota.aead::stream::StreamBE32: index counter + last-chunk flag). So every chunk is 256 000 bytes on the wire, and a chunk tampered with, swapped, repeated, cut, or from another file does not open (ClientError::FileDamaged).chacha20poly13050.10 was already in the tree throughopenmls_rust_crypto; itsstreamfeature adds no crate (Cargo.lock: one new edge).filepayload in MLS:seq(shared withtext/poll),file,fetchToken,key,name,mime,size,thumb? (JPEG ≤ 32 KiB). The key, name, type, exact size and thumbnail travel only here. API:start_upload,create_file,put_chunk,upload,delete_file,compose_file,get_chunk,download,FileUpload,FileRef,Event::File;Transportgains four methods;TransportError::Full(507).relay.rs).Tests (cargo test --workspace --locked, quoted per crate)
filesmodule)New in this PR: client unit +10 (7 in
files, 3 inlib),client/tests/files.rs8,real_relay.rs+1 (the canary),protocol/tests/wire.rs+2,relay/tests/files.rs8;relay.rsandquotas.rsextended (schema, bare 413, flags).Also:
cargo fmt --all --checkclean;cargo clippy --workspace --all-targets --locked -- -D warningsclean;scripts/check-deps.shok (relay does not depend on client; protocol on nothing);scripts/test-hooks.sh7 passed, 0 failed.Not verified
--max-files,--max-file-bytes) is not run (as F23's VM test).The adversarial review is posted in this PR.
Adversarial self-review — F29 files (protocol change)
Evidence
cargo test --workspace --lockedon3a0944e: tabellarius-client 76 passed, 0 failed (unit 24, block 7, files 8, pairing 8, polls 4, push 3, reactions 5, real_relay 3, state 14); tabellarius-protocol 20 passed, 0 failed (wire 20); tabellarius-relay 46 passed, 0 failed (unit 2, files 8, quotas 8, relay 19, wake 9).cargo fmt --all --checkclean;cargo clippy --workspace --all-targets --locked -- -D warningsclean;scripts/check-deps.shok;scripts/test-hooks.sh7 passed, 0 failed.--offline);chacha20poly13050.10.1 was already inCargo.lockthroughopenmls_rust_crypto; thestreamfeature adds no crate.Planted bugs — 14 of 14 caught (each applied alone, the named tests run, the file restored)
only_the_upload_token_puts_and_deletes_and_only_the_fetch_token_getsand 5 morea_chunk_is_exactly_the_chunk_size_and_never_changesfiles_go_after_30_days_and_incomplete_ones_after_a_daythe_relay_wide_file_quota_counts_each_file_at_full_size_from_its_creationa_file_holds_only_its_declared_chunks_and_at_most_the_protocolsnothing_of_a_file_is_left_after_it_is_deleted_or_purged(the first run found no test for this:getalready 404s once the file row is gone, so orphaned chunks were invisible. The test was added, then the bug was caught.)every_chunk_is_the_same_size_and_opens_back_to_the_filethe_padding_must_be_zeros_and_the_size_must_matcha_file_goes_through_the_real_relay_which_never_sees_its_name_type_or_content(the canary)a_blocked_contacts_file_is_droppeda_bad_thumbnail_is_dropped_and_the_file_kepta_file_is_a_message_a_receipt_covers_it_and_a_reaction_or_reply_can_name_itfilemissing from KNOWN_KINDS →every_payload_kind_is_known,an_unknown_kind_needs_a_seq_and_a_known_kind_must_parse_exactlyFindings (each has a line comment)
downloadreserves the claimed size (≤ 100 MiB) before the first chunk; a peer can make a receiver allocate that. Bounded;get_chunkstreams for apps that care.Privacy surface checked by hand
schema_is_exactly_protocol_section_5test updated); no fetch count, no size, no name.main.rsprints only its start line.DebugofFileUpload,FileRef,FileKeyshows no token or key (unit test).Not verified
@@ -0,0 +35,4 @@fn stream(key: &FileKey) -> StreamBE32<ChaCha20Poly1305> {// The nonce prefix is zeros: a key seals one file only, so a (key, nonce) pair never repeats.let prefix = Default::default();Finding 4 (accepted): a zero 7-byte STREAM prefix and empty associated data. Safe because a key is made for one file and never reused (
start_upload); a later forward feature that reuses a key must keep the same chunks, never re-seal. Written in PROTOCOL.md §4 A file's chunks.@@ -793,0 +863,4 @@/// Seals and puts chunk `index` ([`FileUpload::seal`]). Again with the same bytes, the relay/// accepts it: an upload resumes chunk by chunk.////// **The bytes must be the same on every try.** A chunk is sealed under the file's one key atFinding 3 (accepted, documented):
sealis deterministic in (key, index), which is what makes a resumed upload idempotent — but two different plaintexts for one index under the same key reuse a ChaCha20-Poly1305 nonce (keystream and Poly1305 key). The relay refuses the second with 409, yet it has received both bodies. Can happen if an app re-reads a source file that changed between tries. The doc says so; Amicitia stores the parts with the upload and never re-reads. A misuse-resistant AEAD (SIV) would remove the trap; not in the tree today.@@ -793,0 +947,4 @@}/// Fetches and opens every chunk: the whole file, exactly `size` bytes.pub fn download(&self, file: &FileRef) -> Result<Vec<u8>, ClientError> {Finding 6 (note):
downloadreservesfile.sizebytes (at most 100 MiB, checked when the payload arrived) before the first chunk; a peer can make the receiver allocate that much for nothing. Bounded, and the app decides when to call it (Amicitia: automatically only ≤ 10 MiB).get_chunkis there for chunk-by-chunk use.@@ -32,0 +33,4 @@/// `.agents/features/files.md` §2.1).pub const FILE_CHUNK_BYTES: usize = 256_000;/// The most chunks one file may have: 100 MiB of plaintext at 255 984 bytes a chunk.pub const MAX_FILE_CHUNKS: u32 = 410;Finding 7 (note): the relay allows 410 chunks = 104 953 440 plaintext bytes; the client refuses over 100 MiB = 104 857 600. The relay is the looser side by under 100 KB; nothing depends on the two matching. A wire test pins that 410 chunks hold 100 MiB and 409 do not.
@@ -85,2 +97,4 @@}/// A body over the route's limit is axum's rejection, which carries a text: a bare 413 instead.fn bare(body: Result<Bytes, BytesRejection>) -> Result<Bytes, Status> {Finding 1 (fixed): before this, a body over a route's
DefaultBodyLimitgot axum's own 413 with a text body — on the blob route and the JSON routes too, soPROTOCOL.md§8's A refusal has no body was not true. Every handler now takesResult<Bytes, BytesRejection>and answers a bare 413;a_blob_over_256_kib_is_refusedchecks the body is empty for a blob and for a 64 KiB JSON body.@@ -410,0 +460,4 @@})?;let reserved =(chunks.unwrap_or(0) + i64::from(req.chunks.get())) * FILE_CHUNK_BYTES as i64;if files >= self.limits.files || reserved > self.limits.file_bytes {Finding 2 (accepted, documented as G57):
POST /v0/filesneeds no token, and a file reserveschunks × 256 000from creation (so an upload in progress is never cut off). About 40 empty 410-chunk files hold a default relay's 4 GiB until the 24-hour purge of incomplete files. As cheap as filling the blob quota today (F23's stated limit); the operator's flags bound it. Later: a shorter deadline for incomplete files, or reserving per chunk that arrives.@@ -85,2 +96,3 @@Nothing else. No IP, no user agent, no request log beyond counts. *(2026-11-20:)* the relay alsoNothing else. *(2026-11-22:)* a file has no mailbox, no sender and no recipient, and the relaykeeps **no count of fetches**: it sees each one as it serves it. A file is deleted when itsFinding 5 (accepted, stated): no fetch count is stored, but the relay sees each GET as it serves it, and the upload is followed by a blob post to the recipient's mailbox from the same address within seconds — so an operator who logs at the network layer links sender, file and recipient by timing. Same class as §1's existing honest limit; files.md §3 says it.
Merged with
--no-ffas7f46bc5156onmain(remote hash verified). The review is above (14/14 planted bugs caught; 7 findings: one fixed here, four accepted and documented, two notes).Pull request closed