fix(hub): group IPv6 sources by /64 and reserve email budget for resends

Per-source limits no longer dodgeable by rotating IPv6 host bits, and a
separate global reserve lets browsers with a live attempt resend even when
fresh starts exhaust the hourly budget.
This commit is contained in:
Loki 2026-10-24 17:45:36 +02:00
parent cca084b0ae
commit 6ea14d0ec2
Signed by: boba
GPG key ID: 253067914055423B
6 changed files with 140 additions and 240 deletions

View file

@ -49,7 +49,7 @@ Eight digits from the OS CSPRNG with rejection sampling, valid for ten minutes,
### Rate limits (SQLite ledger of MACs, atomic check-and-record)
Per address 1 per 60 s and 5 per hour; per source (`X-Forwarded-For`, as GitHub login) 10 per hour; globally 300 per hour. Budget is spent when a request passes validation, even if delivery later fails. The in-memory per-IP limiter used by GitHub (20 per minute, 300 global) also applies to both endpoints. At most 4096 attempts are pending.
Per address 1 per 60 s and 5 per hour; per source (`X-Forwarded-For`, as GitHub login) 10 per hour, where IPv6 sources are grouped by /64 and IPv4-mapped IPv6 counts as IPv4 (so rotating host bits does not dodge the limit; an attacker with a larger prefix or many networks still can); globally 300 per hour for new sign-ins plus a separate reserve of 100 per hour that only resends can spend, meaning requests carrying the `__Host-vnox_email` cookie of a still-live pending attempt. Flooding the general budget therefore cannot cut off people who are already mid sign-in. It does not protect people who start after the budget is exhausted, and an attacker who first collects live attempts can spend the reserve too (bounded by the per-address and per-source limits). Budget is spent when a request passes validation, even if delivery later fails. The in-memory per-IP limiter used by GitHub (20 per minute, 300 global) also applies to both endpoints. At most 4096 attempts are pending.
### Address handling and accounts

View file

