diff --git a/changelog.d/20261005-validate-release-database.md b/changelog.d/20261005-validate-release-database.md new file mode 100644 index 0000000..918f08f --- /dev/null +++ b/changelog.d/20261005-validate-release-database.md @@ -0,0 +1,7 @@ +--- +area: manager +type: fixed +audience: admins +title: Server packages must include a complete database schema check +--- +Release tools generate the expected database structure on an isolated copy of the signed base, verify all archive files and refuse packages without the schema check. Corrective SQL can restore missing Wildcard tables without replaying old updates. diff --git a/crates/coa-core/Cargo.toml b/crates/coa-core/Cargo.toml index 8558dbb..1bea4ee 100644 --- a/crates/coa-core/Cargo.toml +++ b/crates/coa-core/Cargo.toml @@ -24,9 +24,9 @@ num-bigint = "0.4" zip = { version = "2", default-features = false, features = ["deflate"] } sha1 = "0.10" tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "std"] } +tempfile = "3" [dev-dependencies] -tempfile = "3" rand_core = { version = "0.6", features = ["getrandom"] } ed25519-dalek = { version = "2", features = ["rand_core"] } diff --git a/crates/coa-core/examples/wildcard_repair_e2e.rs b/crates/coa-core/examples/wildcard_repair_e2e.rs new file mode 100644 index 0000000..d9ebec1 --- /dev/null +++ b/crates/coa-core/examples/wildcard_repair_e2e.rs @@ -0,0 +1,58 @@ +//! Reproduce applied migration history with five missing tables on a disposable MySQL fixture. +use std::path::PathBuf; +use coa_core::{backup, fsx, manifest::Migration, migrations::{self, LedgerRow, Status, Store}, release_schema, schema_check, Error, Result}; + +fn main() -> Result<()> { + let mut args = std::env::args().skip(1); + let root = PathBuf::from(args.next().ok_or_else(|| Error::Invalid("fixture path required".into()))?); + let old_sql = PathBuf::from(args.next().ok_or_else(|| Error::Invalid("core SQL directory required".into()))?); + let repairs = PathBuf::from(args.next().ok_or_else(|| Error::Invalid("repair SQL directory required".into()))?); + if !root.file_name().is_some_and(|n| n.to_string_lossy().starts_with("coa-update-recovery-fixture-")) + || !root.join("recovery-e2e-started").exists() { + return Err(Error::Invalid("Refusing a non-disposable recovery fixture.".into())); + } + let tree = tempfile::tempdir()?; + backup::with_database(&root, |db| { + let db = db.clone().for_realm(coa_core::realms::Mode::Coa); + let characters = db.query("SELECT COUNT(*) FROM acore_characters.characters;")?; + db.load()?; + // Record exactly the old file hashes, as in the user's all-applied report. + let mut migrations = Vec::new(); + for name in ["rev_20260929_92_coa_wildcard_skill_cards", "rev_20260929_93_coa_wildcard_skill_card_duplicates", "rev_20260929_94_coa_wildcard_specialization_cache"] { + let path = old_sql.join(format!("{name}.sql")); + let hash = fsx::sha256_file(&path)?; + db.put(&LedgerRow { db: "characters".into(), id: name.into(), sha256: hash.clone(), status: Status::Applied, error: None, baseline: false })?; + migrations.push(Migration { db: "characters".into(), id: name.into(), sha256: hash, destructive: false }); + } + for (_, tables) in schema_check::WILDCARD_TABLES.iter().filter(|(kind,_)| *kind == "characters") { + for table in *tables { db.query(&format!("DROP TABLE IF EXISTS acore_characters.`{table}`;"))?; } + } + assert!(migrations::status(&db, &migrations)?.iter().all(|m| m.status == Status::Applied)); + assert!(!db.tables("acore_characters")?.iter().any(|t| t == "coa_wildcard_skill_card")); + let empty_core = tempfile::tempdir()?; + let files = release_schema::collect(empty_core.path(), None, Some(&repairs))?; + assert_eq!(files.len(), 1); + let repair = &files[0]; + migrations.push(Migration { db: repair.db.clone(), id: repair.id.clone(), sha256: repair.sha256.clone(), destructive: false }); + let staged = tree.path().join("sql/characters"); + fsx::atomic_write(&staged.join(format!("{}.sql", repair.id)), &std::fs::read(&repair.abs)?)?; + let report = migrations::apply_pending(&db, &migrations, &tree.path().join("sql"), &|| Ok("disposable fixture".into()))?; + assert!(report.failed.is_none()); + assert_eq!(report.applied, [repair.id.clone()]); + assert_eq!(db.query("SELECT COUNT(*) FROM acore_characters.characters;")?, characters); + db.query("INSERT INTO acore_characters.coa_wildcard_skill_card VALUES (77,123,456);")?; + db.run_sql_file("acore_characters", &repair.abs)?; + assert_eq!(db.query("SELECT progress FROM acore_characters.coa_wildcard_skill_card WHERE account=77 AND card=123;")?, "456"); + assert!(migrations::apply_pending(&db, &migrations, &tree.path().join("sql"), &|| Ok("disposable fixture".into()))?.applied.is_empty()); + schema_check::capture(&db, tree.path())?; + db.query("DROP TABLE acore_characters.coa_wildcard_skill_card_purchase;")?; + assert!(schema_check::check(&db, tree.path())?.iter().any(|p| p.table == "coa_wildcard_skill_card_purchase" && p.detail == "Missing table")); + db.run_sql_file("acore_characters", &repair.abs)?; + db.query("ALTER TABLE acore_characters.coa_wildcard_skill_card DROP COLUMN progress;")?; + assert!(schema_check::check(&db, tree.path())?.iter().any(|p| p.table == "coa_wildcard_skill_card" && p.column == "progress")); + db.query("ALTER TABLE acore_characters.coa_wildcard_skill_card ADD COLUMN progress INT UNSIGNED NOT NULL DEFAULT 0;")?; + Ok(()) + })?; + println!("Applied history with all five tables missing repaired; characters and existing card rows preserved; full contract detects missing tables and columns."); + Ok(()) +} diff --git a/crates/coa-core/src/cleanbase.rs b/crates/coa-core/src/cleanbase.rs index d9b7b07..9519ff7 100644 --- a/crates/coa-core/src/cleanbase.rs +++ b/crates/coa-core/src/cleanbase.rs @@ -256,6 +256,9 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> { if accounts != "0" || chars != "0" || templates != 21 { return Err(Error::Invalid(format!("The database is not clean ({accounts} accounts, {chars} player characters, {templates} templates)."))); } + if let Some(p) = crate::schema_check::check(db, p.tree)?.first() { + return Err(Error::Invalid(format!("The rebuilt base database is incomplete: {}.{}.{}: {}", p.database, p.table, p.column, p.detail))); + } crate::schema_check::capture(db, out)?; Ok(()) })?; @@ -276,7 +279,7 @@ pub fn build(p: &Params, say: &dyn Fn(&str)) -> Result<()> { fsx::atomic_write_json(&repack_json, &shipped)?; say("Adding the release files"); - copy_dir(p.tree, p.out, &|_| false, p.tree)?; + copy_dir(p.tree, p.out, &|rel| rel == crate::schema_check::CONTRACT, p.tree)?; if let Some(data) = p.data { say("Copying game data"); fs::create_dir_all(p.out.join("Data"))?; diff --git a/crates/coa-core/src/install.rs b/crates/coa-core/src/install.rs index 16df59e..bea801d 100644 --- a/crates/coa-core/src/install.rs +++ b/crates/coa-core/src/install.rs @@ -144,7 +144,7 @@ fn random_hex(n: usize) -> String { /// Rotate the database users' passwords away from the packaged bootstrap values and write the launcher's /// `Settings/database.json`. MySQL is started from `root` and stopped again before returning. -fn bootstrap_database(root: &Path) -> Result<()> { +pub(crate) fn bootstrap_database(root: &Path) -> Result<()> { with_scratch_ports(root, || bootstrap_database_inner(root)) } @@ -154,7 +154,7 @@ fn free_port() -> Result { /// Run `f` with the staging copy listening on unused ports, so an installation never collides with a server (or any /// other program) already using the shipped ports. The shipped ports are put back afterwards. -fn with_scratch_ports(root: &Path, f: impl FnOnce() -> Result) -> Result { +pub(crate) fn with_scratch_ports(root: &Path, f: impl FnOnce() -> Result) -> Result { const KEYS: [&str; 4] = ["mysqlPort", "authPort", "worldPort", "raPort"]; let path = root.join("Settings/repack.json"); let original: serde_json::Value = fsx::read_json(&path)?; diff --git a/crates/coa-core/src/lib.rs b/crates/coa-core/src/lib.rs index a3b0dc1..882bfde 100644 --- a/crates/coa-core/src/lib.rs +++ b/crates/coa-core/src/lib.rs @@ -36,6 +36,7 @@ pub mod realmlist; pub mod realms; pub mod registry; pub mod release; +pub mod release_schema; pub mod update; pub mod upnp; pub mod signing; diff --git a/crates/coa-core/src/package.rs b/crates/coa-core/src/package.rs index f5aa72c..e03b745 100644 --- a/crates/coa-core/src/package.rs +++ b/crates/coa-core/src/package.rs @@ -257,6 +257,11 @@ impl Read for PartsReader { /// Unpack verified `parts_dir` into `dest` (an empty or new staging folder). Every file must be listed in the /// manifest with the recorded size and hash; anything else aborts the extraction. pub fn extract(parts_dir: &Path, manifest: &Manifest, dest: &Path, progress: &dyn Fn(u64, u64)) -> Result<()> { + extract_selected(parts_dir, manifest, dest, &|_| true, progress) +} + +/// Verify the entire archive, writing only selected files for a disposable release-schema fixture. +pub fn extract_selected(parts_dir: &Path, manifest: &Manifest, dest: &Path, select: &dyn Fn(&str) -> bool, progress: &dyn Fn(u64, u64)) -> Result<()> { let archive = manifest.archive.as_ref().ok_or_else(|| Error::InvalidManifest("manifest has no archive".into()))?; let mut paths = Vec::new(); for part in &archive.parts { @@ -271,7 +276,8 @@ pub fn extract(parts_dir: &Path, manifest: &Manifest, dest: &Path, progress: &dy } paths.push(p); } - fsx::require_space(dest, archive.unpacked_size)?; + let selected_size = manifest.files.iter().filter(|f| select(&f.path)).map(|f| f.size).sum(); + fsx::require_space(dest, selected_size)?; fs::create_dir_all(dest)?; let expected: HashMap = manifest.files.iter().map(|f| (f.path.replace('\\', "/").to_lowercase(), f)).collect(); @@ -294,8 +300,11 @@ pub fn extract(parts_dir: &Path, manifest: &Manifest, dest: &Path, progress: &dy return Err(Error::PathRejected(format!("{rel}: appears twice in the archive"))); } let target = fsx::safe_join(dest, &want.path)?; - fs::create_dir_all(target.parent().unwrap())?; - let mut out = BufWriter::new(File::create(&target)?); + let selected = select(&want.path); + let mut out = if selected { + fs::create_dir_all(target.parent().unwrap())?; + Some(BufWriter::new(File::create(&target)?)) + } else { None }; let mut hasher = Sha256::new(); let mut buf = vec![0u8; 1 << 20]; let mut size = 0u64; @@ -313,9 +322,9 @@ pub fn extract(parts_dir: &Path, manifest: &Manifest, dest: &Path, progress: &dy return Err(Error::HashMismatch { path: rel, expected: format!("{} bytes", want.size), actual: "more".into() }); } hasher.update(&buf[..n]); - out.write_all(&buf[..n])?; + if let Some(out) = out.as_mut() { out.write_all(&buf[..n])?; } } - out.flush()?; + if let Some(out) = out.as_mut() { out.flush()?; } let actual = hex::encode(hasher.finalize()); if size != want.size || !actual.eq_ignore_ascii_case(&want.sha256) { return Err(Error::HashMismatch { path: rel, expected: want.sha256.clone(), actual }); @@ -323,7 +332,7 @@ pub fn extract(parts_dir: &Path, manifest: &Manifest, dest: &Path, progress: &dy // The archive does not carry per-file modes, so a Linux binary or script would be extracted unusable. // Recognise them by content (ELF header or `#!`); Windows has no execute bit and ignores this. #[cfg(unix)] - if head.starts_with(b"\x7fELF") || head.starts_with(b"#!") { + if selected && (head.starts_with(b"\x7fELF") || head.starts_with(b"#!")) { use std::os::unix::fs::PermissionsExt; fs::set_permissions(&target, fs::Permissions::from_mode(0o755))?; } @@ -410,6 +419,19 @@ mod tests { assert_eq!(mode("Core/configs/worldserver.conf.dist"), 0, "plain files stay non-executable"); } + #[test] + fn selective_extraction_still_validates_unselected_files() { + let (d, src) = source(); + let out = d.path().join("out"); + let mut manifest = build(&src, &out, &opts(64 * 1024), &|_| {}).unwrap(); + let dest = d.path().join("selected"); + extract_selected(&out, &manifest, &dest, &|p| p.starts_with("Core/"), &|_, _| {}).unwrap(); + assert!(dest.join("Core/worldserver.exe").exists()); + assert!(!dest.join("Data").exists()); + manifest.files.iter_mut().find(|f| f.path.starts_with("Data/")).unwrap().sha256 = "f".repeat(64); + assert!(matches!(extract_selected(&out, &manifest, &d.path().join("bad"), &|_| false, &|_, _| {}), Err(Error::HashMismatch { .. }))); + } + #[test] fn build_split_extract_roundtrip_excludes_noise_and_secrets() { let (d, src) = source(); diff --git a/crates/coa-core/src/release.rs b/crates/coa-core/src/release.rs index 9c0e578..427b03c 100644 --- a/crates/coa-core/src/release.rs +++ b/crates/coa-core/src/release.rs @@ -139,7 +139,7 @@ pub fn pack_update(p: &UpdateParams, progress: &dyn Fn(&str)) -> Result, repairs: Option<&Path>) -> Result> { + let mut files = release::collect_core_sql(core)?; + if let Some(bots) = bots { files.extend(release::collect_bots_sql(bots)?); } + if let Some(repairs) = repairs { + for kind in ["auth", "characters", "world"] { + let dir = repairs.join(kind); + if !dir.exists() { continue; } + let mut paths: Vec<_> = std::fs::read_dir(&dir)?.map(|e| e.map(|e| e.path())).collect::>()?; + paths.sort(); + for path in paths.into_iter().filter(|p| p.extension().is_some_and(|e| e == "sql")) { + let stem = path.file_stem().unwrap().to_string_lossy(); + files.push(SqlFile { db: kind.into(), id: format!("manager_repair__{stem}"), origin: format!("{kind}/{stem}.sql"), sha256: fsx::sha256_file(&path)?, abs: path }); + } + } + } + let mut seen = HashSet::new(); + for file in &files { + if !seen.insert((&file.db, &file.id)) { return Err(Error::Invalid(format!("Duplicate release migration: {} / {}", file.db, file.id))); } + } + Ok(files) +} + +pub fn extract_base(source: &Path, fixture: &Path, trusted_key: &str) -> Result<()> { + if fixture.exists() || !fixture.file_name().is_some_and(|n| n.to_string_lossy().starts_with("coa-schema-fixture-")) { + return Err(Error::Invalid("Use a new coa-schema-fixture-* directory for release validation.".into())); + } + let (manifest, _) = pkgsource::fetch_manifest(&pkgsource::Source::Dir(source.into()), trusted_key)?; + if manifest.kind != Kind::Base { return Err(Error::Invalid("Release schema validation needs a signed base package.".into())); } + package::extract_selected(source, &manifest, fixture, &|path| { + let path = path.to_ascii_lowercase(); + ["mysql/", "settings/", "runtime/", "scripts/", "core/", "bugreport/"].iter().any(|prefix| path.starts_with(prefix)) + }, &|_, _| {})?; + fsx::atomic_write(&fixture.join(MARKER), b"disposable release-schema fixture")?; + install::bootstrap_database(fixture) +} + +pub fn capture_release(fixture: &Path, tree: &Path, sql: &[SqlFile]) -> Result<()> { + if !fixture.file_name().is_some_and(|n| n.to_string_lossy().starts_with("coa-schema-fixture-")) + || std::fs::read(fixture.join(MARKER)).ok().as_deref() != Some(b"disposable release-schema fixture") { + return Err(Error::Invalid("Refusing to apply release SQL to a non-fixture server.".into())); + } + let staging = tempfile::tempdir()?; + let migrations: Vec<_> = sql.iter().map(|f| Migration { db: f.db.clone(), id: f.id.clone(), sha256: f.sha256.clone(), destructive: false }).collect(); + for file in sql { + let path = fsx::safe_join(staging.path(), &format!("{}/{}.sql", file.db, file.id))?; + fsx::atomic_write(&path, &std::fs::read(&file.abs)?)?; + } + install::with_scratch_ports(fixture, || backup::with_database(fixture, |db| { + let db = db.clone().for_realm(crate::realms::Mode::Coa); + let report = migrations::apply_pending(&db, &migrations, staging.path(), &|| Ok("disposable release fixture".into()))?; + if let Some((id, why)) = report.failed { return Err(Error::Invalid(format!("Release migration {id} failed: {why}"))); } + // Validate known critical feature effects before capturing; history may be stale in the base. + let previous = tree.join(schema_check::CONTRACT); + if previous.exists() { return Err(Error::Invalid("Release tree already contains a schema contract; generate it from a fresh tree.".into())); } + let problems = schema_check::check(&db, tree)?; + if let Some(p) = problems.first() { return Err(Error::Invalid(format!("Release database failed validation: {}.{}.{}: {}", p.database, p.table, p.column, p.detail))); } + schema_check::capture(&db, tree)?; + schema_check::require_release_contract(tree)?; + Ok(()) + })) +} + +pub fn verify_package(dir: &Path, trusted_key: &str) -> Result<()> { + let (manifest, _) = pkgsource::fetch_manifest(&pkgsource::Source::Dir(dir.into()), trusted_key)?; + if !manifest.files.iter().any(|f| f.path == schema_check::CONTRACT && f.size > 0) { + return Err(Error::Invalid("The release is missing its database schema contract.".into())); + } + let scratch = tempfile::tempdir()?; + package::extract_selected(dir, &manifest, scratch.path(), &|p| p == schema_check::CONTRACT, &|_, _| {})?; + schema_check::require_release_contract(scratch.path())?; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use base64::{engine::general_purpose::STANDARD, Engine}; + use ed25519_dalek::{Signer, SigningKey}; + + #[test] + fn signed_packages_must_carry_a_real_complete_schema_contract() { + let dir = tempfile::tempdir().unwrap(); + let tree = dir.path().join("tree"); + let out = dir.path().join("out"); + fsx::atomic_write(&tree.join("Core/worldserver.exe"), b"fixture").unwrap(); + let key = SigningKey::from_bytes(&[7; 32]); + let public = STANDARD.encode(key.verifying_key().as_bytes()); + let opts = package::BuildOptions { kind: Kind::Update, version: "1.0.0".into(), core_commit: None, bots_commit: None, built_at: "test".into(), part_size: 1024 * 1024, migrations: vec![] }; + let sign = || { + let bytes = std::fs::read(out.join("manifest.json")).unwrap(); + fsx::atomic_write(&out.join("manifest.json.sig"), STANDARD.encode(key.sign(&bytes).to_bytes()).as_bytes()).unwrap(); + }; + package::build(&tree, &out, &opts, &|_| {}).unwrap(); sign(); + assert!(verify_package(&out, &public).is_err()); + fsx::atomic_write(&tree.join(schema_check::CONTRACT), br#"{"schema":1,"columns":{}}"#).unwrap(); + package::build(&tree, &out, &opts, &|_| {}).unwrap(); sign(); + assert!(verify_package(&out, &public).is_err()); + fsx::atomic_write(&tree.join(schema_check::CONTRACT), br#"{"schema":1,"columns":{"auth":{"account":{"id":"int"}},"characters":{"characters":{"guid":"int"}},"world":{"creature":{"id":"int"}}}}"#).unwrap(); + package::build(&tree, &out, &opts, &|_| {}).unwrap(); sign(); + verify_package(&out, &public).unwrap(); + } + #[test] + fn repair_sql_has_new_ids_and_runs_after_original_sql() { + let dir = tempfile::tempdir().unwrap(); + let core = dir.path().join("core"); + let repairs = dir.path().join("repairs"); + fsx::atomic_write(&core.join("data/sql/updates/pending_db_characters/old.sql"), b"SELECT 1;").unwrap(); + fsx::atomic_write(&repairs.join("characters/20261005_wildcard.sql"), b"SELECT 2;").unwrap(); + let files = collect(&core, None, Some(&repairs)).unwrap(); + assert_eq!(files.iter().map(|f| f.id.as_str()).collect::>(), ["old", "manager_repair__20261005_wildcard"]); + assert_eq!(files[1].db, "characters"); + } + #[test] + fn existing_server_cannot_be_used_for_schema_generation() { + let dir = tempfile::tempdir().unwrap(); + assert!(extract_base(dir.path(), dir.path(), "irrelevant").is_err()); + assert!(capture_release(dir.path(), dir.path(), &[]).is_err()); + assert!(!dir.path().join(schema_check::CONTRACT).exists()); + } +} diff --git a/crates/coa-core/src/schema_check.rs b/crates/coa-core/src/schema_check.rs index 71b4ae4..0bb5e5c 100644 --- a/crates/coa-core/src/schema_check.rs +++ b/crates/coa-core/src/schema_check.rs @@ -26,6 +26,16 @@ pub struct Contract { pub columns: Columns, } +pub fn require_release_contract(root: &Path) -> Result { + let contract: Contract = fsx::read_json(&root.join(CONTRACT))?; + if contract.schema != 1 || ["auth", "characters", "world"].iter().any(|kind| { + contract.columns.get(*kind).is_none_or(|tables| tables.is_empty() || tables.values().any(|columns| columns.is_empty())) + }) { + return Err(crate::Error::Invalid("The release must include a complete supported database schema contract.".into())); + } + Ok(contract) +} + #[derive(Debug, Serialize)] pub struct Problem { pub database: String, pub table: String, pub column: String, pub detail: String } @@ -137,6 +147,20 @@ fn missing_coa_starts(rows: &str) -> Vec { mod tests { use super::*; + #[test] + fn a_release_cannot_use_a_partial_or_empty_schema_contract() { + let dir = tempfile::tempdir().unwrap(); + assert!(require_release_contract(dir.path()).is_err()); + fsx::atomic_write_json(&dir.path().join(CONTRACT), &Contract { schema: 1, columns: Columns::new() }).unwrap(); + assert!(require_release_contract(dir.path()).is_err()); + let mut columns = Columns::new(); + for kind in ["auth", "characters", "world"] { + columns.insert(kind.into(), BTreeMap::from([("table".into(), BTreeMap::from([("id".into(), "int|NO||".into())]))])); + } + fsx::atomic_write_json(&dir.path().join(CONTRACT), &Contract { schema: 1, columns }).unwrap(); + assert!(require_release_contract(dir.path()).is_ok()); + } + #[test] fn detects_a_missing_orc_class_pair_even_when_the_class_exists_for_other_races() { let mut rows = String::new(); diff --git a/crates/coa-release/src/main.rs b/crates/coa-release/src/main.rs index fdb8890..2956080 100644 --- a/crates/coa-release/src/main.rs +++ b/crates/coa-release/src/main.rs @@ -2,7 +2,9 @@ //! //! coa-release pack-base --tree DIR --out DIR --version X [--core-commit SHA] [--part-size BYTES] //! coa-release pack-update --tree DIR --base-manifest FILE --core DIR --out DIR --version X -//! [--bots DIR] [--core-commit SHA] [--bots-commit SHA] [--part-size BYTES] +//! [--bots DIR] [--repairs DIR] [--core-commit SHA] [--bots-commit SHA] [--part-size BYTES] +//! coa-release extract-schema-base --package DIR --fixture NEW-coa-schema-fixture-DIR +//! coa-release schema-contract --repack FIXTURE --tree DIR --core DIR [--bots DIR] [--repairs DIR] //! coa-release clean-base --repack DIR --core DIR --tree DIR --out DIR [--bots DIR] [--data DIR] //! coa-release sign --dir DIR (key: env COA_SIGNING_KEY, or ~/.coa-manager/signing/manifest-signing.key) //! coa-release verify --dir DIR (against the public key built into this tool) @@ -12,7 +14,7 @@ use std::path::PathBuf; use coa_core::manifest::{Kind, Manifest}; use coa_core::package::{build, BuildOptions, DEFAULT_PART_SIZE}; -use coa_core::release::{collect_bots_sql, collect_core_sql, pack_update, sign_manifest, UpdateParams}; +use coa_core::release::{pack_update, sign_manifest, UpdateParams}; fn args(rest: &[String]) -> HashMap { let mut m = HashMap::new(); @@ -44,23 +46,38 @@ fn run() -> Result<(), String> { let e = |x: coa_core::Error| x.to_string(); let part = a.get("part-size").and_then(|v| v.parse().ok()).unwrap_or(DEFAULT_PART_SIZE); match cmd.as_str() { + "extract-schema-base" => { + coa_core::release_schema::extract_base( + &PathBuf::from(need(&a, "package")?), &PathBuf::from(need(&a, "fixture")?), + coa_core::signing::EMBEDDED_PUBLIC_KEY, + ).map_err(e)?; + println!("signed base extracted into a disposable database fixture"); + } "schema-contract" => { let repack = PathBuf::from(need(&a, "repack")?); let tree = PathBuf::from(need(&a, "tree")?); - coa_core::backup::with_database(&repack, |db| coa_core::schema_check::capture(db, &tree)).map_err(e)?; + if let Some(core) = a.get("core") { + let bots = a.get("bots").map(PathBuf::from); + let repairs = a.get("repairs").map(PathBuf::from); + let sql = coa_core::release_schema::collect(&PathBuf::from(core), bots.as_deref(), repairs.as_deref()).map_err(e)?; + coa_core::release_schema::capture_release(&repack, &tree, &sql).map_err(e)?; + } else { + coa_core::backup::with_database(&repack, |db| coa_core::schema_check::capture(db, &tree)).map_err(e)?; + } println!("database schema contract captured"); } "pack-base" => { + coa_core::schema_check::require_release_contract(&PathBuf::from(need(&a, "tree")?)).map_err(e)?; let opts = BuildOptions { kind: Kind::Base, version: need(&a, "version")?.clone(), core_commit: a.get("core-commit").cloned(), built_at: chrono_now(), part_size: part, bots_commit: a.get("bots-commit").cloned(), migrations: vec![] }; let m = build(&PathBuf::from(need(&a, "tree")?), &PathBuf::from(need(&a, "out")?), &opts, &|s| eprintln!("{s}")).map_err(e)?; println!("base {}: {} files, {} parts", m.version, m.files.len(), m.archive.as_ref().map(|x| x.parts.len()).unwrap_or(0)); } "pack-update" => { let base: Manifest = Manifest::parse(&std::fs::read(need(&a, "base-manifest")?).map_err(|x| x.to_string())?).map_err(e)?; - let mut sql = collect_core_sql(&PathBuf::from(need(&a, "core")?)).map_err(e)?; - if let Some(b) = a.get("bots") { - sql.extend(collect_bots_sql(&PathBuf::from(b)).map_err(e)?); - } + coa_core::schema_check::require_release_contract(&PathBuf::from(need(&a, "tree")?)).map_err(e)?; + let bots = a.get("bots").map(PathBuf::from); + let repairs = a.get("repairs").map(PathBuf::from); + let sql = coa_core::release_schema::collect(&PathBuf::from(need(&a, "core")?), bots.as_deref(), repairs.as_deref()).map_err(e)?; let out = PathBuf::from(need(&a, "out")?); let m = pack_update( &UpdateParams { tree: &PathBuf::from(need(&a, "tree")?), base_manifest: &base, sql: &sql, out: &out, version: need(&a, "version")?.clone(), core_commit: a.get("core-commit").cloned(), bots_commit: a.get("bots-commit").cloned(), part_size: part }, @@ -78,22 +95,29 @@ fn run() -> Result<(), String> { } "sign" => { let dir = PathBuf::from(need(&a, "dir")?); + require_contract_entry(&dir)?; sign_manifest(&dir, &signing_key()?).map_err(e)?; + coa_core::release_schema::verify_package(&dir, coa_core::signing::EMBEDDED_PUBLIC_KEY).map_err(e)?; println!("signed {}", dir.join("manifest.json").display()); } "verify" => { let dir = PathBuf::from(need(&a, "dir")?); - let bytes = std::fs::read(dir.join("manifest.json")).map_err(|x| x.to_string())?; - let sig = std::fs::read_to_string(dir.join("manifest.json.sig")).map_err(|x| x.to_string())?; - coa_core::signing::verify_embedded(&bytes, &sig).map_err(e)?; - Manifest::parse(&bytes).map_err(e)?; - println!("signature and manifest OK"); + coa_core::release_schema::verify_package(&dir, coa_core::signing::EMBEDDED_PUBLIC_KEY).map_err(e)?; + println!("signature, archive contents and database schema contract OK"); } other => return Err(format!("unknown command {other}")), } Ok(()) } +fn require_contract_entry(dir: &std::path::Path) -> Result<(), String> { + let manifest = Manifest::parse(&std::fs::read(dir.join("manifest.json")).map_err(|e| e.to_string())?).map_err(|e| e.to_string())?; + if !manifest.files.iter().any(|f| f.path == coa_core::schema_check::CONTRACT && f.size > 0) { + return Err("The release is missing its database schema contract; refusing to sign or publish it.".into()); + } + Ok(()) +} + fn chrono_now() -> String { chrono::Utc::now().to_rfc3339() } diff --git a/docs/REPAIR_AND_REALMS.md b/docs/REPAIR_AND_REALMS.md index b59fc28..e1c2edd 100644 --- a/docs/REPAIR_AND_REALMS.md +++ b/docs/REPAIR_AND_REALMS.md @@ -15,10 +15,15 @@ Repair reports unresolved schema damage. It does not guess ALTER statements or r For each update, capture a fresh contract **from a disposable database already migrated to the exact packaged core**, into the release tree before `pack-update`: ```powershell -cargo run -p coa-release -- schema-contract --repack C:\games\release-fixture --tree C:\games\release-tree +cargo run -p coa-release -- extract-schema-base --package C:\games\signed-base --fixture C:\games\coa-schema-fixture-build +cargo run -p coa-release -- schema-contract --repack C:\games\coa-schema-fixture-build --tree C:\games\release-tree --core C:\games\release-core --bots C:\games\release-bots --repairs C:\games\release-repairs ``` -The regular packager includes the contract and its hash in the signed manifest. Never capture it from a user's server or from the previous release database. +The first command verifies every entry in the signed base archive and extracts only the runtime needed for a disposable database. The second applies the release SQL with temporary ports, checks critical feature tables independently of migration history, and captures the resulting schema. It requires the disposable fixture marker and never starts a world server. + +Base and update packaging require a complete contract for all three databases. Cumulative updates always carry it, even when unchanged. Signing and verification check the archive contents and contract before publication. Never capture it from a user's server or from an unmigrated previous release database. + +Corrective SQL under `repairs/{auth,characters,world}` receives new `manager_repair__` migration IDs and runs after historical SQL. Missing Wildcard tables can therefore be restored without replaying historical data deletion. Existing malformed tables still fail schema checks; creating a missing table does not recover its lost rows. ## Simultaneous worlds