windows: zapret site names and addresses are validated

A zapret2 domain is split into winws2's arguments: one carrying
whitespace could add an argument of its own (a --lua-init running as
SYSTEM). Names and addresses are checked in validation and filtered
again where the command line is built.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5.5 committed 2026-10-27 00:07:38 +03:00
1 parent bceb754e65
commit d1bce9db94
2 files changed
+45 -4

No files matched your search

@@ -439,6 +439,25 @@ fn zapret(cfg: &Config, v: &mut Issues) {
}
}
}
for (label, list) in [
("zapret.zapret2.domains", &z.zapret2.domains),
("zapret.zapret2.excludeDomains", &z.zapret2.exclude_domains),
("zapret.zapret-discord-youtube.domains", &z.discord_youtube.domains),
("zapret.zapret-discord-youtube.excludeDomains", &z.discord_youtube.exclude_domains),
] {
for d in list.iter().filter(|d| !crate::zapret2::host_entry_ok(d)) {
v.error(label, format!("'{d}' is not a site name: letters, digits, '.', '-', '_', '*', and '^' first for an exact match"));
}
}
for (label, list) in [
("zapret.zapret-discord-youtube.ips", &z.discord_youtube.ips),
("zapret.zapret-discord-youtube.excludeIps", &z.discord_youtube.exclude_ips),
("zapret.cidrExemption.cidrs", &z.cidr_exemption.cidrs),
] {
for ip in list.iter().filter(|ip| ip.parse::<ipnet::IpNet>().is_err() && ip.parse::<std::net::IpAddr>().is_err()) {
v.error(label, format!("'{ip}' is not an address or CIDR"));
}
}
if z.engine == ZapretEngine::Zapret2 {
let ah = &z.zapret2.auto_hostlist;
for (label, value) in [("failThreshold", ah.fail_threshold), ("failTime", ah.fail_time), ("retransThreshold", ah.retrans_threshold), ("udpOut", ah.udp_out)] {
+26 -4
View File
@@ -110,6 +110,15 @@ pub struct Paths {
pub auto: String,
}
/// A hostlist entry as nfqws2 takes one: a name (`^` for an exact match), nothing that could
/// read as another argument or another entry.
pub fn host_entry_ok(entry: &str) -> bool {
!entry.is_empty()
&& entry.len() <= 253
&& !entry.starts_with('-')
&& entry.chars().enumerate().all(|(i, c)| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_' | '*') || (c == '^' && i == 0))
}
/// What `<HOSTLIST>` and `<HOSTLIST_NOAUTO>` expand to (zapret2's filter_apply_hostlist_target).
fn hostlist_args(z: &Zapret2, p: &Paths) -> (String, String) {
let a = &z.auto_hostlist;
@@ -137,12 +146,15 @@ fn hostlist_args(z: &Zapret2, p: &Paths) -> (String, String) {
(base.clone(), base)
};
// The configuration's domains join every profile that takes a list.
// Validated already; dropped here too, as this text is split into winws2's arguments.
let domains: Vec<&str> = z.domains.iter().map(String::as_str).filter(|d| host_entry_ok(d)).collect();
let excluded: Vec<&str> = z.exclude_domains.iter().map(String::as_str).filter(|d| host_entry_ok(d)).collect();
let mut statics = String::new();
if !z.domains.is_empty() {
statics.push_str(&format!(" --hostlist-domains={}", z.domains.join(",")));
if !domains.is_empty() {
statics.push_str(&format!(" --hostlist-domains={}", domains.join(",")));
}
if !z.exclude_domains.is_empty() {
statics.push_str(&format!(" --hostlist-exclude-domains={}", z.exclude_domains.join(",")));
if !excluded.is_empty() {
statics.push_str(&format!(" --hostlist-exclude-domains={}", excluded.join(",")));
}
(with_auto + &statics, no_auto + &statics)
}
@@ -388,6 +400,16 @@ UDP_PORTS=443,590:600
assert!(!line.contains("<HOSTLIST") && !line.contains("<STALLWATCH"), "{line}");
}
#[test]
fn a_domain_cannot_carry_another_argument() {
let s = keenetic(CONF, r"C:\z2\keenetic\").unwrap();
let z = Zapret2 { domains: vec!["ok.example".into(), "x.com --lua-init=@C:\\evil.lua".into(), "-x".into()], ..Default::default() };
let line = args(&z, &s, &paths(), false).unwrap().join(" ");
assert!(!line.contains("evil") && !line.contains(",-x"), "{line}");
assert!(line.contains("--hostlist-domains=ok.example"));
assert!(host_entry_ok("^dns.google") && !host_entry_ok("a^b") && !host_entry_ok("a,b"));
}
#[test]
fn ports_merge_into_ranges() {
assert_eq!(merge_ports(&["80,443", "443,1000-1002", "1003", "~5", "*"]), "80,443,1000-1003");