From d1bce9db9487bc6d78b4bf6a03107bfb6a22797d Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 00:07:38 +0300 Subject: [PATCH] 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 --- windows/crates/ps-core/src/config/validate.rs | 19 ++++++++++++ windows/crates/ps-core/src/zapret2.rs | 30 ++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/windows/crates/ps-core/src/config/validate.rs b/windows/crates/ps-core/src/config/validate.rs index ead27c9..5162054 100644 --- a/windows/crates/ps-core/src/config/validate.rs +++ b/windows/crates/ps-core/src/config/validate.rs @@ -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::().is_err() && ip.parse::().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)] { diff --git a/windows/crates/ps-core/src/zapret2.rs b/windows/crates/ps-core/src/zapret2.rs index bcbe76f..913e2fa 100644 --- a/windows/crates/ps-core/src/zapret2.rs +++ b/windows/crates/ps-core/src/zapret2.rs @@ -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 `` and `` 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("