mirror of
https://github.com/Corfirean/coa-server-manager.git
synced 2026-11-25 19:25:56 +00:00
feat(phase15): release and security hardening, DPAPI key store, probe & protocol hardening, secret redaction, audit and readiness report
This commit is contained in:
1 parent
b69fa6b867
commit
51e0f136d6
207 files changed
+38235
-7770
No files matched your search
+161
-41
@@ -1,5 +1,5 @@
|
||||
use std::path::Path;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug, Serialize, PartialEq, Eq)]
|
||||
pub struct Release {
|
||||
@@ -12,18 +12,50 @@ pub fn imported_repack(root: &Path) -> bool {
|
||||
}
|
||||
|
||||
pub fn release(root: &Path) -> Option<Release> {
|
||||
for name in ["Extras/SquidPlayerbots/release.json", "Core/configs/modules/playerbots.conf.settings.json", "CoA-Bots/release.json"] {
|
||||
let Ok(bytes) = std::fs::read(root.join(name)) else { continue; };
|
||||
if bytes.len() > 1024 * 1024 { continue; }
|
||||
let Ok(json) = serde_json::from_slice::<serde_json::Value>(&bytes) else { continue; };
|
||||
let safe = |key: &str| json.get(key).and_then(|value| value.as_str())
|
||||
.filter(|value| !value.is_empty() && value.len() <= 80 && value.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c))).map(str::to_owned);
|
||||
for name in [
|
||||
"Extras/SquidPlayerbots/release.json",
|
||||
"Core/configs/modules/playerbots.conf.settings.json",
|
||||
"CoA-Bots/release.json",
|
||||
] {
|
||||
let Ok(bytes) = std::fs::read(root.join(name)) else {
|
||||
continue;
|
||||
};
|
||||
if bytes.len() > 1024 * 1024 {
|
||||
continue;
|
||||
}
|
||||
let Ok(json) = serde_json::from_slice::<serde_json::Value>(&bytes) else {
|
||||
continue;
|
||||
};
|
||||
let safe = |key: &str| {
|
||||
json.get(key)
|
||||
.and_then(|value| value.as_str())
|
||||
.filter(|value| {
|
||||
!value.is_empty()
|
||||
&& value.len() <= 80
|
||||
&& value
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || "._-".contains(c))
|
||||
})
|
||||
.map(str::to_owned)
|
||||
};
|
||||
let tag = safe("tag").or_else(|| safe("version"));
|
||||
let revision = if name == "CoA-Bots/release.json" {
|
||||
json.get("botsModuleRevision").and_then(|value| value.as_str()).and_then(|value| value.split_whitespace().next()).map(str::to_owned).or_else(|| safe("commit"))
|
||||
} else if name.ends_with(".settings.json") { None } else { safe("commit") };
|
||||
let commit = revision.filter(|value| value.len() >= 7 && value.len() <= 40 && value.chars().all(|c| c.is_ascii_hexdigit()));
|
||||
if tag.is_some() || commit.is_some() { return Some(Release { tag, commit }); }
|
||||
json.get("botsModuleRevision")
|
||||
.and_then(|value| value.as_str())
|
||||
.and_then(|value| value.split_whitespace().next())
|
||||
.map(str::to_owned)
|
||||
.or_else(|| safe("commit"))
|
||||
} else if name.ends_with(".settings.json") {
|
||||
None
|
||||
} else {
|
||||
safe("commit")
|
||||
};
|
||||
let commit = revision.filter(|value| {
|
||||
value.len() >= 7 && value.len() <= 40 && value.chars().all(|c| c.is_ascii_hexdigit())
|
||||
});
|
||||
if tag.is_some() || commit.is_some() {
|
||||
return Some(Release { tag, commit });
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -65,23 +97,45 @@ struct Settings {
|
||||
|
||||
pub fn fields(root: &Path) -> crate::Result<std::collections::BTreeMap<String, Field>> {
|
||||
let path = root.join("Core/configs/modules/playerbots.conf.settings.json");
|
||||
if !path.is_file() { return Ok(Default::default()); }
|
||||
let Ok(bytes) = std::fs::read(path) else { return Ok(Default::default()); };
|
||||
if bytes.len() > 1024 * 1024 { return Ok(Default::default()); }
|
||||
let Ok(settings) = serde_json::from_slice::<Settings>(&bytes) else { return Ok(Default::default()); };
|
||||
if settings.format != 1 { return Ok(Default::default()); }
|
||||
if !path.is_file() {
|
||||
return Ok(Default::default());
|
||||
}
|
||||
let Ok(bytes) = std::fs::read(path) else {
|
||||
return Ok(Default::default());
|
||||
};
|
||||
if bytes.len() > 1024 * 1024 {
|
||||
return Ok(Default::default());
|
||||
}
|
||||
let Ok(settings) = serde_json::from_slice::<Settings>(&bytes) else {
|
||||
return Ok(Default::default());
|
||||
};
|
||||
if settings.format != 1 {
|
||||
return Ok(Default::default());
|
||||
}
|
||||
let mut fields = std::collections::BTreeMap::new();
|
||||
for value in settings.settings {
|
||||
let Ok(mut field) = serde_json::from_value::<Field>(value) else { continue; };
|
||||
let Ok(mut field) = serde_json::from_value::<Field>(value) else {
|
||||
continue;
|
||||
};
|
||||
field.group_title = settings.groups.get(&field.group).cloned();
|
||||
if !matches!(field.kind.as_str(), "bool" | "int" | "float" | "string")
|
||||
|| field.key.len() > 200 || field.key.is_empty()
|
||||
|| !field.key.chars().all(|c| c.is_ascii_alphanumeric() || "._".contains(c))
|
||||
|| field.min.zip(field.max).is_some_and(|(min,max)| min > max)
|
||||
|| field.key.len() > 200
|
||||
|| field.key.is_empty()
|
||||
|| !field
|
||||
.key
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || "._".contains(c))
|
||||
|| field.min.zip(field.max).is_some_and(|(min, max)| min > max)
|
||||
|| !default_type(&field.kind, &field.default)
|
||||
|| field.default_if_missing.as_ref().is_some_and(|value| !default_type(&field.kind, value))
|
||||
|| field
|
||||
.default_if_missing
|
||||
.as_ref()
|
||||
.is_some_and(|value| !default_type(&field.kind, value))
|
||||
|| !valid_choices(&field)
|
||||
|| fields.contains_key(&field.key) { continue; }
|
||||
|| fields.contains_key(&field.key)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
fields.insert(field.key.clone(), field);
|
||||
}
|
||||
Ok(fields)
|
||||
@@ -90,8 +144,14 @@ pub fn fields(root: &Path) -> crate::Result<std::collections::BTreeMap<String, F
|
||||
/// A list of choices must be small, labelled, free of repeats and made of values of the field's own type.
|
||||
fn valid_choices(field: &Field) -> bool {
|
||||
field.choices.len() <= 64
|
||||
&& field.choices.iter().all(|c| !c.label.trim().is_empty() && c.label.len() <= 80 && default_type(&field.kind, &c.value))
|
||||
&& field.choices.iter().enumerate().all(|(i, c)| field.choices[..i].iter().all(|earlier| scalar(&earlier.value) != scalar(&c.value)))
|
||||
&& field.choices.iter().all(|c| {
|
||||
!c.label.trim().is_empty() && c.label.len() <= 80 && default_type(&field.kind, &c.value)
|
||||
})
|
||||
&& field.choices.iter().enumerate().all(|(i, c)| {
|
||||
field.choices[..i]
|
||||
.iter()
|
||||
.all(|earlier| scalar(&earlier.value) != scalar(&c.value))
|
||||
})
|
||||
}
|
||||
|
||||
fn default_type(kind: &str, value: &serde_json::Value) -> bool {
|
||||
@@ -116,13 +176,27 @@ pub fn scalar(value: &serde_json::Value) -> Option<String> {
|
||||
pub fn validate(field: &Field, value: &str) -> crate::Result<()> {
|
||||
let valid = match field.kind.as_str() {
|
||||
"bool" => matches!(value, "0" | "1"),
|
||||
"int" | "float" => value.parse::<f64>().is_ok_and(|number| number.is_finite()
|
||||
&& (field.kind != "int" || number.fract() == 0.0)
|
||||
&& field.min.is_none_or(|min| number >= min) && field.max.is_none_or(|max| number <= max)),
|
||||
"int" | "float" => value.parse::<f64>().is_ok_and(|number| {
|
||||
number.is_finite()
|
||||
&& (field.kind != "int" || number.fract() == 0.0)
|
||||
&& field.min.is_none_or(|min| number >= min)
|
||||
&& field.max.is_none_or(|max| number <= max)
|
||||
}),
|
||||
"string" => true,
|
||||
_ => false,
|
||||
} && (field.choices.is_empty() || field.choices.iter().any(|c| scalar(&c.value).as_deref() == Some(value)));
|
||||
if valid { Ok(()) } else { Err(crate::Error::Invalid(format!("{} must match its documented SquidBots type and range.", field.key))) }
|
||||
} && (field.choices.is_empty()
|
||||
|| field
|
||||
.choices
|
||||
.iter()
|
||||
.any(|c| scalar(&c.value).as_deref() == Some(value)));
|
||||
if valid {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(crate::Error::Invalid(format!(
|
||||
"{} must match its documented SquidBots type and range.",
|
||||
field.key
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -138,9 +212,16 @@ mod tests {
|
||||
let fields = fields(root.path()).unwrap();
|
||||
let locale = &fields["AiPlayerbot.BotTextLocale"];
|
||||
assert_eq!(locale.choices.len(), 3);
|
||||
for ok in ["-1", "0", "8"] { assert!(validate(locale, ok).is_ok(), "{ok}"); }
|
||||
for bad in ["1", "7", "x"] { assert!(validate(locale, bad).is_err(), "{bad}"); }
|
||||
assert!(!fields.contains_key("AiPlayerbot.Broken"), "a choice of the wrong type invalidates the setting");
|
||||
for ok in ["-1", "0", "8"] {
|
||||
assert!(validate(locale, ok).is_ok(), "{ok}");
|
||||
}
|
||||
for bad in ["1", "7", "x"] {
|
||||
assert!(validate(locale, bad).is_err(), "{bad}");
|
||||
}
|
||||
assert!(
|
||||
!fields.contains_key("AiPlayerbot.Broken"),
|
||||
"a choice of the wrong type invalidates the setting"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -150,15 +231,26 @@ mod tests {
|
||||
let fields = fields(root.path()).unwrap();
|
||||
let field = &fields["AiPlayerbot.MinRandomBots"];
|
||||
assert_eq!(scalar(&field.default).as_deref(), Some("500"));
|
||||
assert_eq!(scalar(field.default_if_missing.as_ref().unwrap()).as_deref(), Some("50"));
|
||||
assert_eq!(
|
||||
scalar(field.default_if_missing.as_ref().unwrap()).as_deref(),
|
||||
Some("50")
|
||||
);
|
||||
assert!(validate(field, "5000").is_ok());
|
||||
for value in ["5001", "-1", "0.5", "NaN"] { assert!(validate(field, value).is_err()); }
|
||||
for value in ["5001", "-1", "0.5", "NaN"] {
|
||||
assert!(validate(field, value).is_err());
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn unsupported_metadata_falls_back_and_bad_entries_do_not_hide_valid_fields() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
let path = root.path().join("Core/configs/modules/playerbots.conf.settings.json");
|
||||
for data in [r#"{"format":99,"settings":[]}"#, "invalid json", r#"{"format":1,"settings":{}}"#] {
|
||||
let path = root
|
||||
.path()
|
||||
.join("Core/configs/modules/playerbots.conf.settings.json");
|
||||
for data in [
|
||||
r#"{"format":99,"settings":[]}"#,
|
||||
"invalid json",
|
||||
r#"{"format":1,"settings":{}}"#,
|
||||
] {
|
||||
crate::fsx::atomic_write(&path, data.as_bytes()).unwrap();
|
||||
assert!(fields(root.path()).unwrap().is_empty());
|
||||
}
|
||||
@@ -172,16 +264,44 @@ mod tests {
|
||||
fn imported_revision_uses_its_first_word_and_empty_tags_are_hidden() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
crate::fsx::atomic_write(&root.path().join("CoA-Bots/release.json"), br#"{"tag":"","botsModuleRevision":"48c4786a 2026-10-01 (coa branch)","commit":"abcdef01"}"#).unwrap();
|
||||
assert_eq!(release(root.path()).unwrap(), Release { tag: None, commit: Some("48c4786a".into()) });
|
||||
crate::fsx::atomic_write(&root.path().join("Core/configs/modules/playerbots.conf.settings.json"), br#"{"tag":"v1.8.1","commit":"11111111"}"#).unwrap();
|
||||
assert_eq!(release(root.path()).unwrap(), Release { tag: Some("v1.8.1".into()), commit: None });
|
||||
assert_eq!(
|
||||
release(root.path()).unwrap(),
|
||||
Release {
|
||||
tag: None,
|
||||
commit: Some("48c4786a".into())
|
||||
}
|
||||
);
|
||||
crate::fsx::atomic_write(
|
||||
&root
|
||||
.path()
|
||||
.join("Core/configs/modules/playerbots.conf.settings.json"),
|
||||
br#"{"tag":"v1.8.1","commit":"11111111"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
release(root.path()).unwrap(),
|
||||
Release {
|
||||
tag: Some("v1.8.1".into()),
|
||||
commit: None
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diagnostics_only_expose_release_identifiers() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
crate::fsx::atomic_write(&root.path().join("CoA-Bots/release.json"), br#"{"tag":"v1.8","commit":"48c4786a","password":"secret"}"#).unwrap();
|
||||
crate::fsx::atomic_write(
|
||||
&root.path().join("CoA-Bots/release.json"),
|
||||
br#"{"tag":"v1.8","commit":"48c4786a","password":"secret"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(imported_repack(root.path()));
|
||||
assert_eq!(release(root.path()).unwrap(), Release { tag: Some("v1.8".into()), commit: Some("48c4786a".into()) });
|
||||
assert_eq!(
|
||||
release(root.path()).unwrap(),
|
||||
Release {
|
||||
tag: Some("v1.8".into()),
|
||||
commit: Some("48c4786a".into())
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user