[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

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:
2026-11-22 19:29:37 +01:00
parent 4d0dac0591
commit 3a0944ebff
20 changed files with 2352 additions and 42 deletions
+98 -7
View File
@@ -4,6 +4,7 @@
use crate::store::{Refused, Store};
use crate::wake::Waker;
use axum::body::Bytes;
use axum::extract::rejection::BytesRejection;
use axum::extract::{DefaultBodyLimit, Path, State};
use axum::http::{header, HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
@@ -12,8 +13,9 @@ use axum::{Json, Router};
use serde::de::DeserializeOwned;
use std::sync::Arc;
use tabellarius_protocol::{
CreateInvite, CreateMailbox, FetchedBlobs, InviteId, MailboxId, RotatePostToken, SetPush,
Sha256Hash, Token, VersionInfo, MAX_BLOB_BYTES, VERSION,
CreateFile, CreateInvite, CreateMailbox, FetchedBlobs, FileId, InviteId, MailboxId,
RotatePostToken, SetPush, Sha256Hash, Token, VersionInfo, FILE_CHUNK_BYTES, MAX_BLOB_BYTES,
VERSION,
};
/// Unix seconds. The server passes the system clock; tests pass their own.
@@ -49,17 +51,27 @@ pub fn router(state: AppState) -> Router {
let blob_routes = Router::new()
.route("/v0/mailboxes/{id}/blobs", post(post_blob).get(fetch_blobs))
.layer(DefaultBodyLimit::max(MAX_BLOB_BYTES));
// A chunk is exactly FILE_CHUNK_BYTES; one byte more is refused before it is read.
let chunk_routes = Router::new()
.route(
"/v0/files/{id}/chunks/{index}",
put(put_chunk).get(get_chunk),
)
.layer(DefaultBodyLimit::max(FILE_CHUNK_BYTES));
Router::new()
.route("/v0/mailboxes", post(create_mailbox))
.route("/v0/mailboxes/{id}/blobs/{hash}", delete(acknowledge))
.route("/v0/mailboxes/{id}/post-token", put(rotate_post_token))
.route("/v0/mailboxes/{id}/push", put(set_push).delete(clear_push))
.route("/v0/files", post(create_file))
.route("/v0/files/{id}", delete(delete_file))
.route("/v0/invites", post(create_invite))
.route("/v0/invites/{id}", get(claim_invite))
.route("/version", get(version))
.route("/join", get(crate::join::page))
.layer(DefaultBodyLimit::max(MAX_JSON_BYTES))
.merge(blob_routes)
.merge(chunk_routes)
.with_state(state)
}
@@ -84,6 +96,11 @@ impl From<Refused> for Status {
}
}
/// 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> {
body.map_err(|_| Status(StatusCode::PAYLOAD_TOO_LARGE))
}
/// Parses a JSON body with the protocol's strict types; any failure is a plain 400.
fn json<T: DeserializeOwned>(body: &Bytes) -> Result<T, Status> {
serde_json::from_slice(body).map_err(|_| Status(StatusCode::BAD_REQUEST))
@@ -103,7 +120,11 @@ fn bearer(headers: &HeaderMap) -> Result<Token, Status> {
.ok_or(Status(StatusCode::NOT_FOUND))
}
async fn create_mailbox(State(s): State<AppState>, body: Bytes) -> Result<StatusCode, Status> {
async fn create_mailbox(
State(s): State<AppState>,
body: Result<Bytes, BytesRejection>,
) -> Result<StatusCode, Status> {
let body = bare(body)?;
let req: CreateMailbox = json(&body)?;
s.store.create_mailbox(&req, (s.clock)())?;
Ok(StatusCode::CREATED)
@@ -113,8 +134,9 @@ async fn post_blob(
State(s): State<AppState>,
Path(id): Path<String>,
headers: HeaderMap,
body: Bytes,
body: Result<Bytes, BytesRejection>,
) -> Result<StatusCode, Status> {
let body = bare(body)?;
let id = mailbox(&id)?;
let token = bearer(&headers)?;
let now = (s.clock)();
@@ -153,8 +175,9 @@ async fn rotate_post_token(
State(s): State<AppState>,
Path(id): Path<String>,
headers: HeaderMap,
body: Bytes,
body: Result<Bytes, BytesRejection>,
) -> Result<StatusCode, Status> {
let body = bare(body)?;
let id = mailbox(&id)?;
let token = bearer(&headers)?;
let req: RotatePostToken = json(&body)?;
@@ -167,8 +190,9 @@ async fn set_push(
State(s): State<AppState>,
Path(id): Path<String>,
headers: HeaderMap,
body: Bytes,
body: Result<Bytes, BytesRejection>,
) -> Result<StatusCode, Status> {
let body = bare(body)?;
let id = mailbox(&id)?;
let token = bearer(&headers)?;
let req: SetPush = json(&body)?;
@@ -190,7 +214,74 @@ async fn clear_push(
Ok(StatusCode::NO_CONTENT)
}
async fn create_invite(State(s): State<AppState>, body: Bytes) -> Result<StatusCode, Status> {
/// A malformed file id in the path is treated like an unknown one.
fn file(id: &str) -> Result<FileId, Status> {
FileId::parse(id).map_err(|_| Status(StatusCode::NOT_FOUND))
}
/// A chunk index: decimal digits of a `u32`, no sign, no leading zero. Anything else is a 404.
fn chunk_index(text: &str) -> Result<u32, Status> {
let canonical = !text.is_empty()
&& text.bytes().all(|b| b.is_ascii_digit())
&& (text == "0" || !text.starts_with('0'));
text.parse::<u32>()
.ok()
.filter(|_| canonical)
.ok_or(Status(StatusCode::NOT_FOUND))
}
async fn create_file(
State(s): State<AppState>,
body: Result<Bytes, BytesRejection>,
) -> Result<StatusCode, Status> {
let body = bare(body)?;
let req: CreateFile = json(&body)?;
s.store.create_file(&req, (s.clock)())?;
Ok(StatusCode::CREATED)
}
async fn put_chunk(
State(s): State<AppState>,
Path((id, index)): Path<(String, String)>,
headers: HeaderMap,
body: Result<Bytes, BytesRejection>,
) -> Result<StatusCode, Status> {
let body = bare(body)?;
let id = file(&id)?;
let index = chunk_index(&index)?;
let token = bearer(&headers)?;
s.store.put_chunk(&id, &token, index, &body)?;
Ok(StatusCode::CREATED)
}
async fn get_chunk(
State(s): State<AppState>,
Path((id, index)): Path<(String, String)>,
headers: HeaderMap,
) -> Result<Response, Status> {
let id = file(&id)?;
let index = chunk_index(&index)?;
let token = bearer(&headers)?;
let bytes = s.store.get_chunk(&id, &token, index)?;
Ok(([(header::CONTENT_TYPE, "application/octet-stream")], bytes).into_response())
}
async fn delete_file(
State(s): State<AppState>,
Path(id): Path<String>,
headers: HeaderMap,
) -> Result<StatusCode, Status> {
let id = file(&id)?;
let token = bearer(&headers)?;
s.store.delete_file(&id, &token)?;
Ok(StatusCode::NO_CONTENT)
}
async fn create_invite(
State(s): State<AppState>,
body: Result<Bytes, BytesRejection>,
) -> Result<StatusCode, Status> {
let body = bare(body)?;
let req: CreateInvite = json(&body)?;
s.store.create_invite(&req, (s.clock)())?;
Ok(StatusCode::CREATED)