[relay] F23: quotas (507), a NixOS module behind Caddy with TLS and no logs, and a VM test that no address is kept

This commit is contained in:
2026-11-16 11:39:18 +01:00
parent 2a8daa7570
commit dd576f9feb
11 changed files with 773 additions and 11 deletions
+80 -5
View File
@@ -48,6 +48,36 @@ pub enum Refused {
TooLarge,
/// The database failed; nothing about the request is in the error.
Storage,
/// A quota is reached (`PROTOCOL.md` §8, 507): the mailbox's, after its token, or the relay's.
Full,
}
/// How much the relay holds before it answers [`Refused::Full`]. The per-mailbox caps are the
/// protocol's; the relay-wide ones are the operator's (`.agents/features/home-relay.md` §2).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limits {
pub mailbox_blobs: i64,
pub mailbox_bytes: i64,
pub total_bytes: i64,
pub mailboxes: i64,
pub invites: i64,
}
/// Waiting blobs in one mailbox (`PROTOCOL.md` §8).
pub const MAILBOX_BLOBS: i64 = 1_000;
/// Waiting bytes in one mailbox (`PROTOCOL.md` §8).
pub const MAILBOX_BYTES: i64 = 32 * 1024 * 1024;
impl Default for Limits {
fn default() -> Self {
Limits {
mailbox_blobs: MAILBOX_BLOBS,
mailbox_bytes: MAILBOX_BYTES,
total_bytes: 1024 * 1024 * 1024,
mailboxes: 100_000,
invites: 10_000,
}
}
}
impl From<rusqlite::Error> for Refused {
@@ -58,6 +88,7 @@ impl From<rusqlite::Error> for Refused {
pub struct Store {
conn: Mutex<Connection>,
limits: Limits,
}
/// What a purge removed.
@@ -73,23 +104,39 @@ fn same_hash(stored: &[u8], presented: &Sha256Hash) -> bool {
impl Store {
pub fn open(path: &Path) -> Result<Self, rusqlite::Error> {
Self::init(Connection::open(path)?)
Self::init(Connection::open(path)?, Limits::default())
}
pub fn open_with(path: &Path, limits: Limits) -> Result<Self, rusqlite::Error> {
Self::init(Connection::open(path)?, limits)
}
pub fn open_in_memory() -> Result<Self, rusqlite::Error> {
Self::init(Connection::open_in_memory()?)
Self::init(Connection::open_in_memory()?, Limits::default())
}
fn init(conn: Connection) -> Result<Self, rusqlite::Error> {
pub fn open_in_memory_with(limits: Limits) -> Result<Self, rusqlite::Error> {
Self::init(Connection::open_in_memory()?, limits)
}
fn init(conn: Connection, limits: Limits) -> Result<Self, rusqlite::Error> {
// Deleted rows are overwritten in the file, and there is no -wal copy of anything.
conn.pragma_update(None, "secure_delete", "ON")?;
conn.pragma_update(None, "journal_mode", "DELETE")?;
conn.execute_batch(SCHEMA)?;
Ok(Self {
conn: Mutex::new(conn),
limits,
})
}
/// One number from the database: a count or a sum, 0 for none.
fn number(conn: &Connection, sql: &str, args: impl rusqlite::Params) -> Result<i64, Refused> {
Ok(conn
.query_row(sql, args, |r| r.get::<_, Option<i64>>(0))?
.unwrap_or(0))
}
fn conn(&self) -> std::sync::MutexGuard<'_, Connection> {
// A panic while holding the lock leaves SQLite consistent (every write is one statement or
// one transaction), so a poisoned lock is still safe to use.
@@ -97,7 +144,11 @@ impl Store {
}
pub fn create_mailbox(&self, req: &CreateMailbox, now: i64) -> Result<(), Refused> {
let changed = self.conn().execute(
let conn = self.conn();
if Self::number(&conn, "SELECT count(*) FROM mailboxes", [])? >= self.limits.mailboxes {
return Err(Refused::Full);
}
let changed = conn.execute(
"INSERT OR IGNORE INTO mailboxes (id, post_hash, fetch_hash, created_day)
VALUES (?1, ?2, ?3, ?4)",
params![
@@ -150,6 +201,20 @@ impl Store {
}
let conn = self.conn();
Self::authorize(&conn, id, token, Column::Post)?;
// After the token: a stranger learns nothing about this mailbox from a 507.
let size = blob.len() as i64;
let (count, bytes): (i64, Option<i64>) = conn.query_row(
"SELECT count(*), sum(size) FROM blobs WHERE mailbox_id = ?1",
[&id.as_bytes()[..]],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
let total = Self::number(&conn, "SELECT sum(size) FROM blobs", [])?;
if count >= self.limits.mailbox_blobs
|| bytes.unwrap_or(0) + size > self.limits.mailbox_bytes
|| total + size > self.limits.total_bytes
{
return Err(Refused::Full);
}
conn.execute(
"INSERT INTO blobs (mailbox_id, ciphertext, size, received_hour) VALUES (?1, ?2, ?3, ?4)",
params![&id.as_bytes()[..], blob, blob.len() as i64, hour_of(now)],
@@ -231,7 +296,17 @@ impl Store {
pub fn create_invite(&self, req: &CreateInvite, now: i64) -> Result<(), Refused> {
// Expiry rounded up to the hour: the exact second the invite was made is not kept.
let expiry = (hour_of(now + INVITE_TTL_SECS) + 1) * 3600;
let changed = self.conn().execute(
let conn = self.conn();
// Expired invites wait for the hourly purge; they do not count against new ones.
let waiting = Self::number(
&conn,
"SELECT count(*) FROM invites WHERE expiry > ?1",
[now],
)?;
if waiting >= self.limits.invites {
return Err(Refused::Full);
}
let changed = conn.execute(
"INSERT OR IGNORE INTO invites (id, secret_hash, key_package, expiry) VALUES (?1, ?2, ?3, ?4)",
params![
&req.id.as_bytes()[..],