diff --git a/crates/edge/tests/share_links.rs b/crates/edge/tests/share_links.rs index acf7d74..e3f86d7 100644 --- a/crates/edge/tests/share_links.rs +++ b/crates/edge/tests/share_links.rs @@ -437,23 +437,59 @@ mod unix { .await; assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 302); + // The epoch never goes back: a lower one is refused and the old state kept, so a + // control that lost its records cannot revive stopped links. + let fresh = mint( + &key(), + HOST, + &LinkSpec { + id: ShareId([14; 16]), + epoch: 1, + expires_at: now() + 3600, + prefix: "/", + password: None, + }, + ) + .unwrap(); + let fresh_cookie = share_cookie(&e.get(HOST, &format!("/?share={fresh}"), None).await); + assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 200); + e.edge.write_revocations(0, &[]); + let r = e + .edge + .admin("POST", "/v1/share-revocations/reload", None) + .await; + assert_eq!(r.status, 422, "{}", r.text); + assert!(r.text.contains("epoch"), "{}", r.text); + assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 302, "still stopped"); + assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 200); + // Without the admin call, edge notices a changed file within 5 s. + e.edge.write_revocations(1, &[ShareId([14; 16]).to_hex()]); + tokio::time::sleep(Duration::from_secs(6)).await; + assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 302); + // ...and a lower epoch written behind its back is refused there too. e.edge.write_revocations(0, &[]); tokio::time::sleep(Duration::from_secs(6)).await; - assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 200); + assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 302); + assert!(e.edge.stderr().contains("epoch"), "the refusal is logged"); // A broken file keeps the last good state; a missing one stops all share access. + e.edge.write_revocations(1, &[]); + e.edge + .admin("POST", "/v1/share-revocations/reload", None) + .await; + assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 200); std::fs::write(e.edge.revocations_path(), "{ broken").unwrap(); let r = e .edge .admin("POST", "/v1/share-revocations/reload", None) .await; assert_eq!(r.status, 422); - assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 200); + assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 200); std::fs::remove_file(e.edge.revocations_path()).unwrap(); e.edge .admin("POST", "/v1/share-revocations/reload", None) .await; - assert_eq!(e.get(HOST, "/", Some(&keep_cookie)).await.status, 302); + assert_eq!(e.get(HOST, "/", Some(&fresh_cookie)).await.status, 302); } } diff --git a/crates/edge/tests/share_throttle.rs b/crates/edge/tests/share_throttle.rs new file mode 100644 index 0000000..f078e86 --- /dev/null +++ b/crates/edge/tests/share_throttle.rs @@ -0,0 +1,72 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at https://mozilla.org/MPL/2.0/. + +//! The wrong-password throttle (QC fix round 1, M1): the id it keys on is unproven (anyone can +//! forge a token with any id), so it must not be possible to reset a real link's counter by +//! flooding it with made-up ids, and a full table refuses instead of forgetting. + +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use std::net::IpAddr; + +use ianitor_core::share::ShareId; +use ianitor_edge::share::Throttle; + +fn junk(n: u32) -> ShareId { + let mut id = [0u8; 16]; + id[..4].copy_from_slice(&n.to_be_bytes()); + id[15] = 1; + ShareId(id) +} + +#[test] +fn junk_ids_from_the_same_network_cannot_flush_a_link_counter() { + let t = Throttle::default(); + let victim = ShareId([7; 16]); + let ip: IpAddr = "192.0.2.10".parse().unwrap(); + for _ in 0..5 { + t.failed(victim, Some(ip)); + } + assert!(!t.allowed(victim, Some(ip)), "locked after 5 failures"); + for n in 0u32..10_000 { + t.failed(junk(n), Some(ip)); + } + assert!(!t.allowed(victim, Some(ip)), "10,000 junk failures reset the victim's counter"); +} + +#[test] +fn a_network_is_limited_across_all_links() { + let t = Throttle::default(); + let ip: IpAddr = "192.0.2.10".parse().unwrap(); + let neighbour: IpAddr = "192.0.2.77".parse().unwrap(); + let elsewhere: IpAddr = "198.51.100.1".parse().unwrap(); + let mut n = 0; + while t.allowed(junk(n), Some(ip)) { + t.failed(junk(n), Some(ip)); + n += 1; + assert!(n < 1000, "no limit per network"); + } + assert!(n <= 20, "{n} wrong passwords a minute from one network"); + // The same /24 is limited for a link it never tried; other networks are not. + assert!(!t.allowed(ShareId([9; 16]), Some(neighbour))); + assert!(t.allowed(ShareId([9; 16]), Some(elsewhere))); +} + +#[test] +fn a_full_table_refuses_new_visitors_instead_of_forgetting_old_ones() { + let t = Throttle::default(); + let victim = ShareId([7; 16]); + let ip: IpAddr = "192.0.2.10".parse().unwrap(); + for _ in 0..5 { + t.failed(victim, Some(ip)); + } + // Fill the table from many networks, one failure each. + for n in 0u32..20_000 { + let other = IpAddr::from([10, (n >> 16) as u8, (n >> 8) as u8, n as u8]); + t.failed(junk(n), Some(other)); + } + assert!(!t.allowed(victim, Some(ip)), "the victim's counter survived"); + let newcomer = IpAddr::from([203, 0, 113, 5]); + assert!(!t.allowed(ShareId([3; 16]), Some(newcomer)), "full table: refuse, don't forget"); +}