@ -48,6 +48,10 @@ pub struct EmailLimits {
pub per_address_hour: i64,
pub per_source_hour: i64,
pub global_hour: i64,
/// Extra global budget usable only by resends from a browser that already holds a
/// live attempt, so exhausting `global_hour` with fresh starts cannot lock out people
/// who are mid sign-in.
pub global_resend_reserve: i64,
}
impl Default for EmailLimits {
@ -57,6 +61,7 @@ impl Default for EmailLimits {
per_address_hour: 5,
per_source_hour: 10,
global_hour: 300,
global_resend_reserve: 100,
}
}
}
@ -108,9 +113,16 @@ impl EmailAuth {
pool: &SqlitePool,
email_mac: &str,
source_mac: &str,
resend: bool,
) -> anyhow::Result<bool> {
let t = now();
let limits = self.limits;
let global_limit = limits.global_hour
+ if resend {
limits.global_resend_reserve
} else {
0
};
let result = sqlx::query(
"INSERT INTO email_send_log(email_mac,source_mac,sent_at) SELECT ?,?,? WHERE \
(SELECT COUNT(*) FROM email_send_log WHERE email_mac=? AND sent_at>?)=0 AND \
@ -130,12 +142,24 @@ impl EmailAuth {
.bind(t - 3600)
.bind(limits.per_source_hour)
.bind(t - 3600)
.bind(limits.global_hour)
.bind(global_limit)
.execute(pool)
.await?;
Ok(result.rows_affected() == 1)
}
/// True when this browser cookie maps to an unexpired pending attempt.
pub async fn has_pending(&self, pool: &SqlitePool, browser: &str) -> anyhow::Result<bool> {
let found: Option<i64> = sqlx::query_scalar(
"SELECT 1 FROM email_attempts WHERE browser_hash=? AND expires_at>?",
)
.bind(hash(browser))
.bind(now())
.fetch_optional(pool)
.await?;
Ok(found.is_some())
}
/// Stores a new pending attempt, replacing the one bound to the browser's previous
/// cookie (a resend). Returns the generated code. Fails when capacity is reached.
pub async fn create_attempt(

View file

@ -75,12 +75,25 @@ fn set_cookie(response: &mut Response, name: &'static str, value: String, ttl: i
}
fn client_ip(headers: &HeaderMap) -> String {
// The loopback-only service trusts only the reverse proxy, which overwrites this header.
headers
// IPv6 sources are grouped by /64 (one subscriber's usual allocation or smaller), and
// IPv4-mapped addresses count as IPv4, so rotating host bits cannot dodge per-source limits.
use std::net::IpAddr;
match headers
.get("x-forwarded-for")
.and_then(|v| v.to_str().ok())
.and_then(|v| v.parse::<std::net::IpAddr>().ok())
.map(|v| v.to_string())
.unwrap_or_else(|| "local".into())
.and_then(|v| v.parse::<IpAddr>().ok())
{
Some(IpAddr::V6(v6)) => match v6.to_ipv4_mapped() {
Some(v4) => v4.to_string(),
None => {
let mut octets = v6.octets();
octets[8..].fill(0);
format!("{}/64", std::net::Ipv6Addr::from(octets))
}
},
Some(v4) => v4.to_string(),
None => "local".into(),
}
}
fn allowed(auth: &AuthService, headers: &HeaderMap) -> bool {
auth.allow(&client_ip(headers))
@ -206,8 +219,13 @@ async fn email_start(
};
let lang = Lang::from_tag(body.lang.as_deref());
let source = mail.source_mac(&client_ip(&headers));
let previous = cookie(&headers, EMAIL_BROWSER);
let resend = match &previous {
Some(browser) => mail.has_pending(&auth.pool, browser).await.unwrap_or(false),
None => false,
};
let reserved = mail
.reserve_send(&auth.pool, &mail.email_mac(&address), &source)
.reserve_send(&auth.pool, &mail.email_mac(&address), &source, resend)
.await;
match reserved {
Ok(true) => {}
@ -217,7 +235,6 @@ async fn email_start(
// Housekeeping failures must not block sign-in; expiry is enforced on every read.
let _ = store::cleanup(&auth.pool).await;
let browser = store::token();
let previous = cookie(&headers, EMAIL_BROWSER);
let Ok((attempt_id, code)) = mail
.create_attempt(&auth.pool, &address, &browser, previous.as_deref())
.await

View file

@ -115,6 +115,7 @@ fn relaxed() -> EmailLimits {
per_address_hour: 1000,
per_source_hour: 1000,
global_hour: 1000,
global_resend_reserve: 0,
}
}
@ -651,6 +652,7 @@ async fn source_and_global_limits() {
per_address_hour: 100,
per_source_hour: 3,
global_hour: 5,
global_resend_reserve: 0,
},
false,
true,
@ -819,3 +821,92 @@ async fn expired_attempts_and_old_ledger_rows_are_pruned() {
assert_eq!(n, 0, "{sql}");
}
}
#[tokio::test]
async fn ipv6_sources_are_limited_per_slash_64() {
let h = harness_with(
EmailLimits {
address_cooldown: 0,
per_address_hour: 100,
per_source_hour: 2,
global_hour: 100,
global_resend_reserve: 0,
},
false,
true,
)
.await;
let go = |i: usize, ip: &'static str| {
let app = h.app.clone();
async move {
let mut request = post(
"/v1/auth/email/start",
json!({"email": format!("v6user{i}@example.com")}),
);
request.ip = ip;
send(&app, request.request()).await.0
}
};
assert_eq!(go(0, "2001:db8:1:2::1").await, StatusCode::ACCEPTED);
assert_eq!(
go(1, "2001:db8:1:2:ffff:aaaa:bbbb:cccc").await,
StatusCode::ACCEPTED
);
assert_eq!(
go(2, "2001:db8:1:2:1234::9").await,
StatusCode::TOO_MANY_REQUESTS
);
// A different /64 has its own budget; mapped IPv4 shares the IPv4 bucket.
assert_eq!(go(3, "2001:db8:1:3::1").await, StatusCode::ACCEPTED);
assert_eq!(go(4, "::ffff:192.0.2.9").await, StatusCode::ACCEPTED);
assert_eq!(go(5, "192.0.2.9").await, StatusCode::ACCEPTED);
assert_eq!(
go(6, "::ffff:192.0.2.9").await,
StatusCode::TOO_MANY_REQUESTS
);
}
#[tokio::test]
async fn global_reserve_serves_only_resends_from_a_live_attempt() {
let h = harness_with(
EmailLimits {
address_cooldown: 0,
per_address_hour: 100,
per_source_hour: 100,
global_hour: 2,
global_resend_reserve: 2,
},
false,
true,
)
.await;
let browser = start(&h, "a@example.com").await;
start(&h, "b@example.com").await;
// Fresh starts are now refused, including with a bogus or missing browser cookie.
let fresh = post("/v1/auth/email/start", json!({"email": "c@example.com"}));
assert_eq!(
send(&h.app, fresh.request()).await.0,
StatusCode::TOO_MANY_REQUESTS
);
let mut bogus = post("/v1/auth/email/start", json!({"email": "c@example.com"}));
let stranger = format!("__Host-vnox_email={}", "cd".repeat(32));
bogus.cookie = Some(&stranger);
assert_eq!(
send(&h.app, bogus.request()).await.0,
StatusCode::TOO_MANY_REQUESTS
);
// A browser mid sign-in can still resend from the reserve, until it is used up.
let mut resend = post("/v1/auth/email/start", json!({"email": "a@example.com"}));
resend.cookie = Some(&browser);
let (status, cookies, _) = send(&h.app, resend.request()).await;
assert_eq!(status, StatusCode::ACCEPTED);
let browser = pair(&cookies, "__Host-vnox_email");
let mut resend = post("/v1/auth/email/start", json!({"email": "a@example.com"}));
resend.cookie = Some(&browser);
assert_eq!(send(&h.app, resend.request()).await.0, StatusCode::ACCEPTED);
let mut resend = post("/v1/auth/email/start", json!({"email": "a@example.com"}));
resend.cookie = Some(&browser);
let (status, cookies, _) = send(&h.app, resend.request()).await;
assert_eq!(status, StatusCode::TOO_MANY_REQUESTS);
assert!(cookies.is_empty());
}