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:
parent
cca084b0ae
commit
6ea14d0ec2
6 changed files with 140 additions and 240 deletions
|
|
@ -1,104 +0,0 @@
|
|||
use chacha20poly1305::{
|
||||
ChaCha20Poly1305, Key, KeyInit, Nonce,
|
||||
aead::{Aead, Payload},
|
||||
};
|
||||
|
||||
pub(super) fn make_nonce(cid: &[u8; 8], seq: u64) -> [u8; 12] {
|
||||
let mut nonce = [0u8; 12];
|
||||
nonce[..8].copy_from_slice(cid);
|
||||
nonce[8..12].copy_from_slice(&(seq as u32).to_le_bytes());
|
||||
nonce
|
||||
}
|
||||
|
||||
pub(super) fn encrypt_with_key(
|
||||
key: &[u8; 32],
|
||||
cid: &[u8; 8],
|
||||
seq: u64,
|
||||
plaintext: &[u8],
|
||||
) -> Vec<u8> {
|
||||
encrypt_with_aad(key, cid, seq, plaintext, &[])
|
||||
}
|
||||
|
||||
pub(super) fn encrypt_with_aad(
|
||||
key: &[u8; 32],
|
||||
cid: &[u8; 8],
|
||||
seq: u64,
|
||||
plaintext: &[u8],
|
||||
aad: &[u8],
|
||||
) -> Vec<u8> {
|
||||
let cipher = ChaCha20Poly1305::new(&Key::from(*key));
|
||||
let nonce_bytes = make_nonce(cid, seq);
|
||||
let nonce = Nonce::from(nonce_bytes);
|
||||
cipher
|
||||
.encrypt(
|
||||
&nonce,
|
||||
Payload {
|
||||
msg: plaintext,
|
||||
aad,
|
||||
},
|
||||
)
|
||||
.expect("ChaCha20-Poly1305 encryption is infallible")
|
||||
}
|
||||
|
||||
pub(super) fn decrypt_with_key(
|
||||
key: &[u8; 32],
|
||||
cid: &[u8; 8],
|
||||
seq: u64,
|
||||
ciphertext: &[u8],
|
||||
) -> anyhow::Result<Vec<u8>> {
|
||||
decrypt_with_aad(key, cid, seq, ciphertext, &[])
|
||||
}
|
||||
|
||||
pub(super) fn decrypt_with_aad(
|
||||
key: &[u8; 32],
|
||||
cid: &[u8; 8],
|
||||
seq: u64,
|
||||
ciphertext: &[u8],
|
||||
aad: &[u8],
|
||||
) -> anyhow::Result<Vec<u8>> {
|
||||
let cipher = ChaCha20Poly1305::new(&Key::from(*key));
|
||||
let nonce_bytes = make_nonce(cid, seq);
|
||||
let nonce = Nonce::from(nonce_bytes);
|
||||
cipher
|
||||
.decrypt(
|
||||
&nonce,
|
||||
Payload {
|
||||
msg: ciphertext,
|
||||
aad,
|
||||
},
|
||||
)
|
||||
.map_err(|e| anyhow::anyhow!("decryption failed: {e:?}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn wire_ciphertext_matches_independent_chacha20_poly1305_vector() {
|
||||
let key = std::array::from_fn(|index| index as u8);
|
||||
let cid = [0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43];
|
||||
let seq = 0x4746_4544;
|
||||
let plaintext = b"VNOX dependency migration";
|
||||
// Cross-checked with Python cryptography's ChaCha20Poly1305 implementation,
|
||||
// IETF nonce 070000004041424344454647, and empty associated data.
|
||||
let expected = hex::decode(
|
||||
"1fa23e7a312509b0fc8729521811348ff880daa5598c43a57635975f81278abdf4461b25af86aee1c3",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(encrypt_with_key(&key, &cid, seq, plaintext), expected);
|
||||
assert_eq!(
|
||||
decrypt_with_key(&key, &cid, seq, &expected).unwrap(),
|
||||
plaintext
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tampered_ciphertext_is_rejected() {
|
||||
let key = [7; 32];
|
||||
let cid = [9; 8];
|
||||
let mut ciphertext = encrypt_with_key(&key, &cid, 42, b"authenticated");
|
||||
ciphertext[0] ^= 1;
|
||||
assert!(decrypt_with_key(&key, &cid, 42, &ciphertext).is_err());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,128 +0,0 @@
|
|||
mod cipher;
|
||||
|
||||
use hkdf::Hkdf;
|
||||
use sha2::Sha256;
|
||||
use x25519_dalek::{EphemeralSecret, PublicKey, SharedSecret};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SessionCrypto {
|
||||
c2s_key: [u8; 32],
|
||||
s2c_key: [u8; 32],
|
||||
cid: [u8; 8],
|
||||
}
|
||||
|
||||
impl SessionCrypto {
|
||||
pub fn derive(shared_secret: &[u8; 32], session_id: &str) -> Self {
|
||||
let salt = b"VNOX-LNEx-KDF-v2";
|
||||
let hk = Hkdf::<Sha256>::new(Some(salt), shared_secret);
|
||||
|
||||
let mut keys = [0u8; 64];
|
||||
hk.expand(b"session-keys", &mut keys)
|
||||
.expect("64 bytes is within HKDF max");
|
||||
|
||||
let c2s_key: [u8; 32] = keys[..32].try_into().unwrap();
|
||||
let s2c_key: [u8; 32] = keys[32..64].try_into().unwrap();
|
||||
|
||||
use sha2::Digest;
|
||||
let hash = Sha256::digest(session_id.as_bytes());
|
||||
let cid: [u8; 8] = hash[..8].try_into().unwrap();
|
||||
|
||||
Self {
|
||||
c2s_key,
|
||||
s2c_key,
|
||||
cid,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encrypt_s2c(&self, seq: u64, plaintext: &[u8]) -> Vec<u8> {
|
||||
cipher::encrypt_with_key(&self.s2c_key, &self.cid, seq, plaintext)
|
||||
}
|
||||
|
||||
pub fn decrypt_c2s(&self, seq: u64, ciphertext: &[u8]) -> anyhow::Result<Vec<u8>> {
|
||||
cipher::decrypt_with_key(&self.c2s_key, &self.cid, seq, ciphertext)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn encrypt_c2s_frame(&self, seq: u64, plaintext: &[u8], header: &[u8]) -> Vec<u8> {
|
||||
cipher::encrypt_with_aad(&self.c2s_key, &self.cid, seq, plaintext, header)
|
||||
}
|
||||
pub fn encrypt_s2c_frame(&self, seq: u64, plaintext: &[u8], header: &[u8]) -> Vec<u8> {
|
||||
cipher::encrypt_with_aad(&self.s2c_key, &self.cid, seq, plaintext, header)
|
||||
}
|
||||
pub fn decrypt_c2s_frame(
|
||||
&self,
|
||||
seq: u64,
|
||||
ciphertext: &[u8],
|
||||
header: &[u8],
|
||||
) -> anyhow::Result<Vec<u8>> {
|
||||
cipher::decrypt_with_aad(&self.c2s_key, &self.cid, seq, ciphertext, header)
|
||||
}
|
||||
|
||||
pub fn new_ephemeral() -> (EphemeralSecret, PublicKey) {
|
||||
let mut rng = rand::rng();
|
||||
let sk = EphemeralSecret::random_from_rng(&mut rng);
|
||||
let pk = PublicKey::from(&sk);
|
||||
(sk, pk)
|
||||
}
|
||||
|
||||
pub fn ecdh(secret: EphemeralSecret, peer_public: &PublicKey) -> SharedSecret {
|
||||
secret.diffie_hellman(peer_public)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn encrypt_then_decrypt_roundtrip() {
|
||||
let shared_secret = [0xABu8; 32];
|
||||
let crypto = SessionCrypto::derive(&shared_secret, "test-session-id");
|
||||
|
||||
let plaintext = b"hello encrypted world";
|
||||
let seq = 42;
|
||||
|
||||
let ciphertext = crypto.encrypt_s2c(seq, plaintext);
|
||||
assert_ne!(ciphertext, plaintext);
|
||||
assert!(ciphertext.len() > plaintext.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_keys_cannot_decrypt_each_other() {
|
||||
let shared_secret = [0xABu8; 32];
|
||||
let crypto = SessionCrypto::derive(&shared_secret, "test-session-id");
|
||||
|
||||
let s2c_ct = crypto.encrypt_s2c(0, b"server to client");
|
||||
let result = crypto.decrypt_c2s(0, &s2c_ct);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn different_keys_produce_different_ciphertexts() {
|
||||
let secret_a = [0xAAu8; 32];
|
||||
let secret_b = [0xBBu8; 32];
|
||||
let crypto_a = SessionCrypto::derive(&secret_a, "session-a");
|
||||
let crypto_b = SessionCrypto::derive(&secret_b, "session-b");
|
||||
|
||||
let plaintext = b"same plaintext";
|
||||
let ct_a = crypto_a.encrypt_s2c(0, plaintext);
|
||||
let ct_b = crypto_b.encrypt_s2c(0, plaintext);
|
||||
assert_ne!(ct_a, ct_b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ecdh_derives_same_secret_on_both_sides() {
|
||||
let mut rng = rand::rng();
|
||||
|
||||
let alice_sk = EphemeralSecret::random_from_rng(&mut rng);
|
||||
let alice_pk = PublicKey::from(&alice_sk);
|
||||
|
||||
let bob_sk = EphemeralSecret::random_from_rng(&mut rng);
|
||||
let bob_pk = PublicKey::from(&bob_sk);
|
||||
|
||||
let alice_shared = alice_sk.diffie_hellman(&bob_pk);
|
||||
let bob_shared = bob_sk.diffie_hellman(&alice_pk);
|
||||
|
||||
assert_eq!(alice_shared.as_bytes(), bob_shared.as_bytes());
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue