0.34.13_cleaup_code

This commit is contained in:
Mateusz Gruszczyński committed 2026-10-26 14:14:41 +01:00
1 parent ec46a96a29
commit 5c2e50ec06
87 files changed
+4890 -1338

No files matched your search

+7 -2
View File
@@ -72,7 +72,11 @@ pub struct Config {
pub host_policies: Vec<HostPolicy>,
// Preserve old 0.34.13 and earlier config/backup data until compilation migrates it.
// Migration happens in compile_with_active, before validation and persistence.
#[serde(default, rename = "user_agent_blocking", skip_serializing_if = "Option::is_none")]
#[serde(
default,
rename = "user_agent_blocking",
skip_serializing_if = "Option::is_none"
)]
pub legacy_user_agent_blocking: Option<crate::user_agent::Policy>,
#[serde(default)]
pub global_security: GlobalSecurity,
@@ -124,7 +128,8 @@ impl Config {
impl GlobalSecurity {
pub fn compile(&mut self) -> Result<CompiledGlobalSecurity> {
let user_agent_blocking = self.user_agent_blocking
let user_agent_blocking = self
.user_agent_blocking
.get_or_insert_with(Default::default)
.compile()?;
self.country_access.validate()?;
+68 -16
View File
@@ -1189,21 +1189,41 @@ mod tests {
fn old_configuration_retains_user_agent_policy_and_global_security_defaults_off() {
let config: Config = serde_json::from_value(serde_json::json!({
"user_agent_blocking": {"enabled":true,"patterns":["bad-bot"]}
})).unwrap();
assert_eq!(config.global_security.country_access.mode, crate::rate_limit::CountryMode::Off);
}))
.unwrap();
assert_eq!(
config.global_security.country_access.mode,
crate::rate_limit::CountryMode::Off
);
assert!(config.global_security.acl.is_none());
assert_eq!(config.global_security.method_policy.mode, crate::vhost::MethodPolicyMode::Off);
assert_eq!(
config.global_security.method_policy.mode,
crate::vhost::MethodPolicyMode::Off
);
assert!(!config.global_security.content_type_policy.enabled);
assert!(!config.global_security.rate_limit.enabled);
assert!(!config.global_security.security_headers.hsts.enabled);
let snapshot = compile(config, 1, &policy()).unwrap();
let mut headers = http::HeaderMap::new();
headers.insert(http::header::USER_AGENT, http::HeaderValue::from_static("BAD-BOT/2"));
assert!(snapshot.global_security.user_agent_blocking.blocks(&headers));
assert!(snapshot.envelope.config.legacy_user_agent_blocking.is_none());
headers.insert(
http::header::USER_AGENT,
http::HeaderValue::from_static("BAD-BOT/2"),
);
assert!(snapshot
.global_security
.user_agent_blocking
.blocks(&headers));
assert!(snapshot
.envelope
.config
.legacy_user_agent_blocking
.is_none());
let canonical = serde_json::to_value(&snapshot.envelope.config).unwrap();
assert!(canonical.get("user_agent_blocking").is_none());
assert_eq!(canonical["global_security"]["user_agent_blocking"]["patterns"][0], "bad-bot");
assert_eq!(
canonical["global_security"]["user_agent_blocking"]["patterns"][0],
"bad-bot"
);
assert!(snapshot.global_security.country_access.allows(None));
}
@@ -1214,7 +1234,8 @@ mod tests {
"global_security": {
"user_agent_blocking": { "enabled":false,"patterns":[] }
}
})).unwrap();
}))
.unwrap();
let err = compile(old_and_new, 1, &policy()).unwrap_err().to_string();
assert!(err.contains("conflicting legacy"), "{err}");
}
@@ -1228,12 +1249,20 @@ mod tests {
});
let json = serde_json::to_value(&config).unwrap();
assert!(json.get("user_agent_blocking").is_none());
assert!(json["global_security"]["user_agent_blocking"]["enabled"].as_bool().unwrap());
assert!(json["global_security"]["user_agent_blocking"]["enabled"]
.as_bool()
.unwrap());
let restored: Config = serde_json::from_value(json).unwrap();
let snapshot = compile(restored, 1, &policy()).unwrap();
let mut headers = http::HeaderMap::new();
headers.insert(http::header::USER_AGENT, http::HeaderValue::from_static("trusted-test-bot/1"));
assert!(snapshot.global_security.user_agent_blocking.blocks(&headers));
headers.insert(
http::header::USER_AGENT,
http::HeaderValue::from_static("trusted-test-bot/1"),
);
assert!(snapshot
.global_security
.user_agent_blocking
.blocks(&headers));
}
#[test]
@@ -1260,16 +1289,35 @@ mod tests {
let snapshot = compile(config, 2, &policy()).unwrap();
let rules = &snapshot.global_security;
assert_eq!(snapshot.envelope.config.global_security.country_access.countries, vec!["RU"]);
assert_eq!(
snapshot
.envelope
.config
.global_security
.country_access
.countries,
vec!["RU"]
);
assert!(!rules.country_access.allows(Some("RU")));
assert!(rules.country_access.allows(None));
assert!(!rules.acl.as_ref().unwrap().allows("192.0.2.2".parse().unwrap()));
assert!(rules.acl.as_ref().unwrap().allows("198.51.100.1".parse().unwrap()));
assert!(!rules
.acl
.as_ref()
.unwrap()
.allows("192.0.2.2".parse().unwrap()));
assert!(rules
.acl
.as_ref()
.unwrap()
.allows("198.51.100.1".parse().unwrap()));
assert!(!rules.method_policy.allows(&http::Method::TRACE));
assert!(rules.method_policy.allows(&http::Method::GET));
assert!(rules.rate_limit.enabled);
let mut headers = http::HeaderMap::new();
headers.insert(http::header::CONTENT_TYPE, http::HeaderValue::from_static("application/x-danger; charset=utf-8"));
headers.insert(
http::header::CONTENT_TYPE,
http::HeaderValue::from_static("application/x-danger; charset=utf-8"),
);
assert!(rules.content_type_policy.blocks(&headers));
headers.clear();
rules.headers.apply(&mut headers, true);
@@ -1292,7 +1340,11 @@ mod tests {
config.global_security.security_headers.hsts.preload = true;
assert!(compile(config.clone(), 1, &policy()).is_err());
config.global_security.security_headers.hsts.enabled = true;
config.global_security.security_headers.hsts.include_subdomains = true;
config
.global_security
.security_headers
.hsts
.include_subdomains = true;
assert!(compile(config, 1, &policy()).is_ok());
}