Flags were stored via Debug formatting of async-imap's Flag enum, which
produced Rust-internal spellings ("Seen", "Custom(\"$Junk\")") that don't
round-trip back through Flag::from and aren't what the schema documents.
Map to canonical wire spelling (\Seen, $Junk) before the webapp starts
depending on it, with tests pinning the format.
Adds Gitea Actions CI/release workflows, an nginx rate-limiting config
for the §7 gap, and a shared release-process doc. Records the decision
not to reconcile server-side flag changes or deletions, with the
conditions that should trigger revisiting it.
Also applies rustfmt and clears clippy, so CI passes from the first run.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
3.8 KiB
nginx rate limiting
Covers the rate-limiting half of server-architecture-spec.md §7 ("Rate-limit login/API auth endpoints — can partly live at the nginx layer too").
Doing this in nginx rather than in the API keeps the abuse traffic from ever reaching axum or Postgres, which is the point: a login flood should be rejected before it costs an Argon2 verification. Argon2 is deliberately expensive (that's what makes it a good password hash), so an unthrottled /api/auth/login is a cheap way for an attacker to burn all your CPU. That makes the auth limiter below a DoS control as much as a credential-stuffing one.
Zones
Add to the http block (i.e. nginx.conf proper, not the server block in conf.d/default.conf):
# ~16k unique client IPs per 10m of shared memory.
limit_req_zone $binary_remote_addr zone=tern_auth:10m rate=5r/m;
limit_req_zone $binary_remote_addr zone=tern_api:10m rate=30r/s;
limit_conn_zone $binary_remote_addr zone=tern_conn:10m;
# Return 429 rather than nginx's default 503, which clients misread as
# "server down" and retry against.
limit_req_status 429;
limit_conn_status 429;
Server block
server {
listen 80;
server_name _;
# Auth endpoints: strict. 5/min sustained with a small burst, so a
# legitimate typo-and-retry works but credential stuffing doesn't.
location ~ ^/api/auth/(login|register)$ {
limit_req zone=tern_auth burst=3 nodelay;
proxy_pass http://server:8080;
include /etc/nginx/proxy_params_tern;
}
# Everything else: generous, just a runaway-client backstop. A mail
# client legitimately bursts (opening a thread fetches many messages).
location /api/ {
limit_req zone=tern_api burst=60 nodelay;
limit_conn tern_conn 20;
proxy_pass http://server:8080;
include /etc/nginx/proxy_params_tern;
}
# WebSocket connections are long-lived: rate-limit the handshake, but
# never apply a read timeout that would kill an idle-but-healthy socket.
location /api/ws {
limit_req zone=tern_api burst=10 nodelay;
proxy_pass http://server:8080;
include /etc/nginx/proxy_params_tern;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
location /healthz {
proxy_pass http://server:8080;
access_log off;
}
}
With /etc/nginx/proxy_params_tern:
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
If nginx sits behind another proxy
$binary_remote_addr is the direct peer. If there's a CDN or another reverse proxy in front, every request appears to come from one IP and the limiter will throttle all users collectively the moment anyone trips it. In that case set up real_ip:
set_real_ip_from 10.0.0.0/8; # the upstream proxy's range, never 0.0.0.0/0
real_ip_header X-Forwarded-For;
real_ip_recursive on;
Only trust X-Forwarded-For from proxies you actually control — an attacker who can set that header against a server that trusts it can rotate their apparent IP per request and bypass the limiter entirely.
What this does not cover
Per-IP limiting is a blunt instrument. It doesn't stop a slow distributed attack (one attempt per IP across many hosts), and it can punish many legitimate users behind one NAT. So it complements, rather than replaces, an application-level limiter keyed on the username being targeted — worth adding in the API when auth work is next touched. Tracked as a follow-up rather than done here, since the nginx layer is the cheap 80% and the spec explicitly scopes it as "partly".