windows: whitelist-bypass names its files only after plain names
The names were checked only while whitelist-bypass was on, but proxy-ctl wl names files after them either way: a configuration saved with it off could name a creator 'C:\...\x' (on Windows a join with an absolute path replaces the directory) and have the service write or delete a file of that name as SYSTEM. They are checked on or off now, and every state file goes through one helper that checks the name again. Names (of rule sets, groups, profiles, devices, ...) can no longer be Windows devices either (CON, NUL, COM1, ...): a file named so is the device. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
610e36a28b
commit
b6e54f963d
4 files changed
+76
-18
No files matched your search
@@ -21,7 +21,7 @@ pub use misc::*;
|
||||
pub use privilege::privileged_changes;
|
||||
pub use proxy::*;
|
||||
pub use tunnels::*;
|
||||
pub use validate::{Issue, Severity, validate};
|
||||
pub use validate::{Issue, Severity, path_name_ok, validate};
|
||||
pub use zapret::*;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -50,11 +50,20 @@ fn tag_pattern_ok(tag: &str) -> bool {
|
||||
|
||||
/// A name that becomes part of a file path, a Windows service name or an adapter name:
|
||||
/// letters, digits, `.`, `_` and `-`, not starting with a dot, at most 64 characters.
|
||||
pub(crate) fn path_name_ok(name: &str) -> bool {
|
||||
pub fn path_name_ok(name: &str) -> bool {
|
||||
!name.is_empty()
|
||||
&& name.len() <= 64
|
||||
&& !name.starts_with('.')
|
||||
&& name.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
|
||||
&& !windows_device(name)
|
||||
}
|
||||
|
||||
/// CON, NUL, COM1 and the rest: on Windows a file named so (whatever its extension) is the
|
||||
/// device.
|
||||
fn windows_device(name: &str) -> bool {
|
||||
let stem = name.split('.').next().unwrap_or("").to_ascii_uppercase();
|
||||
matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")
|
||||
|| (stem.len() == 4 && (stem.starts_with("COM") || stem.starts_with("LPT")) && stem.as_bytes()[3].is_ascii_digit())
|
||||
}
|
||||
|
||||
/// A geosite or geoip name: "geolocation-!cn" and "google@cn" among them, never a path
|
||||
@@ -477,13 +486,14 @@ fn tunnels(cfg: &Config, v: &mut Issues) {
|
||||
}
|
||||
|
||||
let wl = &cfg.whitelist_bypass;
|
||||
// Checked on or off: `proxy-ctl wl` names files after them either way.
|
||||
for name in wl.joiners.keys().chain(wl.creators.keys()) {
|
||||
if !path_name_ok(name) {
|
||||
v.error(format!("whitelistBypass.{name}"), "a creator or joiner name takes letters, digits, '.', '_' and '-', and no leading dot");
|
||||
}
|
||||
}
|
||||
if wl.enable {
|
||||
v.requires(!wl.joiners.is_empty(), proxy_on, "whitelistBypass.joiners", "whitelistBypass.joiners", "proxy.enable = true");
|
||||
for name in wl.joiners.keys().chain(wl.creators.keys()) {
|
||||
if !path_name_ok(name) {
|
||||
v.error(format!("whitelistBypass.{name}"), "a creator or joiner name takes letters, digits, '.', '_' and '-', and no leading dot");
|
||||
}
|
||||
}
|
||||
for (name, j) in &wl.joiners {
|
||||
if j.platform == crate::config::WlPlatform::Vk {
|
||||
v.error(format!("whitelistBypass.joiners.{name}.platform"), "VK has no joiner here");
|
||||
@@ -756,6 +766,23 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::config::{Group, Outbound, RoutingRule};
|
||||
|
||||
#[test]
|
||||
fn names_are_plain_file_names_and_never_devices() {
|
||||
for ok in ["laptop", "wl", "a.b_c-1", "COM", "console", "lpt10", "nul-ish"] {
|
||||
assert!(path_name_ok(ok), "{ok}");
|
||||
}
|
||||
for bad in ["", ".hidden", "a/b", r"a\b", "C:x", "..", "CON", "con.txt", "Nul", "PRN", "aux.json", "COM1", "lpt9.link"] {
|
||||
assert!(!path_name_ok(bad), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn whitelist_bypass_names_are_checked_while_it_is_off() {
|
||||
let mut cfg = Config::default();
|
||||
cfg.whitelist_bypass.creators.insert(r"C:\Windows\x".into(), serde_json::from_value(serde_json::json!({ "platform": "wbstream" })).unwrap());
|
||||
assert!(errors(&cfg).iter().any(|e| e.starts_with("whitelistBypass.")), "{:?}", errors(&cfg));
|
||||
}
|
||||
|
||||
fn errors(cfg: &Config) -> Vec<String> {
|
||||
validate(cfg)
|
||||
.into_iter()
|
||||
|
||||
Reference in new issue
Block a user