windows: close gaps in the admin-only edit check
Null-to-object changes are looked into, keys named just 'file' count, raw inbound XRay JSON is admin-only outright, and raw outbound JSON is admin-only when it runs Tor or holds a path-like value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
77d4cefe0e
commit
96958c4e61
1 file changed
+89
-9
@@ -14,12 +14,14 @@ use super::Config;
|
|||||||
const RAW_KEYS: [&str; 4] = ["extraArgs", "extraConfig", "nfqwsArgs", "allowConfigHooks"];
|
const RAW_KEYS: [&str; 4] = ["extraArgs", "extraConfig", "nfqwsArgs", "allowConfigHooks"];
|
||||||
|
|
||||||
/// Option paths (`.`-separated, `*` for any key or index) privileged as a whole.
|
/// Option paths (`.`-separated, `*` for any key or index) privileged as a whole.
|
||||||
const PRIVILEGED_PATHS: [&str; 5] = [
|
const PRIVILEGED_PATHS: [&str; 6] = [
|
||||||
"userControl",
|
"userControl",
|
||||||
"proxy.dns.singBox",
|
"proxy.dns.singBox",
|
||||||
"zapret.zapret2.profiles",
|
"zapret.zapret2.profiles",
|
||||||
"zapret.zapret2.blobs",
|
"zapret.zapret2.blobs",
|
||||||
"inbounds.manager.api",
|
"inbounds.manager.api",
|
||||||
|
// XRay inbound JSON can listen on a socket path or serve a directory: no subset of it is safe.
|
||||||
|
"inbounds.listeners.*.xrayJson",
|
||||||
];
|
];
|
||||||
|
|
||||||
/// Where a value of `new` that differs from `old` needs an administrator, as option paths.
|
/// Where a value of `new` that differs from `old` needs an administrator, as option paths.
|
||||||
@@ -33,8 +35,9 @@ pub fn privileged_changes(old: &Config, new: &Config) -> Vec<String> {
|
|||||||
|
|
||||||
fn walk(path: &mut Vec<String>, old: Option<&Value>, new: Option<&Value>, found: &mut Vec<String>) {
|
fn walk(path: &mut Vec<String>, old: Option<&Value>, new: Option<&Value>, found: &mut Vec<String>) {
|
||||||
// An unset option serializes as null, or not at all: either way it is unset.
|
// An unset option serializes as null, or not at all: either way it is unset.
|
||||||
let unset = |v: Option<&Value>| v.is_none_or(Value::is_null);
|
let old = old.filter(|v| !v.is_null());
|
||||||
if old == new || (unset(old) && unset(new)) {
|
let new = new.filter(|v| !v.is_null());
|
||||||
|
if old == new {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
match privileged_here(path, new) {
|
match privileged_here(path, new) {
|
||||||
@@ -61,10 +64,15 @@ fn walk(path: &mut Vec<String>, old: Option<&Value>, new: Option<&Value>, found:
|
|||||||
path.pop();
|
path.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// An object or list appearing or going: what it holds decides.
|
// An object or list appearing, going, or taking another shape: what each side holds
|
||||||
(None, Some(v @ (Value::Object(_) | Value::Array(_)))) => walk_new(path, v, found),
|
// decides.
|
||||||
(Some(v @ (Value::Object(_) | Value::Array(_))), None) => walk_new(path, v, found),
|
(a, b) => {
|
||||||
_ => {}
|
for v in [a, b].into_iter().flatten() {
|
||||||
|
if matches!(v, Value::Object(_) | Value::Array(_)) {
|
||||||
|
walk_new(path, v, found);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +92,8 @@ fn privileged_here(path: &[String], new: Option<&Value>) -> Option<bool> {
|
|||||||
return Some(true);
|
return Some(true);
|
||||||
}
|
}
|
||||||
let key = path.last()?.as_str();
|
let key = path.last()?.as_str();
|
||||||
if key.ends_with("File") || key.ends_with("Directory") {
|
let lower = key.to_ascii_lowercase();
|
||||||
|
if lower.ends_with("file") || lower.ends_with("directory") {
|
||||||
return Some(true);
|
return Some(true);
|
||||||
}
|
}
|
||||||
if RAW_KEYS.contains(&key) {
|
if RAW_KEYS.contains(&key) {
|
||||||
@@ -93,11 +102,36 @@ fn privileged_here(path: &[String], new: Option<&Value>) -> Option<bool> {
|
|||||||
// Raw backend JSON is fine as long as it names no local file or program, as upstream
|
// Raw backend JSON is fine as long as it names no local file or program, as upstream
|
||||||
// takes it at runtime from its userControl group.
|
// takes it at runtime from its userControl group.
|
||||||
if key.ends_with("Json") {
|
if key.ends_with("Json") {
|
||||||
return Some(new.is_some_and(|v| !crate::links::local_file_keys(v).is_empty()));
|
return Some(new.is_some_and(raw_json_is_unsafe));
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Raw backend JSON a non-administrator may not set: it names a local file or program by key
|
||||||
|
/// (`local_file_keys`), runs Tor, or holds a value shaped like a path (a Unix socket for a
|
||||||
|
/// server address, a file to write).
|
||||||
|
fn raw_json_is_unsafe(value: &Value) -> bool {
|
||||||
|
fn path_like(s: &str) -> bool {
|
||||||
|
let b = s.as_bytes();
|
||||||
|
s.starts_with('/')
|
||||||
|
|| s.starts_with('@')
|
||||||
|
|| s.starts_with('\\')
|
||||||
|
|| s.starts_with('~')
|
||||||
|
|| (b.len() >= 3 && b[0].is_ascii_alphabetic() && b[1] == b':' && matches!(b[2], b'\\' | b'/'))
|
||||||
|
}
|
||||||
|
fn any_string(v: &Value, f: &dyn Fn(&str) -> bool) -> bool {
|
||||||
|
match v {
|
||||||
|
Value::String(s) => f(s),
|
||||||
|
Value::Array(a) => a.iter().any(|v| any_string(v, f)),
|
||||||
|
Value::Object(o) => o.values().any(|v| any_string(v, f)),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
!crate::links::local_file_keys(value).is_empty()
|
||||||
|
|| value.get("type").and_then(Value::as_str) == Some("tor")
|
||||||
|
|| any_string(value, &|s| path_like(s) && !s.starts_with("//"))
|
||||||
|
}
|
||||||
|
|
||||||
fn matches_path(pattern: &str, path: &[String]) -> bool {
|
fn matches_path(pattern: &str, path: &[String]) -> bool {
|
||||||
let parts: Vec<&str> = pattern.split('.').collect();
|
let parts: Vec<&str> = pattern.split('.').collect();
|
||||||
path.len() >= parts.len() && parts.iter().zip(path).all(|(p, s)| *p == "*" || *p == s)
|
path.len() >= parts.len() && parts.iter().zip(path).all(|(p, s)| *p == "*" || *p == s)
|
||||||
@@ -161,6 +195,52 @@ mod tests {
|
|||||||
assert_eq!(privileged_changes(&old, &new), ["proxy.outbounds.0.singBoxJson"]);
|
assert_eq!(privileged_changes(&old, &new), ["proxy.outbounds.0.singBoxJson"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lowercase_file_keys_and_appearing_objects_count() {
|
||||||
|
let old = Config::default();
|
||||||
|
let mut new = old.clone();
|
||||||
|
new.tg_ws_proxy.log.file = Some(r"C:\Windows\System32\drivers\etc\hosts".into());
|
||||||
|
new.tor.bridges.file = Some("x".into());
|
||||||
|
new.amnezia_wg.profiles.insert(
|
||||||
|
"p".into(),
|
||||||
|
AwgProfile {
|
||||||
|
settings: Some(crate::config::AwgSettings { private_key_file: Some("k".into()), ..Default::default() }),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
);
|
||||||
|
let found = privileged_changes(&old, &new);
|
||||||
|
for path in ["tgWsProxy.log.file", "tor.bridges.file", "amneziaWg.profiles.p.settings.privateKeyFile"] {
|
||||||
|
assert!(found.iter().any(|f| f == path), "{path} not in {found:?}");
|
||||||
|
}
|
||||||
|
// settings from unset to set, on a profile that was there before.
|
||||||
|
let mut old = Config::default();
|
||||||
|
old.amnezia_wg.profiles.insert("p".into(), AwgProfile::default());
|
||||||
|
let found = privileged_changes(&old, &new);
|
||||||
|
assert!(found.iter().any(|f| f == "amneziaWg.profiles.p.settings.privateKeyFile"), "{found:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn path_like_raw_json_and_inbound_json_count() {
|
||||||
|
let old = Config::default();
|
||||||
|
let mut new = old.clone();
|
||||||
|
let mut json = serde_json::Map::new();
|
||||||
|
json.insert("protocol".into(), "freedom".into());
|
||||||
|
json.insert("settings".into(), serde_json::json!({ "redirect": "@xray.sock" }));
|
||||||
|
new.proxy.outbounds.push(Outbound { tag: "a".into(), xray_json: Some(json), ..Default::default() });
|
||||||
|
let mut tor = serde_json::Map::new();
|
||||||
|
tor.insert("type".into(), "tor".into());
|
||||||
|
new.proxy.outbounds.push(Outbound { tag: "b".into(), sing_box_json: Some(tor), ..Default::default() });
|
||||||
|
new.inbounds.listeners.insert(
|
||||||
|
"raw".into(),
|
||||||
|
crate::config::InboundListener { xray_json: Some(serde_json::Map::new()), ..Default::default() },
|
||||||
|
);
|
||||||
|
new.inbounds.listeners.get_mut("raw").unwrap().xray_json.as_mut().unwrap().insert("port".into(), 1.into());
|
||||||
|
let found = privileged_changes(&old, &new);
|
||||||
|
for path in ["proxy.outbounds.0.xrayJson", "proxy.outbounds.1.singBoxJson", "inbounds.listeners.raw.xrayJson"] {
|
||||||
|
assert!(found.iter().any(|f| f == path), "{path} not in {found:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn removing_a_privileged_value_counts_too() {
|
fn removing_a_privileged_value_counts_too() {
|
||||||
let mut old = Config::default();
|
let mut old = Config::default();
|
||||||
|
|||||||
Reference in new issue
Block a user