From 36908858a3d793b0049eeeba046222f27d6474f1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 19 Oct 2026 14:25:47 +0200 Subject: [PATCH] Launcher and horizon-server update: install only releases whose signed checksums match The launcher (game installs and its own updates) and `horizon-server update` fetch a release's SHA256SUMS and SHA256SUMS.sig, check the signature against the keys built in from crates/horizon-release-sign/release-keys.txt and the archive against the signed SHA-256, and refuse anything unsigned, signed for another tag or by another key, or that doesn't match, with a message saying why. Merge only once release-keys.txt lists the key and a signed stable release exists: until then every download is refused. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/launcher.yml | 1 + Cargo.lock | 1 + crates/horizon-server/Cargo.toml | 3 +- crates/horizon-server/src/update.rs | 136 ++++++++++++++++++++-------- docs/server.md | 6 +- launcher/Cargo.lock | 9 ++ launcher/Cargo.toml | 2 + launcher/src/backend.rs | 70 +++++++++++--- launcher/src/lib.rs | 1 + launcher/src/main.rs | 9 +- launcher/src/signed.rs | 68 ++++++++++++++ tools/release_train.py | 1 + 12 files changed, 250 insertions(+), 57 deletions(-) create mode 100644 launcher/src/signed.rs diff --git a/.github/workflows/launcher.yml b/.github/workflows/launcher.yml index e7b55c64b..dbd1c3571 100644 --- a/.github/workflows/launcher.yml +++ b/.github/workflows/launcher.yml @@ -20,6 +20,7 @@ on: - 'crates/horizon-mods/**' - 'crates/horizon-port-pack/**' - 'crates/horizon-prepare/**' + - 'crates/horizon-release-sign/**' - 'crates/horizon-telemetry/**' - 'crates/horizon-theme/**' - '.github/workflows/launcher.yml' diff --git a/Cargo.lock b/Cargo.lock index 4100e167f..25c705b23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1568,6 +1568,7 @@ version = "0.1.0" dependencies = [ "chrono", "horizon-bench", + "horizon-release-sign", "horizon-server-files", "libc", "serde", diff --git a/crates/horizon-server/Cargo.toml b/crates/horizon-server/Cargo.toml index ec3b9bbc4..747b041df 100644 --- a/crates/horizon-server/Cargo.toml +++ b/crates/horizon-server/Cargo.toml @@ -17,8 +17,9 @@ serde_json = "1" toml = "1.1" # Edits server.toml in place, keeping the owner's comments and order. toml_edit = "0.25" -# Updates: the release from GitHub, checked against the SHA-256 GitHub publishes (both already in the +# Updates: the release from GitHub, checked against the SHA-256 in its signed checksums (both already in the # lockfile, ureq through horizon-server-files). +horizon-release-sign = { path = "../horizon-release-sign" } sha2 = "0.10" ureq = { version = "2", default-features = false, features = ["tls"] } # Backups: one zip anyone can open, Windows included. diff --git a/crates/horizon-server/src/update.rs b/crates/horizon-server/src/update.rs index f1f60a061..ea24b6a84 100644 --- a/crates/horizon-server/src/update.rs +++ b/crates/horizon-server/src/update.rs @@ -1,8 +1,9 @@ //! Updates and rollbacks (docs/server.md, section 7) for a `horizon-server` installed by install.sh or //! install.ps1: each release in `/releases/`, `/current` pointing at the one in use, and -//! `/previous` naming the one before it. An update downloads the newest release from GitHub, checks it -//! against the SHA-256 GitHub publishes for it, unpacks it beside the others and switches `current`; the -//! supervisor then restarts the game on it and switches back by itself if the new game doesn't come up. +//! `/previous` naming the one before it. An update downloads the newest stable release from GitHub, checks it +//! against the SHA-256 in the release's signed SHA256SUMS (docs/security.md), unpacks it beside the others and +//! switches `current`; the supervisor then restarts the game on it and switches back by itself if the new game +//! doesn't come up. use crate::{api::Client, backup, config::Config, engine, supervisor}; use serde_json::{Value, json}; @@ -231,27 +232,47 @@ pub struct Release { pub sha256: String, } -/// The newest release (or `tag`), from GitHub's API. +/// GitHub's API, or a release asset: the body of `url`. +fn fetch(url: &str) -> Result, String> { + let mut body = Vec::new(); + ureq::get(url) + .set("Accept", "application/vnd.github+json") + .set("User-Agent", "horizon-server") + .call() + .map_err(|e| match e { + ureq::Error::Status(404, _) => "404".to_string(), + e => format!("GitHub didn't answer: {e}"), + })? + .into_reader() + .take(64 << 20) + .read_to_end(&mut body) + .map_err(|e| format!("GitHub's answer: {e}"))?; + Ok(body) +} + +/// The newest stable release (or `tag`), from GitHub's API, with the SHA-256 its signed checksums +/// give for this computer's archive. pub fn release(tag: Option<&str>) -> Result { let url = match tag { Some(tag) => format!("https://api.github.com/repos/{REPO}/releases/tags/{tag}"), None => format!("https://api.github.com/repos/{REPO}/releases/latest"), }; - let text = ureq::get(&url) - .set("Accept", "application/vnd.github+json") - .set("User-Agent", "horizon-server") - .call() - .map_err(|e| match e { - ureq::Error::Status(404, _) => format!("no release {} on GitHub", tag.unwrap_or("")), - e => format!("GitHub didn't answer: {e}"), - })? - .into_string() - .map_err(|e| format!("GitHub's answer: {e}"))?; - pick(&serde_json::from_str(&text).map_err(|e| format!("GitHub's answer: {e}"))?) + let text = fetch(&url).map_err(|e| match e.as_str() { + "404" => format!("no release {} on GitHub", tag.unwrap_or("")), + _ => e, + })?; + let json = serde_json::from_slice(&text).map_err(|e| format!("GitHub's answer: {e}"))?; + pick(&json, fetch, &horizon_release_sign::trusted_keys()) } -/// The archive for this computer in a release's JSON, with the SHA-256 GitHub publishes for it. -fn pick(release: &Value) -> Result { +/// The archive for this computer in a release's JSON, with the SHA-256 the release's SHA256SUMS +/// gives for it, once its signature checks with one of `keys` (docs/security.md). A release that +/// isn't signed, or not by Horizon's key, is refused. +fn pick( + release: &Value, + fetch: impl Fn(&str) -> Result, String>, + keys: &[horizon_release_sign::PublicKey], +) -> Result { let tag = release["tag_name"] .as_str() .ok_or("GitHub's answer has no tag")?; @@ -266,10 +287,24 @@ fn pick(release: &Value) -> Result { ) })?; let name = asset["name"].as_str().unwrap_or_default(); - let sha256 = asset["digest"] - .as_str() - .and_then(|d| d.strip_prefix("sha256:")) - .ok_or_else(|| format!("release {tag} publishes no SHA-256 for {name}"))?; + let attached = |file: &str| -> Result>, String> { + match assets.iter().find(|a| a["name"] == file) { + Some(a) => fetch(a["browser_download_url"].as_str().unwrap_or_default()) + .map(Some) + .map_err(|e| format!("{file} of {tag}: {e}")), + None => Ok(None), + } + }; + let sums = attached(horizon_release_sign::SUMS)?; + let signature = attached(horizon_release_sign::SIGNATURE)? + .map(|bytes| String::from_utf8_lossy(&bytes).into_owned()); + let sha256 = horizon_release_sign::signed_digest( + tag, + name, + sums.as_deref(), + signature.as_deref(), + keys, + )?; Ok(Release { tag: tag.to_owned(), asset: name.to_owned(), @@ -277,7 +312,7 @@ fn pick(release: &Value) -> Result { .as_str() .unwrap_or_default() .to_owned(), - sha256: sha256.to_ascii_lowercase(), + sha256, }) } @@ -717,31 +752,56 @@ mod tests { } #[test] - fn the_archive_for_this_computer_is_picked_with_its_digest() { - let asset = |name: &str| { - json!({ - "name": name, - "browser_download_url": format!("https://example.invalid/{name}"), - "digest": "sha256:ABCDEF", - }) + fn the_archive_for_this_computer_is_picked_with_its_signed_digest() { + use horizon_release_sign::{SigningKey, sha256_hex, sums_file}; + let asset = |name: &str| json!({ "name": name, "browser_download_url": format!("https://example.invalid/{name}") }); + let (key, _) = SigningKey::generate().unwrap(); + let listed: Vec<(String, String)> = ASSETS + .iter() + .map(|n| (n.to_string(), sha256_hex(n.as_bytes()))) + .collect(); + let sums = sums_file(&listed); + let signature = key.sign("v2026.10.19", sums.as_bytes()); + let files = |url: &str| -> Result, String> { + match url.rsplit('/').next() { + Some("SHA256SUMS") => Ok(sums.clone().into_bytes()), + Some("SHA256SUMS.sig") => Ok(signature.clone().into_bytes()), + _ => Err("404".into()), + } }; - let release = json!({ + let names = ASSETS.iter().rev().copied().chain(["other.zip"]); + let signed = json!({ "tag_name": "v2026.10.19", - "assets": ASSETS.iter().rev().map(|n| asset(n)).chain([asset("other.zip")]).collect::>(), + "assets": names.chain(["SHA256SUMS", "SHA256SUMS.sig"]).map(asset).collect::>(), }); if let Some(first) = ASSETS.first() { - let picked = pick(&release).unwrap(); + let picked = pick(&signed, files, &[key.public()]).unwrap(); assert_eq!(picked.tag, "v2026.10.19"); // The most specific archive (Linux: the server build), even when listed last. assert_eq!(picked.asset, *first); - assert_eq!(picked.sha256, "abcdef"); - let undigested = json!({ "tag_name": "v1", "assets": [{ "name": first, "browser_download_url": "x" }] }); - assert!(pick(&undigested).unwrap_err().contains("no SHA-256")); + assert_eq!(picked.sha256, sha256_hex(first.as_bytes())); + // Signed by another key, or not signed at all: refused. + let (other, _) = SigningKey::generate().unwrap(); + assert!(pick(&signed, files, &[other.public()]).is_err()); + let unsigned = json!({ "tag_name": "v1", "assets": [asset(first)] }); + assert!( + pick(&unsigned, files, &[key.public()]) + .unwrap_err() + .contains("not signed") + ); + // The same signed list under another tag: refused. + let mut replayed = signed.clone(); + replayed["tag_name"] = json!("v2026.10.20"); + assert!(pick(&replayed, files, &[key.public()]).is_err()); } assert!( - pick(&json!({ "tag_name": "v1", "assets": [asset("other.zip")] })) - .unwrap_err() - .contains("no archive for this computer") + pick( + &json!({ "tag_name": "v1", "assets": [asset("other.zip")] }), + files, + &[] + ) + .unwrap_err() + .contains("no archive for this computer") ); } diff --git a/docs/server.md b/docs/server.md index 4d623b6e8..d7959aed0 100644 --- a/docs/server.md +++ b/docs/server.md @@ -410,8 +410,10 @@ runtime or new dependency. Server-sent events can replace the polling later if a newer release a minute after it starts and then every 6 hours, and only says so, in its log, in `status` and in the panel (`HORIZON_NO_UPDATE_CHECK=1` turns the check off). `update --check` asks GitHub once. `update`: - 1. downloads the release archive for this computer (on Linux the server build) and checks it against the - SHA-256 GitHub publishes for it; + 1. downloads the newest stable release's archive for this computer (on Linux the server build; `--version` + picks another, a beta included) and checks it against the release's SHA256SUMS, whose Ed25519 signature + must check with a key built into `horizon-server` (docs/release-flow.md, "Signed checksums"): an unsigned + or tampered release is refused; 2. unpacks it into `/releases/`, beside the others, with its game files linked to `/game-files` as the installers do; 3. stops the game, backs up the world (`backups/