[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:
@@ -5,7 +5,8 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tabellarius_protocol::{
|
||||
blob_hash, CreateInvite, CreateMailbox, InviteId, MailboxId, PushEndpoint, Sha256Hash, Token,
|
||||
blob_hash, CreateFile, CreateInvite, CreateMailbox, FileId, InviteId, MailboxId, PushEndpoint,
|
||||
Sha256Hash, Token, FILE_CHUNK_BYTES,
|
||||
};
|
||||
|
||||
/// Why the relay said no. Carries nothing of the request.
|
||||
@@ -13,8 +14,10 @@ use tabellarius_protocol::{
|
||||
pub enum TransportError {
|
||||
/// 404: unknown, wrong token, expired, or already claimed.
|
||||
NotFound,
|
||||
/// 409: the id is taken.
|
||||
/// 409: the id is taken, or a file chunk is already there with other bytes.
|
||||
Conflict,
|
||||
/// 507: a quota is reached — the relay's, a mailbox's, or a file's (`PROTOCOL.md` §8).
|
||||
Full,
|
||||
/// Anything else: the network, a 5xx, a body that did not parse.
|
||||
Failed,
|
||||
}
|
||||
@@ -49,6 +52,28 @@ pub trait Transport {
|
||||
fetch: &Token,
|
||||
endpoint: Option<&PushEndpoint>,
|
||||
) -> Result<(), TransportError>;
|
||||
/// `POST /v0/files` (`.agents/features/files.md` §2.3).
|
||||
fn create_file(&self, req: &CreateFile) -> Result<(), TransportError>;
|
||||
/// `PUT /v0/files/{id}/chunks/{index}` with the upload token.
|
||||
fn put_chunk(
|
||||
&self,
|
||||
id: &FileId,
|
||||
upload: &Token,
|
||||
index: u32,
|
||||
chunk: &[u8],
|
||||
) -> Result<(), TransportError>;
|
||||
/// `GET /v0/files/{id}/chunks/{index}` with the fetch token.
|
||||
fn get_chunk(&self, id: &FileId, fetch: &Token, index: u32) -> Result<Vec<u8>, TransportError>;
|
||||
/// `DELETE /v0/files/{id}` with the upload token.
|
||||
fn delete_file(&self, id: &FileId, upload: &Token) -> Result<(), TransportError>;
|
||||
}
|
||||
|
||||
/// A file as the relay keeps it: the two hashes, the chunk count, and the chunks by index.
|
||||
struct StoredFile {
|
||||
upload: Sha256Hash,
|
||||
fetch: Sha256Hash,
|
||||
chunks: u32,
|
||||
held: HashMap<u32, Vec<u8>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -56,6 +81,7 @@ struct Boxes {
|
||||
mailboxes: HashMap<MailboxId, (Sha256Hash, Sha256Hash, Vec<Vec<u8>>)>,
|
||||
invites: HashMap<InviteId, (Sha256Hash, Vec<u8>)>,
|
||||
push: HashMap<MailboxId, PushEndpoint>,
|
||||
files: HashMap<FileId, StoredFile>,
|
||||
}
|
||||
|
||||
/// An in-memory relay with the same rules as the real one (no expiry: there is no clock here).
|
||||
@@ -82,7 +108,17 @@ impl MemoryTransport {
|
||||
self.boxes().push.get(id).map(|e| e.as_str().to_string())
|
||||
}
|
||||
|
||||
/// Every byte the relay holds, for privacy tests: ids, hashes, blobs, KeyPackages, endpoints.
|
||||
/// How many chunks the relay holds, in all files.
|
||||
pub fn stored_chunks(&self) -> Vec<Vec<u8>> {
|
||||
self.boxes()
|
||||
.files
|
||||
.values()
|
||||
.flat_map(|f| f.held.values().cloned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Every byte the relay holds, for privacy tests: ids, hashes, blobs, KeyPackages, endpoints,
|
||||
/// files and their chunks.
|
||||
pub fn everything_stored(&self) -> Vec<u8> {
|
||||
let b = self.boxes();
|
||||
let mut out = Vec::new();
|
||||
@@ -101,6 +137,12 @@ impl MemoryTransport {
|
||||
out.extend_from_slice(id.as_bytes());
|
||||
out.extend_from_slice(endpoint.as_str().as_bytes());
|
||||
}
|
||||
for (id, f) in &b.files {
|
||||
out.extend_from_slice(id.as_bytes());
|
||||
out.extend_from_slice(f.upload.as_bytes());
|
||||
out.extend_from_slice(f.fetch.as_bytes());
|
||||
f.held.values().for_each(|c| out.extend_from_slice(c));
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
@@ -208,4 +250,69 @@ impl Transport for MemoryTransport {
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_file(&self, req: &CreateFile) -> Result<(), TransportError> {
|
||||
let mut b = self.boxes();
|
||||
if b.files.contains_key(&req.id) {
|
||||
return Err(TransportError::Conflict);
|
||||
}
|
||||
b.files.insert(
|
||||
req.id,
|
||||
StoredFile {
|
||||
upload: req.upload_token_hash,
|
||||
fetch: req.fetch_token_hash,
|
||||
chunks: req.chunks.get(),
|
||||
held: HashMap::new(),
|
||||
},
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn put_chunk(
|
||||
&self,
|
||||
id: &FileId,
|
||||
upload: &Token,
|
||||
index: u32,
|
||||
chunk: &[u8],
|
||||
) -> Result<(), TransportError> {
|
||||
if chunk.len() != FILE_CHUNK_BYTES {
|
||||
return Err(TransportError::Failed);
|
||||
}
|
||||
let mut b = self.boxes();
|
||||
let f = match b.files.get_mut(id) {
|
||||
Some(f) if f.upload == upload.hash() => f,
|
||||
_ => return Err(TransportError::NotFound),
|
||||
};
|
||||
if index >= f.chunks {
|
||||
return Err(TransportError::Full);
|
||||
}
|
||||
match f.held.get(&index) {
|
||||
Some(held) if held.as_slice() == chunk => Ok(()),
|
||||
Some(_) => Err(TransportError::Conflict),
|
||||
None => {
|
||||
f.held.insert(index, chunk.to_vec());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_chunk(&self, id: &FileId, fetch: &Token, index: u32) -> Result<Vec<u8>, TransportError> {
|
||||
match self.boxes().files.get(id) {
|
||||
Some(f) if f.fetch == fetch.hash() => {
|
||||
f.held.get(&index).cloned().ok_or(TransportError::NotFound)
|
||||
}
|
||||
_ => Err(TransportError::NotFound),
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_file(&self, id: &FileId, upload: &Token) -> Result<(), TransportError> {
|
||||
let mut b = self.boxes();
|
||||
match b.files.get(id) {
|
||||
Some(f) if f.upload == upload.hash() => {
|
||||
b.files.remove(id);
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(TransportError::NotFound),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user