oxedyne/fe2o3/fe2o3_steel/tests/auth_guard_sign_in.rs
3.2 KiB, 16 runs
created by r1191540701:77593, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The tighter sign-in rate limit reaches every sign-in route. |
| 2 | //! |
| 3 | //! The guard is built from the default config as the server builds it, and asked through the one gate |
| 4 | //! the request path asks, so a sign-in route missing from that gate shows here as unlimited. |
| 5 | //! |
| 6 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 7 | //! Anthropic Claude |
| 8 | |
| 9 | use oxedyne_fe2o3_core::prelude::*; |
| 10 | use oxedyne_fe2o3_steel::srv::{ |
| 11 | admin::{ |
| 12 | guard, |
| 13 | handler, |
| 14 | }, |
| 15 | cfg::ServerConfig, |
| 16 | console, |
| 17 | }; |
| 18 | |
| 19 | use std::net::{ |
| 20 | IpAddr, |
| 21 | Ipv4Addr, |
| 22 | }; |
| 23 | |
| 24 | // How many of six rapid requests from one address to `path` the sign-in limiter drops. |
| 25 | fn dropped_of_six(cfg: &ServerConfig, path: &str, ip: &IpAddr) -> Outcome<usize> { |
| 26 | let auth = res!(guard::new_auth_shared_with(cfg.get_auth_guard_settings())); |
| 27 | let mut dropped = 0; |
| 28 | for _ in 0..6 { |
| 29 | match res!(guard::auth_gate(&auth, &cfg.auth_path_prefixes, path, ip)) { |
| 30 | Some(d) if d.should_drop() => dropped += 1, |
| 31 | Some(_) => (), |
| 32 | None => return Err(err!( |
| 33 | "'{}' is not watched by the sign-in limiter.", path; |
| 34 | Invalid, Input)), |
| 35 | } |
| 36 | } |
| 37 | Ok(dropped) |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn six_rapid_sign_ins_at_manage_login_are_limited() -> Outcome<()> { |
| 42 | let cfg = ServerConfig::default(); |
| 43 | let ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 9)); // fixed: documentation range |
| 44 | let dropped = res!(dropped_of_six(&cfg, console::PATH_LOGIN, &ip)); |
| 45 | assert!(dropped > 0, "six rapid sign-ins at '{}' were all let through", console::PATH_LOGIN); |
| 46 | |
| 47 | // A reader's page is not a sign-in and is left to the general guard. |
| 48 | let auth = res!(guard::new_auth_shared_with(cfg.get_auth_guard_settings())); |
| 49 | let other = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 10)); |
| 50 | assert_eq!(res!(guard::auth_gate(&auth, &cfg.auth_path_prefixes, "/posts/on-rent", &other)), None); |
| 51 | Ok(()) |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn six_rapid_signed_logins_at_admin_signed_login_are_limited() -> Outcome<()> { |
| 56 | // An operator who empties the configured prefixes still has every sign-in route limited. |
| 57 | let mut cfg = ServerConfig::default(); |
| 58 | cfg.auth_path_prefixes = Vec::new(); |
| 59 | let ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 11)); // fixed: documentation range |
| 60 | let dropped = res!(dropped_of_six(&cfg, handler::PATH_SIGNED_LOGIN, &ip)); |
| 61 | assert!(dropped > 0, "six rapid signed logins at '{}' were all let through", handler::PATH_SIGNED_LOGIN); |
| 62 | Ok(()) |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn a_configured_prefix_matches_whole_segments_only() -> Outcome<()> { |
| 67 | let cfg = ServerConfig::default(); |
| 68 | let auth = res!(guard::new_auth_shared_with(cfg.get_auth_guard_settings())); |
| 69 | let ip = IpAddr::V4(Ipv4Addr::new(203, 0, 113, 12)); // fixed: documentation range |
| 70 | let extra = vec![fmt!("/login")]; |
| 71 | for path in ["/login.css", "/logins-explained"] { |
| 72 | assert_eq!(res!(guard::auth_gate(&auth, &extra, path, &ip)), None, |
| 73 | "'{}' is not under the configured '/login'", path); |
| 74 | } |
| 75 | for path in ["/login", "/login/form"] { |
| 76 | assert!(res!(guard::auth_gate(&auth, &extra, path, &ip)).is_some(), |
| 77 | "'{}' is under the configured '/login'", path); |
| 78 | } |
| 79 | Ok(()) |
| 80 | } |