From d3adced897b2276e9cc3b77b029fb15e1aaf1480 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:46:21 +0000 Subject: [PATCH] fix(appd): require a usable HOME for ownership decisions and keep records for earlier-layout data Without an absolute HOME the earlier data layout could not be checked, so a first-launch or install owner record was written anyway and that data was migrated to the new owner later. Both now refuse instead. Uninstall keeps an owner record while data exists in either layout, or while it cannot tell. Launch tests use a private HOME, and the test data-home cleanup never removes the running process's own. --- crates/weft-appd/src/launch.rs | 29 ++++++++++++++-------------- crates/weft-appd/src/main.rs | 1 + crates/weft-ipc-types/src/package.rs | 15 +++++++++++--- crates/weft-pack/src/main.rs | 28 ++++++++++++++++++--------- docs/security.md | 2 +- tests/frame/check_app_data.py | 5 +++-- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index f12c6bb..10203d6 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -215,10 +215,9 @@ fn record_first_owner( } // Data in the earlier layout counts: it would be moved to this app's // data directory when the session starts. - let home = std::env::var_os("HOME") - .map(std::path::PathBuf::from) - .filter(|home| home.is_absolute()); - match weft_ipc_types::package::existing_app_data(data_home, home.as_deref(), app_id) { + let home = weft_ipc_types::package::home_dir() + .ok_or_else(|| Refusal::new(500, "HOME is not set to an absolute path"))?; + match weft_ipc_types::package::existing_app_data(data_home, &home, app_id) { Err(e) => Err(Refusal::new( 500, format!("cannot inspect the app data of {app_id}: {e}"), @@ -264,6 +263,10 @@ mod tests { const ID: &str = "org.weft.test.resolve"; + /// HOME as the test process found it, restored by `finish`. + static HOME_AT_START: std::sync::OnceLock> = + std::sync::OnceLock::new(); + /// A store holding a directory install of `ID`, with WEFT_APP_STORE /// pointing at it. Callers hold env_lock. fn store(name: &str, manifest_id: &str, module: &str) -> PathBuf { @@ -289,10 +292,12 @@ mod tests { Owner::Development, ) .unwrap(); + HOME_AT_START.get_or_init(|| std::env::var_os("HOME")); // SAFETY: callers hold env_lock, which serialises environment changes. unsafe { std::env::set_var("WEFT_APP_STORE", &store); std::env::set_var("XDG_DATA_HOME", store.join("share")); + std::env::set_var("HOME", store.join("home")); } store } @@ -304,6 +309,10 @@ mod tests { std::env::remove_var("WEFT_MOUNT_HELPER"); std::env::remove_var("XDG_DATA_HOME"); std::env::remove_var("WEFT_TRUSTED_KEYS"); + match HOME_AT_START.get() { + Some(Some(home)) => std::env::set_var("HOME", home), + _ => std::env::remove_var("HOME"), + } } let _ = std::fs::remove_dir_all(store); } @@ -462,19 +471,9 @@ mod tests { fn unowned_data_in_the_earlier_layout_is_not_given_away() { let _env = crate::tests::env_lock().blocking_lock(); let store = demo_store("legacy_data", true); - let home = store.join("home"); - let legacy = weft_ipc_types::package::legacy_app_data_dir(&home, DEMO); + let legacy = weft_ipc_types::package::legacy_app_data_dir(&store.join("home"), DEMO); std::fs::create_dir_all(&legacy).unwrap(); - let prior = std::env::var_os("HOME"); - // SAFETY: env_lock is held. - unsafe { std::env::set_var("HOME", &home) }; let refused = refusal(resolve(DEMO)); - unsafe { - match prior { - Some(v) => std::env::set_var("HOME", v), - None => std::env::remove_var("HOME"), - } - } let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); finish(&store); assert_eq!(refused.code, 403, "{}", refused.message); diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 3956896..a681762 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -727,6 +727,7 @@ mod tests { let stale = entry .file_name() .to_str() + .filter(|n| *n != name) .and_then(|n| n.strip_prefix("weft-appd-tests-data-")) .is_some_and(|pid| !std::path::Path::new("/proc").join(pid).exists()); if stale { diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index ac9b5b2..9da0a2f 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -79,16 +79,25 @@ pub fn legacy_app_data_dir(home: &Path, app_id: &str) -> PathBuf { .join("data") } +/// The home directory, when `HOME` is set to an absolute path. +pub fn home_dir() -> Option { + std::env::var_os("HOME") + .map(PathBuf::from) + .filter(|home| home.is_absolute()) +} + /// Where app data for `app_id` already exists, in the current layout or in /// the earlier one under `home`, if anywhere. Data in the earlier layout is /// moved to the current one when the app next launches, so it counts too. pub fn existing_app_data( data_home: &Path, - home: Option<&Path>, + home: &Path, app_id: &str, ) -> std::io::Result> { - let places = std::iter::once(app_data_dir(data_home, app_id)) - .chain(home.map(|home| legacy_app_data_dir(home, app_id))); + let places = [ + app_data_dir(data_home, app_id), + legacy_app_data_dir(home, app_id), + ]; for place in places { match std::fs::symlink_metadata(&place) { Ok(_) => return Ok(Some(place)), diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 9b0cd87..66ba655 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -493,12 +493,11 @@ fn admit( None => { // Data in the earlier layout counts: weft-appd moves it to the // app's data directory when the app next launches. - let home = std::env::var_os("HOME") - .map(PathBuf::from) - .filter(|home| home.is_absolute()); - let existing = - weft_ipc_types::package::existing_app_data(data_home, home.as_deref(), app_id) - .with_context(|| format!("inspect the app data of {app_id}"))?; + let home = weft_ipc_types::package::home_dir().context( + "HOME is not set to an absolute path, so earlier app data cannot be found", + )?; + let existing = weft_ipc_types::package::existing_app_data(data_home, &home, app_id) + .with_context(|| format!("inspect the app data of {app_id}"))?; if let Some(data) = existing.filter(|_| !claim_data) { anyhow::bail!( "{} holds app data for {app_id} with no recorded owner; install with \ @@ -602,9 +601,13 @@ fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { /// Removes the owner record of `app_id` when the app has no data, and /// reports whether a record was removed. fn release_owner_without_data(data_home: &Path, app_id: &str) -> bool { - let no_data = - std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(data_home, app_id)) - .is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound); + // Data in either layout keeps the record; so does not being able to look. + let no_data = weft_ipc_types::package::home_dir().is_some_and(|home| { + matches!( + weft_ipc_types::package::existing_app_data(data_home, &home, app_id), + Ok(None) + ) + }); no_data && std::fs::remove_file(weft_ipc_types::trust::owner_record_path(data_home, app_id)).is_ok() } @@ -1343,6 +1346,13 @@ mod tests { std::fs::remove_dir_all(&data).unwrap(); with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); assert!(!record.exists()); + + // Data in the earlier layout keeps the record too. + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + let legacy = weft_ipc_types::package::legacy_app_data_dir(&home, app_id); + std::fs::create_dir_all(&legacy).unwrap(); + assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); + assert!(record.exists()); let _ = std::fs::remove_dir_all(&home); } diff --git a/docs/security.md b/docs/security.md index 8987b0d..39020fc 100644 --- a/docs/security.md +++ b/docs/security.md @@ -26,7 +26,7 @@ Packages are signed with Ed25519 over their content digest: the SHA-256 of an in The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. Installations and uninstallations of one ID under one data home hold a per-ID lock, so concurrent installations cannot leave a placed package without its record. Every later installation of that ID must have the same owner, so neither another trusted publisher nor a development build can take over an ID, and with it the app's data. The record survives uninstall while the app's data remains; uninstalling with no data left frees the ID. App data with no recorded owner, as left by installations from before owner records, is given to a new owner only with `--claim-data`. A record is written under a temporary name and linked into place, so it is never partial, and a record created by an installation that then fails to place its package is removed. A record left by an installation that was killed before placing its package is released by `weft-pack uninstall ` once no data remains. A record is only released while no known store (the user store, the system store and `$WEFT_APP_STORE`) holds the package; a package installed into another custom store is uninstalled with that store selected. The demo key in `examples/keys` is a test fixture, not a signing authority. -`weft-appd` checks every package again before a session starts. A package recorded as a publisher's runs only while that key is still trusted and its signature covers the package's current content. A package with no record runs only when a trusted key signed it, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, in the current layout or the earlier one that is migrated at launch, the launch is refused rather than handing the data to that publisher; `weft-pack install` applies the same rule unless given `--claim-data`. The record is written before the session starts, so it remains even if that first launch then fails; `weft-pack uninstall` releases it once no data exists and no known store holds the package. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or while the session runs, is not detected; verified images are not writable. +`weft-appd` checks every package again before a session starts. A package recorded as a publisher's runs only while that key is still trusted and its signature covers the package's current content. A package with no record runs only when a trusted key signed it, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, in the current layout or the earlier one that is migrated at launch, the launch is refused rather than handing the data to that publisher; `weft-pack install` applies the same rule unless given `--claim-data`. The record is written before the session starts, so it remains even if that first launch then fails; `weft-pack uninstall` releases it once no data exists in either layout and no known store holds the package. Upgrading from installations made before owner records: data with no record is refused at launch until it is claimed. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or while the session runs, is not detected; verified images are not writable. Not yet covered: key rotation and ownership transfer have no workflow, and removing a key is the only revocation; and capabilities are granted at launch without asking the user, independently of the signature. diff --git a/tests/frame/check_app_data.py b/tests/frame/check_app_data.py index d1fad5e..95130af 100644 --- a/tests/frame/check_app_data.py +++ b/tests/frame/check_app_data.py @@ -41,8 +41,9 @@ def run(args, desktop, home, xdotool): (legacy / "notes.txt").write_bytes(NOTES) data_home = home / "data-home" target = data_home / "weft/app-data" / APP_ID - # The earlier data belongs to the demo publisher, as installing the - # signed package with weft-pack would have recorded. + # The earlier data is recorded as the demo publisher's. Data from before + # owner records has no record and is refused until claimed with + # weft-pack install --claim-data. owner = data_home / "weft/owners" / APP_ID owner.parent.mkdir(parents=True) key = (ROOT / "examples/keys/weft-sign.pub").read_text().strip()