Skip to content

Commit 036a95f

Browse files
authored
Merge pull request #9029 from loopx-project/codex/macos-preview-adoption-20261118
Require runtime qualification before preparing macOS preview updates
2 parents 8b1b4ff + 0673655 commit 036a95f

7 files changed

Lines changed: 136 additions & 9 deletions

File tree

‎apps/desktop/loopx-control-plane/src-tauri/src/bundled_runtime.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ pub fn identity(app: &AppHandle) -> Result<Value, String> {
2121

2222
fn identity_at(resources: &Path) -> Result<Value, String> {
2323
let path = resources.join("runtime/identity.json");
24-
let value: Value =
25-
serde_json::from_slice(&fs::read(path).map_err(|_| "runtime_bundle_missing")?)
26-
.map_err(|_| "runtime_bundle_invalid")?;
24+
parse_identity(&fs::read(path).map_err(|_| "runtime_bundle_missing")?)
25+
}
26+
27+
pub(crate) fn parse_identity(bytes: &[u8]) -> Result<Value, String> {
28+
let value: Value = serde_json::from_slice(bytes).map_err(|_| "runtime_bundle_invalid")?;
2729
if value["schema_version"] != "desktop_runtime_bundle_v1"
2830
|| !value["source_revision"]
2931
.as_str()

‎apps/desktop/loopx-control-plane/src-tauri/src/macos_update.rs‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,57 @@
33
//! the existing updater. This is only its macOS filesystem effect adapter.
44
use std::{
55
fs,
6-
io::{self, Cursor},
6+
io::{self, Cursor, Read},
77
path::{Component, Path},
88
};
99

1010
use flate2::read::GzDecoder;
11+
use serde_json::Value;
1112
use tauri_plugin_updater::Update;
1213

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+
1357
// Provider-local errors, never a new persisted or caller-facing vocabulary.
1458
#[derive(Debug, PartialEq)]
1559
enum InstallError {
@@ -132,6 +176,55 @@ mod tests {
132176
builder.into_inner().unwrap().finish().unwrap()
133177
}
134178

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+
135228
#[test]
136229
fn verified_candidate_replaces_app_and_retains_intact_previous_bundle() {
137230
let root = tempfile::tempdir().unwrap();

‎apps/desktop/loopx-control-plane/src-tauri/src/maintenance.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,14 +896,27 @@ async fn perform(
896896
let bytes = if let Some(cached) = cached {
897897
cached.bytes
898898
} else {
899-
download_update(&update, |chunk, total| {
899+
let bytes = download_update(&update, |chunk, total| {
900900
received += chunk as u64;
901901
state.publish(
902902
"downloading",
903903
json!({"received":received,"total":total,"version":update.version}),
904904
);
905905
})
906-
.await?
906+
.await?;
907+
#[cfg(target_os = "macos")]
908+
{
909+
let version = update.version.clone();
910+
let channel = channel.to_owned();
911+
tauri::async_runtime::spawn_blocking(move || {
912+
crate::macos_update::qualify_candidate(&channel, &version, &bytes)?;
913+
Ok::<_, String>(bytes)
914+
})
915+
.await
916+
.map_err(|_| "runtime_bundle_invalid".to_string())??
917+
}
918+
#[cfg(not(target_os = "macos"))]
919+
bytes
907920
};
908921
let target = update.version.clone();
909922
install_verified_app(app, target, move || {

‎apps/desktop/loopx-control-plane/src-tauri/src/prepared_update.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ pub(crate) fn save(
7575
if !matches!(channel, "main" | "stable") || semver::Version::parse(version).is_err() {
7676
return Err(INVALID.into());
7777
}
78+
#[cfg(target_os = "macos")]
79+
crate::macos_update::qualify_candidate(channel, version, bytes)?;
7880
fs::create_dir_all(root).map_err(|_| INVALID)?;
7981
let old = metadata(root).ok().flatten();
8082
let value = json!({"schema_version":"desktop_prepared_update_v1", "channel":channel,
@@ -156,6 +158,8 @@ fn read(
156158
let signature = Signature::decode(std::str::from_utf8(&signature).map_err(|_| INVALID)?)
157159
.map_err(|_| INVALID)?;
158160
key.verify(&bytes, &signature, true).map_err(|_| INVALID)?;
161+
#[cfg(target_os = "macos")]
162+
crate::macos_update::qualify_candidate(channel, version, &bytes)?;
159163
Ok(Some(PreparedUpdate {
160164
version: version.into(),
161165
signature: value["signature"].as_str().ok_or(INVALID)?.into(),
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"surface": "quota_packet",
3+
"schema_version": null,
4+
"interaction_schema_version": "loopx_interaction_contract_v0"
5+
}

‎loopx/cli_commands/update_artifact.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@
3131
from ..capabilities.issue_fix.repository_snapshot import _REPO_PATTERN as REPOSITORY_PATTERN
3232

3333
RUNTIME_ARTIFACT_SCHEMA_VERSION = "loopx_runtime_artifact_v1"
34-
PUBLIC_DEFAULT_CONTRACT = {"surface": "quota_packet", "schema_version": None,
35-
"interaction_schema_version": "loopx_interaction_contract_v0"}
34+
PUBLIC_DEFAULT_CONTRACT = json.loads(
35+
Path(__file__).with_name("public_default_contract.json").read_text(encoding="utf-8")
36+
)
3637
SHA = re.compile(r"[0-9a-f]{40}")
3738
MAX_MANIFEST = 256 * 1024
3839
MAX_ARCHIVE = 256 * 1024 * 1024
@@ -61,7 +62,15 @@ def standalone_source() -> str:
6162
if source.count(old) != 1:
6263
raise ValueError("Bootstrap grammar binding no longer matches its source import")
6364
source = source.replace(old, "\n" + binding + "\n", 1)
64-
return source
65+
# The public installer is one file, so embed the policy that the Python
66+
# publishers and the native updater read from the distribution contract.
67+
binding_start = source.index("\nPUBLIC_DEFAULT_CONTRACT = ")
68+
binding_end = source.index("\nSHA = ", binding_start)
69+
return (
70+
source[:binding_start]
71+
+ f"\nPUBLIC_DEFAULT_CONTRACT = {PUBLIC_DEFAULT_CONTRACT!r}"
72+
+ source[binding_end:]
73+
)
6574

6675

6776
def safe_url(url: str) -> str:

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ include = ["loopx*"]
8181

8282
[tool.setuptools.package-data]
8383
"loopx" = ["web/chat/*.html", "web/chat/*.json", "web/chat/assets/*", "web/chat/manifest.webmanifest", "web/chat/pwa/*"]
84+
"loopx.cli_commands" = ["public_default_contract.json"]
8485
"loopx.control_plane" = ["*.json", "*.ts", "presentation/*.ts"]
8586
"loopx.semantics" = ["*.json"]
8687
"loopx.control_plane.agents" = ["*.ts"]

0 commit comments

Comments
 (0)