feat(cli,api): human-readable ban durations, absolute expires_at, strict target/port validation
This commit is contained in:
parent
e2adf8601c
commit
da85225070
15 changed files with 570 additions and 36 deletions
66
tests/cli_duration.rs
Normal file
66
tests/cli_duration.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
//! Human-readable duration parser of `rampart-cli` and its clap value parsers.
|
||||
|
||||
use rampart::cli::commands::blacklist::{parse_duration_arg, parse_target_arg};
|
||||
use rampart::cli::duration::{format_duration_secs, parse_duration_secs};
|
||||
|
||||
#[test]
|
||||
fn accepts_single_and_compound_units() {
|
||||
assert_eq!(parse_duration_secs("30s"), Ok(30));
|
||||
assert_eq!(parse_duration_secs("5m"), Ok(300));
|
||||
assert_eq!(parse_duration_secs("1h30m"), Ok(5400));
|
||||
assert_eq!(parse_duration_secs("2d"), Ok(172_800));
|
||||
assert_eq!(parse_duration_secs("1d2h3m4s"), Ok(93_784));
|
||||
assert_eq!(parse_duration_secs("0"), Ok(0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bare_numbers_are_rejected_with_a_unit_hint() {
|
||||
for text in ["90", "1", "3600"] {
|
||||
let error = parse_duration_secs(text).expect_err(text);
|
||||
assert!(error.contains("valid units"), "{text}: {error}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_input_is_rejected() {
|
||||
for text in [
|
||||
"", " ", "5 m", " 5m", "5m ", "m", "-5m", "+5m", "1.5h", "5x", "5M", "1h1h", "30s5m", "5m30", "1h-5m", "0x10s",
|
||||
"5m", "5mm",
|
||||
] {
|
||||
assert!(parse_duration_secs(text).is_err(), "'{text}' must be rejected");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflow_is_an_error_not_a_wrap() {
|
||||
assert!(parse_duration_secs("99999999999999999999s").is_err());
|
||||
assert!(parse_duration_secs("999999999999999999d").is_err());
|
||||
assert!(parse_duration_secs("18446744073709551615h").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ban_flag_rejects_zero_and_formats_remaining_time() {
|
||||
assert_eq!(parse_duration_arg("1h30m"), Ok(5400));
|
||||
assert!(parse_duration_arg("0").is_err());
|
||||
assert!(parse_duration_arg("90").is_err());
|
||||
assert_eq!(format_duration_secs(5400), "1h30m");
|
||||
assert_eq!(format_duration_secs(90_061), "1d1h");
|
||||
assert_eq!(format_duration_secs(45), "45s");
|
||||
assert_eq!(format_duration_secs(0), "0s");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cli_target_is_a_single_strict_address() {
|
||||
assert_eq!(parse_target_arg("1.2.3.4"), Ok("1.2.3.4".to_string()));
|
||||
for text in [
|
||||
"",
|
||||
" 1.2.3.4",
|
||||
"1.2.3.4 ",
|
||||
"1.2.3.4x",
|
||||
"10.0.0.0/8",
|
||||
"1.2.3.4/0",
|
||||
"1.2.3.4/33",
|
||||
] {
|
||||
assert!(parse_target_arg(text).is_err(), "'{text}' must be rejected");
|
||||
}
|
||||
}
|
||||
|
|
@ -102,7 +102,19 @@ async fn protected_routes_require_a_token() {
|
|||
#[tokio::test]
|
||||
async fn blacklist_rejects_invalid_targets() {
|
||||
let router = app();
|
||||
for target in ["not-an-ip", "10.0.0.0/8", "999.1.1.1", ""] {
|
||||
for target in [
|
||||
"not-an-ip",
|
||||
"10.0.0.0/8",
|
||||
"999.1.1.1",
|
||||
"",
|
||||
" 1.2.3.4",
|
||||
"1.2.3.4 ",
|
||||
"1.2.3.4x",
|
||||
"1.2.3.4/0",
|
||||
"1.2.3.4/33",
|
||||
"::1/129",
|
||||
"1.2.3.4/+8",
|
||||
] {
|
||||
let body = format!(r#"{{"target":"{target}","type":"ip","reason":"manual_ban"}}"#);
|
||||
let response = status(
|
||||
&router,
|
||||
|
|
@ -280,3 +292,49 @@ fn list_query_is_capped_regardless_of_the_request() {
|
|||
(0, 0)
|
||||
);
|
||||
}
|
||||
|
||||
async fn post_ban(router: &Router, extra: &str) -> StatusCode {
|
||||
let body = format!(r#"{{"target":"1.2.3.4","type":"ip","reason":"manual_ban"{extra}}}"#);
|
||||
status(
|
||||
router,
|
||||
authorized(request(Method::POST, "/api/v1/blacklist", Body::from(body))),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ban_accepts_absolute_expiry_as_an_alternative_to_duration() {
|
||||
let router = app();
|
||||
let now = rampart::timeutil::unix_now();
|
||||
// Valid requests pass validation and fail only at the unreachable Redis (503).
|
||||
for extra in [
|
||||
String::new(),
|
||||
r#","duration_secs":3600"#.to_string(),
|
||||
format!(r#","expires_at":{}"#, now + 3600),
|
||||
format!(r#","expires_at":{}"#, now + 31_536_000 - 5),
|
||||
] {
|
||||
assert_eq!(
|
||||
post_ban(&router, &extra).await,
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
"{extra}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn ban_rejects_invalid_expiry_combinations() {
|
||||
let router = app();
|
||||
let now = rampart::timeutil::unix_now();
|
||||
for extra in [
|
||||
format!(r#","duration_secs":60,"expires_at":{}"#, now + 60),
|
||||
format!(r#","expires_at":{}"#, now - 10),
|
||||
r#","expires_at":0"#.to_string(),
|
||||
r#","expires_at":-5"#.to_string(),
|
||||
format!(r#","expires_at":{}"#, now + 31_536_000 + 60),
|
||||
format!(r#","expires_at":{}"#, i64::MAX),
|
||||
r#","duration_secs":0"#.to_string(),
|
||||
r#","duration_secs":31536001"#.to_string(),
|
||||
] {
|
||||
assert_eq!(post_ban(&router, &extra).await, StatusCode::BAD_REQUEST, "{extra}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
98
tests/manager_input.rs
Normal file
98
tests/manager_input.rs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
//! Strict input validation for ban targets, ports and ban windows.
|
||||
|
||||
use rampart::manager::input::{parse_port, parse_target, resolve_ban_window};
|
||||
|
||||
const NOW: i64 = 1_800_000_000;
|
||||
const YEAR: i64 = 31_536_000;
|
||||
|
||||
#[test]
|
||||
fn accepts_addresses_and_valid_prefixes() {
|
||||
assert_eq!(parse_target("1.2.3.4").map(|t| t.prefix), Ok(None));
|
||||
assert_eq!(parse_target("::1").map(|t| t.prefix), Ok(None));
|
||||
assert_eq!(parse_target("10.0.0.0/8").map(|t| t.prefix), Ok(Some(8)));
|
||||
assert_eq!(parse_target("1.2.3.4/32").map(|t| t.prefix), Ok(Some(32)));
|
||||
assert_eq!(parse_target("2001:db8::/128").map(|t| t.prefix), Ok(Some(128)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_malformed_targets() {
|
||||
for text in [
|
||||
"",
|
||||
" ",
|
||||
" 1.2.3.4",
|
||||
"1.2.3.4 ",
|
||||
"1.2.3.4\n",
|
||||
"1. 2.3.4",
|
||||
"1.2.3.4x",
|
||||
"1.2.3.4:80",
|
||||
"1.2.3.4/",
|
||||
"1.2.3.4/0",
|
||||
"::/0",
|
||||
"1.2.3.4/33",
|
||||
"::1/129",
|
||||
"1.2.3.4/+8",
|
||||
"1.2.3.4/8x",
|
||||
"1.2.3.4/-1",
|
||||
"1.2.3.4/256",
|
||||
"1.2.3.4/8/8",
|
||||
"999.1.1.1",
|
||||
"not-an-ip",
|
||||
] {
|
||||
assert!(parse_target(text).is_err(), "'{text}' must be rejected");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ports_are_strict_decimal_in_range() {
|
||||
assert_eq!(parse_port("1"), Ok(1));
|
||||
assert_eq!(parse_port("443"), Ok(443));
|
||||
assert_eq!(parse_port("65535"), Ok(65535));
|
||||
for text in [
|
||||
"",
|
||||
"0",
|
||||
"00",
|
||||
"65536",
|
||||
"99999999999",
|
||||
"-1",
|
||||
"+80",
|
||||
" 80",
|
||||
"80 ",
|
||||
"80x",
|
||||
"0x50",
|
||||
"8.0",
|
||||
] {
|
||||
assert!(parse_port(text).is_err(), "'{text}' must be rejected");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn window_defaults_and_duration_are_unchanged() {
|
||||
assert_eq!(resolve_ban_window(None, None, 300, NOW), Ok((300, NOW + 300)));
|
||||
assert_eq!(resolve_ban_window(Some(3600), None, 300, NOW), Ok((3600, NOW + 3600)));
|
||||
assert!(resolve_ban_window(Some(0), None, 300, NOW).is_err());
|
||||
assert!(resolve_ban_window(Some(YEAR as u64 + 1), None, 300, NOW).is_err());
|
||||
assert_eq!(
|
||||
resolve_ban_window(Some(YEAR as u64), None, 300, NOW),
|
||||
Ok((YEAR as u64, NOW + YEAR))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn absolute_expiry_is_validated() {
|
||||
assert_eq!(resolve_ban_window(None, Some(NOW + 60), 300, NOW), Ok((60, NOW + 60)));
|
||||
assert_eq!(
|
||||
resolve_ban_window(None, Some(NOW + YEAR), 300, NOW),
|
||||
Ok((YEAR as u64, NOW + YEAR))
|
||||
);
|
||||
for at in [NOW, NOW - 1, 0, -1, i64::MIN, i64::MAX, NOW + YEAR + 1] {
|
||||
assert!(
|
||||
resolve_ban_window(None, Some(at), 300, NOW).is_err(),
|
||||
"{at} must be rejected"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duration_and_expiry_are_mutually_exclusive() {
|
||||
assert!(resolve_ban_window(Some(60), Some(NOW + 60), 300, NOW).is_err());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue