feat(phase15): release and security hardening, DPAPI key store, probe & protocol hardening, secret redaction, audit and readiness report

This commit is contained in:
Дмитрий committed 2026-10-09 21:59:44 +05:00
1 parent b69fa6b867
commit 51e0f136d6
207 files changed
+38235 -7770

No files matched your search

+211 -49
View File
@@ -36,7 +36,10 @@ pub struct Preflight {
}
pub(crate) fn problem(code: &'static str, message: &str) -> Problem {
Problem { code, message: message.into() }
Problem {
code,
message: message.into(),
}
}
/// Checks whether `dest` is a sensible place for a new server (spec section 32).
@@ -45,24 +48,53 @@ pub fn preflight(dest: &Path, needed_bytes: u64, registry: &Registry) -> Preflig
let s = dest.to_string_lossy();
if !dest.is_absolute() {
problems.push(problem("relative", "Choose a full folder path, such as C:\\Games\\CoA Server."));
problems.push(problem(
"relative",
"Choose a full folder path, such as C:\\Games\\CoA Server.",
));
}
if !s.is_ascii() {
problems.push(problem("non_ascii", "Choose a folder whose path uses only English letters and digits, such as C:\\Games\\CoA Server."));
}
if s.len() > 100 {
problems.push(problem("too_long", "That folder path is too long. Choose a shorter one."));
problems.push(problem(
"too_long",
"That folder path is too long. Choose a shorter one.",
));
}
let lower = s.to_lowercase().replace('/', "\\");
let trimmed = lower.trim_end_matches('\\');
let protected_roots = ["c:\\windows", "c:\\program files", "c:\\program files (x86)", "c:\\programdata"];
if trimmed.len() <= 3 || protected_roots.iter().any(|p| trimmed == *p || trimmed.starts_with(&format!("{p}\\"))) {
problems.push(problem("system_folder", "Please choose a normal folder for your games, not a system location or a drive root."));
let protected_roots = [
"c:\\windows",
"c:\\program files",
"c:\\program files (x86)",
"c:\\programdata",
];
if trimmed.len() <= 3
|| protected_roots
.iter()
.any(|p| trimmed == *p || trimmed.starts_with(&format!("{p}\\")))
{
problems.push(problem(
"system_folder",
"Please choose a normal folder for your games, not a system location or a drive root.",
));
}
if let Some(profile) = std::env::var_os("USERPROFILE") {
let profile = profile.to_string_lossy().to_lowercase();
let special = ["", "\\documents", "\\desktop", "\\downloads", "\\pictures", "\\music", "\\videos"];
if special.iter().any(|sfx| trimmed == format!("{profile}{sfx}")) {
let special = [
"",
"\\documents",
"\\desktop",
"\\downloads",
"\\pictures",
"\\music",
"\\videos",
];
if special
.iter()
.any(|sfx| trimmed == format!("{profile}{sfx}"))
{
problems.push(problem("personal_folder", "This folder contains unrelated files. Choose another folder or create a new CoA Server folder."));
}
}
@@ -83,16 +115,25 @@ pub fn preflight(dest: &Path, needed_bytes: u64, registry: &Registry) -> Preflig
}
}
}
Err(_) => problems.push(problem("unreadable", "This folder cannot be read. Choose another one.")),
Err(_) => problems.push(problem(
"unreadable",
"This folder cannot be read. Choose another one.",
)),
}
}
if let Ok(list) = registry.list() {
for (_, existing) in list {
let (a, b) = (fsx::canonicalize_lenient(dest), fsx::canonicalize_lenient(&existing));
let (a, b) = (
fsx::canonicalize_lenient(dest),
fsx::canonicalize_lenient(&existing),
);
if let (Ok(a), Ok(b)) = (a, b) {
if fsx::starts_with_ci(&a, &b) || fsx::starts_with_ci(&b, &a) {
problems.push(problem("registered", "A server is already registered at or around this location."));
problems.push(problem(
"registered",
"A server is already registered at or around this location.",
));
break;
}
}
@@ -100,11 +141,24 @@ pub fn preflight(dest: &Path, needed_bytes: u64, registry: &Registry) -> Preflig
}
let free_bytes = fsx::free_space(dest).unwrap_or(0);
let need = needed_bytes.saturating_add(needed_bytes / 5).saturating_add(512 * 1024 * 1024);
let need = needed_bytes
.saturating_add(needed_bytes / 5)
.saturating_add(512 * 1024 * 1024);
if free_bytes < need {
problems.push(problem("space", &format!("Not enough free space: about {} GB needed, {} GB available.", need / (1 << 30) + 1, free_bytes / (1 << 30))));
problems.push(problem(
"space",
&format!(
"Not enough free space: about {} GB needed, {} GB available.",
need / (1 << 30) + 1,
free_bytes / (1 << 30)
),
));
}
Preflight {
ok: problems.is_empty(),
problems,
free_bytes,
}
Preflight { ok: problems.is_empty(), problems, free_bytes }
}
pub use crate::pkgsource::Source;
@@ -149,7 +203,9 @@ pub(crate) fn bootstrap_database(root: &Path) -> Result<()> {
}
pub(crate) fn free_port() -> Result<u16> {
Ok(std::net::TcpListener::bind("127.0.0.1:0")?.local_addr()?.port())
Ok(std::net::TcpListener::bind("127.0.0.1:0")?
.local_addr()?
.port())
}
/// Run `f` with the staging copy listening on unused ports, so an installation never collides with a server (or any
@@ -177,36 +233,55 @@ fn bootstrap_database_inner(root: &Path) -> Result<()> {
let boot = root.join(BOOTSTRAP_CREDENTIALS);
let target = root.join("Settings/database.json");
if !boot.is_file() {
return Err(Error::Invalid("The package has no database bootstrap information.".into()));
return Err(Error::Invalid(
"The package has no database bootstrap information.".into(),
));
}
fs::copy(&boot, &target)?; // launcher needs database.json to render its files at first start
let started = driver::run(root, Verb::StartMysql)?;
if !started.ok {
return Err(Error::Invalid(format!("The database could not be started. {}", driver::startup_failure(root, &started))));
return Err(Error::Invalid(format!(
"The database could not be started. {}",
driver::startup_failure(root, &started)
)));
}
let result = (|| -> Result<()> {
let db = Db::from_repack(root, Account::Admin)?;
let (root_pw, app_pw) = (random_hex(48), random_hex(48));
let accounts = db.query("SELECT CONCAT(user,'@',host) FROM mysql.user WHERE user IN ('root','acore');")?;
let accounts = db.query(
"SELECT CONCAT(user,'@',host) FROM mysql.user WHERE user IN ('root','acore');",
)?;
let mut sql = String::new();
for line in accounts.lines() {
let (user, host) = line.split_once('@').ok_or_else(|| Error::Invalid("unexpected user list".into()))?;
let (user, host) = line
.split_once('@')
.ok_or_else(|| Error::Invalid("unexpected user list".into()))?;
let pw = if user == "root" { &root_pw } else { &app_pw };
if !host.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '%' | ':' | '-')) {
if !host
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '%' | ':' | '-'))
{
return Err(Error::Invalid("unexpected database host name".into()));
}
sql.push_str(&format!("ALTER USER '{user}'@'{host}' IDENTIFIED BY '{pw}';\n"));
sql.push_str(&format!(
"ALTER USER '{user}'@'{host}' IDENTIFIED BY '{pw}';\n"
));
}
sql.push_str("FLUSH PRIVILEGES;\n");
db.query(&sql)?;
fsx::atomic_write_json(&target, &serde_json::json!({ "rootPassword": root_pw, "appPassword": app_pw }))?;
fsx::atomic_write_json(
&target,
&serde_json::json!({ "rootPassword": root_pw, "appPassword": app_pw }),
)?;
// The server console gets its own account with a random password instead of a shared default.
let console_pw = Db::from_repack(root, Account::Admin)?.provision_service_account()?; // new root password
db::write_console_credentials(root, &console_pw)?;
// The new root password must actually work before we throw the old one away.
if !Db::from_repack(root, Account::Admin)?.ping() {
return Err(Error::Invalid("The new database password did not work.".into()));
return Err(Error::Invalid(
"The new database password did not work.".into(),
));
}
Ok(())
})();
@@ -215,7 +290,12 @@ fn bootstrap_database_inner(root: &Path) -> Result<()> {
// A database that keeps running locks its files, and the folder could not be moved into place afterwards.
match stopped {
Ok(out) if out.ok => {}
Ok(out) => return Err(Error::Invalid(format!("The database could not be stopped after preparing it. {}", driver::startup_failure(root, &out)))),
Ok(out) => {
return Err(Error::Invalid(format!(
"The database could not be stopped after preparing it. {}",
driver::startup_failure(root, &out)
)))
}
Err(e) => return Err(e),
}
fs::remove_file(&boot)?;
@@ -223,10 +303,19 @@ fn bootstrap_database_inner(root: &Path) -> Result<()> {
}
pub fn install_base(p: &Params, report: &dyn Fn(Step)) -> Result<Installed> {
let say = |step: &'static str, percent: u8, detail: Option<String>| report(Step { step, percent, detail });
let say = |step: &'static str, percent: u8, detail: Option<String>| {
report(Step {
step,
percent,
detail,
})
};
let dest = p.dest.clone();
let staging_root = {
let mut n = dest.file_name().ok_or_else(|| Error::Invalid("bad destination".into()))?.to_os_string();
let mut n = dest
.file_name()
.ok_or_else(|| Error::Invalid("bad destination".into()))?
.to_os_string();
n.push(".installing");
dest.with_file_name(n)
};
@@ -236,20 +325,35 @@ pub fn install_base(p: &Params, report: &dyn Fn(Step)) -> Result<Installed> {
// 1. Manifest first: it is signed, and tells us how much space we need.
let (m, manifest_bytes) = fetch_manifest(&p.source, p.trusted_key)?;
if m.kind != manifest::Kind::Base || !m.compatible_with_manager(crate::MANAGER_VERSION) {
return Err(Error::Invalid("This package needs a newer version of CoA Server Manager.".into()));
return Err(Error::Invalid(
"This package needs a newer version of CoA Server Manager.".into(),
));
}
let archive = m.archive.clone().ok_or_else(|| Error::InvalidManifest("no archive".into()))?;
let archive = m
.archive
.clone()
.ok_or_else(|| Error::InvalidManifest("no archive".into()))?;
let download_size: u64 = archive.parts.iter().map(|x| x.size).sum();
let pre = preflight(&dest, archive.unpacked_size + download_size, p.registry);
if !pre.ok {
return Err(Error::Invalid(pre.problems.iter().map(|x| x.message.clone()).collect::<Vec<_>>().join(" ")));
return Err(Error::Invalid(
pre.problems
.iter()
.map(|x| x.message.clone())
.collect::<Vec<_>>()
.join(" "),
));
}
// 2. Download (or use the local folder), verifying every part.
let parts_dir = fetch_parts(&p.source, &m, &meta_dir.join("staging").join("download"), &p.cancel, &|frac, detail| {
say("Downloading server", 5 + (frac * 45.0) as u8, detail)
})?;
let parts_dir = fetch_parts(
&p.source,
&m,
&meta_dir.join("staging").join("download"),
&p.cancel,
&|frac, detail| say("Downloading server", 5 + (frac * 45.0) as u8, detail),
)?;
// 3. Extract into our own staging folder.
if staging_root.exists() {
@@ -264,13 +368,18 @@ pub fn install_base(p: &Params, report: &dyn Fn(Step)) -> Result<Installed> {
let result = (|| -> Result<Installed> {
say("Unpacking", 50, None);
package::extract(&parts_dir, &m, &staging_root, &|done, total| say("Unpacking", 50 + (done * 30 / total.max(1)) as u8, None))?;
package::extract(&parts_dir, &m, &staging_root, &|done, total| {
say("Unpacking", 50 + (done * 30 / total.max(1)) as u8, None)
})?;
say("Preparing database", 82, None);
bootstrap_database(&staging_root)?;
crate::backup::with_database(&staging_root, |db| {
let problems = crate::schema_check::check(db, &staging_root)?;
if let Some(p) = problems.first() {
return Err(Error::Invalid(format!("The packaged database is incomplete: {}.{}.{}: {}.", p.database, p.table, p.column, p.detail)));
return Err(Error::Invalid(format!(
"The packaged database is incomplete: {}.{}.{}: {}.",
p.database, p.table, p.column, p.detail
)));
}
Ok(())
})?;
@@ -286,18 +395,30 @@ pub fn install_base(p: &Params, report: &dyn Fn(Step)) -> Result<Installed> {
let mut meta = InstallMeta::new(InstallKind::New, &dest);
meta.core.commit = m.core.commit.clone();
meta.core.version = Some(m.version.clone());
meta.database.schemas = vec!["acore_auth".into(), "acore_characters".into(), "acore_world".into()];
meta.database.schemas = vec![
"acore_auth".into(),
"acore_characters".into(),
"acore_world".into(),
];
meta.managed_files = m.files.iter().map(|f| f.path.clone()).collect();
for f in &m.files {
meta.original_hashes.insert(f.path.clone(), f.sha256.clone());
meta.original_hashes
.insert(f.path.clone(), f.sha256.clone());
}
let md = MetaDir::create(&dest, &meta)?;
fsx::atomic_write(&md.root.join("manifests").join("base.json"), &manifest_bytes)?;
fsx::atomic_write(
&md.root.join("manifests").join("base.json"),
&manifest_bytes,
)?;
// Complete from here on: the marker goes just before the server is listed.
fs::remove_file(dest.join(MARKER))?;
p.registry.register(&meta.id, &dest)?;
say("Ready", 100, None);
Ok(Installed { id: meta.id, path: dest.to_string_lossy().into_owned(), version: m.version.clone() })
Ok(Installed {
id: meta.id,
path: dest.to_string_lossy().into_owned(),
version: m.version.clone(),
})
})();
if result.is_err() {
@@ -361,9 +482,14 @@ mod tests {
fn refuses_system_folders_relative_paths_and_registered_overlaps() {
let d = tempfile::tempdir().unwrap();
for bad in ["C:\\", "C:\\Windows\\coa", "C:\\Program Files\\coa"] {
assert!(codes(&preflight(Path::new(bad), 1, &reg(d.path()))).contains(&"system_folder"), "{bad}");
assert!(
codes(&preflight(Path::new(bad), 1, &reg(d.path()))).contains(&"system_folder"),
"{bad}"
);
}
assert!(codes(&preflight(Path::new("relative\\dir"), 1, &reg(d.path()))).contains(&"relative"));
assert!(
codes(&preflight(Path::new("relative\\dir"), 1, &reg(d.path()))).contains(&"relative")
);
let r = reg(d.path());
let existing = d.path().join("existing");
fs::create_dir_all(&existing).unwrap();
@@ -374,20 +500,29 @@ mod tests {
#[test]
fn refuses_when_there_is_not_enough_space() {
let d = tempfile::tempdir().unwrap();
assert!(codes(&preflight(&d.path().join("x"), u64::MAX / 4, &reg(d.path()))).contains(&"space"));
assert!(codes(&preflight(
&d.path().join("x"),
u64::MAX / 4,
&reg(d.path())
))
.contains(&"space"));
}
#[test]
fn non_ascii_paths_are_rejected_because_the_launcher_cannot_run_from_them() {
let d = tempfile::tempdir().unwrap();
assert!(codes(&preflight(Path::new("C:\\Игры\\CoA"), 1, &reg(d.path()))).contains(&"non_ascii"));
assert!(
codes(&preflight(Path::new("C:\\Игры\\CoA"), 1, &reg(d.path()))).contains(&"non_ascii")
);
}
#[test]
fn random_credentials_have_the_launcher_required_shape() {
let a = random_hex(48);
assert_eq!(a.len(), 48);
assert!(a.chars().all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()));
assert!(a
.chars()
.all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()));
assert_ne!(a, random_hex(48));
}
@@ -400,22 +535,49 @@ mod tests {
let src = d.path().join("src");
layout::testkit::fake_repack(&src);
fs::create_dir_all(src.join("Settings")).unwrap();
fs::write(src.join("Settings/database.json"), br#"{"rootPassword":"a","appPassword":"b"}"#).unwrap();
let opts = package::BuildOptions { kind: manifest::Kind::Base, version: "0.1.0".into(), core_commit: None, built_at: "x".into(), part_size: 1 << 20, bots_commit: None, migrations: vec![] };
fs::write(
src.join("Settings/database.json"),
br#"{"rootPassword":"a","appPassword":"b"}"#,
)
.unwrap();
let opts = package::BuildOptions {
kind: manifest::Kind::Base,
version: "0.1.0".into(),
core_commit: None,
built_at: "x".into(),
part_size: 1 << 20,
bots_commit: None,
migrations: vec![],
};
package::build(&src, &pkg, &opts, &|_| {}).unwrap();
let good = SigningKey::generate(&mut rand_core::OsRng);
let evil = SigningKey::generate(&mut rand_core::OsRng);
let mbytes = fs::read(pkg.join("manifest.json")).unwrap();
let enc = |k: &SigningKey| base64::engine::general_purpose::STANDARD.encode(k.sign(&mbytes).to_bytes());
let trusted = base64::engine::general_purpose::STANDARD.encode(good.verifying_key().to_bytes());
let enc = |k: &SigningKey| {
base64::engine::general_purpose::STANDARD.encode(k.sign(&mbytes).to_bytes())
};
let trusted =
base64::engine::general_purpose::STANDARD.encode(good.verifying_key().to_bytes());
let r = reg(d.path());
let dest = d.path().join("dest");
let run = |sig: &str| {
fs::write(pkg.join("manifest.json.sig"), sig).unwrap();
install_base(&Params { source: Source::Dir(pkg.clone()), dest: dest.clone(), trusted_key: &trusted, registry: &r, cancel: Cancel::default() }, &|_| {})
install_base(
&Params {
source: Source::Dir(pkg.clone()),
dest: dest.clone(),
trusted_key: &trusted,
registry: &r,
cancel: Cancel::default(),
},
&|_| {},
)
};
assert!(run(&enc(&evil)).is_err(), "signed by the wrong key");
assert!(run("").is_err(), "empty signature");
assert!(!dest.exists() && !dest.with_file_name("dest.installing").exists(), "nothing created when the signature is bad");
assert!(
!dest.exists() && !dest.with_file_name("dest.installing").exists(),
"nothing created when the signature is bad"
);
}
}