diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 8f8f2cc..f12c6bb 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -213,28 +213,29 @@ fn record_first_owner( } None => {} } - let data = weft_ipc_types::package::app_data_dir(data_home, app_id); - match std::fs::symlink_metadata(&data) { - Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} - Err(e) => { - return Err(Refusal::new( - 500, - format!("cannot inspect {}: {e}", data.display()), - )); - } - Ok(_) => { - return Err(Refusal::new( - 403, - format!( - "{} holds app data for {app_id} with no recorded owner; it is not given to \ - publisher {} without weft-pack install --claim-data", - data.display(), - signer.to_hex() - ), - )); - } + // 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) { + Err(e) => Err(Refusal::new( + 500, + format!("cannot inspect the app data of {app_id}: {e}"), + )), + Ok(Some(data)) => Err(Refusal::new( + 403, + format!( + "{} holds app data for {app_id} with no recorded owner, so it is not given to \ + publisher {}. To give it to this publisher, install the package into the user \ + store with weft-pack install --claim-data (uninstalling it there first); \ + otherwise move the data aside", + data.display(), + signer.to_hex() + ), + )), + Ok(None) => write_owner(record, Owner::Verified(signer)).map_err(host), } - write_owner(record, Owner::Verified(signer)).map_err(host) } impl LaunchPackage { @@ -457,6 +458,65 @@ mod tests { assert_eq!(owner, None); } + #[test] + 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); + 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); + assert!( + refused.message.contains("no recorded owner"), + "{}", + refused.message + ); + assert_eq!(owner, None); + } + + #[test] + fn only_the_signed_manifest_is_accepted() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("manifest", true); + record(&store, DEMO, Owner::Verified(demo_key())); + let root = store.join(DEMO); + let signed = std::fs::read(root.join(MANIFEST_FILE)).unwrap(); + let mut other = signed.clone(); + other.extend_from_slice(b"\n# other\n"); + let accepted = verify_owner(DEMO, &root, &signed); + let refused = verify_owner(DEMO, &root, &other).err(); + finish(&store); + assert!(accepted.is_ok()); + let refused = refused.expect("a manifest that was not signed was accepted"); + assert_eq!(refused.code, 403, "{}", refused.message); + } + + #[test] + fn development_content_does_not_depend_on_the_trust_store() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = store("dev_store", ID, "app.wasm"); + let keys = store.join("keys"); + std::fs::create_dir_all(&keys).unwrap(); + std::fs::write(keys.join("broken.pub"), "not a key").unwrap(); + // SAFETY: as in `store`. + unsafe { std::env::set_var("WEFT_TRUSTED_KEYS", &keys) }; + let resolved = resolve(ID).map(|p| p.root); + finish(&store); + assert_eq!(resolved.ok(), Some(store.join(ID))); + } + #[test] fn a_damaged_trust_store_or_record_is_a_host_error() { let _env = crate::tests::env_lock().blocking_lock(); diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 0df9632..3956896 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -723,10 +723,12 @@ mod tests { let name = format!("weft-appd-tests-data-{}", std::process::id()); if let Ok(entries) = std::fs::read_dir(std::env::temp_dir()) { for entry in entries.flatten() { + // Only data homes of test processes that have exited. let stale = entry .file_name() .to_str() - .is_some_and(|n| n.starts_with("weft-appd-tests-data-") && 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 { let _ = std::fs::remove_dir_all(entry.path()); } diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index 6018c3a..ac9b5b2 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -79,6 +79,30 @@ pub fn legacy_app_data_dir(home: &Path, app_id: &str) -> PathBuf { .join("data") } +/// 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>, + 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))); + for place in places { + match std::fs::symlink_metadata(&place) { + Ok(_) => return Ok(Some(place)), + Err(e) + if matches!( + e.kind(), + std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory + ) => {} + Err(e) => return Err(e), + } + } + Ok(None) +} + /// The result of moving data from the earlier layout. #[derive(Debug, PartialEq, Eq)] pub enum Migration { diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index b76cbd1..9b0cd87 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -491,13 +491,15 @@ fn admit( its owner, so another publisher or a development build cannot take the ID over" ), None => { - let data = weft_ipc_types::package::app_data_dir(data_home, app_id); - let no_data = match std::fs::symlink_metadata(&data) { - Err(e) if e.kind() == std::io::ErrorKind::NotFound => true, - Err(e) => return Err(e).with_context(|| format!("inspect {}", data.display())), - Ok(_) => false, - }; - if !no_data && !claim_data { + // 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}"))?; + if let Some(data) = existing.filter(|_| !claim_data) { anyhow::bail!( "{} holds app data for {app_id} with no recorded owner; install with \ --claim-data to give it to {owner}, or move it aside", @@ -1399,6 +1401,28 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn unowned_data_in_the_earlier_layout_needs_a_claim() { + let home = temp_root("legacy_claim"); + let app_id = "org.weft.test.legacyclaim"; + write_package(&home.join("src"), app_id, APP_DATA); + let legacy = weft_ipc_types::package::legacy_app_data_dir(&home, app_id); + std::fs::create_dir_all(&legacy).unwrap(); + let other = home.join("other"); + let unclaimed = with_home(&home, || { + install_package_to(&home.join("src"), &other, InstallMode::Development) + }); + let message = format!("{:#}", unclaimed.unwrap_err()); + assert!(message.contains("--claim-data"), "{message}"); + assert_eq!(owner_of(&home, app_id), None); + with_home(&home, || { + install_into(&home.join("src"), &other, InstallMode::Development, true) + }) + .unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstalling_one_of_two_copies_keeps_the_record() { let home = temp_root("two_copies"); diff --git a/docs/security.md b/docs/security.md index 24c289d..8987b0d 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, the launch is refused rather than handing the data to that publisher. 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 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. 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 50d10e2..d1fad5e 100644 --- a/tests/frame/check_app_data.py +++ b/tests/frame/check_app_data.py @@ -41,6 +41,12 @@ 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. + owner = data_home / "weft/owners" / APP_ID + owner.parent.mkdir(parents=True) + key = (ROOT / "examples/keys/weft-sign.pub").read_text().strip() + owner.write_text(f"verified {key}\n") t = Path(args.target) desktop.launch_client("appd", [t / "weft-appd"], {