[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
ci / check (pull_request) Has been cancelled
ci / check (pull_request) Has been cancelled
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.
This commit is contained in:
@@ -51,6 +51,7 @@ impl Http {
|
||||
"200" | "201" | "204" => Ok(resp[split + 4..].to_vec()),
|
||||
"404" => Err(TransportError::NotFound),
|
||||
"409" => Err(TransportError::Conflict),
|
||||
"507" => Err(TransportError::Full),
|
||||
_ => Err(TransportError::Failed),
|
||||
}
|
||||
}
|
||||
@@ -148,6 +149,31 @@ impl Transport for Http {
|
||||
}
|
||||
.map(|_| ())
|
||||
}
|
||||
fn create_file(&self, req: &CreateFile) -> Result<(), TransportError> {
|
||||
let body = serde_json::to_vec(req).map_err(|_| TransportError::Failed)?;
|
||||
self.call("POST", "/v0/files", None, &body, true)
|
||||
.map(|_| ())
|
||||
}
|
||||
fn put_chunk(
|
||||
&self,
|
||||
id: &FileId,
|
||||
upload: &Token,
|
||||
index: u32,
|
||||
chunk: &[u8],
|
||||
) -> Result<(), TransportError> {
|
||||
let path = format!("/v0/files/{}/chunks/{index}", id.to_wire());
|
||||
self.call("PUT", &path, Some(upload), chunk, false)
|
||||
.map(|_| ())
|
||||
}
|
||||
fn get_chunk(&self, id: &FileId, fetch: &Token, index: u32) -> Result<Vec<u8>, TransportError> {
|
||||
let path = format!("/v0/files/{}/chunks/{index}", id.to_wire());
|
||||
self.call("GET", &path, Some(fetch), &[], false)
|
||||
}
|
||||
fn delete_file(&self, id: &FileId, upload: &Token) -> Result<(), TransportError> {
|
||||
let path = format!("/v0/files/{}", id.to_wire());
|
||||
self.call("DELETE", &path, Some(upload), &[], false)
|
||||
.map(|_| ())
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the real relay on 127.0.0.1:<free port> with its database in `db`.
|
||||
@@ -279,6 +305,86 @@ fn pair_and_talk_through_a_real_relay_which_never_sees_a_name_or_a_word() {
|
||||
assert!(waiting.len() >= 4096, "the relay stored something");
|
||||
}
|
||||
|
||||
/// Files (`.agents/features/files.md` §5): a photo through the real relay. Its name, type,
|
||||
/// thumbnail and content are in no byte of the relay's database, before or after the download, and
|
||||
/// every chunk the relay holds is the same size.
|
||||
#[test]
|
||||
fn a_file_goes_through_the_real_relay_which_never_sees_its_name_type_or_content() {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let db = dir.path().join("relay.db");
|
||||
let relay = Http(start_relay(&db));
|
||||
let mut eva = Client::new("Eva", relay.clone()).expect("eva");
|
||||
let mut jan = Client::new("Jan", relay.clone()).expect("jan");
|
||||
eva.set_receipts(false);
|
||||
jan.set_receipts(false);
|
||||
let invite = eva.create_invite().expect("invite");
|
||||
let jan_side = jan.accept_invite(&invite).expect("accept");
|
||||
eva.poll().expect("poll");
|
||||
jan.poll().expect("poll");
|
||||
|
||||
// Two chunks and a bit, every one of them full of canaries, so any plaintext would be found.
|
||||
let line = b"FILE-CONTENT-CANARY-7f3a ";
|
||||
let content: Vec<u8> = line
|
||||
.iter()
|
||||
.copied()
|
||||
.cycle()
|
||||
.take(2 * FILE_CHUNK_PLAIN + 999)
|
||||
.collect();
|
||||
let name = "FILENAME-CANARY-Menu.jpg";
|
||||
let mime = "image/x-mime-canary";
|
||||
let mut thumb = vec![0xFF, 0xD8, 0xFF, 0xE0];
|
||||
thumb.extend_from_slice(b"THUMBNAIL-CANARY");
|
||||
let up = jan.start_upload(content.len() as u64).expect("start");
|
||||
jan.upload(&up, &content).expect("upload");
|
||||
let out = jan
|
||||
.compose_file(jan_side, &up, name, mime, Some(&thumb))
|
||||
.expect("compose");
|
||||
jan.deliver(&out).expect("deliver");
|
||||
let waiting = std::fs::read(&db).expect("db");
|
||||
|
||||
let events = eva.poll().expect("poll");
|
||||
let Some(Event::File {
|
||||
file,
|
||||
name: got_name,
|
||||
mime: got_mime,
|
||||
thumb: got_thumb,
|
||||
seq,
|
||||
..
|
||||
}) = events.first().cloned()
|
||||
else {
|
||||
panic!("no file: {events:?}");
|
||||
};
|
||||
assert_eq!((got_name.as_str(), got_mime.as_str(), seq), (name, mime, 1));
|
||||
assert_eq!(got_thumb.as_deref(), Some(thumb.as_slice()));
|
||||
assert_eq!(file.chunks(), 3);
|
||||
assert_eq!(eva.download(&file).expect("download"), content);
|
||||
let after = std::fs::read(&db).expect("db");
|
||||
|
||||
for snapshot in [&waiting, &after] {
|
||||
for (what, needle) in [
|
||||
("file content", &line[..]),
|
||||
("file name", name.as_bytes()),
|
||||
("media type", mime.as_bytes()),
|
||||
("thumbnail", b"THUMBNAIL-CANARY".as_slice()),
|
||||
] {
|
||||
assert!(
|
||||
!contains(snapshot, needle),
|
||||
"{what} is readable in the relay's database"
|
||||
);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
waiting.len() >= 3 * FILE_CHUNK_BYTES,
|
||||
"the three chunks were stored"
|
||||
);
|
||||
// The sender deletes it early; the relay answers 404 from then on.
|
||||
jan.delete_file(&up).expect("delete");
|
||||
assert_eq!(
|
||||
eva.get_chunk(&file, 0),
|
||||
Err(ClientError::Transport(TransportError::NotFound))
|
||||
);
|
||||
}
|
||||
|
||||
// Wake-up (`.agents/features/wake-up.md` §2.4): an endpoint per mailbox, set and cleared through the
|
||||
// real relay with the mailbox's fetch token.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user