[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:
@@ -29,6 +29,13 @@ pub const MAX_PUSH_ENDPOINT_BYTES: usize = 1000;
|
||||
pub const WAKE_BODY: &[u8] = b"wake";
|
||||
/// A mailbox is woken at most once in this many seconds.
|
||||
pub const WAKE_EVERY_SECS: i64 = 60;
|
||||
/// Every file chunk on the wire is exactly this many bytes, the last one too (250 KiB; G41,
|
||||
/// `.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;
|
||||
/// A file that is not complete this long after it was created is purged (`files.md` §2.3).
|
||||
pub const INCOMPLETE_FILE_TTL_SECS: i64 = 24 * 3600;
|
||||
|
||||
/// Why a value from the wire was refused. Carries no part of the input.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -148,6 +155,11 @@ fixed_bytes!(
|
||||
InviteId,
|
||||
16
|
||||
);
|
||||
fixed_bytes!(
|
||||
/// A file on the relay: 16 random bytes made by its uploader (`PROTOCOL.md` §8, files).
|
||||
FileId,
|
||||
16
|
||||
);
|
||||
fixed_bytes!(
|
||||
/// A message's author: the 16 random bytes in their MLS credential, made on their device, never
|
||||
/// a name. With the author's `seq` it names a message (`PROTOCOL.md` §4, reactions and replies).
|
||||
@@ -166,6 +178,20 @@ fixed_bytes!(
|
||||
32
|
||||
);
|
||||
|
||||
fixed_bytes!(
|
||||
/// A file's key: 32 random bytes made for one file, travelling only inside MLS (`PROTOCOL.md`
|
||||
/// §4, `file`). The relay never sees it.
|
||||
FileKey,
|
||||
32
|
||||
);
|
||||
|
||||
// A file key is a secret: never printed.
|
||||
impl fmt::Debug for FileKey {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str("FileKey(..)")
|
||||
}
|
||||
}
|
||||
|
||||
impl Token {
|
||||
/// The hash the relay stores and compares against.
|
||||
pub fn hash(&self) -> Sha256Hash {
|
||||
@@ -199,7 +225,7 @@ macro_rules! debug_as_wire {
|
||||
}
|
||||
)*};
|
||||
}
|
||||
debug_as_wire!(MailboxId, InviteId, Sha256Hash, Author);
|
||||
debug_as_wire!(MailboxId, InviteId, Sha256Hash, Author, FileId);
|
||||
|
||||
/// The SHA-256 of a blob: how its recipient acknowledges it.
|
||||
pub fn blob_hash(blob: &[u8]) -> Sha256Hash {
|
||||
@@ -333,6 +359,43 @@ pub struct VersionInfo {
|
||||
pub relay: String,
|
||||
}
|
||||
|
||||
/// How many chunks a file has: 1 to [`MAX_FILE_CHUNKS`]; anything else does not parse.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct ChunkCount(u32);
|
||||
|
||||
impl ChunkCount {
|
||||
pub fn new(n: u32) -> Result<Self, ParseError> {
|
||||
if (1..=MAX_FILE_CHUNKS).contains(&n) {
|
||||
Ok(Self(n))
|
||||
} else {
|
||||
Err(ParseError::TooLarge {
|
||||
max: MAX_FILE_CHUNKS as usize,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for ChunkCount {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
ChunkCount::new(u32::deserialize(d)?).map_err(serde::de::Error::custom)
|
||||
}
|
||||
}
|
||||
|
||||
/// `POST /v0/files`: the uploader sends hashes of the two tokens it made, and the chunk count.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct CreateFile {
|
||||
pub id: FileId,
|
||||
pub upload_token_hash: Sha256Hash,
|
||||
pub fetch_token_hash: Sha256Hash,
|
||||
pub chunks: ChunkCount,
|
||||
}
|
||||
|
||||
/// Rounds unix seconds down to the hour, as the relay stores arrival times.
|
||||
pub fn hour_of(unix_secs: i64) -> i64 {
|
||||
unix_secs.div_euclid(3600)
|
||||
|
||||
Reference in New Issue
Block a user