diff --git a/.githooks/pre-push b/.githooks/pre-push index a886ccf0c..d067dd226 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -1,2 +1,2 @@ #!/bin/sh -exec python3 tools/check_public_tree.py --history +exec python3 tools/check_public_tree.py --pre-push diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5191ea1f7..e6e959186 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -39,6 +39,8 @@ probe against Horizon, and compares their outputs. There are about 290 of them. Never commit game files, decompiled source or prepared resources (`.local/`): `tools/check_public_tree.py` rejects them (`git config core.hooksPath .githooks` runs it before every commit). +The pre-push hook checks the full history reachable from the branches and tags being pushed. Run +`python3 tools/check_public_tree.py --history` to audit the history of all local refs. ## Only original code diff --git a/Cargo.lock b/Cargo.lock index 5af8dfd15..54be02330 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1331,8 +1331,10 @@ name = "horizon-mods" version = "0.1.0" dependencies = [ "log", + "minisign-verify", "serde", "serde_json", + "sha2", "toml", "wasmtime", "wit-component 0.239.0", @@ -1919,6 +1921,12 @@ dependencies = [ "libc", ] +[[package]] +name = "minisign-verify" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871285dc19d8d0ebe0eef3d0e99a205f2a71363b122632cbbfa4a6c370a960ce" + [[package]] name = "miniz_oxide" version = "0.8.9" diff --git a/crates/cargo-horizon-mod/Cargo.toml b/crates/cargo-horizon-mod/Cargo.toml index 6d0a231ec..34eefe227 100644 --- a/crates/cargo-horizon-mod/Cargo.toml +++ b/crates/cargo-horizon-mod/Cargo.toml @@ -4,10 +4,10 @@ version = "0.1.0" edition.workspace = true license.workspace = true rust-version = "1.86" -description = "cargo horizon-mod new/build/run: make Horizon mods in Rust (docs/mods.md)" +description = "cargo horizon-mod new/build/run/check: make Horizon mods in Rust (docs/mods.md)" [dependencies] -horizon-mods = { path = "../horizon-mods", default-features = false, features = ["pack"] } +horizon-mods = { path = "../horizon-mods", default-features = false, features = ["pack", "runtime", "index"] } serde_json = "1.0.151" [lints] diff --git a/crates/cargo-horizon-mod/src/check.rs b/crates/cargo-horizon-mod/src/check.rs new file mode 100644 index 000000000..0294bec98 --- /dev/null +++ b/crates/cargo-horizon-mod/src/check.rs @@ -0,0 +1,268 @@ +//! `cargo horizon-mod check `: what the registry checks of a submission, +//! run anywhere: its kind's own checks (docs/api/mod-index.md), its signature +//! when given a key, and for a mod a trial run in the sandbox: it must start, +//! survive ticks, frames, a chat line and each of its commands, and import +//! no more than its capabilities. +use horizon_mods::{ + Clock, Mods, Output, Package, Player, Snapshot, Status, + index::{self, Facts, Kind}, + manifest::component_imports, +}; +use serde_json::{Value, json}; +use std::path::Path; + +/// How long the trial run plays: two seconds of game. +const TICKS: u64 = 40; +const FRAMES_PER_TICK: u64 = 3; + +pub struct Report { + pub kind: Kind, + pub facts: Facts, + pub sha256: String, + pub size: u64, + pub signed: bool, + /// A mod's component imports. + pub imports: Vec, + /// A mod's commands, each run once. + pub commands: Vec, + /// What the mod showed during the trial run. + pub output: Vec, +} + +impl Report { + pub fn json(&self) -> Value { + let requires: Vec = self + .facts + .requires + .iter() + .map(|r| json!({"id": r.id, "version": r.version, "sha256": r.sha256})) + .collect(); + json!({ + "kind": self.kind.name(), + "id": self.facts.id, + "version": self.facts.version, + "api": self.facts.api, + "side": self.facts.side, + "capabilities": self.facts.capabilities, + "game": self.facts.game, + "requires": requires, + "sha256": self.sha256, + "size": self.size, + "signed": self.signed, + "imports": self.imports, + "commands": self.commands, + "output": self.output, + }) + } + + pub fn text(&self) -> String { + let f = &self.facts; + let mut out = format!( + "{} {} ({}", + f.id.as_deref().unwrap_or("resource pack"), + f.version.as_deref().unwrap_or(""), + self.kind.name() + ); + if let Some(side) = f.side { + let side = serde_json::to_value(side).unwrap_or_default(); + out += &format!(", API {}, {}", f.api, side.as_str().unwrap_or("?")); + } else if !f.api.is_empty() { + out += &format!(", format {}", f.api); + } + if let Some(game) = &f.game { + out += &format!(", for {game}"); + } + let signed = if self.signed { + "signature verified" + } else { + "unsigned" + }; + out += &format!( + ")\n {} bytes, SHA-256 {}, {signed}\n", + self.size, self.sha256 + ); + for c in &f.capabilities { + out += &format!(" may {}\n", c.describe()); + } + for r in &f.requires { + out += &format!(" needs {} {} ({})\n", r.id, r.version, &r.sha256[..12]); + } + if self.kind == Kind::Mod { + let commands = if self.commands.is_empty() { + String::new() + } else { + format!(", /{}", self.commands.join(", /")) + }; + out += &format!( + " trial run: started, {TICKS} ticks, {} frames, a chat line{commands}: ok\n", + TICKS * FRAMES_PER_TICK + ); + } + out + } +} + +/// The kind a file's extension says. +pub fn kind_of(path: &Path) -> Option { + match path.extension()?.to_str()? { + "hmod" => Some(Kind::Mod), + "hpack" => Some(Kind::Modpack), + "zip" => Some(Kind::ResourcePack), + _ => None, + } +} + +/// Checks the file at `path` as `kind`; `key` and `signature` (the +/// `.minisig` text) check its signature. +pub fn check( + path: &Path, + kind: Kind, + key: Option<&str>, + signature: Option<&str>, +) -> Result { + let size = std::fs::metadata(path) + .map_err(|e| format!("{}: {e}", path.display()))? + .len(); + if size > index::MAX_FILE { + return Err(format!("over {} MiB", index::MAX_FILE >> 20)); + } + index::open(&kind)?; + let facts = index::inspect(&kind, path)?; + let signed = match (key, signature) { + (Some(key), Some(sig)) => { + index::verify_signature(key, sig, path)?; + true + } + (None, None) => false, + _ => return Err("--key and --signature go together".into()), + }; + let mut report = Report { + kind, + facts, + sha256: index::sha256_file(path)?, + size, + signed, + imports: Vec::new(), + commands: Vec::new(), + output: Vec::new(), + }; + if report.kind == Kind::Mod { + trial_run(path, &mut report)?; + } + Ok(report) +} + +/// Runs a mod in the sandbox, as the game does, from a scratch copy so its +/// storage lands in a folder removed afterwards. +fn trial_run(path: &Path, report: &mut Report) -> Result<(), String> { + let package = Package::read(path)?; + let wasm = package.wasm.as_deref().ok_or("the .hmod has no mod.wasm")?; + report.imports = component_imports(wasm)?; + package + .manifest + .check_imports(report.imports.iter().map(String::as_str))?; + let dir = std::env::temp_dir().join(format!("horizon-mod-check-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; + let copy = dir.join("mod.hmod"); + let result = std::fs::copy(path, ©) + .map_err(|e| e.to_string()) + .and_then(|_| play(©, report)); + let _ = std::fs::remove_dir_all(&dir); + result +} + +fn play(path: &Path, report: &mut Report) -> Result<(), String> { + let mut mods = Mods::load(&[path.to_path_buf()], |_| true, |_| false); + let failed = |mods: &Mods| match &mods.entries()[0].status { + Status::Failed(reason) => Some(reason.clone()), + _ => None, + }; + if let Some(reason) = failed(&mods) { + return Err(format!("it didn't start: {reason}")); + } + report.commands = mods.commands().map(str::to_owned).collect(); + for t in 0..TICKS { + mods.set_snapshot(Snapshot { + player: Some(Player { + x: 0.5 + t as f64 * 0.1, + y: 64.0, + z: 0.5, + yaw: 90.0, + pitch: 0.0, + health: 20.0, + food: 20, + dimension: "minecraft:overworld".into(), + }), + clock: Some(Clock { + game_time: t, + day_time: 1000 + t, + }), + }); + mods.tick(t); + for _ in 0..FRAMES_PER_TICK { + mods.frame(1.0 / 60.0); + } + } + mods.chat(" hello"); + for name in report.commands.clone() { + mods.command(&name); + } + for out in mods.drain() { + report.output.push(match out { + Output::Chat { text, .. } => format!("chat: {text}"), + Output::Feedback { text, error } => { + format!("{}: {text}", if error { "error" } else { "feedback" }) + } + Output::Stopped { reason, .. } => return Err(format!("it stopped: {reason}")), + }); + } + match failed(&mods) { + Some(reason) => Err(format!("it stopped: {reason}")), + None => Ok(()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn fixture(name: &str) -> std::path::PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../horizon-mods/tests/index") + .join(name) + } + + #[test] + fn checks_each_kind_its_own_way() { + let pack = fixture("cozy-1.0.0.hpack"); + let report = check(&pack, kind_of(&pack).unwrap(), None, None).unwrap(); + assert_eq!(report.facts.requires[0].id, "coords_hud"); + assert!( + report + .text() + .contains("cozy 1.0.0 (modpack, format 1, for 26.3)") + ); + // The fixture mod's code is a stand-in: its manifest reads, its run fails. + let hmod = fixture("coords_hud-0.1.0.hmod"); + let key = std::fs::read_to_string(fixture("test.pub")).unwrap(); + let sig = std::fs::read_to_string(fixture("coords_hud-0.1.0.hmod.minisig")).unwrap(); + let key = key.lines().last().unwrap(); + let e = check(&hmod, Kind::Mod, Some(key), Some(&sig)) + .err() + .unwrap(); + assert!(e.contains("didn't start"), "{e}"); + let e = check( + &hmod, + Kind::Mod, + Some(key), + Some("untrusted comment: x\nRUQ=\n"), + ) + .err() + .unwrap(); + assert!(e.contains("signature"), "{e}"); + let e = check(&pack, Kind::Shader, None, None).err().unwrap(); + assert!(e.contains("shader packs open"), "{e}"); + assert!(check(&pack, Kind::Mod, None, None).is_err()); + } +} diff --git a/crates/cargo-horizon-mod/src/main.rs b/crates/cargo-horizon-mod/src/main.rs index 6b088d278..925633930 100644 --- a/crates/cargo-horizon-mod/src/main.rs +++ b/crates/cargo-horizon-mod/src/main.rs @@ -4,10 +4,16 @@ //! - `build [--debug]`: the `.hmod` in `target/horizon-mod/`. //! - `run [--game ] [-- ]`: build, then play with //! the mod; run `build` again and the game reloads it. +//! - `check [--json]`: what the registry checks of a submission +//! (`.hmod`, `.hpack`, resource pack `.zip`), with a trial run for a mod. +//! - `registry build|review|check|list`: the registry repository's CI. use horizon_mods::{ Manifest, Package, manifest::{MANIFEST, component_imports, componentize}, }; +mod check; +mod registry; + use std::{ path::{Path, PathBuf}, process::{Command, ExitCode, Stdio}, @@ -28,7 +34,9 @@ fn main() -> ExitCode { println!("Built {}", p.display()); }), Some("run") => run(&args[1..]), - _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ]".into()), + Some("check") => check(&args[1..]), + Some("registry") => registry::main(&args[1..]), + _ => Err("usage: cargo horizon-mod new | build [--debug] | run [--game ] [-- ] | check [--kind ] [--key --signature ] [--json] | registry build|review|check|list".into()), }; match result { Ok(()) => ExitCode::SUCCESS, @@ -150,6 +158,37 @@ fn build(release: bool) -> Result { Ok(path) } +/// `check `: the registry's checks of a submission, printed; fails +/// when one does. +fn check(args: &[String]) -> Result<(), String> { + let file = args + .first() + .filter(|a| !a.starts_with("--")) + .ok_or("cargo horizon-mod check ")?; + let path = Path::new(file); + let kind = match flag(args, "--kind") { + Some(k) => horizon_mods::index::Kind::from(k.to_string()), + None => check::kind_of(path) + .ok_or("pass --kind mod|modpack|resourcepack: the extension doesn't say")?, + }; + let signature = flag(args, "--signature") + .map(|p| std::fs::read_to_string(p).map_err(|e| format!("{p}: {e}"))) + .transpose()?; + let report = check::check(path, kind, flag(args, "--key"), signature.as_deref()); + if args.iter().any(|a| a == "--json") { + let json = match &report { + Ok(r) => serde_json::json!({"ok": true, "report": r.json()}), + Err(e) => serde_json::json!({"ok": false, "error": e}), + }; + println!("{json}"); + } + let report = report?; + if !args.iter().any(|a| a == "--json") { + print!("{}", report.text()); + } + Ok(()) +} + fn run(args: &[String]) -> Result<(), String> { let path = build(true)?; let game = flag(args, "--game") diff --git a/crates/cargo-horizon-mod/src/registry.rs b/crates/cargo-horizon-mod/src/registry.rs new file mode 100644 index 000000000..ac5e77ea4 --- /dev/null +++ b/crates/cargo-horizon-mod/src/registry.rs @@ -0,0 +1,159 @@ +//! `cargo horizon-mod registry build|review|check|list`: the registry repository's CI +//! (`ludovic111/horizon-mods`, docs/api/mod-index.md). Network-free: the +//! workflow downloads the files and publishes what this writes. +use horizon_mods::index::{ + FILE_HOST, + registry::{self, Sources}, +}; +use std::path::Path; + +/// Where the registry's site is published (its icons). +const SITE: &str = "https://ludovic111.github.io/horizon-mods/"; + +const USAGE: &str = "usage: cargo horizon-mod registry build --files --out [--release ] [--site ] [--generated