|
3 | 3 | //! the existing updater. This is only its macOS filesystem effect adapter. |
4 | 4 | use std::{ |
5 | 5 | fs, |
6 | | - io::{self, Cursor}, |
| 6 | + io::{self, Cursor, Read}, |
7 | 7 | path::{Component, Path}, |
8 | 8 | }; |
9 | 9 |
|
10 | 10 | use flate2::read::GzDecoder; |
| 11 | +use serde_json::Value; |
11 | 12 | use tauri_plugin_updater::Update; |
12 | 13 |
|
| 14 | +const PUBLIC_DEFAULT_CONTRACT: &str = |
| 15 | + include_str!("../../../../../loopx/cli_commands/public_default_contract.json"); |
| 16 | + |
| 17 | +/// Read the publisher's observed contract inside the signature-verified bytes |
| 18 | +/// before an update can be cached, close the workspace, or replace its App. |
| 19 | +pub(crate) fn qualify_candidate( |
| 20 | + channel: &str, |
| 21 | + version: &str, |
| 22 | + verified_bytes: &[u8], |
| 23 | +) -> Result<(), String> { |
| 24 | + // Historical stable bundles predate the attestation and remain usable. |
| 25 | + // A preview stays subject to the check even if cache metadata changes channel. |
| 26 | + if channel != "main" |
| 27 | + && !semver::Version::parse(version) |
| 28 | + .is_ok_and(|value| value.pre.as_str().starts_with("main.")) |
| 29 | + { |
| 30 | + return Ok(()); |
| 31 | + } |
| 32 | + let invalid = "runtime_bundle_invalid"; |
| 33 | + let unavailable = "update_platform_unavailable"; |
| 34 | + let supported: Value = serde_json::from_str(PUBLIC_DEFAULT_CONTRACT).map_err(|_| invalid)?; |
| 35 | + let identity_path = Path::new("LoopX.app/Contents/Resources/runtime/identity.json"); |
| 36 | + let mut archive = tar::Archive::new(GzDecoder::new(Cursor::new(verified_bytes))); |
| 37 | + let mut qualified = false; |
| 38 | + for entry in archive.entries().map_err(|_| invalid)? { |
| 39 | + let mut entry = entry.map_err(|_| invalid)?; |
| 40 | + if entry.path().map_err(|_| invalid)? != identity_path { |
| 41 | + continue; |
| 42 | + } |
| 43 | + if qualified || !entry.header().entry_type().is_file() || entry.size() > 16 * 1024 { |
| 44 | + return Err(invalid.into()); |
| 45 | + } |
| 46 | + let mut raw = Vec::new(); |
| 47 | + entry.read_to_end(&mut raw).map_err(|_| invalid)?; |
| 48 | + let identity = crate::bundled_runtime::parse_identity(&raw)?; |
| 49 | + if identity["default_contract"] != supported { |
| 50 | + return Err(unavailable.into()); |
| 51 | + } |
| 52 | + qualified = true; |
| 53 | + } |
| 54 | + qualified.then_some(()).ok_or_else(|| unavailable.into()) |
| 55 | +} |
| 56 | + |
13 | 57 | // Provider-local errors, never a new persisted or caller-facing vocabulary. |
14 | 58 | #[derive(Debug, PartialEq)] |
15 | 59 | enum InstallError { |
@@ -132,6 +176,55 @@ mod tests { |
132 | 176 | builder.into_inner().unwrap().finish().unwrap() |
133 | 177 | } |
134 | 178 |
|
| 179 | + #[test] |
| 180 | + fn only_qualified_preview_can_replace_the_prepared_candidate() { |
| 181 | + let root = tempfile::tempdir().unwrap(); |
| 182 | + let candidate = root.path().join("candidate"); |
| 183 | + let resources = candidate.join("Contents/Resources/runtime"); |
| 184 | + fs::create_dir_all(&resources).unwrap(); |
| 185 | + let cache = root.path().join("prepared"); |
| 186 | + let version = "0.0.0-main.2.1"; |
| 187 | + let identity_path = resources.join("identity.json"); |
| 188 | + let mut identity = serde_json::json!({ |
| 189 | + "schema_version": "desktop_runtime_bundle_v1", |
| 190 | + "source_revision": "1111111111111111111111111111111111111111" |
| 191 | + }); |
| 192 | + let rejected = [ |
| 193 | + None, |
| 194 | + Some(serde_json::json!({"surface": "unknown"})), |
| 195 | + Some(serde_json::json!({ |
| 196 | + "surface": "complete_turn", |
| 197 | + "schema_version": "loopx_turn_envelope_v1", |
| 198 | + "interaction_schema_version": "loopx_interaction_contract_v0" |
| 199 | + })), |
| 200 | + ]; |
| 201 | + for contract in rejected { |
| 202 | + if let Some(contract) = contract { |
| 203 | + identity["default_contract"] = contract; |
| 204 | + } |
| 205 | + fs::write(&identity_path, identity.to_string()).unwrap(); |
| 206 | + let bytes = archive(&candidate); |
| 207 | + assert_eq!( |
| 208 | + crate::prepared_update::save(&cache, "main", version, "unused", &bytes), |
| 209 | + Err("update_platform_unavailable".into()) |
| 210 | + ); |
| 211 | + assert!(!cache.exists()); |
| 212 | + assert_eq!( |
| 213 | + qualify_candidate("stable", version, &bytes), |
| 214 | + Err("update_platform_unavailable".into()) |
| 215 | + ); |
| 216 | + assert!(qualify_candidate("stable", "2.3.0", &bytes).is_ok()); |
| 217 | + } |
| 218 | + |
| 219 | + identity["default_contract"] = serde_json::from_str(PUBLIC_DEFAULT_CONTRACT).unwrap(); |
| 220 | + fs::write(identity_path, identity.to_string()).unwrap(); |
| 221 | + let bytes = archive(&candidate); |
| 222 | + crate::prepared_update::save(&cache, "main", version, "unused", &bytes).unwrap(); |
| 223 | + let pointer = fs::read(cache.join("prepared.json")).unwrap(); |
| 224 | + assert!(crate::prepared_update::save(&cache, "main", version, "unused", b"bad").is_err()); |
| 225 | + assert_eq!(fs::read(cache.join("prepared.json")).unwrap(), pointer); |
| 226 | + } |
| 227 | + |
135 | 228 | #[test] |
136 | 229 | fn verified_candidate_replaces_app_and_retains_intact_previous_bundle() { |
137 | 230 | let root = tempfile::tempdir().unwrap(); |
|
0 commit comments