mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
fix(appd): count app data in the earlier layout as existing before recording an owner
App data in the earlier layout, <home>/.local/share/weft/apps/<id>/data, is migrated to the app data directory after the owner check, so a trusted package from another store could be recorded as owner on its first launch and then receive that data. Both layouts now count as existing data, in weft-appd and in weft-pack install, so unowned data is only given away with --claim-data. The refusal says how to claim it. Stale test data homes are removed only when their process has exited, and the app-data check records the owner of the data it seeds.
This commit is contained in:
parent
39aacac822
commit
95db80a86d
6 changed files with 146 additions and 30 deletions
|
|
@ -213,29 +213,30 @@ fn record_first_owner(
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
let data = weft_ipc_types::package::app_data_dir(data_home, app_id);
|
// Data in the earlier layout counts: it would be moved to this app's
|
||||||
match std::fs::symlink_metadata(&data) {
|
// data directory when the session starts.
|
||||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
let home = std::env::var_os("HOME")
|
||||||
Err(e) => {
|
.map(std::path::PathBuf::from)
|
||||||
return Err(Refusal::new(
|
.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,
|
500,
|
||||||
format!("cannot inspect {}: {e}", data.display()),
|
format!("cannot inspect the app data of {app_id}: {e}"),
|
||||||
));
|
)),
|
||||||
}
|
Ok(Some(data)) => Err(Refusal::new(
|
||||||
Ok(_) => {
|
|
||||||
return Err(Refusal::new(
|
|
||||||
403,
|
403,
|
||||||
format!(
|
format!(
|
||||||
"{} holds app data for {app_id} with no recorded owner; it is not given to \
|
"{} holds app data for {app_id} with no recorded owner, so it is not given to \
|
||||||
publisher {} without weft-pack install --claim-data",
|
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(),
|
data.display(),
|
||||||
signer.to_hex()
|
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 {
|
impl LaunchPackage {
|
||||||
/// A package for supervisor tests whose children ignore their files.
|
/// A package for supervisor tests whose children ignore their files.
|
||||||
|
|
@ -457,6 +458,65 @@ mod tests {
|
||||||
assert_eq!(owner, None);
|
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]
|
#[test]
|
||||||
fn a_damaged_trust_store_or_record_is_a_host_error() {
|
fn a_damaged_trust_store_or_record_is_a_host_error() {
|
||||||
let _env = crate::tests::env_lock().blocking_lock();
|
let _env = crate::tests::env_lock().blocking_lock();
|
||||||
|
|
|
||||||
|
|
@ -723,10 +723,12 @@ mod tests {
|
||||||
let name = format!("weft-appd-tests-data-{}", std::process::id());
|
let name = format!("weft-appd-tests-data-{}", std::process::id());
|
||||||
if let Ok(entries) = std::fs::read_dir(std::env::temp_dir()) {
|
if let Ok(entries) = std::fs::read_dir(std::env::temp_dir()) {
|
||||||
for entry in entries.flatten() {
|
for entry in entries.flatten() {
|
||||||
|
// Only data homes of test processes that have exited.
|
||||||
let stale = entry
|
let stale = entry
|
||||||
.file_name()
|
.file_name()
|
||||||
.to_str()
|
.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 {
|
if stale {
|
||||||
let _ = std::fs::remove_dir_all(entry.path());
|
let _ = std::fs::remove_dir_all(entry.path());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,30 @@ pub fn legacy_app_data_dir(home: &Path, app_id: &str) -> PathBuf {
|
||||||
.join("data")
|
.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<Option<PathBuf>> {
|
||||||
|
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.
|
/// The result of moving data from the earlier layout.
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Migration {
|
pub enum Migration {
|
||||||
|
|
|
||||||
|
|
@ -491,13 +491,15 @@ fn admit(
|
||||||
its owner, so another publisher or a development build cannot take the ID over"
|
its owner, so another publisher or a development build cannot take the ID over"
|
||||||
),
|
),
|
||||||
None => {
|
None => {
|
||||||
let data = weft_ipc_types::package::app_data_dir(data_home, app_id);
|
// Data in the earlier layout counts: weft-appd moves it to the
|
||||||
let no_data = match std::fs::symlink_metadata(&data) {
|
// app's data directory when the app next launches.
|
||||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => true,
|
let home = std::env::var_os("HOME")
|
||||||
Err(e) => return Err(e).with_context(|| format!("inspect {}", data.display())),
|
.map(PathBuf::from)
|
||||||
Ok(_) => false,
|
.filter(|home| home.is_absolute());
|
||||||
};
|
let existing =
|
||||||
if !no_data && !claim_data {
|
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!(
|
anyhow::bail!(
|
||||||
"{} holds app data for {app_id} with no recorded owner; install with \
|
"{} holds app data for {app_id} with no recorded owner; install with \
|
||||||
--claim-data to give it to {owner}, or move it aside",
|
--claim-data to give it to {owner}, or move it aside",
|
||||||
|
|
@ -1399,6 +1401,28 @@ mod tests {
|
||||||
let _ = std::fs::remove_dir_all(&home);
|
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]
|
#[test]
|
||||||
fn uninstalling_one_of_two_copies_keeps_the_record() {
|
fn uninstalling_one_of_two_copies_keeps_the_record() {
|
||||||
let home = temp_root("two_copies");
|
let home = temp_root("two_copies");
|
||||||
|
|
|
||||||
|
|
@ -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/<id>`: 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 <id>` 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.
|
The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/<id>`: 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 <id>` 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.
|
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.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,12 @@ def run(args, desktop, home, xdotool):
|
||||||
(legacy / "notes.txt").write_bytes(NOTES)
|
(legacy / "notes.txt").write_bytes(NOTES)
|
||||||
data_home = home / "data-home"
|
data_home = home / "data-home"
|
||||||
target = data_home / "weft/app-data" / APP_ID
|
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)
|
t = Path(args.target)
|
||||||
desktop.launch_client("appd", [t / "weft-appd"], {
|
desktop.launch_client("appd", [t / "weft-appd"], {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue