From 88c2e9f6a529c8f067862e964af7845dfab00713 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 13:49:49 +0000 Subject: [PATCH 01/61] fix(pack): install only trusted packages and bind each app ID to its owner weft-pack install copied any package into the store: signatures were never checked, no key was trusted for anything, any publisher could install over another's app ID and reach its retained data, and the signing inventory followed symbolic links, so a package could sign and install files from outside itself. Installation now requires a signature by a key in the trust store ($XDG_CONFIG_HOME/weft/trusted-keys and /etc/weft/trusted-keys, or $WEFT_TRUSTED_KEYS alone). Unsigned or untrusted content installs only with --dev, recorded as development content. The package is copied into the store, checked and verified on that copy, and renamed into place, so the verified bytes are the installed bytes. The first installation of an app ID records its owner beside the app data; later installations must have the same owner, so neither another publisher nor a development build can take the ID over. The record outlives uninstall while app data remains. The content digest moves to weft_ipc_types::trust and refuses links, special files and non-UTF-8 paths; it keeps the existing inventory format, and both committed demo signatures still verify. Catalogs list only directories named after their package ID. The security guide described a signature over wapp.toml and app.wasm only; it now describes the full inventory and the trust and ownership rules. --- Cargo.lock | 3 + README.md | 2 +- crates/weft-appd/src/main.rs | 5 + crates/weft-ipc-types/Cargo.toml | 3 + crates/weft-ipc-types/src/lib.rs | 1 + crates/weft-ipc-types/src/package.rs | 4 +- crates/weft-ipc-types/src/trust.rs | 400 +++++++++++++++++ crates/weft-pack/src/main.rs | 616 +++++++++++++++++++-------- docs/architecture.md | 4 +- docs/building.md | 10 + docs/security.md | 12 +- 11 files changed, 880 insertions(+), 180 deletions(-) create mode 100644 crates/weft-ipc-types/src/trust.rs diff --git a/Cargo.lock b/Cargo.lock index 20cbbdf..bc4cf59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10519,9 +10519,12 @@ dependencies = [ name = "weft-ipc-types" version = "0.1.0" dependencies = [ + "ed25519-dalek", + "hex", "rmp-serde", "rustix 1.1.4", "serde", + "sha2", "toml 0.8.23", ] diff --git a/README.md b/README.md index 022b3d0..f6a5c18 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web **Runtime** — `weft-runtime` runs WASI Component Model binaries under Wasmtime 30 (`--features wasmtime-runtime`). Provides `weft:app/notify`, `weft:app/ipc`, `weft:app/fetch`, `weft:app/notifications`, and `weft:app/clipboard` host imports. Preopens filesystem paths according to declared capabilities. -**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. App data is stored apart from packages and survives uninstall. +**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall. **File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking. diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 433b0e8..4512211 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -634,6 +634,11 @@ fn scan_installed_apps() -> Vec { let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else { continue; }; + // Only a directory named after the ID its manifest declares is an + // installed package; staging copies and stray directories are not. + if entry.file_name().to_str() != Some(m.package.id.as_str()) { + continue; + } if seen.insert(m.package.id.clone()) { apps.push(AppInfo { app_id: m.package.id, diff --git a/crates/weft-ipc-types/Cargo.toml b/crates/weft-ipc-types/Cargo.toml index 8b65cd3..b69064f 100644 --- a/crates/weft-ipc-types/Cargo.toml +++ b/crates/weft-ipc-types/Cargo.toml @@ -12,6 +12,9 @@ path = "src/lib.rs" serde = { version = "1", features = ["derive"] } rmp-serde = "1" toml = "0.8" +sha2 = "0.10" +hex = "0.4" +ed25519-dalek = "2" [lints] workspace = true diff --git a/crates/weft-ipc-types/src/lib.rs b/crates/weft-ipc-types/src/lib.rs index 0590c77..07e3583 100644 --- a/crates/weft-ipc-types/src/lib.rs +++ b/crates/weft-ipc-types/src/lib.rs @@ -1,6 +1,7 @@ pub mod capability; pub mod manifest; pub mod package; +pub mod trust; use serde::{Deserialize, Serialize}; diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index be2a170..6018c3a 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -180,13 +180,13 @@ pub fn migrate_app_data(legacy: &Path, target: &Path) -> Result std::io::Result<()> { +pub fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { use rustix::fs::{CWD, RenameFlags, renameat_with}; renameat_with(CWD, from, CWD, to, RenameFlags::NOREPLACE).map_err(std::io::Error::from) } #[cfg(not(target_os = "linux"))] -fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { +pub fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { if std::fs::symlink_metadata(to).is_ok() { return Err(std::io::ErrorKind::AlreadyExists.into()); } diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs new file mode 100644 index 0000000..dc9dfd6 --- /dev/null +++ b/crates/weft-ipc-types/src/trust.rs @@ -0,0 +1,400 @@ +//! Package content identity, publisher keys and the trust decisions made +//! before a package is installed or launched. +//! +//! A package is signed over its content digest: the SHA-256 of a canonical +//! inventory listing every file except the root `signature.sig`, one +//! `\t\n` line per file, sorted by path. Paths use +//! `/` and must be UTF-8. Symbolic links and special files are refused, so the +//! signed bytes are exactly the files a package holds. + +use std::fmt; +use std::path::{Path, PathBuf}; + +use ed25519_dalek::{Signature, Verifier, VerifyingKey}; +use sha2::{Digest, Sha256}; + +/// The signature file at the root of a signed package. +pub const SIGNATURE_FILE: &str = "signature.sig"; + +/// Why a package's content or trust could not be established. +#[derive(Debug)] +pub enum TrustError { + /// The package holds a symbolic link. + Link(PathBuf), + /// The package holds something other than directories and regular files. + NotAFile(PathBuf), + /// A path in the package is not UTF-8. + NonUtf8(PathBuf), + /// A key, signature or owner record is malformed. + Malformed(PathBuf, String), + Io(PathBuf, std::io::Error), +} + +impl fmt::Display for TrustError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Link(p) => write!(f, "{} is a symbolic link", p.display()), + Self::NotAFile(p) => write!(f, "{} is not a regular file or directory", p.display()), + Self::NonUtf8(p) => write!(f, "{} is not a UTF-8 path", p.display()), + Self::Malformed(p, why) => write!(f, "{}: {why}", p.display()), + Self::Io(p, e) => write!(f, "{}: {e}", p.display()), + } + } +} + +impl std::error::Error for TrustError {} + +fn io(path: &Path) -> impl FnOnce(std::io::Error) -> TrustError + '_ { + move |e| TrustError::Io(path.to_path_buf(), e) +} + +/// The SHA-256 content digest a package signature covers. +pub fn content_digest(package_root: &Path) -> Result<[u8; 32], TrustError> { + let mut entries = Vec::new(); + collect(package_root, package_root, &mut entries)?; + entries.sort(); + let mut inventory = String::new(); + for (path, hash) in &entries { + inventory.push_str(path); + inventory.push('\t'); + inventory.push_str(&hex::encode(hash)); + inventory.push('\n'); + } + Ok(Sha256::digest(inventory.as_bytes()).into()) +} + +fn collect( + root: &Path, + dir: &Path, + entries: &mut Vec<(String, [u8; 32])>, +) -> Result<(), TrustError> { + for entry in std::fs::read_dir(dir).map_err(io(dir))? { + let path = entry.map_err(io(dir))?.path(); + let relative = path + .strip_prefix(root) + .expect("read_dir yields children of the directory it reads") + .to_str() + .ok_or_else(|| TrustError::NonUtf8(path.clone()))? + .to_owned(); + let kind = std::fs::symlink_metadata(&path) + .map_err(io(&path))? + .file_type(); + if kind.is_symlink() { + return Err(TrustError::Link(path)); + } else if kind.is_dir() { + collect(root, &path, entries)?; + } else if !kind.is_file() { + return Err(TrustError::NotAFile(path)); + } else if relative != SIGNATURE_FILE { + let bytes = std::fs::read(&path).map_err(io(&path))?; + entries.push((relative, Sha256::digest(&bytes).into())); + } + } + Ok(()) +} + +/// A publisher's Ed25519 public key. +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct PublisherKey(VerifyingKey); + +impl fmt::Debug for PublisherKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "PublisherKey({})", self.to_hex()) + } +} + +impl PublisherKey { + /// Parses 64 hex digits, the format `weft-pack generate-key` writes. + pub fn from_hex(text: &str) -> Result { + let bytes: [u8; 32] = hex::decode(text.trim()) + .map_err(|_| "expected 64 hex digits".to_owned())? + .try_into() + .map_err(|_| "expected a 32-byte key".to_owned())?; + VerifyingKey::from_bytes(&bytes) + .map(Self) + .map_err(|_| "not a valid Ed25519 public key".to_owned()) + } + + pub fn from_file(path: &Path) -> Result { + let text = std::fs::read_to_string(path).map_err(io(path))?; + Self::from_hex(&text).map_err(|why| TrustError::Malformed(path.to_path_buf(), why)) + } + + /// The key as 64 lowercase hex digits; it also identifies the publisher. + pub fn to_hex(&self) -> String { + hex::encode(self.0.as_bytes()) + } + + /// Whether `signature` signs `digest` with this key. + pub fn signed(&self, digest: &[u8; 32], signature: &Signature) -> bool { + self.0.verify(digest, signature).is_ok() + } +} + +/// Reads the package's signature, or `None` when the package is unsigned. +pub fn read_signature(package_root: &Path) -> Result, TrustError> { + let path = package_root.join(SIGNATURE_FILE); + match std::fs::symlink_metadata(&path) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(TrustError::Io(path, e)), + Ok(m) if !m.file_type().is_file() => return Err(TrustError::NotAFile(path)), + Ok(_) => {} + } + let text = std::fs::read_to_string(&path).map_err(io(&path))?; + let bytes: [u8; 64] = hex::decode(text.trim()) + .ok() + .and_then(|b| b.try_into().ok()) + .ok_or_else(|| TrustError::Malformed(path, "expected 128 hex digits".to_owned()))?; + Ok(Some(Signature::from_bytes(&bytes))) +} + +/// The publisher keys this host trusts to sign packages. +#[derive(Debug, Default)] +pub struct TrustStore { + keys: Vec, +} + +impl TrustStore { + /// The trust store directories, in order: `$WEFT_TRUSTED_KEYS` alone when + /// set; otherwise the user's `$XDG_CONFIG_HOME/weft/trusted-keys` (or + /// `~/.config/weft/trusted-keys`) and the system `/etc/weft/trusted-keys`. + pub fn directories() -> Vec { + if let Some(dir) = std::env::var_os("WEFT_TRUSTED_KEYS") { + return vec![PathBuf::from(dir)]; + } + let config = std::env::var_os("XDG_CONFIG_HOME") + .map(PathBuf::from) + .filter(|dir| dir.is_absolute()) + .or_else(|| { + std::env::var_os("HOME") + .map(PathBuf::from) + .filter(|home| home.is_absolute()) + .map(|home| home.join(".config")) + }); + let mut dirs: Vec = config + .map(|dir| dir.join("weft/trusted-keys")) + .into_iter() + .collect(); + dirs.push(PathBuf::from("/etc/weft/trusted-keys")); + dirs + } + + /// Loads every `*.pub` file in `directories`. A missing directory holds no + /// keys; a malformed key file is an error rather than silently ignored. + pub fn load(directories: &[PathBuf]) -> Result { + let mut keys = Vec::new(); + for dir in directories { + let entries = match std::fs::read_dir(dir) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue, + other => other.map_err(io(dir))?, + }; + let mut files: Vec = entries + .map(|e| e.map(|e| e.path()).map_err(io(dir))) + .collect::>()?; + files.sort(); + for file in files + .into_iter() + .filter(|f| f.extension().is_some_and(|e| e == "pub")) + { + keys.push(PublisherKey::from_file(&file)?); + } + } + Ok(Self { keys }) + } + + pub fn keys(&self) -> &[PublisherKey] { + &self.keys + } + + /// The trusted key that signed the package at `package_root`, if any. + pub fn signer(&self, package_root: &Path) -> Result, TrustError> { + let Some(signature) = read_signature(package_root)? else { + return Ok(None); + }; + let digest = content_digest(package_root)?; + Ok(self + .keys + .iter() + .copied() + .find(|k| k.signed(&digest, &signature))) + } +} + +/// Who an application ID belongs to on this host. The record outlives the +/// package while the app's data remains, so another publisher cannot take +/// over the ID, and the data with it, by reusing it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Owner { + /// Installed from packages signed by this trusted key. + Verified(PublisherKey), + /// Installed as development content, without a trusted signature. + Development, +} + +impl fmt::Display for Owner { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Verified(key) => write!(f, "publisher {}", key.to_hex()), + Self::Development => f.write_str("development installation"), + } + } +} + +/// Where the owner of `app_id` is recorded, beside the app's data. +pub fn owner_record_path(data_home: &Path, app_id: &str) -> PathBuf { + data_home.join("weft/owners").join(app_id) +} + +/// Reads an owner record: `verified ` or `development`. +pub fn read_owner(record: &Path) -> Result, TrustError> { + let text = match std::fs::read_to_string(record) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + other => other.map_err(io(record))?, + }; + let malformed = |why: &str| TrustError::Malformed(record.to_path_buf(), why.to_owned()); + match text.trim().split_once(' ') { + Some(("verified", key)) => PublisherKey::from_hex(key) + .map(|k| Some(Owner::Verified(k))) + .map_err(|why| malformed(&why)), + None if text.trim() == "development" => Ok(Some(Owner::Development)), + _ => Err(malformed("expected 'verified ' or 'development'")), + } +} + +/// Records the owner of an app ID. An existing record is never replaced. +pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { + use std::io::Write; + let parent = record.parent().expect("owner records live in a directory"); + std::fs::create_dir_all(parent).map_err(io(parent))?; + let text = match owner { + Owner::Verified(key) => format!("verified {}\n", key.to_hex()), + Owner::Development => "development\n".to_owned(), + }; + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(record) + .map_err(io(record))?; + file.write_all(text.as_bytes()).map_err(io(record))?; + file.sync_all().map_err(io(record)) +} + +#[cfg(test)] +mod tests { + use super::*; + use ed25519_dalek::{Signer, SigningKey}; + + fn dir(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("weft_trust_{name}_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("ui")).unwrap(); + std::fs::write(dir.join("wapp.toml"), b"[package]\n").unwrap(); + std::fs::write(dir.join("ui/index.html"), b"

").unwrap(); + dir + } + + fn sign(dir: &Path, seed: u8) -> PublisherKey { + let key = SigningKey::from_bytes(&[seed; 32]); + let signature = key.sign(&content_digest(dir).unwrap()); + std::fs::write(dir.join(SIGNATURE_FILE), hex::encode(signature.to_bytes())).unwrap(); + PublisherKey(key.verifying_key()) + } + + fn store(keys: &[PublisherKey]) -> TrustStore { + TrustStore { + keys: keys.to_vec(), + } + } + + #[test] + fn the_committed_demo_signatures_verify_with_the_demo_key() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let key = PublisherKey::from_file(&root.join("examples/keys/weft-sign.pub")).unwrap(); + for demo in ["org.weft.demo.counter", "org.weft.demo.notes"] { + let package = root.join("examples").join(demo); + assert_eq!(store(&[key]).signer(&package).unwrap(), Some(key), "{demo}"); + } + } + + #[test] + fn any_changed_file_breaks_the_signature() { + let dir = dir("tamper"); + let key = sign(&dir, 1); + assert_eq!(store(&[key]).signer(&dir).unwrap(), Some(key)); + std::fs::write(dir.join("ui/index.html"), b"

changed").unwrap(); + assert_eq!(store(&[key]).signer(&dir).unwrap(), None); + std::fs::write(dir.join("ui/index.html"), b"

").unwrap(); + std::fs::write(dir.join("ui/extra.js"), b"").unwrap(); + assert_eq!(store(&[key]).signer(&dir).unwrap(), None); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn only_trusted_keys_are_signers_and_unsigned_packages_have_none() { + let dir = dir("untrusted"); + assert_eq!(store(&[]).signer(&dir).unwrap(), None); + let key = sign(&dir, 2); + let other = PublisherKey(SigningKey::from_bytes(&[3; 32]).verifying_key()); + assert_eq!(store(&[other]).signer(&dir).unwrap(), None); + assert_eq!(store(&[other, key]).signer(&dir).unwrap(), Some(key)); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[cfg(target_os = "linux")] + #[test] + fn links_and_special_files_have_no_content_digest() { + let dir = dir("links"); + std::os::unix::fs::symlink("/etc/hostname", dir.join("ui/linked")).unwrap(); + assert!(matches!(content_digest(&dir), Err(TrustError::Link(_)))); + std::fs::remove_file(dir.join("ui/linked")).unwrap(); + std::os::unix::fs::symlink(dir.join("ui"), dir.join("view")).unwrap(); + assert!(matches!(content_digest(&dir), Err(TrustError::Link(_)))); + std::fs::remove_file(dir.join("view")).unwrap(); + rustix::fs::mknodat( + rustix::fs::CWD, + dir.join("fifo"), + rustix::fs::FileType::Fifo, + rustix::fs::Mode::from_raw_mode(0o600), + 0, + ) + .unwrap(); + assert!(matches!(content_digest(&dir), Err(TrustError::NotAFile(_)))); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn trust_stores_load_pub_files_and_refuse_malformed_keys() { + let dir = dir("store"); + let keys = dir.join("keys"); + std::fs::create_dir_all(&keys).unwrap(); + let key = PublisherKey(SigningKey::from_bytes(&[4; 32]).verifying_key()); + std::fs::write(keys.join("a.pub"), key.to_hex()).unwrap(); + std::fs::write(keys.join("notes.txt"), "not a key").unwrap(); + let loaded = TrustStore::load(&[keys.clone(), dir.join("missing")]).unwrap(); + assert_eq!(loaded.keys(), [key]); + std::fs::write(keys.join("b.pub"), "zz").unwrap(); + assert!(matches!( + TrustStore::load(&[keys]), + Err(TrustError::Malformed(..)) + )); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn owner_records_round_trip_and_are_never_replaced() { + let dir = dir("owners"); + let key = PublisherKey(SigningKey::from_bytes(&[5; 32]).verifying_key()); + let verified = owner_record_path(&dir, "org.weft.test.a"); + let development = owner_record_path(&dir, "org.weft.test.b"); + assert_eq!(read_owner(&verified).unwrap(), None); + write_owner(&verified, Owner::Verified(key)).unwrap(); + write_owner(&development, Owner::Development).unwrap(); + assert_eq!(read_owner(&verified).unwrap(), Some(Owner::Verified(key))); + assert_eq!(read_owner(&development).unwrap(), Some(Owner::Development)); + assert!(write_owner(&verified, Owner::Development).is_err()); + assert_eq!(read_owner(&verified).unwrap(), Some(Owner::Verified(key))); + std::fs::write(&development, "someone else\n").unwrap(); + assert!(read_owner(&development).is_err()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 00ca0fc..df905d5 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf}; use anyhow::Context; use weft_ipc_types::manifest::{Manifest, entry_path}; use weft_ipc_types::package::ImageFiles; +use weft_ipc_types::trust::{Owner, PublisherKey, TrustStore}; fn main() -> anyhow::Result<()> { let args: Vec = std::env::args().collect(); @@ -19,8 +20,14 @@ fn main() -> anyhow::Result<()> { print_info(&manifest); } Some("install") => { - let dir = args.get(2).context("usage: weft-pack install

")?; - install_package(Path::new(dir))?; + let usage = "usage: weft-pack install [--dev]"; + let dir = args.get(2).context(usage)?; + let mode = match args.get(3).map(String::as_str) { + None => InstallMode::Verified, + Some("--dev") => InstallMode::Development, + Some(other) => anyhow::bail!("unexpected argument '{other}'; {usage}"), + }; + install_package(Path::new(dir), mode)?; } Some("uninstall") => { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; @@ -86,7 +93,7 @@ fn main() -> anyhow::Result<()> { .find(|w| w[0] == "--key") .map(|w| &w[1]) .context("missing --key ")?; - let ok = verify_package(Path::new(dir), Path::new(key))?; + let ok = verify_package(Path::new(dir), &PublisherKey::from_file(Path::new(key))?)?; if ok { println!("OK"); } else { @@ -98,7 +105,12 @@ fn main() -> anyhow::Result<()> { eprintln!("usage:"); eprintln!(" weft-pack check validate a package directory"); eprintln!(" weft-pack info print package metadata"); - eprintln!(" weft-pack install install package to app store"); + eprintln!( + " weft-pack install [--dev] install a package signed by a trusted key" + ); + eprintln!( + " (--dev: unsigned development content)" + ); eprintln!(" weft-pack uninstall remove installed package"); eprintln!(" weft-pack list list installed packages"); eprintln!( @@ -128,6 +140,12 @@ fn check_package(dir: &Path) -> anyhow::Result { } }; + // A package holds only directories and regular files: its signature + // covers exactly those, and installation copies exactly those. + if let Err(e) = weft_ipc_types::trust::content_digest(dir) { + errors.push(e.to_string()); + } + // Earlier versions kept app data in `/data`; the name stays // reserved so package content is never mistaken for user data. if std::fs::symlink_metadata(dir.join("data")).is_ok() { @@ -229,12 +247,22 @@ fn resolve_install_root() -> anyhow::Result { anyhow::bail!("cannot determine install root: HOME and WEFT_APP_STORE are both unset") } -fn install_package(path: &Path) -> anyhow::Result<()> { - let root = resolve_install_root()?; - install_package_to(path, &root) +/// How a package is admitted to the store. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum InstallMode { + /// The package must be signed by a key in the trust store. + Verified, + /// Unsigned or untrusted content, chosen explicitly by a developer and + /// recorded as development content. + Development, } -fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> { +fn install_package(path: &Path, mode: InstallMode) -> anyhow::Result<()> { + let root = resolve_install_root()?; + install_package_to(path, &root, mode) +} + +fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { let dir_buf; let dir: &Path = if path.extension().is_some_and(|e| e == "zst" || e == "tar") || path.to_string_lossy().ends_with(".app.tar.zst") @@ -280,12 +308,78 @@ fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> { dest.display() ); } - copy_dir(dir, &dest) - .with_context(|| format!("copy {} -> {}", dir.display(), dest.display()))?; - println!("installed {} -> {}", app_id, dest.display()); + + // The package is copied into the store first and every decision is made + // on that copy, so the bytes that are checked and trusted are the bytes + // that become installed. Only then does one rename make it available. + std::fs::create_dir_all(store_root) + .with_context(|| format!("create {}", store_root.display()))?; + let staging = store_root.join(format!( + ".staging-{app_id}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + let staged = copy_dir(dir, &staging) + .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) + .and_then(|()| admit(&staging, app_id, mode)) + .and_then(|owner| { + weft_ipc_types::package::rename_no_replace(&staging, &dest) + .with_context(|| format!("move {} -> {}", staging.display(), dest.display()))?; + Ok(owner) + }); + let owner = match staged { + Ok(owner) => owner, + Err(e) => { + let _ = std::fs::remove_dir_all(&staging); + return Err(e); + } + }; + println!("installed {} -> {} ({owner})", app_id, dest.display()); Ok(()) } +/// Checks the staged copy of a package and establishes who owns its app ID: +/// the trusted publisher that signed it, or, only when asked, development. +/// The first installation of an ID records its owner, and every later one +/// must have the same owner. +fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { + use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; + check_package(staged)?; + let manifest = load_manifest(staged)?; + anyhow::ensure!( + manifest.package.id == app_id, + "the package changed while it was copied" + ); + let owner = match mode { + InstallMode::Development => Owner::Development, + InstallMode::Verified => { + let dirs = TrustStore::directories(); + let signer = TrustStore::load(&dirs)?.signer(staged)?; + Owner::Verified(signer.with_context(|| { + format!( + "{app_id} is not signed by a trusted key (trusted keys are read from {}); \ + install it with --dev to use it as development content", + dirs.iter() + .map(|d| d.display().to_string()) + .collect::>() + .join(", ") + ) + })?) + } + }; + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to record who owns the app ID")?; + let record = owner_record_path(&data_home, app_id); + match read_owner(&record)? { + Some(existing) if existing == owner => {} + Some(existing) => anyhow::bail!( + "{app_id} belongs to {existing}; this package is {owner}. Its app data stays with \ + its owner, so another publisher or a development build cannot take the ID over" + ), + None => write_owner(&record, owner)?, + } + Ok(owner) +} + fn uninstall_package(app_id: &str) -> anyhow::Result<()> { let root = resolve_install_root()?; uninstall_package_from(app_id, &root) @@ -326,6 +420,14 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> } _ => {} } + // The owner record keeps the app ID, and the data with it, for its owner + // while the data remains; with no data left, the ID is free again. + if let Some(data_home) = weft_ipc_types::package::data_home() + && std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(&data_home, app_id)) + .is_err() + { + let _ = std::fs::remove_file(weft_ipc_types::trust::owner_record_path(&data_home, app_id)); + } println!("uninstalled {}", app_id); Ok(()) } @@ -416,6 +518,11 @@ fn list_installed() { let Ok(m) = Manifest::read(&entry.path()) else { continue; }; + // Only a directory named after the ID its manifest declares is an + // installed package; staging copies and stray directories are not. + if entry.file_name().to_str() != Some(m.package.id.as_str()) { + continue; + } if seen.insert(m.package.id.clone()) { pkgs.push((m.package.id, m.package.name, m.package.version)); } @@ -431,17 +538,26 @@ fn list_installed() { } } +/// Copies a package directory. Packages hold only directories and regular +/// files, so a symbolic link or special file stops the copy rather than +/// being followed. fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { - std::fs::create_dir_all(dst)?; + std::fs::create_dir(dst).with_context(|| format!("create {}", dst.display()))?; for entry in std::fs::read_dir(src)? { let entry = entry?; let src_path = entry.path(); let dst_path = dst.join(entry.file_name()); - if src_path.is_dir() { + let kind = std::fs::symlink_metadata(&src_path)?.file_type(); + if kind.is_dir() { copy_dir(&src_path, &dst_path)?; - } else { + } else if kind.is_file() { std::fs::copy(&src_path, &dst_path) .with_context(|| format!("copy {}", src_path.display()))?; + } else { + anyhow::bail!( + "{} is not a regular file or directory; packages cannot contain links", + src_path.display() + ); } } Ok(()) @@ -540,47 +656,6 @@ fn unbundle_package(archive: &Path, out_dir: &Path) -> anyhow::Result<()> { Ok(()) } -fn collect_bundle_files( - base: &Path, - dir: &Path, - entries: &mut Vec<(String, [u8; 32])>, -) -> anyhow::Result<()> { - use sha2::{Digest, Sha256}; - for entry in std::fs::read_dir(dir).with_context(|| format!("read dir {}", dir.display()))? { - let entry = entry?; - let path = entry.path(); - let rel = path - .strip_prefix(base) - .context("strip prefix")? - .to_string_lossy() - .into_owned(); - if rel == "signature.sig" { - continue; - } - if path.is_dir() { - collect_bundle_files(base, &path, entries)?; - } else { - let contents = - std::fs::read(&path).with_context(|| format!("read {}", path.display()))?; - let hash: [u8; 32] = Sha256::digest(&contents).into(); - entries.push((rel, hash)); - } - } - Ok(()) -} - -fn canonical_bundle_hash(dir: &Path) -> anyhow::Result<[u8; 32]> { - use sha2::{Digest, Sha256}; - let mut entries: Vec<(String, [u8; 32])> = Vec::new(); - collect_bundle_files(dir, dir, &mut entries)?; - entries.sort_by(|a, b| a.0.cmp(&b.0)); - let mut canonical = String::new(); - for (path, hash) in &entries { - canonical.push_str(&format!("{path}\t{}\n", hex::encode(hash))); - } - Ok(Sha256::digest(canonical.as_bytes()).into()) -} - fn generate_key(output_dir: &Path) -> anyhow::Result<()> { use ed25519_dalek::SigningKey; use rand::rngs::OsRng; @@ -614,34 +689,20 @@ fn sign_package(dir: &Path, key_file: &Path) -> anyhow::Result<()> { .try_into() .map_err(|_| anyhow::anyhow!("signing key must be 32 bytes"))?; let signing_key = SigningKey::from_bytes(&key_bytes); - let hash = canonical_bundle_hash(dir)?; - let signature = signing_key.sign(&hash); - let sig_path = dir.join("signature.sig"); + let digest = weft_ipc_types::trust::content_digest(dir)?; + let signature = signing_key.sign(&digest); + let sig_path = dir.join(weft_ipc_types::trust::SIGNATURE_FILE); std::fs::write(&sig_path, hex::encode(signature.to_bytes())) .with_context(|| format!("write {}", sig_path.display()))?; println!("signed: {}", sig_path.display()); Ok(()) } -fn verify_package(dir: &Path, pub_key_file: &Path) -> anyhow::Result { - use ed25519_dalek::{Signature, Verifier, VerifyingKey}; - let pub_hex = std::fs::read_to_string(pub_key_file) - .with_context(|| format!("read {}", pub_key_file.display()))?; - let pub_bytes: [u8; 32] = hex::decode(pub_hex.trim()) - .context("decode public key: expected 64 hex chars")? - .try_into() - .map_err(|_| anyhow::anyhow!("public key must be 32 bytes"))?; - let verifying_key = VerifyingKey::from_bytes(&pub_bytes).context("invalid public key bytes")?; - let sig_path = dir.join("signature.sig"); - let sig_hex = std::fs::read_to_string(&sig_path) - .with_context(|| format!("read {}", sig_path.display()))?; - let sig_bytes: [u8; 64] = hex::decode(sig_hex.trim()) - .context("decode signature: expected 128 hex chars")? - .try_into() - .map_err(|_| anyhow::anyhow!("signature must be 64 bytes"))?; - let signature = Signature::from_bytes(&sig_bytes); - let hash = canonical_bundle_hash(dir)?; - Ok(verifying_key.verify(&hash, &signature).is_ok()) +fn verify_package(dir: &Path, key: &PublisherKey) -> anyhow::Result { + let signature = weft_ipc_types::trust::read_signature(dir)? + .with_context(|| format!("{} is not signed", dir.display()))?; + let digest = weft_ipc_types::trust::content_digest(dir)?; + Ok(key.signed(&digest, &signature)) } #[cfg(test)] @@ -704,13 +765,187 @@ mod tests { const APP_DATA: &str = "\"fs:rw:app-data\""; + /// Runs `f` like `with_home`, with only the keys in `home/keys` trusted. + fn with_trust(home: &Path, f: impl FnOnce() -> T) -> T { + with_home(home, || { + let prior = std::env::var_os("WEFT_TRUSTED_KEYS"); + // SAFETY: with_home holds env_lock. + unsafe { std::env::set_var("WEFT_TRUSTED_KEYS", home.join("keys")) }; + let result = f(); + unsafe { + match prior { + Some(v) => std::env::set_var("WEFT_TRUSTED_KEYS", v), + None => std::env::remove_var("WEFT_TRUSTED_KEYS"), + } + } + result + }) + } + + /// Writes a signing key derived from `seed` to `home/.key` and + /// returns its path; with `trusted`, its public key joins `home/keys`. + fn key(home: &Path, name: &str, seed: u8, trusted: bool) -> PathBuf { + let signing = ed25519_dalek::SigningKey::from_bytes(&[seed; 32]); + std::fs::create_dir_all(home.join("keys")).unwrap(); + let path = home.join(format!("{name}.key")); + std::fs::write(&path, hex::encode(signing.to_bytes())).unwrap(); + if trusted { + std::fs::write( + home.join("keys").join(format!("{name}.pub")), + hex::encode(signing.verifying_key().to_bytes()), + ) + .unwrap(); + } + path + } + + fn owner_of(home: &Path, app_id: &str) -> Option { + weft_ipc_types::trust::read_owner(&weft_ipc_types::trust::owner_record_path( + &home.join("share"), + app_id, + )) + .unwrap() + } + + fn staging_left(store: &Path) -> bool { + std::fs::read_dir(store).is_ok_and(|entries| { + entries + .flatten() + .any(|e| e.file_name().to_string_lossy().starts_with(".staging-")) + }) + } + + #[test] + fn only_packages_signed_by_a_trusted_key_install_as_verified() { + let home = temp_root("verified"); + let store = home.join("store"); + let app_id = "org.weft.test.verified"; + write_package(&home.join("src"), app_id, ""); + let publisher = key(&home, "publisher", 1, true); + let stranger = key(&home, "stranger", 2, false); + + // Unsigned, then signed by a key the host does not trust. + let unsigned = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + sign_package(&home.join("src"), &stranger).unwrap(); + let untrusted = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + for refused in [&unsigned, &untrusted] { + let message = format!("{:#}", refused.as_ref().unwrap_err()); + assert!(message.contains("not signed by a trusted key"), "{message}"); + } + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + assert_eq!(owner_of(&home, app_id), None); + + // Signed by a trusted key. + sign_package(&home.join("src"), &publisher).unwrap(); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }) + .unwrap(); + assert!(store.join(app_id).join("signature.sig").exists()); + let key = PublisherKey::from_file(&home.join("keys/publisher.pub")).unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Verified(key))); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn content_changed_after_signing_is_refused() { + let home = temp_root("tampered"); + let store = home.join("store"); + let app_id = "org.weft.test.tampered"; + write_package(&home.join("src"), app_id, ""); + sign_package(&home.join("src"), &key(&home, "publisher", 3, true)).unwrap(); + std::fs::write(home.join("src/ui/index.html"), "").unwrap(); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + assert!(refused.is_err()); + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn an_app_id_stays_with_its_first_owner_while_its_data_remains() { + let home = temp_root("owner"); + let store = home.join("store"); + let app_id = "org.weft.test.owned"; + write_package(&home.join("src"), app_id, APP_DATA); + sign_package(&home.join("src"), &key(&home, "first", 4, true)).unwrap(); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }) + .unwrap(); + let data = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + std::fs::create_dir_all(&data).unwrap(); + with_trust(&home, || uninstall_package_from(app_id, &store)).unwrap(); + + // Another trusted publisher, and a development build, cannot take the + // ID over while the first owner's data remains. + sign_package(&home.join("src"), &key(&home, "second", 5, true)).unwrap(); + let other_publisher = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + let development = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + for refused in [&other_publisher, &development] { + let message = format!("{:#}", refused.as_ref().unwrap_err()); + assert!(message.contains("belongs to publisher"), "{message}"); + } + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + + // With no data left, uninstall frees the ID. + std::fs::remove_dir_all(&data).unwrap(); + sign_package(&home.join("src"), &home.join("first.key")).unwrap(); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }) + .unwrap(); + with_trust(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert_eq!(owner_of(&home, app_id), None); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] + #[test] + fn packages_with_links_are_refused() { + let home = temp_root("links"); + let store = home.join("store"); + let app_id = "org.weft.test.links"; + write_package(&home.join("src"), app_id, ""); + std::os::unix::fs::symlink("/etc/hostname", home.join("src/ui/host.txt")).unwrap(); + assert!(check_package(&home.join("src")).is_err()); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + assert!(refused.is_err()); + assert!(!store.join(app_id).exists()); + assert!(sign_package(&home.join("src"), &key(&home, "publisher", 6, true)).is_err()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); let app_id = "com.example.keep"; write_package(&home.join("src"), app_id, APP_DATA); let store = home.join(".local/share/weft/apps"); - install_package_to(&home.join("src"), &store).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); let legacy = store.join(app_id).join("data"); std::fs::create_dir_all(&legacy).unwrap(); std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap(); @@ -730,7 +965,10 @@ mod tests { } // A second copy cannot be merged: uninstall refuses and removes nothing. - install_package_to(&home.join("src"), &store).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); std::fs::create_dir_all(&legacy).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); assert!(legacy.is_dir() && kept.is_dir()); @@ -744,7 +982,10 @@ mod tests { // No app-data capability: the data directory is left and uninstall stops. write_package(&home.join("src"), app_id, ""); let store = home.join(".local/share/weft/apps"); - install_package_to(&home.join("src"), &store).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); @@ -757,11 +998,14 @@ mod tests { // Another store: earlier versions never kept data there, so it stops too. write_package(&home.join("src2"), app_id, APP_DATA); let other = home.join("other-store"); - install_package_to(&home.join("src2"), &other).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src2"), &other, InstallMode::Development) + }) + .unwrap(); std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &other)).is_err()); assert!(other.join(app_id).join("data").is_dir()); - assert!(!home.join("share").exists()); + assert!(!home.join("share/weft/app-data").exists()); let _ = std::fs::remove_dir_all(&home); } @@ -808,15 +1052,21 @@ mod tests { #[test] fn packages_cannot_ship_a_top_level_data_entry() { - let root = temp_root("ships-data"); - let src = root.join("src"); - write_package(&src, "com.example.ships", ""); - std::fs::create_dir_all(src.join("data")).unwrap(); - std::fs::write(src.join("data/asset.json"), "{}").unwrap(); - let err = check_package(&src).unwrap_err().to_string(); - assert!(err.contains("'data'"), "{err}"); - assert!(install_package_to(&src, &root.join("store")).is_err()); - let _ = std::fs::remove_dir_all(&root); + let home = temp_root("packages_cannot_ship_a_top_level_data_entry"); + with_home(&home.clone(), move || { + let root = temp_root("ships-data"); + let src = root.join("src"); + write_package(&src, "com.example.ships", ""); + std::fs::create_dir_all(src.join("data")).unwrap(); + std::fs::write(src.join("data/asset.json"), "{}").unwrap(); + let err = check_package(&src).unwrap_err().to_string(); + assert!(err.contains("'data'"), "{err}"); + assert!( + install_package_to(&src, &root.join("store"), InstallMode::Development).is_err() + ); + let _ = std::fs::remove_dir_all(&root); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] @@ -828,7 +1078,10 @@ mod tests { std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); - with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); assert!(store.join(app_id).join("wapp.toml").exists()); assert!(!store.join(app_id).join("data").exists()); let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); @@ -837,7 +1090,10 @@ mod tests { // An empty directory left behind does not block installation either. with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); std::fs::create_dir_all(store.join(app_id)).unwrap(); - with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); let _ = std::fs::remove_dir_all(&home); } @@ -970,60 +1226,68 @@ entry = "ui/index.html" #[test] fn install_package_copies_to_store() { - use std::fs; - let id = format!("weft.pack.install{}", std::process::id()); - let src = std::env::temp_dir().join(format!("weft_pack_install_src_{}", id)); - let store = std::env::temp_dir().join(format!("weft_pack_install_store_{}", id)); - let ui_dir = src.join("ui"); - let _ = fs::create_dir_all(&ui_dir); - fs::write(src.join("app.wasm"), b"\0asm").unwrap(); - fs::write(ui_dir.join("index.html"), b"").unwrap(); - let app_id = format!("com.example.t{}", std::process::id()); - fs::write( - src.join("wapp.toml"), - format!( - "[package]\nid = \"{app_id}\"\nname = \"Test\"\nversion = \"1.0.0\"\n\n\ - [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" - ), - ) - .unwrap(); - let result = install_package_to(&src, &store); - assert!(result.is_ok(), "{result:?}"); - assert!(store.join(&app_id).join("app.wasm").exists()); - assert!(store.join(&app_id).join("wapp.toml").exists()); - assert!(store.join(&app_id).join("ui").join("index.html").exists()); - assert!(install_package_to(&src, &store).is_err()); - let _ = fs::remove_dir_all(&src); - let _ = fs::remove_dir_all(&store); + let home = temp_root("install_package_copies_to_store"); + with_home(&home.clone(), move || { + use std::fs; + let id = format!("weft.pack.install{}", std::process::id()); + let src = std::env::temp_dir().join(format!("weft_pack_install_src_{}", id)); + let store = std::env::temp_dir().join(format!("weft_pack_install_store_{}", id)); + let ui_dir = src.join("ui"); + let _ = fs::create_dir_all(&ui_dir); + fs::write(src.join("app.wasm"), b"\0asm").unwrap(); + fs::write(ui_dir.join("index.html"), b"").unwrap(); + let app_id = format!("com.example.t{}", std::process::id()); + fs::write( + src.join("wapp.toml"), + format!( + "[package]\nid = \"{app_id}\"\nname = \"Test\"\nversion = \"1.0.0\"\n\n\ + [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); + let result = install_package_to(&src, &store, InstallMode::Development); + assert!(result.is_ok(), "{result:?}"); + assert!(store.join(&app_id).join("app.wasm").exists()); + assert!(store.join(&app_id).join("wapp.toml").exists()); + assert!(store.join(&app_id).join("ui").join("index.html").exists()); + assert!(install_package_to(&src, &store, InstallMode::Development).is_err()); + let _ = fs::remove_dir_all(&src); + let _ = fs::remove_dir_all(&store); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] fn uninstall_package_removes_directory() { - use std::fs; - let id = format!("weft.pack.uninstall{}", std::process::id()); - let src = std::env::temp_dir().join(format!("weft_pack_uninstall_src_{}", id)); - let store = std::env::temp_dir().join(format!("weft_pack_uninstall_store_{}", id)); - let ui_dir = src.join("ui"); - let _ = fs::create_dir_all(&ui_dir); - fs::write(src.join("app.wasm"), b"\0asm").unwrap(); - fs::write(ui_dir.join("index.html"), b"").unwrap(); - let app_id = format!("com.example.u{}", std::process::id()); - fs::write( - src.join("wapp.toml"), - format!( - "[package]\nid = \"{app_id}\"\nname = \"U\"\nversion = \"1.0.0\"\n\n\ - [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" - ), - ) - .unwrap(); - install_package_to(&src, &store).unwrap(); - assert!(store.join(&app_id).exists()); - let result = uninstall_package_from(&app_id, &store); - assert!(result.is_ok(), "{result:?}"); - assert!(!store.join(&app_id).exists()); - assert!(uninstall_package_from(&app_id, &store).is_err()); - let _ = fs::remove_dir_all(&src); - let _ = fs::remove_dir_all(&store); + let home = temp_root("uninstall_package_removes_directory"); + with_home(&home.clone(), move || { + use std::fs; + let id = format!("weft.pack.uninstall{}", std::process::id()); + let src = std::env::temp_dir().join(format!("weft_pack_uninstall_src_{}", id)); + let store = std::env::temp_dir().join(format!("weft_pack_uninstall_store_{}", id)); + let ui_dir = src.join("ui"); + let _ = fs::create_dir_all(&ui_dir); + fs::write(src.join("app.wasm"), b"\0asm").unwrap(); + fs::write(ui_dir.join("index.html"), b"").unwrap(); + let app_id = format!("com.example.u{}", std::process::id()); + fs::write( + src.join("wapp.toml"), + format!( + "[package]\nid = \"{app_id}\"\nname = \"U\"\nversion = \"1.0.0\"\n\n\ + [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); + install_package_to(&src, &store, InstallMode::Development).unwrap(); + assert!(store.join(&app_id).exists()); + let result = uninstall_package_from(&app_id, &store); + assert!(result.is_ok(), "{result:?}"); + assert!(!store.join(&app_id).exists()); + assert!(uninstall_package_from(&app_id, &store).is_err()); + let _ = fs::remove_dir_all(&src); + let _ = fs::remove_dir_all(&store); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] @@ -1089,36 +1353,40 @@ entry = "ui/index.html" #[test] fn install_package_from_archive() { - use std::fs; - let id = format!("instarch_{}", std::process::id()); - let src = std::env::temp_dir().join(&id); - let ui = src.join("ui"); - let _ = fs::create_dir_all(&ui); - fs::write(src.join("app.wasm"), b"\0asm\x01\0\0\0").unwrap(); - fs::write(ui.join("index.html"), b"").unwrap(); - let app_id = format!("com.example.ia{}", std::process::id()); - fs::write( - src.join("wapp.toml"), - format!( - "[package]\nid = \"{app_id}\"\nname = \"IA\"\nversion = \"1.0.0\"\n\n\ - [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" - ), - ) - .unwrap(); + let home = temp_root("install_package_from_archive"); + with_home(&home.clone(), move || { + use std::fs; + let id = format!("instarch_{}", std::process::id()); + let src = std::env::temp_dir().join(&id); + let ui = src.join("ui"); + let _ = fs::create_dir_all(&ui); + fs::write(src.join("app.wasm"), b"\0asm\x01\0\0\0").unwrap(); + fs::write(ui.join("index.html"), b"").unwrap(); + let app_id = format!("com.example.ia{}", std::process::id()); + fs::write( + src.join("wapp.toml"), + format!( + "[package]\nid = \"{app_id}\"\nname = \"IA\"\nversion = \"1.0.0\"\n\n\ + [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); - let bundle_dir = std::env::temp_dir().join(format!("{id}_bnd")); - let _ = fs::create_dir_all(&bundle_dir); - bundle_package(&src, Some(&bundle_dir)).unwrap(); - let archive = bundle_dir.join(format!("{app_id}.app.tar.zst")); + let bundle_dir = std::env::temp_dir().join(format!("{id}_bnd")); + let _ = fs::create_dir_all(&bundle_dir); + bundle_package(&src, Some(&bundle_dir)).unwrap(); + let archive = bundle_dir.join(format!("{app_id}.app.tar.zst")); - let store = std::env::temp_dir().join(format!("{id}_store")); - let _ = fs::create_dir_all(&store); - install_package_to(&archive, &store).unwrap(); - assert!(store.join(&app_id).join("app.wasm").exists()); + let store = std::env::temp_dir().join(format!("{id}_store")); + let _ = fs::create_dir_all(&store); + install_package_to(&archive, &store, InstallMode::Development).unwrap(); + assert!(store.join(&app_id).join("app.wasm").exists()); - let _ = fs::remove_dir_all(&src); - let _ = fs::remove_dir_all(&bundle_dir); - let _ = fs::remove_dir_all(&store); + let _ = fs::remove_dir_all(&src); + let _ = fs::remove_dir_all(&bundle_dir); + let _ = fs::remove_dir_all(&store); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] @@ -1181,7 +1449,7 @@ entry = "ui/index.html" sign_package(&dir, &key_file).unwrap(); assert!(dir.join("signature.sig").exists()); - let ok = verify_package(&dir, &pub_file).unwrap(); + let ok = verify_package(&dir, &PublisherKey::from_file(&pub_file).unwrap()).unwrap(); assert!(ok, "signature should verify"); let _ = fs::remove_dir_all(&dir); @@ -1210,7 +1478,11 @@ entry = "ui/index.html" fs::write(dir.join("app.wasm"), b"\0asm\x01\0\0\x01").unwrap(); - let ok = verify_package(&dir, &key_dir.join("weft-sign.pub")).unwrap(); + let ok = verify_package( + &dir, + &PublisherKey::from_file(&key_dir.join("weft-sign.pub")).unwrap(), + ) + .unwrap(); assert!(!ok, "tampered bundle should not verify"); let _ = fs::remove_dir_all(&dir); diff --git a/docs/architecture.md b/docs/architecture.md index ee92a31..4a94562 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -28,7 +28,7 @@ WASI Preview 2 + Component Model execution host (Wasmtime 30). Runs the componen ### weft-pack -Package management CLI. Subcommands: `check` (validate wapp.toml + wasm module), `sign` (Ed25519 signature), `verify` (verify signature), `generate-key`, `install`, `uninstall`, `list`, `build-image` (EROFS dm-verity), `info`. +Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner), `uninstall`, `list`, `build-image` (EROFS dm-verity), `info`. ### weft-file-portal @@ -102,7 +102,7 @@ Fetch grants name hosts, not addresses: ports are not restricted, and a host tha app.wasm — WASI Component Model binary ui/ index.html — entry point served by weft-app-shell - signature.sig — Ed25519 signature over SHA-256 of (wapp.toml + app.wasm) + signature.sig — Ed25519 signature over the content digest of every other file (see security.md) ``` Package store roots (in priority order): diff --git a/docs/building.md b/docs/building.md index fca337a..141b217 100644 --- a/docs/building.md +++ b/docs/building.md @@ -125,6 +125,16 @@ weft-pack sign ./examples/org.weft.demo.counter --key ./keys/weft-sign.key weft-pack verify ./examples/org.weft.demo.counter --key ./keys/weft-sign.pub ``` +Installation requires a signature by a trusted key. To install the demos with the demo key, trust it for your user (the key is a test fixture; do not trust it on a system you depend on): + +```sh +mkdir -p ~/.config/weft/trusted-keys +cp examples/keys/weft-sign.pub ~/.config/weft/trusted-keys/weft-demo.pub +weft-pack install examples/org.weft.demo.notes +``` + +Unsigned packages under development install with `weft-pack install --dev`, recorded as development content. See `docs/security.md` for the trust store and app ID ownership. + ## NixOS VM (requires Nix with flakes) Build the VM image: diff --git a/docs/security.md b/docs/security.md index 09a6b4a..cd25cc1 100644 --- a/docs/security.md +++ b/docs/security.md @@ -18,11 +18,17 @@ Each app session runs as a separate OS process (`weft-runtime`). When systemd is Apps access the filesystem only through WASI preopened directories. Each capability maps to a specific host path preopened at a fixed guest path. The `weft-file-portal` process applies the same directory grants and access modes, but checks paths lexically: it blocks `..` traversal, not symbolic links. Components cannot currently reach its socket, since the runtime grants them no socket access. -## Package Signing +## Package Signing and Trust -Packages are signed with Ed25519 (`ed25519-dalek`). The signature covers the SHA-256 hash of `wapp.toml` and `app.wasm`. `weft-pack verify` checks the signature before installation. +Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -For verified read-only package storage, `weft-pack build-image` produces an EROFS image protected with dm-verity. Mounting requires the setuid `weft-mount-helper` which calls `veritysetup`. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. + +The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. 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. The demo key in `examples/keys` is a test fixture, not a signing authority. + +Not yet covered: `weft-appd` does not check signatures or owners at launch, so the installed directory is trusted as installed; key revocation, rotation and ownership transfer have no workflow; and capabilities are granted at launch without asking the user, independently of the signature. + +For verified read-only package storage, `weft-pack build-image` produces an EROFS image protected with dm-verity. Mounting requires the setuid `weft-mount-helper` which calls `veritysetup`. The image's root hash is not yet authenticated (see `architecture.md`). ## Seccomp From d175bf012e574e4dd47d75457c921fa90317ba0f Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 13:59:54 +0000 Subject: [PATCH 02/61] fix(pack): normalize installed modes and never leave a partial or orphaned owner record Installed files take fixed modes (0644, or 0755 when executable) and directories 0755, so a world-writable source cannot install as such. An owner record created by an installation whose package then fails to be placed is removed, and records are written under a temporary name and linked into place, so they are never partial. Archives unpack into a random private directory that is always removed. Uninstall frees an ID only when the app data is truly absent, signatures are checked with verify_strict, and list marks development installs. --- crates/weft-ipc-types/src/trust.rs | 36 +++- crates/weft-pack/src/main.rs | 288 ++++++++++++++++++++++++----- docs/building.md | 2 +- docs/security.md | 4 +- 4 files changed, 268 insertions(+), 62 deletions(-) diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index dc9dfd6..7c00111 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -10,7 +10,7 @@ use std::fmt; use std::path::{Path, PathBuf}; -use ed25519_dalek::{Signature, Verifier, VerifyingKey}; +use ed25519_dalek::{Signature, VerifyingKey}; use sha2::{Digest, Sha256}; /// The signature file at the root of a signed package. @@ -127,7 +127,7 @@ impl PublisherKey { /// Whether `signature` signs `digest` with this key. pub fn signed(&self, digest: &[u8; 32], signature: &Signature) -> bool { - self.0.verify(digest, signature).is_ok() + self.0.verify_strict(digest, signature).is_ok() } } @@ -261,7 +261,9 @@ pub fn read_owner(record: &Path) -> Result, TrustError> { } } -/// Records the owner of an app ID. An existing record is never replaced. +/// Records the owner of an app ID. An existing record is never replaced, and +/// a record is either absent or complete: it is written and synced under a +/// temporary name, then linked into place. pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { use std::io::Write; let parent = record.parent().expect("owner records live in a directory"); @@ -270,13 +272,27 @@ pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { Owner::Verified(key) => format!("verified {}\n", key.to_hex()), Owner::Development => "development\n".to_owned(), }; - let mut file = std::fs::OpenOptions::new() - .write(true) - .create_new(true) - .open(record) - .map_err(io(record))?; - file.write_all(text.as_bytes()).map_err(io(record))?; - file.sync_all().map_err(io(record)) + let name = record + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("record"); + let temp = parent.join(format!(".{name}.{}.tmp", std::process::id())); + let _ = std::fs::remove_file(&temp); + let written = (|| { + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&temp)?; + file.write_all(text.as_bytes())?; + file.sync_all() + })() + .map_err(io(&temp)); + let linked = written.and_then(|()| std::fs::hard_link(&temp, record).map_err(io(record))); + let _ = std::fs::remove_file(&temp); + linked?; + std::fs::File::open(parent) + .and_then(|dir| dir.sync_all()) + .map_err(io(parent)) } #[cfg(test)] diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index df905d5..38131f6 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -263,31 +263,44 @@ fn install_package(path: &Path, mode: InstallMode) -> anyhow::Result<()> { } fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { - let dir_buf; - let dir: &Path = if path.extension().is_some_and(|e| e == "zst" || e == "tar") - || path.to_string_lossy().ends_with(".app.tar.zst") + std::fs::create_dir_all(store_root) + .with_context(|| format!("create {}", store_root.display()))?; + if !(path.extension().is_some_and(|e| e == "zst" || e == "tar") + || path.to_string_lossy().ends_with(".app.tar.zst")) { - let tmp = std::env::temp_dir().join(format!( - "weft-install-{}", - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .subsec_nanos() - )); - std::fs::create_dir_all(&tmp) - .with_context(|| format!("create temp dir {}", tmp.display()))?; - unbundle_package(path, &tmp)?; - let name = path - .file_name() - .and_then(|n| n.to_str()) - .and_then(|n| n.strip_suffix(".app.tar.zst")) - .context("cannot determine app_id from archive filename")?; - dir_buf = tmp.join(name); - &dir_buf - } else { - path - }; + return install_dir(path, store_root, mode); + } + let name = path + .file_name() + .and_then(|n| n.to_str()) + .and_then(|n| n.strip_suffix(".app.tar.zst")) + .context("cannot determine app_id from archive filename")?; + // A fresh private directory in the store, removed whatever happens, so + // nothing can be planted in it and nothing is left behind. + let unpacked = private_scratch_dir(store_root, ".unpack")?; + let result = unbundle_package(path, &unpacked) + .and_then(|()| install_dir(&unpacked.join(name), store_root, mode)); + let _ = std::fs::remove_dir_all(&unpacked); + result +} +/// Creates a new directory with a random name in `parent`, accessible only to +/// the user. +fn private_scratch_dir(parent: &Path, prefix: &str) -> anyhow::Result { + let dir = parent.join(format!( + "{prefix}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); + builder + .create(&dir) + .with_context(|| format!("create {}", dir.display()))?; + Ok(dir) +} + +fn install_dir(dir: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { check_package(dir)?; let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -312,36 +325,53 @@ fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyh // The package is copied into the store first and every decision is made // on that copy, so the bytes that are checked and trusted are the bytes // that become installed. Only then does one rename make it available. - std::fs::create_dir_all(store_root) - .with_context(|| format!("create {}", store_root.display()))?; let staging = store_root.join(format!( ".staging-{app_id}-{}", hex::encode(rand::random::<[u8; 8]>()) )); - let staged = copy_dir(dir, &staging) + let admitted = copy_dir(dir, &staging) .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| admit(&staging, app_id, mode)) - .and_then(|owner| { - weft_ipc_types::package::rename_no_replace(&staging, &dest) - .with_context(|| format!("move {} -> {}", staging.display(), dest.display()))?; - Ok(owner) - }); - let owner = match staged { - Ok(owner) => owner, + .and_then(|()| admit(&staging, app_id, mode)); + let admission = match admitted { + Ok(admission) => admission, Err(e) => { let _ = std::fs::remove_dir_all(&staging); return Err(e); } }; + let owner = admission.owner; + place(&staging, &dest, admission)?; println!("installed {} -> {} ({owner})", app_id, dest.display()); Ok(()) } +/// The owner a staged package was admitted under, and the owner record this +/// installation created, if any. +struct Admission { + owner: Owner, + created_record: Option, +} + +/// Makes the staged package available at `dest`. If that fails, the staging +/// copy is removed, and so is an owner record this installation created, so +/// a failed first installation leaves no claim on the app ID. +fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<()> { + if let Err(e) = weft_ipc_types::package::rename_no_replace(staging, dest) { + let _ = std::fs::remove_dir_all(staging); + if let Some(record) = &admission.created_record { + let _ = std::fs::remove_file(record); + } + return Err(e).with_context(|| format!("move {} -> {}", staging.display(), dest.display())); + } + Ok(()) +} + /// Checks the staged copy of a package and establishes who owns its app ID: /// the trusted publisher that signed it, or, only when asked, development. /// The first installation of an ID records its owner, and every later one -/// must have the same owner. -fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { +/// must have the same owner. The record is written before the package is +/// placed, so concurrent installations by different owners cannot both win. +fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; check_package(staged)?; let manifest = load_manifest(staged)?; @@ -369,15 +399,21 @@ fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result {} + let created_record = match read_owner(&record)? { + Some(existing) if existing == owner => None, Some(existing) => anyhow::bail!( "{app_id} belongs to {existing}; this package is {owner}. Its app data stays with \ its owner, so another publisher or a development build cannot take the ID over" ), - None => write_owner(&record, owner)?, - } - Ok(owner) + None => { + write_owner(&record, owner)?; + Some(record) + } + }; + Ok(Admission { + owner, + created_record, + }) } fn uninstall_package(app_id: &str) -> anyhow::Result<()> { @@ -424,7 +460,7 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> // while the data remains; with no data left, the ID is free again. if let Some(data_home) = weft_ipc_types::package::data_home() && std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(&data_home, app_id)) - .is_err() + .is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound) { let _ = std::fs::remove_file(weft_ipc_types::trust::owner_record_path(&data_home, app_id)); } @@ -524,7 +560,20 @@ fn list_installed() { continue; } if seen.insert(m.package.id.clone()) { - pkgs.push((m.package.id, m.package.name, m.package.version)); + let development = weft_ipc_types::package::data_home().is_some_and(|home| { + matches!( + weft_ipc_types::trust::read_owner( + &weft_ipc_types::trust::owner_record_path(&home, &m.package.id) + ), + Ok(Some(Owner::Development)) + ) + }); + let version = if development { + format!("{} (development)", m.package.version) + } else { + m.package.version + }; + pkgs.push((m.package.id, m.package.name, version)); } } pkgs.sort_by(|a, b| a.0.cmp(&b.0)); @@ -540,18 +589,25 @@ fn list_installed() { /// Copies a package directory. Packages hold only directories and regular /// files, so a symbolic link or special file stops the copy rather than -/// being followed. +/// being followed. Modes are not copied: the signature does not cover them, +/// so installed directories are 0755 and files 0644, or 0755 when the source +/// is executable, and nothing installed is writable by other users. fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { - std::fs::create_dir(dst).with_context(|| format!("create {}", dst.display()))?; + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o755); + builder + .create(dst) + .with_context(|| format!("create {}", dst.display()))?; for entry in std::fs::read_dir(src)? { let entry = entry?; let src_path = entry.path(); let dst_path = dst.join(entry.file_name()); - let kind = std::fs::symlink_metadata(&src_path)?.file_type(); - if kind.is_dir() { + let metadata = std::fs::symlink_metadata(&src_path)?; + if metadata.is_dir() { copy_dir(&src_path, &dst_path)?; - } else if kind.is_file() { - std::fs::copy(&src_path, &dst_path) + } else if metadata.is_file() { + copy_file(&src_path, &dst_path, &metadata) .with_context(|| format!("copy {}", src_path.display()))?; } else { anyhow::bail!( @@ -563,6 +619,22 @@ fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { Ok(()) } +fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + let executable = metadata.permissions().mode() & 0o111 != 0; + options.mode(if executable { 0o755 } else { 0o644 }); + } + #[cfg(not(unix))] + let _ = metadata; + let mut target = options.open(dst)?; + std::io::copy(&mut std::fs::File::open(src)?, &mut target)?; + Ok(()) +} + fn build_image(dir: &Path, out_path: Option<&Path>) -> anyhow::Result<()> { let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -936,6 +1008,119 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn a_development_id_does_not_become_verified() { + let home = temp_root("dev_owned"); + let store = home.join("store"); + let app_id = "org.weft.test.devowned"; + write_package(&home.join("src"), app_id, APP_DATA); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); + let data = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + std::fs::create_dir_all(&data).unwrap(); + with_trust(&home, || uninstall_package_from(app_id, &store)).unwrap(); + + sign_package(&home.join("src"), &key(&home, "publisher", 7, true)).unwrap(); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + let message = format!("{:#}", refused.unwrap_err()); + assert!(message.contains("belongs to development"), "{message}"); + assert!(!store.join(app_id).exists()); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_failed_placement_leaves_no_claim_on_the_id() { + let home = temp_root("placement"); + let staging = home.join(".staging-x"); + let dest = home.join("org.weft.test.placed"); + std::fs::create_dir_all(&staging).unwrap(); + std::fs::create_dir_all(dest.join("ui")).unwrap(); + let record = home.join("owners/org.weft.test.placed"); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + + let admission = Admission { + owner: Owner::Development, + created_record: Some(record.clone()), + }; + assert!(place(&staging, &dest, admission).is_err()); + assert!(!staging.exists()); + assert!(!record.exists()); + assert!( + dest.join("ui").is_dir(), + "the existing package is untouched" + ); + + // A record that existed before this installation is kept. + std::fs::create_dir_all(&staging).unwrap(); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + let admission = Admission { + owner: Owner::Development, + created_record: None, + }; + assert!(place(&staging, &dest, admission).is_err()); + assert!(record.exists()); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] + #[test] + fn installed_files_are_not_writable_by_others() { + use std::os::unix::fs::PermissionsExt; + let home = temp_root("modes"); + let store = home.join("store"); + let app_id = "org.weft.test.modes"; + write_package(&home.join("src"), app_id, ""); + let set = |rel: &str, mode: u32| { + std::fs::set_permissions( + home.join("src").join(rel), + std::fs::Permissions::from_mode(mode), + ) + .unwrap() + }; + set("ui/index.html", 0o666); + set("app.wasm", 0o777); + set("ui", 0o777); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); + let mode = |rel: &str| { + std::fs::metadata(store.join(app_id).join(rel)) + .unwrap() + .permissions() + .mode() + & 0o777 + }; + assert_eq!(mode("ui/index.html"), 0o644); + assert_eq!(mode("app.wasm"), 0o755); + assert_eq!(mode("ui"), 0o755); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(target_os = "linux")] + #[test] + fn packages_with_non_utf8_names_are_refused() { + use std::os::unix::ffi::OsStrExt; + let home = temp_root("non_utf8"); + let store = home.join("store"); + let app_id = "org.weft.test.nonutf8"; + write_package(&home.join("src"), app_id, ""); + let name = std::ffi::OsStr::from_bytes(b"ui/\xff.html"); + std::fs::write(home.join("src").join(name), b"").unwrap(); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + assert!(refused.is_err()); + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); @@ -1381,6 +1566,11 @@ entry = "ui/index.html" let _ = fs::create_dir_all(&store); install_package_to(&archive, &store, InstallMode::Development).unwrap(); assert!(store.join(&app_id).join("app.wasm").exists()); + let scratch_left = fs::read_dir(&store).unwrap().flatten().any(|e| { + let name = e.file_name().to_string_lossy().into_owned(); + name.starts_with(".unpack") || name.starts_with(".staging-") + }); + assert!(!scratch_left, "archive install left scratch directories"); let _ = fs::remove_dir_all(&src); let _ = fs::remove_dir_all(&bundle_dir); diff --git a/docs/building.md b/docs/building.md index 141b217..5afe1d9 100644 --- a/docs/building.md +++ b/docs/building.md @@ -133,7 +133,7 @@ cp examples/keys/weft-sign.pub ~/.config/weft/trusted-keys/weft-demo.pub weft-pack install examples/org.weft.demo.notes ``` -Unsigned packages under development install with `weft-pack install --dev`, recorded as development content. See `docs/security.md` for the trust store and app ID ownership. +Unsigned packages under development install with `weft-pack install --dev`, recorded as development content and marked `(development)` by `weft-pack list`. See `docs/security.md` for the trust store and app ID ownership. ## NixOS VM (requires Nix with flakes) diff --git a/docs/security.md b/docs/security.md index cd25cc1..deba623 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,9 +22,9 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. -The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. 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. 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/`: the publisher key, or development. 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. 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. The demo key in `examples/keys` is a test fixture, not a signing authority. Not yet covered: `weft-appd` does not check signatures or owners at launch, so the installed directory is trusted as installed; key revocation, rotation and ownership transfer have no workflow; and capabilities are granted at launch without asking the user, independently of the signature. From 39f7ca2a3f016fc6099cafa802fe5cb2ff24b329 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:04:53 +0000 Subject: [PATCH 03/61] fix(pack): close inventory forgery, serialize ownership changes and refuse to follow links Names with control characters have no content digest, so a crafted name can no longer reproduce other inventory lines and drop signed files. Installs and uninstalls of an ID hold a per-ID lock, so a failed concurrent install cannot remove the record of the one that won. App data without a recorded owner goes to a new owner only with --claim-data. Uninstall releases a record left by an interrupted install once no data remains. Files are opened without following links and, on Linux, must be the file that was listed. --- Cargo.lock | 1 + crates/weft-ipc-types/src/trust.rs | 62 ++++++++- crates/weft-pack/Cargo.toml | 1 + crates/weft-pack/src/main.rs | 193 +++++++++++++++++++++++++---- docs/security.md | 6 +- 5 files changed, 232 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc4cf59..65456f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10542,6 +10542,7 @@ dependencies = [ "anyhow", "ed25519-dalek", "hex", + "libc", "rand 0.8.5", "sha2", "tar", diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 7c00111..0d52d8e 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -4,8 +4,9 @@ //! A package is signed over its content digest: the SHA-256 of a canonical //! inventory listing every file except the root `signature.sig`, one //! `\t\n` line per file, sorted by path. Paths use -//! `/` and must be UTF-8. Symbolic links and special files are refused, so the -//! signed bytes are exactly the files a package holds. +//! `/` and must be UTF-8 without control characters, so no name can imitate +//! the separators of other lines. Symbolic links and special files are +//! refused, so the signed bytes are exactly the files a package holds. use std::fmt; use std::path::{Path, PathBuf}; @@ -25,6 +26,8 @@ pub enum TrustError { NotAFile(PathBuf), /// A path in the package is not UTF-8. NonUtf8(PathBuf), + /// A name in the package holds a control character. + ControlCharacter(PathBuf), /// A key, signature or owner record is malformed. Malformed(PathBuf, String), Io(PathBuf, std::io::Error), @@ -36,6 +39,9 @@ impl fmt::Display for TrustError { Self::Link(p) => write!(f, "{} is a symbolic link", p.display()), Self::NotAFile(p) => write!(f, "{} is not a regular file or directory", p.display()), Self::NonUtf8(p) => write!(f, "{} is not a UTF-8 path", p.display()), + Self::ControlCharacter(p) => { + write!(f, "{:?} has a control character in its name", p.display()) + } Self::Malformed(p, why) => write!(f, "{}: {why}", p.display()), Self::Io(p, e) => write!(f, "{}: {e}", p.display()), } @@ -76,6 +82,9 @@ fn collect( .to_str() .ok_or_else(|| TrustError::NonUtf8(path.clone()))? .to_owned(); + if relative.chars().any(char::is_control) { + return Err(TrustError::ControlCharacter(path)); + } let kind = std::fs::symlink_metadata(&path) .map_err(io(&path))? .file_type(); @@ -295,6 +304,22 @@ pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { .map_err(io(parent)) } +/// Takes the lock that serialises every change to the installation and +/// owner record of `app_id`, held until the returned file is dropped. +pub fn lock_owner(data_home: &Path, app_id: &str) -> Result { + let dir = data_home.join("weft/owners"); + std::fs::create_dir_all(&dir).map_err(io(&dir))?; + let path = dir.join(format!(".{app_id}.lock")); + let file = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(false) + .open(&path) + .map_err(io(&path))?; + file.lock().map_err(io(&path))?; + Ok(file) +} + #[cfg(test)] mod tests { use super::*; @@ -356,6 +381,39 @@ mod tests { std::fs::remove_dir_all(&dir).unwrap(); } + #[cfg(unix)] + #[test] + fn names_cannot_imitate_other_inventory_lines() { + // With files ui/policy.txt and ui/z.css signed, a directory named + // "ui/policy.txt\t\nui" holding z.css would reproduce the + // inventory without policy.txt. Such names have no digest. + let dir = dir("forged"); + std::fs::write(dir.join("ui/policy.txt"), b"default-src 'self'").unwrap(); + std::fs::write(dir.join("ui/z.css"), b"p{}").unwrap(); + let signed = content_digest(&dir).unwrap(); + let policy_hash = hex::encode(Sha256::digest(b"default-src 'self'")); + std::fs::remove_file(dir.join("ui/policy.txt")).unwrap(); + let forged = dir.join(format!("ui/policy.txt\t{policy_hash}\nui")); + std::fs::create_dir_all(&forged).unwrap(); + std::fs::rename(dir.join("ui/z.css"), forged.join("z.css")).unwrap(); + let refused = content_digest(&dir); + assert!(matches!(refused, Err(TrustError::ControlCharacter(_)))); + assert_ne!(refused.ok(), Some(signed)); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn owner_locks_exclude_each_other() { + let home = std::env::temp_dir().join(format!("weft_trust_lock_{}", std::process::id())); + let held = lock_owner(&home, "org.weft.test.lock").unwrap(); + let path = home.join("weft/owners/.org.weft.test.lock.lock"); + let other = std::fs::OpenOptions::new().write(true).open(&path).unwrap(); + assert!(other.try_lock().is_err()); + drop(held); + assert!(other.try_lock().is_ok()); + std::fs::remove_dir_all(&home).unwrap(); + } + #[cfg(target_os = "linux")] #[test] fn links_and_special_files_have_no_content_digest() { diff --git a/crates/weft-pack/Cargo.toml b/crates/weft-pack/Cargo.toml index c580df4..ab8db04 100644 --- a/crates/weft-pack/Cargo.toml +++ b/crates/weft-pack/Cargo.toml @@ -15,5 +15,6 @@ ed25519-dalek = { version = "2", features = ["rand_core"] } sha2 = "0.10" rand = "0.8" hex = "0.4" +libc = "0.2" tar = "0.4" zstd = "0.13" diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 38131f6..b3f5ee7 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -20,14 +20,18 @@ fn main() -> anyhow::Result<()> { print_info(&manifest); } Some("install") => { - let usage = "usage: weft-pack install [--dev]"; + let usage = "usage: weft-pack install [--dev] [--claim-data]"; let dir = args.get(2).context(usage)?; - let mode = match args.get(3).map(String::as_str) { - None => InstallMode::Verified, - Some("--dev") => InstallMode::Development, - Some(other) => anyhow::bail!("unexpected argument '{other}'; {usage}"), - }; - install_package(Path::new(dir), mode)?; + let mut mode = InstallMode::Verified; + let mut claim_data = false; + for arg in &args[3..] { + match arg.as_str() { + "--dev" => mode = InstallMode::Development, + "--claim-data" => claim_data = true, + other => anyhow::bail!("unexpected argument '{other}'; {usage}"), + } + } + install_package(Path::new(dir), mode, claim_data)?; } Some("uninstall") => { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; @@ -109,7 +113,10 @@ fn main() -> anyhow::Result<()> { " weft-pack install [--dev] install a package signed by a trusted key" ); eprintln!( - " (--dev: unsigned development content)" + " (--dev: unsigned development content;" + ); + eprintln!( + " --claim-data: adopt app data with no owner)" ); eprintln!(" weft-pack uninstall remove installed package"); eprintln!(" weft-pack list list installed packages"); @@ -257,18 +264,32 @@ enum InstallMode { Development, } -fn install_package(path: &Path, mode: InstallMode) -> anyhow::Result<()> { +fn install_package(path: &Path, mode: InstallMode, claim_data: bool) -> anyhow::Result<()> { let root = resolve_install_root()?; - install_package_to(path, &root, mode) + install_into(path, &root, mode, claim_data) } +#[cfg(test)] fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { + install_into(path, store_root, mode, false) +} + +/// Installs the package at `path` into `store_root`. App data that exists +/// for the ID without a recorded owner, as left by an installation that +/// predates owner records, goes to this package's owner only with +/// `claim_data`. +fn install_into( + path: &Path, + store_root: &Path, + mode: InstallMode, + claim_data: bool, +) -> anyhow::Result<()> { std::fs::create_dir_all(store_root) .with_context(|| format!("create {}", store_root.display()))?; if !(path.extension().is_some_and(|e| e == "zst" || e == "tar") || path.to_string_lossy().ends_with(".app.tar.zst")) { - return install_dir(path, store_root, mode); + return install_dir(path, store_root, mode, claim_data); } let name = path .file_name() @@ -279,7 +300,7 @@ fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyh // nothing can be planted in it and nothing is left behind. let unpacked = private_scratch_dir(store_root, ".unpack")?; let result = unbundle_package(path, &unpacked) - .and_then(|()| install_dir(&unpacked.join(name), store_root, mode)); + .and_then(|()| install_dir(&unpacked.join(name), store_root, mode, claim_data)); let _ = std::fs::remove_dir_all(&unpacked); result } @@ -300,11 +321,21 @@ fn private_scratch_dir(parent: &Path, prefix: &str) -> anyhow::Result { Ok(dir) } -fn install_dir(dir: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { +fn install_dir( + dir: &Path, + store_root: &Path, + mode: InstallMode, + claim_data: bool, +) -> anyhow::Result<()> { check_package(dir)?; let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; let dest = store_root.join(app_id); + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to record who owns the app ID")?; + // Installations and uninstallations of one ID run one at a time, so the + // owner record always describes the package that is placed. + let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; // An earlier weft-appd could leave only app data, or an empty directory, // where the package now goes; move the data to its own location so it // neither blocks nor joins the package. @@ -329,9 +360,14 @@ fn install_dir(dir: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Resu ".staging-{app_id}-{}", hex::encode(rand::random::<[u8; 8]>()) )); - let admitted = copy_dir(dir, &staging) + // Copied from the resolved source path, which every file opened during + // the copy is checked against. + let admitted = dir + .canonicalize() + .with_context(|| format!("resolve {}", dir.display())) + .and_then(|source| copy_dir(&source, &staging)) .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| admit(&staging, app_id, mode)); + .and_then(|()| admit(&staging, app_id, mode, &data_home, claim_data)); let admission = match admitted { Ok(admission) => admission, Err(e) => { @@ -371,7 +407,13 @@ fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<() /// The first installation of an ID records its owner, and every later one /// must have the same owner. The record is written before the package is /// placed, so concurrent installations by different owners cannot both win. -fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { +fn admit( + staged: &Path, + app_id: &str, + mode: InstallMode, + data_home: &Path, + claim_data: bool, +) -> anyhow::Result { use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; check_package(staged)?; let manifest = load_manifest(staged)?; @@ -396,9 +438,7 @@ fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result None, Some(existing) => anyhow::bail!( @@ -406,6 +446,14 @@ fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { + let data = weft_ipc_types::package::app_data_dir(data_home, app_id); + if std::fs::symlink_metadata(&data).is_ok() && !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", + data.display() + ); + } write_owner(&record, owner)?; Some(record) } @@ -425,8 +473,22 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> if !weft_ipc_types::package::is_valid_app_id(app_id) { anyhow::bail!("'{}' is not a valid app ID", app_id); } + let data_home = weft_ipc_types::package::data_home(); + let _lock = match &data_home { + Some(home) => Some(weft_ipc_types::trust::lock_owner(home, app_id)?), + None => None, + }; let target = store_root.join(app_id); if !target.exists() { + // An installation interrupted between recording its owner and placing + // the package leaves a record with nothing installed; with no data + // either, it holds the ID for nothing and is released. + if let Some(home) = &data_home + && release_owner_without_data(home, app_id) + { + println!("released {app_id}, which was not installed"); + return Ok(()); + } anyhow::bail!( "package '{}' is not installed at {}", app_id, @@ -458,16 +520,23 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> } // The owner record keeps the app ID, and the data with it, for its owner // while the data remains; with no data left, the ID is free again. - if let Some(data_home) = weft_ipc_types::package::data_home() - && 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) - { - let _ = std::fs::remove_file(weft_ipc_types::trust::owner_record_path(&data_home, app_id)); + if let Some(home) = &data_home { + release_owner_without_data(home, app_id); } println!("uninstalled {}", app_id); Ok(()) } +/// 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); + no_data + && std::fs::remove_file(weft_ipc_types::trust::owner_record_path(data_home, app_id)).is_ok() +} + /// Whether a package directory is empty or contains nothing but `data`. fn only_holds_app_data(package_dir: &Path) -> anyhow::Result { let entries = std::fs::read_dir(package_dir) @@ -620,6 +689,7 @@ fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { } fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { + let source = open_regular_file(src)?; let mut options = std::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] @@ -631,10 +701,36 @@ fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::R #[cfg(not(unix))] let _ = metadata; let mut target = options.open(dst)?; - std::io::copy(&mut std::fs::File::open(src)?, &mut target)?; + std::io::copy(&mut &source, &mut target)?; Ok(()) } +/// Opens `src` for reading only if it is still the regular file it was when +/// listed: the last component is not followed, and on Linux the opened file +/// must be at `src` itself, so a link swapped in for any directory on the way +/// cannot redirect the copy outside the package. +fn open_regular_file(src: &Path) -> std::io::Result { + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::custom_flags(&mut options, libc::O_NOFOLLOW); + let file = options.open(src)?; + let changed = + || std::io::Error::other(format!("{} changed while it was copied", src.display())); + if !file.metadata()?.is_file() { + return Err(changed()); + } + #[cfg(target_os = "linux")] + { + use std::os::fd::AsRawFd; + let opened = std::fs::read_link(format!("/proc/self/fd/{}", file.as_raw_fd()))?; + if opened != src { + return Err(changed()); + } + } + Ok(file) +} + fn build_image(dir: &Path, out_path: Option<&Path>) -> anyhow::Result<()> { let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -1121,6 +1217,43 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn uninstall_releases_a_record_left_by_an_interrupted_install() { + let home = temp_root("interrupted"); + let store = home.join("store"); + let app_id = "org.weft.test.interrupted"; + let record = weft_ipc_types::trust::owner_record_path(&home.join("share"), app_id); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + let data = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + std::fs::create_dir_all(&data).unwrap(); + + // While data remains the record stays, and uninstall reports nothing + // installed. + assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); + assert!(record.exists()); + std::fs::remove_dir_all(&data).unwrap(); + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(!record.exists()); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(target_os = "linux")] + #[test] + fn copies_never_follow_links_swapped_in() { + let home = temp_root("swapped"); + std::fs::create_dir_all(home.join("outside")).unwrap(); + std::fs::write(home.join("outside/secret"), b"secret").unwrap(); + std::fs::create_dir_all(home.join("pkg")).unwrap(); + let root = home.canonicalize().unwrap(); + // A file replaced by a link, and a directory replaced by a link. + std::os::unix::fs::symlink(root.join("outside/secret"), root.join("pkg/file")).unwrap(); + std::os::unix::fs::symlink(root.join("outside"), root.join("pkg/dir")).unwrap(); + assert!(open_regular_file(&root.join("pkg/file")).is_err()); + assert!(open_regular_file(&root.join("pkg/dir/secret")).is_err()); + assert!(open_regular_file(&root.join("outside/secret")).is_ok()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); @@ -1263,8 +1396,16 @@ mod tests { std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); - with_home(&home, || { + // The moved data has no recorded owner, so it is given to this + // package only when asked. + let unclaimed = with_home(&home, || { install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + let message = format!("{:#}", unclaimed.unwrap_err()); + assert!(message.contains("--claim-data"), "{message}"); + assert!(!store.join(app_id).join("wapp.toml").exists()); + with_home(&home, || { + install_into(&home.join("src"), &store, InstallMode::Development, true) }) .unwrap(); assert!(store.join(app_id).join("wapp.toml").exists()); diff --git a/docs/security.md b/docs/security.md index deba623..6fcab73 100644 --- a/docs/security.md +++ b/docs/security.md @@ -20,11 +20,11 @@ Apps access the filesystem only through WASI preopened directories. Each capabil ## Package Signing and Trust -Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. +Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links, and on Linux each opened file must be the file the copy listed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. -The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. 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. 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. 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/`: the publisher key, or development. Installations and uninstallations of one ID hold a per-ID lock, so the record always describes the package placed. 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. The demo key in `examples/keys` is a test fixture, not a signing authority. Not yet covered: `weft-appd` does not check signatures or owners at launch, so the installed directory is trusted as installed; key revocation, rotation and ownership transfer have no workflow; and capabilities are granted at launch without asking the user, independently of the signature. From c64ee531ee3db6dcaa10a57e5fdd775d8d60a503 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:13:18 +0000 Subject: [PATCH 04/61] fix(pack): take the app ID from the staged copy and check copied file identity The app ID now comes from the staged copy, so a source changed after validation cannot steer paths outside the store or the owners directory, and lock_owner refuses invalid IDs. Copied files must keep the device and inode they were listed with, and are opened without following links or blocking on FIFOs, which no longer depends on /proc. Uninstall keeps an owner record while another known store holds the package, and a failure to inspect app data refuses rather than counting as no data. --- Cargo.lock | 1 + crates/weft-ipc-types/src/trust.rs | 9 +- crates/weft-pack/Cargo.toml | 3 + crates/weft-pack/src/main.rs | 202 +++++++++++++++++++---------- docs/security.md | 4 +- 5 files changed, 150 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 65456f1..d3077df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10544,6 +10544,7 @@ dependencies = [ "hex", "libc", "rand 0.8.5", + "rustix 1.1.4", "sha2", "tar", "weft-ipc-types", diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 0d52d8e..a6e8c45 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -249,7 +249,8 @@ impl fmt::Display for Owner { } } -/// Where the owner of `app_id` is recorded, beside the app's data. +/// Where the owner of `app_id` is recorded, beside the app's data. Callers +/// pass a valid app ID, which cannot leave the owners directory. pub fn owner_record_path(data_home: &Path, app_id: &str) -> PathBuf { data_home.join("weft/owners").join(app_id) } @@ -308,6 +309,12 @@ pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { /// owner record of `app_id`, held until the returned file is dropped. pub fn lock_owner(data_home: &Path, app_id: &str) -> Result { let dir = data_home.join("weft/owners"); + if !crate::package::is_valid_app_id(app_id) { + return Err(TrustError::Malformed( + dir, + format!("'{app_id}' is not a valid app ID"), + )); + } std::fs::create_dir_all(&dir).map_err(io(&dir))?; let path = dir.join(format!(".{app_id}.lock")); let file = std::fs::OpenOptions::new() diff --git a/crates/weft-pack/Cargo.toml b/crates/weft-pack/Cargo.toml index ab8db04..deacf4e 100644 --- a/crates/weft-pack/Cargo.toml +++ b/crates/weft-pack/Cargo.toml @@ -18,3 +18,6 @@ hex = "0.4" libc = "0.2" tar = "0.4" zstd = "0.13" + +[target.'cfg(target_os = "linux")'.dev-dependencies] +rustix = { version = "1", features = ["fs"] } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index b3f5ee7..454073f 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -110,7 +110,7 @@ fn main() -> anyhow::Result<()> { eprintln!(" weft-pack check validate a package directory"); eprintln!(" weft-pack info print package metadata"); eprintln!( - " weft-pack install [--dev] install a package signed by a trusted key" + " weft-pack install [--dev] [--claim-data] install a package signed by a trusted key" ); eprintln!( " (--dev: unsigned development content;" @@ -328,7 +328,34 @@ fn install_dir( claim_data: bool, ) -> anyhow::Result<()> { check_package(dir)?; - let manifest = load_manifest(dir)?; + // The package is copied into the store first and every decision, the app + // ID included, is made on that copy, so the bytes that are checked and + // trusted are the bytes that become installed. Only then does one rename + // make it available. + let staging = store_root.join(format!( + ".staging-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + let result = dir + .canonicalize() + .with_context(|| format!("resolve {}", dir.display())) + .and_then(|source| copy_dir(&source, &staging)) + .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) + .and_then(|()| install_staged(&staging, store_root, mode, claim_data)); + if result.is_err() { + let _ = std::fs::remove_dir_all(&staging); + } + result +} + +fn install_staged( + staging: &Path, + store_root: &Path, + mode: InstallMode, + claim_data: bool, +) -> anyhow::Result<()> { + check_package(staging)?; + let manifest = load_manifest(staging)?; let app_id = &manifest.package.id; let dest = store_root.join(app_id); let data_home = weft_ipc_types::package::data_home() @@ -352,31 +379,9 @@ fn install_dir( dest.display() ); } - - // The package is copied into the store first and every decision is made - // on that copy, so the bytes that are checked and trusted are the bytes - // that become installed. Only then does one rename make it available. - let staging = store_root.join(format!( - ".staging-{app_id}-{}", - hex::encode(rand::random::<[u8; 8]>()) - )); - // Copied from the resolved source path, which every file opened during - // the copy is checked against. - let admitted = dir - .canonicalize() - .with_context(|| format!("resolve {}", dir.display())) - .and_then(|source| copy_dir(&source, &staging)) - .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| admit(&staging, app_id, mode, &data_home, claim_data)); - let admission = match admitted { - Ok(admission) => admission, - Err(e) => { - let _ = std::fs::remove_dir_all(&staging); - return Err(e); - } - }; + let admission = admit(staging, app_id, mode, &data_home, claim_data)?; let owner = admission.owner; - place(&staging, &dest, admission)?; + place(staging, &dest, admission)?; println!("installed {} -> {} ({owner})", app_id, dest.display()); Ok(()) } @@ -402,7 +407,7 @@ fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<() Ok(()) } -/// Checks the staged copy of a package and establishes who owns its app ID: +/// Establishes who owns the app ID of a checked, staged package: /// the trusted publisher that signed it, or, only when asked, development. /// The first installation of an ID records its owner, and every later one /// must have the same owner. The record is written before the package is @@ -415,12 +420,6 @@ fn admit( claim_data: bool, ) -> anyhow::Result { use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; - check_package(staged)?; - let manifest = load_manifest(staged)?; - anyhow::ensure!( - manifest.package.id == app_id, - "the package changed while it was copied" - ); let owner = match mode { InstallMode::Development => Owner::Development, InstallMode::Verified => { @@ -447,7 +446,12 @@ fn admit( ), None => { let data = weft_ipc_types::package::app_data_dir(data_home, app_id); - if std::fs::symlink_metadata(&data).is_ok() && !claim_data { + 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 { 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", @@ -484,6 +488,7 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> // the package leaves a record with nothing installed; with no data // either, it holds the ID for nothing and is released. if let Some(home) = &data_home + && !installed_in_another_store(app_id, store_root) && release_owner_without_data(home, app_id) { println!("released {app_id}, which was not installed"); @@ -520,13 +525,31 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> } // The owner record keeps the app ID, and the data with it, for its owner // while the data remains; with no data left, the ID is free again. - if let Some(home) = &data_home { + if let Some(home) = &data_home + && !installed_in_another_store(app_id, store_root) + { release_owner_without_data(home, app_id); } println!("uninstalled {}", app_id); Ok(()) } +/// Whether `app_id` is installed in a known store other than `store_root`. +/// Owner records belong to the data home, not to a store, so a record stays +/// while any store this host knows holds the package. +fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { + let mut roots = list_installed_roots(); + roots.extend( + ["/usr/share/weft/apps"].iter().map(PathBuf::from).chain( + std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/share/weft/apps")), + ), + ); + roots + .iter() + .filter(|root| root.as_path() != store_root) + .any(|root| std::fs::symlink_metadata(root.join(app_id)).is_ok()) +} + /// 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 { @@ -625,7 +648,9 @@ fn list_installed() { }; // Only a directory named after the ID its manifest declares is an // installed package; staging copies and stray directories are not. - if entry.file_name().to_str() != Some(m.package.id.as_str()) { + if entry.file_name().to_str() != Some(m.package.id.as_str()) + || !weft_ipc_types::package::is_valid_app_id(&m.package.id) + { continue; } if seen.insert(m.package.id.clone()) { @@ -689,7 +714,7 @@ fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { } fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { - let source = open_regular_file(src)?; + let source = open_regular_file(src, metadata)?; let mut options = std::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] @@ -698,35 +723,38 @@ fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::R let executable = metadata.permissions().mode() & 0o111 != 0; options.mode(if executable { 0o755 } else { 0o644 }); } - #[cfg(not(unix))] - let _ = metadata; let mut target = options.open(dst)?; std::io::copy(&mut &source, &mut target)?; Ok(()) } -/// Opens `src` for reading only if it is still the regular file it was when -/// listed: the last component is not followed, and on Linux the opened file -/// must be at `src` itself, so a link swapped in for any directory on the way -/// cannot redirect the copy outside the package. -fn open_regular_file(src: &Path) -> std::io::Result { +/// Opens `src` for reading only if it is still the regular file that was +/// listed as `listed`: the last component is not followed, a FIFO or device +/// swapped in is not waited on, and the opened file must have the listed +/// device and inode, so a link swapped in for any directory on the way, or +/// another file renamed into place, cannot redirect the copy. +fn open_regular_file(src: &Path, listed: &std::fs::Metadata) -> std::io::Result { let mut options = std::fs::OpenOptions::new(); options.read(true); #[cfg(unix)] - std::os::unix::fs::OpenOptionsExt::custom_flags(&mut options, libc::O_NOFOLLOW); + std::os::unix::fs::OpenOptionsExt::custom_flags( + &mut options, + libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_NOCTTY, + ); let file = options.open(src)?; - let changed = - || std::io::Error::other(format!("{} changed while it was copied", src.display())); - if !file.metadata()?.is_file() { - return Err(changed()); - } - #[cfg(target_os = "linux")] - { - use std::os::fd::AsRawFd; - let opened = std::fs::read_link(format!("/proc/self/fd/{}", file.as_raw_fd()))?; - if opened != src { - return Err(changed()); - } + let opened = file.metadata()?; + #[cfg(unix)] + let same = { + use std::os::unix::fs::MetadataExt; + opened.dev() == listed.dev() && opened.ino() == listed.ino() + }; + #[cfg(not(unix))] + let same = listed.is_file(); + if !opened.is_file() || !same { + return Err(std::io::Error::other(format!( + "{} changed while it was copied", + src.display() + ))); } Ok(file) } @@ -1237,20 +1265,62 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn a_record_stays_while_another_store_holds_the_package() { + let home = temp_root("other_store"); + let app_id = "org.weft.test.otherstore"; + write_package(&home.join("src"), app_id, ""); + let user_store = home.join(".local/share/weft/apps"); + let elsewhere = home.join("elsewhere"); + with_home(&home, || { + install_package_to(&home.join("src"), &user_store, InstallMode::Development) + }) + .unwrap(); + assert!(with_home(&home, || uninstall_package_from(app_id, &elsewhere)).is_err()); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + with_home(&home, || uninstall_package_from(app_id, &user_store)).unwrap(); + assert_eq!(owner_of(&home, app_id), None); + let _ = std::fs::remove_dir_all(&home); + } + #[cfg(target_os = "linux")] #[test] fn copies_never_follow_links_swapped_in() { let home = temp_root("swapped"); - std::fs::create_dir_all(home.join("outside")).unwrap(); + for dir in ["outside", "pkg/dir", "pkg/held"] { + std::fs::create_dir_all(home.join(dir)).unwrap(); + } std::fs::write(home.join("outside/secret"), b"secret").unwrap(); - std::fs::create_dir_all(home.join("pkg")).unwrap(); - let root = home.canonicalize().unwrap(); - // A file replaced by a link, and a directory replaced by a link. - std::os::unix::fs::symlink(root.join("outside/secret"), root.join("pkg/file")).unwrap(); - std::os::unix::fs::symlink(root.join("outside"), root.join("pkg/dir")).unwrap(); - assert!(open_regular_file(&root.join("pkg/file")).is_err()); - assert!(open_regular_file(&root.join("pkg/dir/secret")).is_err()); - assert!(open_regular_file(&root.join("outside/secret")).is_ok()); + std::fs::write(home.join("pkg/file"), b"listed").unwrap(); + std::fs::write(home.join("pkg/dir/secret"), b"listed").unwrap(); + let listed = |rel: &str| std::fs::symlink_metadata(home.join(rel)).unwrap(); + let (file, in_dir) = (listed("pkg/file"), listed("pkg/dir/secret")); + assert!(open_regular_file(&home.join("pkg/file"), &file).is_ok()); + + // The listed file moved aside and replaced by a link, then by a hard + // link to another existing file. + std::fs::rename(home.join("pkg/file"), home.join("pkg/held/file")).unwrap(); + std::os::unix::fs::symlink(home.join("outside/secret"), home.join("pkg/file")).unwrap(); + assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); + std::fs::remove_file(home.join("pkg/file")).unwrap(); + std::fs::hard_link(home.join("outside/secret"), home.join("pkg/file")).unwrap(); + assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); + + // A listed directory replaced by a link to one holding the same name. + std::fs::rename(home.join("pkg/dir"), home.join("pkg/held/dir")).unwrap(); + std::os::unix::fs::symlink(home.join("outside"), home.join("pkg/dir")).unwrap(); + assert!(open_regular_file(&home.join("pkg/dir/secret"), &in_dir).is_err()); + + // A FIFO swapped in is refused without waiting for a writer. + rustix::fs::mknodat( + rustix::fs::CWD, + home.join("pkg/fifo"), + rustix::fs::FileType::Fifo, + rustix::fs::Mode::from_raw_mode(0o600), + 0, + ) + .unwrap(); + assert!(open_regular_file(&home.join("pkg/fifo"), &file).is_err()); let _ = std::fs::remove_dir_all(&home); } diff --git a/docs/security.md b/docs/security.md index 6fcab73..c2a9110 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,9 +22,9 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links, and on Linux each opened file must be the file the copy listed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links or waiting on special files, and each opened file must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. -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 hold a per-ID lock, so the record always describes the package placed. 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. 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/`: 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. Not yet covered: `weft-appd` does not check signatures or owners at launch, so the installed directory is trusted as installed; key revocation, rotation and ownership transfer have no workflow; and capabilities are granted at launch without asking the user, independently of the signature. From 181aa0fefa05d107eed6e8df6c9b157de4117d19 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:17:09 +0000 Subject: [PATCH 05/61] fix(pack): copy packages by directory descriptor so swapped links cannot redirect the copy Checking each file's device and inode against a path-based listing still followed a directory replaced by a link after it was listed. The copy now holds each source directory open and opens its entries relative to it with O_NOFOLLOW, checking type, device and inode against the listing. Only real packages in other stores keep an owner record, the store list is deduplicated, archive names must be valid app IDs, and the help text is wrapped. --- Cargo.lock | 1 - crates/weft-ipc-types/src/trust.rs | 2 + crates/weft-pack/Cargo.toml | 3 +- crates/weft-pack/src/main.rs | 249 +++++++++++++++++------------ docs/security.md | 2 +- 5 files changed, 154 insertions(+), 103 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3077df..c42e600 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10542,7 +10542,6 @@ dependencies = [ "anyhow", "ed25519-dalek", "hex", - "libc", "rand 0.8.5", "rustix 1.1.4", "sha2", diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index a6e8c45..2f3ea9f 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -418,6 +418,8 @@ mod tests { assert!(other.try_lock().is_err()); drop(held); assert!(other.try_lock().is_ok()); + assert!(lock_owner(&home, "../outside").is_err()); + assert!(!home.join("weft/outside.lock").exists()); std::fs::remove_dir_all(&home).unwrap(); } diff --git a/crates/weft-pack/Cargo.toml b/crates/weft-pack/Cargo.toml index deacf4e..8890949 100644 --- a/crates/weft-pack/Cargo.toml +++ b/crates/weft-pack/Cargo.toml @@ -15,9 +15,8 @@ ed25519-dalek = { version = "2", features = ["rand_core"] } sha2 = "0.10" rand = "0.8" hex = "0.4" -libc = "0.2" tar = "0.4" zstd = "0.13" -[target.'cfg(target_os = "linux")'.dev-dependencies] +[target.'cfg(unix)'.dependencies] rustix = { version = "1", features = ["fs"] } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 454073f..1823865 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -109,14 +109,15 @@ fn main() -> anyhow::Result<()> { eprintln!("usage:"); eprintln!(" weft-pack check validate a package directory"); eprintln!(" weft-pack info print package metadata"); + eprintln!(" weft-pack install [--dev] [--claim-data]"); eprintln!( - " weft-pack install [--dev] [--claim-data] install a package signed by a trusted key" + " install a package signed by a trusted key" ); eprintln!( - " (--dev: unsigned development content;" + " --dev: as unsigned development content" ); eprintln!( - " --claim-data: adopt app data with no owner)" + " --claim-data: adopt app data with no owner" ); eprintln!(" weft-pack uninstall remove installed package"); eprintln!(" weft-pack list list installed packages"); @@ -295,7 +296,8 @@ fn install_into( .file_name() .and_then(|n| n.to_str()) .and_then(|n| n.strip_suffix(".app.tar.zst")) - .context("cannot determine app_id from archive filename")?; + .filter(|n| weft_ipc_types::package::is_valid_app_id(n)) + .context("the archive must be named .app.tar.zst")?; // A fresh private directory in the store, removed whatever happens, so // nothing can be planted in it and nothing is left behind. let unpacked = private_scratch_dir(store_root, ".unpack")?; @@ -539,15 +541,16 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> /// while any store this host knows holds the package. fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { let mut roots = list_installed_roots(); - roots.extend( - ["/usr/share/weft/apps"].iter().map(PathBuf::from).chain( - std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/share/weft/apps")), - ), - ); + roots.push(PathBuf::from("/usr/share/weft/apps")); + roots.extend(std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/share/weft/apps"))); + roots.sort(); + roots.dedup(); + // Only a package counts, as in `list`: a leftover empty or data-only + // directory does not hold the ID. roots .iter() .filter(|root| root.as_path() != store_root) - .any(|root| std::fs::symlink_metadata(root.join(app_id)).is_ok()) + .any(|root| Manifest::read(&root.join(app_id)).is_ok_and(|m| m.package.id == app_id)) } /// Removes the owner record of `app_id` when the app has no data, and @@ -683,82 +686,94 @@ fn list_installed() { /// Copies a package directory. Packages hold only directories and regular /// files, so a symbolic link or special file stops the copy rather than -/// being followed. Modes are not copied: the signature does not cover them, -/// so installed directories are 0755 and files 0644, or 0755 when the source -/// is executable, and nothing installed is writable by other users. +/// being followed. The walk holds each directory open and opens its entries +/// relative to it without following links, checking that each opened entry +/// has the device and inode it was listed with, so a link or another file +/// swapped in anywhere in the source during the copy cannot redirect it. +/// Modes are not copied: the signature does not cover them, so installed +/// directories are 0755 and files 0644, or 0755 when the source is +/// executable, and nothing installed is writable by other users. +#[cfg(unix)] fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { - let mut builder = std::fs::DirBuilder::new(); - #[cfg(unix)] - std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o755); - builder + use rustix::fs::{CWD, Mode, OFlags}; + let root = rustix::fs::openat( + CWD, + src, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ) + .with_context(|| format!("open {}", src.display()))?; + copy_tree(&root, src, dst) +} + +#[cfg(not(unix))] +fn copy_dir(_src: &Path, _dst: &Path) -> anyhow::Result<()> { + anyhow::bail!("installing packages requires a Unix host") +} + +#[cfg(unix)] +fn copy_tree(dir: &rustix::fd::OwnedFd, shown: &Path, dst: &Path) -> anyhow::Result<()> { + use rustix::fs::{AtFlags, FileType, Mode, OFlags}; + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt}; + + std::fs::DirBuilder::new() + .mode(0o755) .create(dst) .with_context(|| format!("create {}", dst.display()))?; - for entry in std::fs::read_dir(src)? { - let entry = entry?; - let src_path = entry.path(); - let dst_path = dst.join(entry.file_name()); - let metadata = std::fs::symlink_metadata(&src_path)?; - if metadata.is_dir() { - copy_dir(&src_path, &dst_path)?; - } else if metadata.is_file() { - copy_file(&src_path, &dst_path, &metadata) - .with_context(|| format!("copy {}", src_path.display()))?; - } else { - anyhow::bail!( + let mut entries = + rustix::fs::Dir::read_from(dir).with_context(|| format!("read {}", shown.display()))?; + while let Some(entry) = entries.read() { + let entry = entry.with_context(|| format!("read {}", shown.display()))?; + let name = entry.file_name(); + if matches!(name.to_bytes(), b"." | b"..") { + continue; + } + let src_path = shown.join(std::ffi::OsStr::from_bytes(name.to_bytes())); + let dst_path = dst.join(std::ffi::OsStr::from_bytes(name.to_bytes())); + let listed = rustix::fs::statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) + .with_context(|| format!("inspect {}", src_path.display()))?; + let kind = FileType::from_raw_mode(listed.st_mode); + let flags = match kind { + FileType::Directory => OFlags::DIRECTORY, + FileType::RegularFile => OFlags::NONBLOCK | OFlags::NOCTTY, + _ => anyhow::bail!( "{} is not a regular file or directory; packages cannot contain links", src_path.display() - ); + ), + }; + let opened = rustix::fs::openat( + dir, + name, + flags | OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ) + .with_context(|| format!("open {}", src_path.display()))?; + let found = rustix::fs::fstat(&opened)?; + anyhow::ensure!( + FileType::from_raw_mode(found.st_mode) == kind + && found.st_dev == listed.st_dev + && found.st_ino == listed.st_ino, + "{} changed while it was copied", + src_path.display() + ); + if kind == FileType::Directory { + copy_tree(&opened, &src_path, &dst_path)?; + } else { + let executable = found.st_mode & 0o111 != 0; + let mut target = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .mode(if executable { 0o755 } else { 0o644 }) + .open(&dst_path) + .with_context(|| format!("create {}", dst_path.display()))?; + std::io::copy(&mut std::fs::File::from(opened), &mut target) + .with_context(|| format!("copy {}", src_path.display()))?; } } Ok(()) } -fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { - let source = open_regular_file(src, metadata)?; - let mut options = std::fs::OpenOptions::new(); - options.write(true).create_new(true); - #[cfg(unix)] - { - use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; - let executable = metadata.permissions().mode() & 0o111 != 0; - options.mode(if executable { 0o755 } else { 0o644 }); - } - let mut target = options.open(dst)?; - std::io::copy(&mut &source, &mut target)?; - Ok(()) -} - -/// Opens `src` for reading only if it is still the regular file that was -/// listed as `listed`: the last component is not followed, a FIFO or device -/// swapped in is not waited on, and the opened file must have the listed -/// device and inode, so a link swapped in for any directory on the way, or -/// another file renamed into place, cannot redirect the copy. -fn open_regular_file(src: &Path, listed: &std::fs::Metadata) -> std::io::Result { - let mut options = std::fs::OpenOptions::new(); - options.read(true); - #[cfg(unix)] - std::os::unix::fs::OpenOptionsExt::custom_flags( - &mut options, - libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_NOCTTY, - ); - let file = options.open(src)?; - let opened = file.metadata()?; - #[cfg(unix)] - let same = { - use std::os::unix::fs::MetadataExt; - opened.dev() == listed.dev() && opened.ino() == listed.ino() - }; - #[cfg(not(unix))] - let same = listed.is_file(); - if !opened.is_file() || !same { - return Err(std::io::Error::other(format!( - "{} changed while it was copied", - src.display() - ))); - } - Ok(file) -} - fn build_image(dir: &Path, out_path: Option<&Path>) -> anyhow::Result<()> { let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -1283,44 +1298,80 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } - #[cfg(target_os = "linux")] + #[test] + fn uninstalling_one_of_two_copies_keeps_the_record() { + let home = temp_root("two_copies"); + let app_id = "org.weft.test.twocopies"; + write_package(&home.join("src"), app_id, ""); + let user_store = home.join(".local/share/weft/apps"); + let other = home.join("other"); + for store in [&user_store, &other] { + with_home(&home, || { + install_package_to(&home.join("src"), store, InstallMode::Development) + }) + .unwrap(); + } + with_home(&home, || uninstall_package_from(app_id, &other)).unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + + // A leftover directory that is not a package does not hold the ID. + std::fs::remove_dir_all(user_store.join(app_id)).unwrap(); + std::fs::create_dir_all(user_store.join(app_id)).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &other, InstallMode::Development) + }) + .unwrap(); + with_home(&home, || uninstall_package_from(app_id, &other)).unwrap(); + assert_eq!(owner_of(&home, app_id), None); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] #[test] fn copies_never_follow_links_swapped_in() { + use rustix::fs::{CWD, Mode, OFlags}; let home = temp_root("swapped"); - for dir in ["outside", "pkg/dir", "pkg/held"] { + for dir in ["outside", "pkg/dir", "held"] { std::fs::create_dir_all(home.join(dir)).unwrap(); } - std::fs::write(home.join("outside/secret"), b"secret").unwrap(); - std::fs::write(home.join("pkg/file"), b"listed").unwrap(); + std::fs::write(home.join("outside/secret"), b"outside").unwrap(); std::fs::write(home.join("pkg/dir/secret"), b"listed").unwrap(); - let listed = |rel: &str| std::fs::symlink_metadata(home.join(rel)).unwrap(); - let (file, in_dir) = (listed("pkg/file"), listed("pkg/dir/secret")); - assert!(open_regular_file(&home.join("pkg/file"), &file).is_ok()); + let open_dir = |path: &Path| { + rustix::fs::openat( + CWD, + path, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW, + Mode::empty(), + ) + .unwrap() + }; - // The listed file moved aside and replaced by a link, then by a hard - // link to another existing file. - std::fs::rename(home.join("pkg/file"), home.join("pkg/held/file")).unwrap(); - std::os::unix::fs::symlink(home.join("outside/secret"), home.join("pkg/file")).unwrap(); - assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); - std::fs::remove_file(home.join("pkg/file")).unwrap(); - std::fs::hard_link(home.join("outside/secret"), home.join("pkg/file")).unwrap(); - assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); - - // A listed directory replaced by a link to one holding the same name. - std::fs::rename(home.join("pkg/dir"), home.join("pkg/held/dir")).unwrap(); + // A directory replaced by a link after it was opened, before its + // entries are read: the copy reads the directory that was opened. + let dir = open_dir(&home.join("pkg/dir")); + std::fs::rename(home.join("pkg/dir"), home.join("held/dir")).unwrap(); std::os::unix::fs::symlink(home.join("outside"), home.join("pkg/dir")).unwrap(); - assert!(open_regular_file(&home.join("pkg/dir/secret"), &in_dir).is_err()); + copy_tree(&dir, &home.join("pkg/dir"), &home.join("copied")).unwrap(); + assert_eq!( + std::fs::read(home.join("copied/secret")).unwrap(), + b"listed" + ); - // A FIFO swapped in is refused without waiting for a writer. + // A link where an entry is listed is refused, and so is a FIFO, + // without waiting for a writer. + let pkg = open_dir(&home.join("pkg")); + assert!(copy_tree(&pkg, &home.join("pkg"), &home.join("copied_link")).is_err()); + std::fs::remove_file(home.join("pkg/dir")).unwrap(); rustix::fs::mknodat( - rustix::fs::CWD, + CWD, home.join("pkg/fifo"), rustix::fs::FileType::Fifo, - rustix::fs::Mode::from_raw_mode(0o600), + Mode::from_raw_mode(0o600), 0, ) .unwrap(); - assert!(open_regular_file(&home.join("pkg/fifo"), &file).is_err()); + let pkg = open_dir(&home.join("pkg")); + assert!(copy_tree(&pkg, &home.join("pkg"), &home.join("copied_fifo")).is_err()); let _ = std::fs::remove_dir_all(&home); } diff --git a/docs/security.md b/docs/security.md index c2a9110..ce71b88 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links or waiting on special files, and each opened file must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, without following links or waiting on special files, and each opened entry must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From 8221e6fc3dd829e7a2491106d2d49a8bc9410323 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:20:35 +0000 Subject: [PATCH 06/61] fix(pack): set installed modes regardless of umask and hold one descriptor per level Modes requested at creation were masked by the umask, so a root install under umask 077 produced system packages other users could not read. They are now set explicitly after creation. The copy reads each directory's names and closes the listing before recursing, so deep trees hold one descriptor per level. The owner-lock test now uses an ID that would otherwise open, and the docs describe the copy check and its device-node residual precisely. --- crates/weft-ipc-types/src/trust.rs | 5 +++-- crates/weft-pack/src/main.rs | 34 ++++++++++++++++++++++++------ docs/security.md | 2 +- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 2f3ea9f..79143b5 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -418,8 +418,9 @@ mod tests { assert!(other.try_lock().is_err()); drop(held); assert!(other.try_lock().is_ok()); - assert!(lock_owner(&home, "../outside").is_err()); - assert!(!home.join("weft/outside.lock").exists()); + // A name that would open fine but is not an app ID is refused. + assert!(lock_owner(&home, "Org.weft.test").is_err()); + assert!(!home.join("weft/owners/.Org.weft.test.lock").exists()); std::fs::remove_dir_all(&home).unwrap(); } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 1823865..cbdc516 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -715,20 +715,32 @@ fn copy_dir(_src: &Path, _dst: &Path) -> anyhow::Result<()> { fn copy_tree(dir: &rustix::fd::OwnedFd, shown: &Path, dst: &Path) -> anyhow::Result<()> { use rustix::fs::{AtFlags, FileType, Mode, OFlags}; use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt}; + use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt}; + // Modes are set explicitly rather than requested at creation, so the + // installer's umask cannot make an installed package unreadable. + let set_mode = |path: &Path, mode: u32| { + std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)) + .with_context(|| format!("set the mode of {}", path.display())) + }; std::fs::DirBuilder::new() - .mode(0o755) + .mode(0o700) .create(dst) .with_context(|| format!("create {}", dst.display()))?; + set_mode(dst, 0o755)?; + // The names are read first and the listing closed, so the walk holds one + // descriptor per directory level. + let mut names = Vec::new(); let mut entries = rustix::fs::Dir::read_from(dir).with_context(|| format!("read {}", shown.display()))?; while let Some(entry) = entries.read() { let entry = entry.with_context(|| format!("read {}", shown.display()))?; - let name = entry.file_name(); - if matches!(name.to_bytes(), b"." | b"..") { - continue; + if !matches!(entry.file_name().to_bytes(), b"." | b"..") { + names.push(entry.file_name().to_owned()); } + } + drop(entries); + for name in &names { let src_path = shown.join(std::ffi::OsStr::from_bytes(name.to_bytes())); let dst_path = dst.join(std::ffi::OsStr::from_bytes(name.to_bytes())); let listed = rustix::fs::statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) @@ -764,11 +776,18 @@ fn copy_tree(dir: &rustix::fd::OwnedFd, shown: &Path, dst: &Path) -> anyhow::Res let mut target = std::fs::OpenOptions::new() .write(true) .create_new(true) - .mode(if executable { 0o755 } else { 0o644 }) + .mode(0o600) .open(&dst_path) .with_context(|| format!("create {}", dst_path.display()))?; std::io::copy(&mut std::fs::File::from(opened), &mut target) .with_context(|| format!("copy {}", src_path.display()))?; + target + .set_permissions(std::fs::Permissions::from_mode(if executable { + 0o755 + } else { + 0o644 + })) + .with_context(|| format!("set the mode of {}", dst_path.display()))?; } } Ok(()) @@ -1334,7 +1353,7 @@ mod tests { for dir in ["outside", "pkg/dir", "held"] { std::fs::create_dir_all(home.join(dir)).unwrap(); } - std::fs::write(home.join("outside/secret"), b"outside").unwrap(); + std::fs::write(home.join("outside/other"), b"outside").unwrap(); std::fs::write(home.join("pkg/dir/secret"), b"listed").unwrap(); let open_dir = |path: &Path| { rustix::fs::openat( @@ -1356,6 +1375,7 @@ mod tests { std::fs::read(home.join("copied/secret")).unwrap(), b"listed" ); + assert!(!home.join("copied/other").exists()); // A link where an entry is listed is refused, and so is a FIFO, // without waiting for a writer. diff --git a/docs/security.md b/docs/security.md index ce71b88..b9f7a2a 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, without following links or waiting on special files, and each opened entry must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker does not own. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From d67ea24004fe174c2c5308c6c993c3f68ddec9c1 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:22:37 +0000 Subject: [PATCH 07/61] fix(pack): create a missing store at 0755 regardless of umask A first root install under a restrictive umask created the store and its parents unreadable to other users even though the package inside was 0755/0644. Missing store directories are now created and set to 0755. The copy comment and the hard-link note in the security docs describe exactly what is checked and what the kernel enforces. --- crates/weft-pack/src/main.rs | 33 +++++++++++++++++++++++++++++---- docs/security.md | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index cbdc516..97719ce 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -285,8 +285,7 @@ fn install_into( mode: InstallMode, claim_data: bool, ) -> anyhow::Result<()> { - std::fs::create_dir_all(store_root) - .with_context(|| format!("create {}", store_root.display()))?; + create_store(store_root)?; if !(path.extension().is_some_and(|e| e == "zst" || e == "tar") || path.to_string_lossy().ends_with(".app.tar.zst")) { @@ -307,6 +306,31 @@ fn install_into( result } +/// Creates the store and any missing parents at 0755 whatever the umask, so +/// a store created by a root install is readable by every user. +fn create_store(store_root: &Path) -> anyhow::Result<()> { + let mut missing: Vec<&Path> = store_root + .ancestors() + .take_while(|dir| std::fs::symlink_metadata(dir).is_err()) + .collect(); + missing.reverse(); + for dir in missing { + match std::fs::create_dir(dir) { + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, + other => other.with_context(|| format!("create {}", dir.display()))?, + } + #[cfg(unix)] + std::fs::set_permissions(dir, std::os::unix::fs::PermissionsExt::from_mode(0o755)) + .with_context(|| format!("set the mode of {}", dir.display()))?; + } + anyhow::ensure!( + store_root.is_dir(), + "{} is not a directory", + store_root.display() + ); + Ok(()) +} + /// Creates a new directory with a random name in `parent`, accessible only to /// the user. fn private_scratch_dir(parent: &Path, prefix: &str) -> anyhow::Result { @@ -688,8 +712,9 @@ fn list_installed() { /// files, so a symbolic link or special file stops the copy rather than /// being followed. The walk holds each directory open and opens its entries /// relative to it without following links, checking that each opened entry -/// has the device and inode it was listed with, so a link or another file -/// swapped in anywhere in the source during the copy cannot redirect it. +/// has the type, device and inode it had when examined just before the +/// open, so a link or another file swapped in anywhere in the source during +/// the copy cannot redirect it. /// Modes are not copied: the signature does not cover them, so installed /// directories are 0755 and files 0644, or 0755 when the source is /// executable, and nothing installed is writable by other users. diff --git a/docs/security.md b/docs/security.md index b9f7a2a..8b99453 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker does not own. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker cannot both read and write. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users; a store that does not exist yet is created at 0755. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From 025388f84e7ca278092beaa167338e477dbc2462 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:25:09 +0000 Subject: [PATCH 08/61] fix(pack): create relative stores and set new store modes through a no-follow descriptor A relative store none of whose components existed failed, because the empty ancestor was treated as a directory to create. New store directories are now created private and set to 0755 through a descriptor opened without following links, so a swapped path cannot redirect the mode change. The security docs state the hard-link protection as the kernel enforces it. --- crates/weft-pack/src/main.rs | 50 +++++++++++++++++++++++++++++++++--- docs/security.md | 2 +- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 97719ce..73a5497 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -307,21 +307,37 @@ fn install_into( } /// Creates the store and any missing parents at 0755 whatever the umask, so -/// a store created by a root install is readable by every user. +/// a store created by a root install is readable by every user. Each new +/// directory is created private and its mode set through a descriptor that +/// does not follow links, so only the directory just created is changed. fn create_store(store_root: &Path) -> anyhow::Result<()> { let mut missing: Vec<&Path> = store_root .ancestors() + .filter(|dir| !dir.as_os_str().is_empty()) .take_while(|dir| std::fs::symlink_metadata(dir).is_err()) .collect(); missing.reverse(); for dir in missing { - match std::fs::create_dir(dir) { + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); + match builder.create(dir) { Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, other => other.with_context(|| format!("create {}", dir.display()))?, } #[cfg(unix)] - std::fs::set_permissions(dir, std::os::unix::fs::PermissionsExt::from_mode(0o755)) - .with_context(|| format!("set the mode of {}", dir.display()))?; + { + use rustix::fs::{CWD, Mode, OFlags}; + let created = rustix::fs::openat( + CWD, + dir, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ) + .with_context(|| format!("open {}", dir.display()))?; + rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755)) + .with_context(|| format!("set the mode of {}", dir.display()))?; + } } anyhow::ensure!( store_root.is_dir(), @@ -1342,6 +1358,32 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[cfg(unix)] + #[test] + fn a_missing_store_is_created_readable_by_everyone() { + use std::os::unix::fs::PermissionsExt; + let home = temp_root("new_store"); + std::fs::create_dir_all(&home).unwrap(); + let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777; + // An absolute store with missing parents. + let absolute = home.join("a/b/store"); + create_store(&absolute).unwrap(); + for dir in [home.join("a"), home.join("a/b"), absolute.clone()] { + assert_eq!(mode(&dir), 0o755, "{}", dir.display()); + } + // A relative store none of whose components exist yet. + let relative = PathBuf::from(format!("weft-pack-store-{}/sub", std::process::id())); + let _ = std::fs::remove_dir_all(relative.parent().unwrap()); + create_store(&relative).unwrap(); + assert_eq!(mode(&relative), 0o755); + std::fs::remove_dir_all(relative.parent().unwrap()).unwrap(); + // An existing store is left as it is. + std::fs::set_permissions(&absolute, std::fs::Permissions::from_mode(0o750)).unwrap(); + create_store(&absolute).unwrap(); + assert_eq!(mode(&absolute), 0o750); + 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 8b99453..5ac7ff2 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker cannot both read and write. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users; a store that does not exist yet is created at 0755. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices unless the attacker owns them or holds CAP_FOWNER. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users; a store, and any of its parents, that does not exist yet is created at 0755. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From fb13a99e1731266e92c9ae70445f53cb1271dfcc Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:26:43 +0000 Subject: [PATCH 09/61] fix(pack): remove a new store directory whose mode cannot be set A directory left private after a failed mode change would stay unreadable to other users on every later install, since existing directories are not touched. It is now removed again. The test's relative store is cleaned up even when an assertion fails. --- crates/weft-pack/src/main.rs | 37 +++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 73a5497..b76cbd1 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -308,8 +308,9 @@ fn install_into( /// Creates the store and any missing parents at 0755 whatever the umask, so /// a store created by a root install is readable by every user. Each new -/// directory is created private and its mode set through a descriptor that -/// does not follow links, so only the directory just created is changed. +/// directory is created private and its mode set through a descriptor opened +/// without following a link in its last component; a directory whose mode +/// cannot be set is removed again rather than left private. fn create_store(store_root: &Path) -> anyhow::Result<()> { let mut missing: Vec<&Path> = store_root .ancestors() @@ -328,15 +329,18 @@ fn create_store(store_root: &Path) -> anyhow::Result<()> { #[cfg(unix)] { use rustix::fs::{CWD, Mode, OFlags}; - let created = rustix::fs::openat( + let opened = rustix::fs::openat( CWD, dir, OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, Mode::empty(), - ) - .with_context(|| format!("open {}", dir.display()))?; - rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755)) - .with_context(|| format!("set the mode of {}", dir.display()))?; + ); + if let Err(e) = + opened.and_then(|created| rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755))) + { + let _ = std::fs::remove_dir(dir); + return Err(e).with_context(|| format!("set the mode of {}", dir.display())); + } } } anyhow::ensure!( @@ -1371,12 +1375,23 @@ mod tests { for dir in [home.join("a"), home.join("a/b"), absolute.clone()] { assert_eq!(mode(&dir), 0o755, "{}", dir.display()); } - // A relative store none of whose components exist yet. - let relative = PathBuf::from(format!("weft-pack-store-{}/sub", std::process::id())); - let _ = std::fs::remove_dir_all(relative.parent().unwrap()); + // A relative store none of whose components exist yet, removed even + // if an assertion fails. + struct Removed(PathBuf); + impl Drop for Removed { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let top = Removed(PathBuf::from(format!( + "weft-pack-store-{}", + std::process::id() + ))); + let _ = std::fs::remove_dir_all(&top.0); + let relative = top.0.join("sub"); create_store(&relative).unwrap(); assert_eq!(mode(&relative), 0o755); - std::fs::remove_dir_all(relative.parent().unwrap()).unwrap(); + drop(top); // An existing store is left as it is. std::fs::set_permissions(&absolute, std::fs::Permissions::from_mode(0o750)).unwrap(); create_store(&absolute).unwrap(); From 11cea5e7e3b0c862c0dc5579d8cef41960608501 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:29:14 +0000 Subject: [PATCH 10/61] fix(appd): verify package owner and signature before a session starts weft-appd trusted an installed directory as installed, so content changed after installation, or signed by a key since removed from the trust store, still ran with the owner's app data. Before anything starts, a package recorded as a publisher's must carry that publisher's valid signature over its current content, with the key still trusted; a package with no record must be signed by a trusted key; development content, recorded by weft-pack install --dev, runs unverified. Failures are refused with code 403. The desktop checks trust the demo key, and the confinement check, whose package deliberately holds a link, records it as development content. --- crates/weft-appd/src/launch.rs | 184 ++++++++++++++++++++++++++++++- crates/weft-appd/src/main.rs | 16 +++ docs/security.md | 4 +- tests/frame/check_app_data.py | 1 + tests/frame/check_confinement.py | 8 ++ tests/frame/check_counter.py | 1 + tests/frame/check_notes.py | 1 + 7 files changed, 213 insertions(+), 2 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 85b5bbc..2e777f5 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -3,11 +3,20 @@ //! The runtime and the app shell receive the exact files resolved here and //! never look for the package themselves, so the component, the UI and the //! capabilities a session is granted all come from the same package root. +//! +//! Before anything starts, the package must still be what its owner +//! installed: a package recorded as a publisher's must carry that +//! publisher's valid signature over its current content, and a package with +//! no record must be signed by a trusted key. Development content, recorded +//! as such by `weft-pack install --dev`, runs unverified. use std::path::{Path, PathBuf}; use weft_ipc_types::manifest::{MANIFEST_FILE, Manifest, ManifestError, entry_path}; use weft_ipc_types::package::{ImageFiles, is_valid_app_id}; +use weft_ipc_types::trust::{ + Owner, TrustError, TrustStore, content_digest, owner_record_path, read_owner, read_signature, +}; use crate::grants::Refusal; use crate::mount::Mount; @@ -99,6 +108,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result) -> Result Result<(), Refusal> { + let refused = |e: TrustError| match e { + TrustError::Io(..) => Refusal::new(500, e.to_string()), + _ => Refusal::new(403, e.to_string()), + }; + let data_home = weft_ipc_types::package::data_home().ok_or_else(|| { + Refusal::new( + 500, + "cannot locate the data home holding package owner records", + ) + })?; + let owner = read_owner(&owner_record_path(&data_home, app_id)).map_err(refused)?; + let trusted = TrustStore::load(&TrustStore::directories()).map_err(refused)?; + match owner { + Some(Owner::Development) => Ok(()), + Some(Owner::Verified(key)) => { + // The key must still be trusted, and its signature must cover + // the package's current content. + let signature = read_signature(root).map_err(refused)?; + let digest = content_digest(root).map_err(refused)?; + let valid = trusted.keys().contains(&key) + && signature.is_some_and(|sig| key.signed(&digest, &sig)); + if valid { + Ok(()) + } else { + Err(Refusal::new( + 403, + format!( + "{} is not signed by its owner, publisher {}, or that key is no longer \ + trusted; the package was changed or its key was withdrawn", + root.display(), + key.to_hex() + ), + )) + } + } + None => match trusted.signer(root).map_err(refused)? { + Some(_) => Ok(()), + None => Err(Refusal::new( + 403, + format!( + "{} has no recorded owner and is not signed by a trusted key; install it \ + with weft-pack", + root.display() + ), + )), + }, + } +} + impl LaunchPackage { /// A package for supervisor tests whose children ignore their files. #[cfg(test)] @@ -160,8 +221,17 @@ mod tests { ), ) .unwrap(); + // The fixture is unsigned development content, recorded as such. + weft_ipc_types::trust::write_owner( + &owner_record_path(&store.join("share"), ID), + Owner::Development, + ) + .unwrap(); // SAFETY: callers hold env_lock, which serialises environment changes. - unsafe { std::env::set_var("WEFT_APP_STORE", &store) }; + unsafe { + std::env::set_var("WEFT_APP_STORE", &store); + std::env::set_var("XDG_DATA_HOME", store.join("share")); + } store } @@ -170,6 +240,8 @@ mod tests { unsafe { std::env::remove_var("WEFT_APP_STORE"); std::env::remove_var("WEFT_MOUNT_HELPER"); + std::env::remove_var("XDG_DATA_HOME"); + std::env::remove_var("WEFT_TRUSTED_KEYS"); } let _ = std::fs::remove_dir_all(store); } @@ -249,6 +321,116 @@ mod tests { } } + const DEMO: &str = "org.weft.demo.counter"; + + /// A store holding a copy of the signed Counter demo, with the demo key + /// trusted when `trusted`. Callers hold env_lock. + fn demo_store(name: &str, trusted: bool) -> PathBuf { + let store = store(name, ID, "app.wasm"); + let examples = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../examples"); + copy_tree(&examples.join(DEMO), &store.join(DEMO)); + let keys = store.join("keys"); + std::fs::create_dir_all(&keys).unwrap(); + if trusted { + std::fs::copy(examples.join("keys/weft-sign.pub"), keys.join("demo.pub")).unwrap(); + } + // SAFETY: as in `store`. + unsafe { std::env::set_var("WEFT_TRUSTED_KEYS", &keys) }; + store + } + + fn copy_tree(src: &Path, dst: &Path) { + std::fs::create_dir_all(dst).unwrap(); + for entry in std::fs::read_dir(src).unwrap().flatten() { + let to = dst.join(entry.file_name()); + if entry.file_type().unwrap().is_dir() { + copy_tree(&entry.path(), &to); + } else { + std::fs::copy(entry.path(), to).unwrap(); + } + } + } + + fn record(store: &Path, app_id: &str, owner: Owner) { + weft_ipc_types::trust::write_owner(&owner_record_path(&store.join("share"), app_id), owner) + .unwrap(); + } + + fn demo_key() -> weft_ipc_types::trust::PublisherKey { + weft_ipc_types::trust::PublisherKey::from_file( + &Path::new(env!("CARGO_MANIFEST_DIR")).join("../../examples/keys/weft-sign.pub"), + ) + .unwrap() + } + + #[test] + fn a_package_signed_by_its_owner_launches() { + let _env = crate::tests::env_lock().blocking_lock(); + // Recorded as the publisher's, and with no record but a trusted + // signature. + let store = demo_store("signed", true); + let unrecorded = resolve(DEMO).map(|p| p.root); + record(&store, DEMO, Owner::Verified(demo_key())); + let recorded = resolve(DEMO).map(|p| p.root); + finish(&store); + assert_eq!(unrecorded.ok(), Some(store.join(DEMO))); + assert_eq!(recorded.ok(), Some(store.join(DEMO))); + } + + #[test] + fn a_package_changed_after_installation_is_refused() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("changed", true); + record(&store, DEMO, Owner::Verified(demo_key())); + let page = store.join(DEMO).join("ui/index.html"); + let mut text = std::fs::read_to_string(&page).unwrap(); + text.push_str(""); + std::fs::write(&page, text).unwrap(); + let changed = refusal(resolve(DEMO)); + // Without a record, the changed package matches no trusted key. + std::fs::remove_file(owner_record_path(&store.join("share"), DEMO)).unwrap(); + let unrecorded = refusal(resolve(DEMO)); + finish(&store); + assert_eq!(changed.code, 403, "{}", changed.message); + assert!( + changed.message.contains("not signed by its owner"), + "{}", + changed.message + ); + assert_eq!(unrecorded.code, 403, "{}", unrecorded.message); + assert!( + unrecorded.message.contains("no recorded owner"), + "{}", + unrecorded.message + ); + } + + #[test] + fn a_withdrawn_key_no_longer_launches_its_packages() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("withdrawn", false); + record(&store, DEMO, Owner::Verified(demo_key())); + let refused = refusal(resolve(DEMO)); + finish(&store); + assert_eq!(refused.code, 403, "{}", refused.message); + assert!(refused.message.contains("no longer"), "{}", refused.message); + } + + #[test] + fn unsigned_content_needs_a_development_record() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = store("unrecorded", ID, "app.wasm"); + std::fs::remove_file(owner_record_path(&store.join("share"), ID)).unwrap(); + let refused = refusal(resolve(ID)); + // Another owner's record does not admit it either. + record(&store, ID, Owner::Verified(demo_key())); + let other_owner = refusal(resolve(ID)); + finish(&store); + for refused in [refused, other_owner] { + assert_eq!(refused.code, 403, "{}", refused.message); + } + } + #[test] fn a_package_declaring_another_id_is_refused() { 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 4512211..d0c549d 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -712,6 +712,22 @@ mod tests { ), ) .unwrap(); + record_development(id); + } + + /// Records `id` as development content in a data home private to this + /// test process, which XDG_DATA_HOME then points at. Callers hold + /// env_lock. + fn record_development(id: &str) { + let data_home = + std::env::temp_dir().join(format!("weft-appd-tests-data-{}", std::process::id())); + let record = weft_ipc_types::trust::owner_record_path(&data_home, id); + if !record.exists() { + weft_ipc_types::trust::write_owner(&record, weft_ipc_types::trust::Owner::Development) + .unwrap(); + } + // SAFETY: callers hold env_lock, which serialises environment changes. + unsafe { std::env::set_var("XDG_DATA_HOME", &data_home) }; } fn make_registry() -> Registry { diff --git a/docs/security.md b/docs/security.md index 5ac7ff2..12016fa 100644 --- a/docs/security.md +++ b/docs/security.md @@ -26,7 +26,9 @@ 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. -Not yet covered: `weft-appd` does not check signatures or owners at launch, so the installed directory is trusted as installed; key revocation, rotation and ownership transfer have no workflow; and capabilities are granted at launch without asking the user, independently of the signature. +`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; development content runs unverified. A changed package, or one whose key was removed from the trust store, is refused with code 403. The check reads the package before the session starts, so a directory install changed by its owning user 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. For verified read-only package storage, `weft-pack build-image` produces an EROFS image protected with dm-verity. Mounting requires the setuid `weft-mount-helper` which calls `veritysetup`. The image's root hash is not yet authenticated (see `architecture.md`). diff --git a/tests/frame/check_app_data.py b/tests/frame/check_app_data.py index 2f43cb8..50d10e2 100644 --- a/tests/frame/check_app_data.py +++ b/tests/frame/check_app_data.py @@ -47,6 +47,7 @@ def run(args, desktop, home, xdotool): "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), + "WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(free_port()), "HOME": str(home), diff --git a/tests/frame/check_confinement.py b/tests/frame/check_confinement.py index c484905..d027921 100644 --- a/tests/frame/check_confinement.py +++ b/tests/frame/check_confinement.py @@ -142,6 +142,12 @@ def make_package(store, http_port, ws_port, other_dir): (package / "outside.png").write_bytes(png()) other.write_bytes(png()) (package / "ui/linked").symlink_to(other_dir) + # The package holds a link, which weft-pack refuses to install, so it is + # planted directly and recorded as development content, which runs + # unverified: the renderer's own confinement is what this check probes. + record = store / "home/share/weft/owners" / APP_ID + record.parent.mkdir(parents=True) + record.write_text("development\n") def run(args, desktop, store, other_dir): @@ -157,6 +163,8 @@ def run(args, desktop, store, other_dir): "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_STORE": str(store), + "HOME": str(store / "home"), + "XDG_DATA_HOME": str(store / "home/share"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(free_port()), }) diff --git a/tests/frame/check_counter.py b/tests/frame/check_counter.py index aa8ecdb..0da4e88 100644 --- a/tests/frame/check_counter.py +++ b/tests/frame/check_counter.py @@ -162,6 +162,7 @@ def run(args, desktop, xdotool): "WEFT_RUNTIME_BIN": str(target / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(target / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), + "WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(port), }) diff --git a/tests/frame/check_notes.py b/tests/frame/check_notes.py index 2d562ee..6ee3cba 100644 --- a/tests/frame/check_notes.py +++ b/tests/frame/check_notes.py @@ -75,6 +75,7 @@ def run(args, desktop, home, xdotool): "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), + "WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(free_port()), "HOME": str(home), From 39aacac8226c56b56f0d98415ef6ab932518897a Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:36:06 +0000 Subject: [PATCH 11/61] fix(appd): bind the launched manifest to the signature and record first owners The manifest is now read once, and its bytes must match the verified inventory, so a manifest swapped in around the check cannot grant capabilities or entries the owner did not sign. A trusted package with no owner record is recorded as its signer's on its first launch, and app data without a recorded owner is refused rather than handed to the first publisher, matching weft-pack's --claim-data rule. A damaged trust store or owner record is a host error (500), and the trust store is not consulted for development content. Supervisor tests use a private data home and an empty trust store; the counter check runs with a private home, and the notes check records the owner of the data it seeds. --- crates/weft-appd/src/launch.rs | 192 +++++++++++++++++++------- crates/weft-appd/src/main.rs | 27 +++- crates/weft-ipc-types/src/manifest.rs | 16 ++- crates/weft-ipc-types/src/trust.rs | 65 +++++++-- docs/security.md | 2 +- tests/frame/check_confinement.py | 2 +- tests/frame/check_counter.py | 6 + tests/frame/check_notes.py | 6 + 8 files changed, 244 insertions(+), 72 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 2e777f5..8f8f2cc 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -7,15 +7,17 @@ //! Before anything starts, the package must still be what its owner //! installed: a package recorded as a publisher's must carry that //! publisher's valid signature over its current content, and a package with -//! no record must be signed by a trusted key. Development content, recorded -//! as such by `weft-pack install --dev`, runs unverified. +//! no record must be signed by a trusted key, which is then recorded as its +//! owner. Development content, recorded as such by `weft-pack install +//! --dev`, runs unverified. use std::path::{Path, PathBuf}; use weft_ipc_types::manifest::{MANIFEST_FILE, Manifest, ManifestError, entry_path}; use weft_ipc_types::package::{ImageFiles, is_valid_app_id}; use weft_ipc_types::trust::{ - Owner, TrustError, TrustStore, content_digest, owner_record_path, read_owner, read_signature, + Inventory, Owner, PublisherKey, TrustError, TrustStore, lock_owner, owner_record_path, + read_owner, read_signature, write_owner, }; use crate::grants::Refusal; @@ -94,9 +96,14 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result Refusal::new(500, e.to_string()), - ManifestError::Parse(..) => Refusal::new(403, e.to_string()), + // The manifest is read once; the same bytes are parsed and checked + // against the signed inventory, so the capabilities and entries a + // session gets are the ones the owner signed. + let manifest_path = root.join(MANIFEST_FILE); + let manifest_bytes = std::fs::read(&manifest_path) + .map_err(|e| Refusal::new(500, format!("cannot read {}: {e}", manifest_path.display())))?; + let manifest = Manifest::parse(&manifest_path, &manifest_bytes).map_err(|e| match e { + ManifestError::Io(..) | ManifestError::Parse(..) => Refusal::new(403, e.to_string()), })?; if manifest.package.id != app_id { return Err(Refusal::new( @@ -108,7 +115,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result) -> Result Result<(), Refusal> { - let refused = |e: TrustError| match e { +/// Checks that the package at `root`, whose manifest was read as +/// `manifest_bytes`, is what its owner installed. A trusted package with no +/// owner record is recorded as its signer's on its first launch, so its app +/// data stays with that publisher; existing app data with no recorded owner +/// is not handed to whichever publisher launches first. +fn verify_owner(app_id: &str, root: &Path, manifest_bytes: &[u8]) -> Result<(), Refusal> { + // Errors in the host's trust store or owner records are the host's, not + // the package's. + let host = |e: TrustError| Refusal::new(500, e.to_string()); + let package = |e: TrustError| match e { TrustError::Io(..) => Refusal::new(500, e.to_string()), _ => Refusal::new(403, e.to_string()), }; @@ -137,43 +151,90 @@ fn verify_owner(app_id: &str, root: &Path) -> Result<(), Refusal> { "cannot locate the data home holding package owner records", ) })?; - let owner = read_owner(&owner_record_path(&data_home, app_id)).map_err(refused)?; - let trusted = TrustStore::load(&TrustStore::directories()).map_err(refused)?; - match owner { - Some(Owner::Development) => Ok(()), - Some(Owner::Verified(key)) => { - // The key must still be trusted, and its signature must cover - // the package's current content. - let signature = read_signature(root).map_err(refused)?; - let digest = content_digest(root).map_err(refused)?; - let valid = trusted.keys().contains(&key) - && signature.is_some_and(|sig| key.signed(&digest, &sig)); - if valid { - Ok(()) - } else { - Err(Refusal::new( - 403, - format!( - "{} is not signed by its owner, publisher {}, or that key is no longer \ - trusted; the package was changed or its key was withdrawn", - root.display(), - key.to_hex() - ), - )) - } - } - None => match trusted.signer(root).map_err(refused)? { - Some(_) => Ok(()), - None => Err(Refusal::new( + let record = owner_record_path(&data_home, app_id); + let owner = read_owner(&record).map_err(host)?; + if owner == Some(Owner::Development) { + return Ok(()); + } + let trusted = TrustStore::load(&TrustStore::directories()).map_err(host)?; + let inventory = Inventory::read(root).map_err(package)?; + if !inventory.holds(MANIFEST_FILE, manifest_bytes) { + return Err(Refusal::new( + 403, + format!("{} changed while it was checked", root.display()), + )); + } + let digest = inventory.digest(); + let signature = read_signature(root).map_err(package)?; + let signer = signature.and_then(|sig| trusted.signer_of(&digest, &sig)); + match (owner, signer) { + (Some(Owner::Verified(key)), Some(signer)) if key == signer => Ok(()), + (Some(Owner::Verified(key)), _) => Err(Refusal::new( + 403, + format!( + "{} is not signed by its owner, publisher {}, or that key is no longer \ + trusted; the package was changed or its key was withdrawn", + root.display(), + key.to_hex() + ), + )), + (_, None) => Err(Refusal::new( + 403, + format!( + "{} has no recorded owner and is not signed by a trusted key; install it \ + with weft-pack", + root.display() + ), + )), + (_, Some(signer)) => record_first_owner(app_id, &data_home, &record, signer), + } +} + +/// Records `signer` as the owner of `app_id` on the first launch of a trusted +/// package that has no owner record and no app data yet. +fn record_first_owner( + app_id: &str, + data_home: &Path, + record: &Path, + signer: PublisherKey, +) -> Result<(), Refusal> { + let host = |e: TrustError| Refusal::new(500, e.to_string()); + let _lock = lock_owner(data_home, app_id).map_err(host)?; + match read_owner(record).map_err(host)? { + Some(Owner::Verified(key)) if key == signer => return Ok(()), + Some(existing) => { + return Err(Refusal::new( 403, format!( - "{} has no recorded owner and is not signed by a trusted key; install it \ - with weft-pack", - root.display() + "{app_id} belongs to {existing}, not publisher {}", + signer.to_hex() ), - )), - }, + )); + } + 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() + ), + )); + } + } + write_owner(record, Owner::Verified(signer)).map_err(host) } impl LaunchPackage { @@ -369,12 +430,47 @@ mod tests { // Recorded as the publisher's, and with no record but a trusted // signature. let store = demo_store("signed", true); - let unrecorded = resolve(DEMO).map(|p| p.root); - record(&store, DEMO, Owner::Verified(demo_key())); - let recorded = resolve(DEMO).map(|p| p.root); + let first = resolve(DEMO).map(|p| p.root); + let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); + let again = resolve(DEMO).map(|p| p.root); finish(&store); - assert_eq!(unrecorded.ok(), Some(store.join(DEMO))); - assert_eq!(recorded.ok(), Some(store.join(DEMO))); + assert_eq!(first.ok(), Some(store.join(DEMO))); + assert_eq!(owner, Some(Owner::Verified(demo_key()))); + assert_eq!(again.ok(), Some(store.join(DEMO))); + } + + #[test] + fn unowned_app_data_is_not_given_to_the_first_publisher() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("unowned_data", true); + let data = weft_ipc_types::package::app_data_dir(&store.join("share"), DEMO); + std::fs::create_dir_all(&data).unwrap(); + let refused = refusal(resolve(DEMO)); + 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("--claim-data"), + "{}", + refused.message + ); + assert_eq!(owner, None); + } + + #[test] + fn a_damaged_trust_store_or_record_is_a_host_error() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("damaged", true); + std::fs::write(store.join("keys/broken.pub"), "not a key").unwrap(); + let bad_key = refusal(resolve(DEMO)); + std::fs::remove_file(store.join("keys/broken.pub")).unwrap(); + let record = owner_record_path(&store.join("share"), DEMO); + std::fs::write(&record, "nonsense\n").unwrap(); + let bad_record = refusal(resolve(DEMO)); + finish(&store); + for refused in [bad_key, bad_record] { + assert_eq!(refused.code, 500, "{}", refused.message); + } } #[test] diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index d0c549d..0df9632 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -716,18 +716,35 @@ mod tests { } /// Records `id` as development content in a data home private to this - /// test process, which XDG_DATA_HOME then points at. Callers hold - /// env_lock. + /// test process, which XDG_DATA_HOME then points at, with an empty trust + /// store so the host's keys never affect a test. Data homes left by + /// earlier test processes are removed. Callers hold env_lock. fn record_development(id: &str) { - let data_home = - std::env::temp_dir().join(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()) { + for entry in entries.flatten() { + let stale = entry + .file_name() + .to_str() + .is_some_and(|n| n.starts_with("weft-appd-tests-data-") && n != name); + if stale { + let _ = std::fs::remove_dir_all(entry.path()); + } + } + } + let data_home = std::env::temp_dir().join(name); + let keys = data_home.join("trusted-keys"); + std::fs::create_dir_all(&keys).unwrap(); let record = weft_ipc_types::trust::owner_record_path(&data_home, id); if !record.exists() { weft_ipc_types::trust::write_owner(&record, weft_ipc_types::trust::Owner::Development) .unwrap(); } // SAFETY: callers hold env_lock, which serialises environment changes. - unsafe { std::env::set_var("XDG_DATA_HOME", &data_home) }; + unsafe { + std::env::set_var("XDG_DATA_HOME", &data_home); + std::env::set_var("WEFT_TRUSTED_KEYS", &keys); + } } fn make_registry() -> Registry { diff --git a/crates/weft-ipc-types/src/manifest.rs b/crates/weft-ipc-types/src/manifest.rs index 0115bc4..c15ff3b 100644 --- a/crates/weft-ipc-types/src/manifest.rs +++ b/crates/weft-ipc-types/src/manifest.rs @@ -62,9 +62,19 @@ impl Manifest { /// Reads `/wapp.toml`. pub fn read(package_root: &Path) -> Result { let path = package_root.join(MANIFEST_FILE); - let text = - std::fs::read_to_string(&path).map_err(|e| ManifestError::Io(path.clone(), e))?; - toml::from_str(&text).map_err(|e| ManifestError::Parse(path, e)) + let bytes = std::fs::read(&path).map_err(|e| ManifestError::Io(path.clone(), e))?; + Self::parse(&path, &bytes) + } + + /// Parses manifest bytes already read from `path`. + pub fn parse(path: &Path, bytes: &[u8]) -> Result { + let text = std::str::from_utf8(bytes).map_err(|e| { + ManifestError::Io( + path.to_path_buf(), + std::io::Error::new(std::io::ErrorKind::InvalidData, e), + ) + })?; + toml::from_str(text).map_err(|e| ManifestError::Parse(path.to_path_buf(), e)) } pub fn capabilities(&self) -> &[String] { diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 79143b5..0547b5a 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -56,17 +56,40 @@ fn io(path: &Path) -> impl FnOnce(std::io::Error) -> TrustError + '_ { /// The SHA-256 content digest a package signature covers. pub fn content_digest(package_root: &Path) -> Result<[u8; 32], TrustError> { - let mut entries = Vec::new(); - collect(package_root, package_root, &mut entries)?; - entries.sort(); - let mut inventory = String::new(); - for (path, hash) in &entries { - inventory.push_str(path); - inventory.push('\t'); - inventory.push_str(&hex::encode(hash)); - inventory.push('\n'); + Inventory::read(package_root).map(|inventory| inventory.digest()) +} + +/// The files of a package and their SHA-256 hashes, as read once: the digest +/// and the hash of any one file come from the same reading. +#[derive(Debug)] +pub struct Inventory(Vec<(String, [u8; 32])>); + +impl Inventory { + pub fn read(package_root: &Path) -> Result { + let mut entries = Vec::new(); + collect(package_root, package_root, &mut entries)?; + entries.sort(); + Ok(Self(entries)) + } + + /// The content digest a package signature covers. + pub fn digest(&self) -> [u8; 32] { + let mut inventory = String::new(); + for (path, hash) in &self.0 { + inventory.push_str(path); + inventory.push('\t'); + inventory.push_str(&hex::encode(hash)); + inventory.push('\n'); + } + Sha256::digest(inventory.as_bytes()).into() + } + + /// Whether the file at `path`, relative to the package root, had exactly + /// these bytes when the inventory was read. + pub fn holds(&self, path: &str, bytes: &[u8]) -> bool { + let hash: [u8; 32] = Sha256::digest(bytes).into(); + self.0.iter().any(|(p, h)| p == path && *h == hash) } - Ok(Sha256::digest(inventory.as_bytes()).into()) } fn collect( @@ -220,12 +243,15 @@ impl TrustStore { let Some(signature) = read_signature(package_root)? else { return Ok(None); }; - let digest = content_digest(package_root)?; - Ok(self - .keys + Ok(self.signer_of(&content_digest(package_root)?, &signature)) + } + + /// The trusted key that made `signature` over `digest`, if any. + pub fn signer_of(&self, digest: &[u8; 32], signature: &Signature) -> Option { + self.keys .iter() .copied() - .find(|k| k.signed(&digest, &signature))) + .find(|k| k.signed(digest, signature)) } } @@ -409,6 +435,17 @@ mod tests { std::fs::remove_dir_all(&dir).unwrap(); } + #[test] + fn an_inventory_knows_the_bytes_it_hashed() { + let dir = dir("inventory"); + let inventory = Inventory::read(&dir).unwrap(); + assert_eq!(inventory.digest(), content_digest(&dir).unwrap()); + assert!(inventory.holds("wapp.toml", b"[package]\n")); + assert!(!inventory.holds("wapp.toml", b"[package]\ncapabilities = []\n")); + assert!(!inventory.holds("absent.toml", b"[package]\n")); + std::fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn owner_locks_exclude_each_other() { let home = std::env::temp_dir().join(format!("weft_trust_lock_{}", std::process::id())); diff --git a/docs/security.md b/docs/security.md index 12016fa..24c289d 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; development content runs unverified. A changed package, or one whose key was removed from the trust store, is refused with code 403. The check reads the package before the session starts, so a directory install changed by its owning user 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, 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. 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_confinement.py b/tests/frame/check_confinement.py index d027921..88e76f8 100644 --- a/tests/frame/check_confinement.py +++ b/tests/frame/check_confinement.py @@ -146,7 +146,7 @@ def make_package(store, http_port, ws_port, other_dir): # planted directly and recorded as development content, which runs # unverified: the renderer's own confinement is what this check probes. record = store / "home/share/weft/owners" / APP_ID - record.parent.mkdir(parents=True) + record.parent.mkdir(parents=True, exist_ok=True) record.write_text("development\n") diff --git a/tests/frame/check_counter.py b/tests/frame/check_counter.py index 0da4e88..33086ef 100644 --- a/tests/frame/check_counter.py +++ b/tests/frame/check_counter.py @@ -158,11 +158,17 @@ def count_area(screenshot, card): def run(args, desktop, xdotool): port = free_port() target = Path(args.target) + # A private home, so owner records appd writes stay out of the user's. + home = args.output / "home" + shutil.rmtree(home, ignore_errors=True) + home.mkdir(parents=True) desktop.launch_client("appd", [target / "weft-appd"], { "WEFT_RUNTIME_BIN": str(target / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(target / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), "WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"), + "HOME": str(home), + "XDG_DATA_HOME": str(home / "share"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(port), }) diff --git a/tests/frame/check_notes.py b/tests/frame/check_notes.py index 6ee3cba..42f3d47 100644 --- a/tests/frame/check_notes.py +++ b/tests/frame/check_notes.py @@ -69,6 +69,12 @@ def run(args, desktop, home, xdotool): notes = data_home / "weft/app-data" / APP_ID / "notes.txt" notes.parent.mkdir(parents=True) notes.write_text(SEED, encoding="utf-8") + # The seeded 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"], { From 95db80a86d4c206e3e962197c729c603b74b0099 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:41:23 +0000 Subject: [PATCH 12/61] fix(appd): count app data in the earlier layout as existing before recording an owner App data in the earlier layout, /.local/share/weft/apps//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. --- crates/weft-appd/src/launch.rs | 102 +++++++++++++++++++++------ crates/weft-appd/src/main.rs | 4 +- crates/weft-ipc-types/src/package.rs | 24 +++++++ crates/weft-pack/src/main.rs | 38 ++++++++-- docs/security.md | 2 +- tests/frame/check_app_data.py | 6 ++ 6 files changed, 146 insertions(+), 30 deletions(-) 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"], { From d3adced897b2276e9cc3b77b029fb15e1aaf1480 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:46:21 +0000 Subject: [PATCH 13/61] 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() From af6f716823b3727a8a3fbbc28b8cbe783395dfa2 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:56:42 +0000 Subject: [PATCH 14/61] fix(pack): let --claim-data install without HOME and test HOME refusals Claiming data does not depend on finding earlier-layout data, so an install that claims it no longer needs HOME. Tests cover a first launch and a first install with an unusable HOME refusing and recording no owner, and the security docs state the HOME requirement. --- crates/weft-appd/src/launch.rs | 13 +++++++++++++ crates/weft-pack/src/main.rs | 24 +++++++++++++++++++++++- docs/security.md | 2 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 10203d6..89efc67 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -485,6 +485,19 @@ mod tests { assert_eq!(owner, None); } + #[test] + fn a_first_launch_without_a_usable_home_records_nothing() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("no_home", true); + // SAFETY: env_lock is held; finish restores HOME. + unsafe { std::env::set_var("HOME", "relative") }; + let refused = refusal(resolve(DEMO)); + let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); + finish(&store); + assert_eq!(refused.code, 500, "{}", refused.message); + assert_eq!(owner, None); + } + #[test] fn only_the_signed_manifest_is_accepted() { let _env = crate::tests::env_lock().blocking_lock(); diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 66ba655..c8f56c1 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -490,6 +490,10 @@ fn admit( "{app_id} belongs to {existing}; this package is {owner}. Its app data stays with \ its owner, so another publisher or a development build cannot take the ID over" ), + None if claim_data => { + write_owner(&record, owner)?; + Some(record) + } None => { // Data in the earlier layout counts: weft-appd moves it to the // app's data directory when the app next launches. @@ -498,7 +502,7 @@ fn admit( )?; 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) { + if let Some(data) = existing { 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", @@ -1433,6 +1437,24 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn a_first_install_without_a_usable_home_records_nothing() { + let home = temp_root("no_home"); + let app_id = "org.weft.test.nohome"; + write_package(&home.join("src"), app_id, ""); + let store = home.join("store"); + let refused = with_home(&home, || { + // SAFETY: with_home holds env_lock and restores HOME afterwards. + unsafe { std::env::set_var("HOME", "relative") }; + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + let message = format!("{:#}", refused.unwrap_err()); + assert!(message.contains("HOME"), "{message}"); + assert_eq!(owner_of(&home, app_id), None); + assert!(!store.join(app_id).exists()); + 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 39020fc..55bdf1d 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 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. +`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. Finding earlier-layout data needs `HOME`: without an absolute `HOME`, a first launch is refused (500), a first install fails unless it claims the data, and uninstall keeps the record. 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. From 7cf8639ae35d8e4f4407c4913012c77b75ac1c64 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:07:33 +0000 Subject: [PATCH 15/61] fix(compositor): bind each app session's Wayland client to its session through appd The compositor stored appd's (pid, session) announcements and never used them, so no surface could be attributed to a session and any client could claim any app ID or the panel role. weft-appd now creates a connected socket pair for each session, gives the app shell one end as standard input with WAYLAND_SOCKET=0 and no WAYLAND_DISPLAY, and sends the other end to the compositor with the session and app ID over the compositor IPC socket (SCM_RIGHTS). The compositor inserts it as a client bound to that session: holding the connection is the session's identity. A session's zweft_shell windows carry its app ID, the panel and overlay roles are refused to sessions with a new role_not_permitted error, the session's first toplevel is reported to appd, an ended session's client is closed, and a client's disconnection is reported. The compositor accepts one appd connection at a time. The counter desktop check now requires the compositor to bind the app's client to its session and to close it when the session is terminated. --- Cargo.lock | 2 + crates/weft-appd/Cargo.toml | 1 + crates/weft-appd/src/compositor_client.rs | 86 ++++++-- crates/weft-appd/src/main.rs | 118 ++++++++++- crates/weft-appd/src/runtime.rs | 102 ++++++--- crates/weft-compositor/Cargo.toml | 1 + crates/weft-compositor/src/appd_ipc.rs | 246 ++++++++++++++++++++-- crates/weft-compositor/src/state.rs | 48 ++++- crates/weft-ipc-types/src/lib.rs | 31 +-- docs/architecture.md | 4 +- docs/security.md | 4 +- protocol/weft-shell-unstable-v1.xml | 7 + tests/frame/check_counter.py | 18 +- 13 files changed, 570 insertions(+), 98 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 20cbbdf..82580ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10473,6 +10473,7 @@ dependencies = [ "futures-util", "libc", "rmp-serde", + "rustix 1.1.4", "sd-notify", "serde", "serde_json", @@ -10494,6 +10495,7 @@ version = "0.1.0" dependencies = [ "anyhow", "bitflags 2.11.0", + "rustix 1.1.4", "sd-notify", "smithay", "smithay-drm-extras", diff --git a/crates/weft-appd/Cargo.toml b/crates/weft-appd/Cargo.toml index 62b004e..d1fbeea 100644 --- a/crates/weft-appd/Cargo.toml +++ b/crates/weft-appd/Cargo.toml @@ -21,6 +21,7 @@ futures-util = { version = "0.3", default-features = false, features = ["sink", tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } libc = "0.2" +rustix = { version = "1", features = ["net"] } [target.'cfg(unix)'.dependencies] sd-notify = "0.4" diff --git a/crates/weft-appd/src/compositor_client.rs b/crates/weft-appd/src/compositor_client.rs index 3b97437..01bb1ac 100644 --- a/crates/weft-appd/src/compositor_client.rs +++ b/crates/weft-appd/src/compositor_client.rs @@ -1,3 +1,4 @@ +use std::os::fd::OwnedFd; use std::path::PathBuf; use tokio::io::AsyncWriteExt; @@ -6,7 +7,19 @@ use weft_ipc_types::{ AppdToCompositor, CompositorToAppd, MAX_FRAME_LEN, frame_decode, frame_encode, }; -pub type CompositorSender = mpsc::Sender; +/// A message for the compositor and the file descriptor it carries, if any. +pub struct Outbound { + pub msg: AppdToCompositor, + pub fd: Option, +} + +impl From for Outbound { + fn from(msg: AppdToCompositor) -> Self { + Self { msg, fd: None } + } +} + +pub type CompositorSender = mpsc::Sender; /// Resolve the compositor IPC socket path. /// @@ -26,15 +39,14 @@ pub fn socket_path() -> Option { /// /// The task connects to `socket_path`, retrying every 2 s on failure. If the /// connection drops while sending, it waits 500 ms then reconnects. Incoming -/// `CompositorToAppd` frames are decoded and logged; no behavioural action is -/// taken yet (surface lifecycle hookup happens in a later task). +/// `CompositorToAppd` frames are decoded and logged. pub fn spawn(socket_path: PathBuf) -> CompositorSender { - let (tx, rx) = mpsc::channel::(32); + let (tx, rx) = mpsc::channel::(32); tokio::spawn(run_client(socket_path, rx)); tx } -async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver) { +async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver) { loop { let stream = loop { match tokio::net::UnixStream::connect(&socket_path).await { @@ -53,17 +65,22 @@ async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver { - if write_half.write_all(&frame).await.is_err() { - tracing::warn!("compositor IPC write failed; reconnecting"); - break; - } + let frame = match frame_encode(&outbound.msg) { + Ok(frame) => frame, + Err(e) => { + tracing::warn!(?e, "compositor IPC encode error"); + continue; } - Err(e) => tracing::warn!(?e, "compositor IPC encode error"), + }; + if send_frame(&mut write_half, &frame, outbound.fd) + .await + .is_err() + { + tracing::warn!("compositor IPC write failed; reconnecting"); + break; } } @@ -71,6 +88,45 @@ async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver, +) -> std::io::Result<()> { + use rustix::net::{SendAncillaryBuffer, SendAncillaryMessage, SendFlags, sendmsg}; + use std::mem::MaybeUninit; + use std::os::fd::AsFd; + let Some(fd) = fd else { + return stream.write_all(frame).await; + }; + let socket: &tokio::net::UnixStream = stream.as_ref(); + let sent = loop { + socket.writable().await?; + let result = socket.try_io(tokio::io::Interest::WRITABLE, || { + let mut space = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmRights(1))]; + let mut control = SendAncillaryBuffer::new(&mut space); + let fds = [fd.as_fd()]; + control.push(SendAncillaryMessage::ScmRights(&fds)); + sendmsg( + socket, + &[std::io::IoSlice::new(frame)], + &mut control, + SendFlags::NOSIGNAL, + ) + .map_err(std::io::Error::from) + }); + match result { + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => continue, + other => break other?, + } + }; + // The descriptor travelled with the first bytes; the rest of a partly + // sent frame follows as plain data. + stream.write_all(&frame[sent..]).await +} + async fn read_unix_incoming(mut reader: tokio::net::unix::OwnedReadHalf) { use tokio::io::AsyncReadExt; @@ -110,8 +166,8 @@ fn handle_incoming(msg: CompositorToAppd) { CompositorToAppd::SurfaceReady { session_id } => { tracing::debug!(session_id, "SurfaceReady from compositor"); } - CompositorToAppd::ClientDisconnected { pid } => { - tracing::debug!(pid, "ClientDisconnected from compositor"); + CompositorToAppd::ClientDisconnected { session_id } => { + tracing::debug!(session_id, "ClientDisconnected from compositor"); } } } diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 433b0e8..a8c75ac 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -8,15 +8,7 @@ use anyhow::Context; use tokio::io::AsyncWriteExt; use tokio::sync::Mutex; -#[cfg(unix)] mod compositor_client; -#[cfg(not(unix))] -mod compositor_client { - use tokio::sync::mpsc; - use weft_ipc_types::AppdToCompositor; - - pub type CompositorSender = mpsc::Sender; -} mod grants; mod ipc; mod launch; @@ -1249,6 +1241,116 @@ mod tests { assert!(!session_ipc_socket_path(session_id).unwrap().exists()); } + #[tokio::test(flavor = "current_thread")] + async fn the_app_shell_connects_only_through_its_session_connection() { + use std::io::BufRead; + use std::os::unix::fs::PermissionsExt; + let _env = env_lock().lock().await; + use_test_runtime_dir(); + let dir = std::env::temp_dir().join(format!("weft_test_attach_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let app = dir.join("store/org.example.attach"); + write_test_package(&app, "org.example.attach", ""); + // Each child writes its Wayland environment to its standard input, + // which only the app shell has connected to anything. + let child = dir.join("child.sh"); + std::fs::write( + &child, + "#!/bin/sh\nprintf '%s %s\\n' \"${WAYLAND_SOCKET-unset}\" \ + \"${WAYLAND_DISPLAY-unset}\" >&0 2>/dev/null\n\ + echo READY $WEFT_READY_TOKEN\nexec sleep 1\n", + ) + .unwrap(); + std::fs::set_permissions(&child, std::fs::Permissions::from_mode(0o755)).unwrap(); + let vars = [ + ("WEFT_RUNTIME_BIN", child.clone().into_os_string()), + ("WEFT_APP_SHELL_BIN", child.clone().into_os_string()), + ("WEFT_DISABLE_CGROUP", "1".into()), + ("WEFT_APP_STORE", dir.join("store").into_os_string()), + ("WAYLAND_DISPLAY", "wayland-ambient".into()), + ]; + let prior: Vec<_> = vars + .iter() + .map(|(k, _)| (*k, std::env::var_os(k))) + .collect(); + for (key, value) in &vars { + // SAFETY: env_lock is held on a current_thread runtime. + unsafe { std::env::set_var(key, value) }; + } + + let registry = make_registry(); + let (tx, mut compositor) = tokio::sync::mpsc::channel(8); + registry.lock().await.compositor_tx = Some(tx); + let mut rx = registry.lock().await.subscribe(); + let ack = dispatch( + Request::LaunchApp { + app_id: "org.example.attach".into(), + surface_id: 0, + }, + ®istry, + ) + .await; + let attached = tokio::time::timeout(std::time::Duration::from_secs(10), compositor.recv()) + .await + .ok() + .flatten(); + let stopped = tokio::time::timeout(std::time::Duration::from_secs(10), async { + while !matches!( + rx.recv().await, + Ok(Response::AppState { + state: AppStateKind::Stopped, + .. + }) + ) {} + }) + .await; + let released = compositor.try_recv().ok().map(|out| out.msg); + + for (key, value) in prior { + // SAFETY: as above. + unsafe { + match value { + Some(v) => std::env::set_var(key, v), + None => std::env::remove_var(key), + } + } + } + let _ = std::fs::remove_dir_all(&dir); + let Response::LaunchAck { session_id, .. } = ack else { + panic!("{ack:?}"); + }; + assert!(stopped.is_ok(), "session did not stop"); + let attached = attached.expect("no connection was handed to the compositor"); + assert!( + matches!( + &attached.msg, + weft_ipc_types::AppdToCompositor::AttachClient { session_id: s, app_id } + if *s == session_id && app_id == "org.example.attach" + ), + "{:?}", + attached.msg + ); + // The compositor's end reaches the app shell, which sees only + // WAYLAND_SOCKET and no display to connect to instead. + let stream = std::os::unix::net::UnixStream::from(attached.fd.expect("no descriptor")); + stream + .set_read_timeout(Some(std::time::Duration::from_secs(5))) + .unwrap(); + let mut line = String::new(); + std::io::BufReader::new(stream) + .read_line(&mut line) + .unwrap(); + assert_eq!(line.trim(), "0 unset"); + assert!( + matches!( + released, + Some(weft_ipc_types::AppdToCompositor::AppSurfaceDestroyed { session_id: s }) + if s == session_id + ), + "{released:?}" + ); + } + #[tokio::test(flavor = "current_thread")] async fn children_receive_the_resolved_package_files() { use std::os::unix::fs::PermissionsExt; diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 6f9cce9..1106155 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -191,8 +191,26 @@ fn spawn_app_shell( package: &LaunchPackage, token: &str, bridge_token: Option, + wayland: Option, ) -> std::io::Result { let mut command = tokio::process::Command::new(bin); + match wayland { + // The app shell's only Wayland connection is the one created for its + // session, handed over as its standard input: WAYLAND_SOCKET names + // that descriptor, and no display socket is offered to connect + // anywhere else. Only this child receives the descriptor. + Some(stream) => { + command + .stdin(std::process::Stdio::from(std::os::fd::OwnedFd::from( + stream, + ))) + .env("WAYLAND_SOCKET", "0") + .env_remove("WAYLAND_DISPLAY"); + } + None => { + command.stdin(std::process::Stdio::null()); + } + } if let Some(bridge_token) = bridge_token { // The page holds this credential; it must differ from the readiness // token, which page output could otherwise use to fake readiness. @@ -204,7 +222,6 @@ fn spawn_app_shell( .arg("--ui") .arg(&package.ui_entry) .env(READY_TOKEN_ENV, token) - .stdin(std::process::Stdio::null()) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::piped()) .kill_on_drop(true) @@ -330,7 +347,7 @@ pub(crate) async fn supervise( relay: Some(relay), image, compositor_tx, - surface_announced: false, + client_attached: false, }; let mut runtime = match cmd.spawn() { @@ -341,23 +358,6 @@ pub(crate) async fn supervise( .await; } }; - if let Some(tx) = &session.compositor_tx { - let pid = runtime.id().unwrap_or(0); - // A full queue (compositor disconnected) must not stall the session. - session.surface_announced = tx - .try_send(AppdToCompositor::AppSurfaceCreated { - app_id: app_id.to_owned(), - session_id, - pid, - }) - .is_ok(); - if !session.surface_announced { - tracing::warn!( - session_id, - "compositor queue unavailable; surface not announced" - ); - } - } let runtime_stdout = runtime.stdout.take().expect("stdout piped"); tokio::spawn(drain_stderr( runtime.stderr.take().expect("stderr piped"), @@ -377,16 +377,35 @@ pub(crate) async fn supervise( }; tokio::spawn(drain_stdout(runtime_stdout, session_id)); - let bridge_token = registry.lock().await.bridge_token(session_id); - let mut app_shell = - match spawn_app_shell(&shell_bin, session_id, &package, &token, bridge_token) { - Ok(child) => child, - Err(e) => { - return session - .settle(®istry, &format!("failed to spawn app shell: {e}")) - .await; + // With a compositor, the app shell connects only through a connection + // the compositor received for this session, so every surface it creates + // is bound to the session. + let wayland = match &session.compositor_tx { + None => None, + Some(tx) => match attach_client(tx, session_id, app_id) { + Ok(stream) => { + session.client_attached = true; + Some(stream) } - }; + Err(reason) => return session.settle(®istry, &reason).await, + }, + }; + let bridge_token = registry.lock().await.bridge_token(session_id); + let mut app_shell = match spawn_app_shell( + &shell_bin, + session_id, + &package, + &token, + bridge_token, + wayland, + ) { + Ok(child) => child, + Err(e) => { + return session + .settle(®istry, &format!("failed to spawn app shell: {e}")) + .await; + } + }; let shell_stdout = app_shell.stdout.take().expect("stdout piped"); tokio::spawn(drain_stderr( app_shell.stderr.take().expect("stderr piped"), @@ -438,6 +457,26 @@ pub(crate) async fn supervise( session.settle(®istry, &reason).await } +/// Creates the session's Wayland connection and queues the compositor's end +/// for the compositor, returning the app shell's end. +fn attach_client( + tx: &CompositorSender, + session_id: u64, + app_id: &str, +) -> Result { + let (shell, compositor) = std::os::unix::net::UnixStream::pair() + .map_err(|e| format!("cannot create the app's Wayland connection: {e}"))?; + tx.try_send(crate::compositor_client::Outbound { + msg: AppdToCompositor::AttachClient { + session_id, + app_id: app_id.to_owned(), + }, + fd: Some(compositor.into()), + }) + .map_err(|_| "the compositor connection is not available".to_owned())?; + Ok(shell) +} + /// Marks a session that never started any process as stopped. async fn stop_unstarted( registry: &Registry, @@ -467,7 +506,8 @@ struct OwnedSession { /// The mounted package image, released after both children have exited. image: Option, compositor_tx: Option, - surface_announced: bool, + /// Whether the compositor was given this session's client connection. + client_attached: bool, } impl OwnedSession { @@ -476,10 +516,10 @@ impl OwnedSession { tracing::info!(session_id, reason, "stopping session"); kill_child(self.app_shell).await; kill_child(self.runtime).await; - if self.surface_announced + if self.client_attached && let Some(tx) = &self.compositor_tx && tx - .try_send(AppdToCompositor::AppSurfaceDestroyed { session_id }) + .try_send(AppdToCompositor::AppSurfaceDestroyed { session_id }.into()) .is_err() { tracing::warn!( diff --git a/crates/weft-compositor/Cargo.toml b/crates/weft-compositor/Cargo.toml index 3db3c76..e3d4ce1 100644 --- a/crates/weft-compositor/Cargo.toml +++ b/crates/weft-compositor/Cargo.toml @@ -26,6 +26,7 @@ wayland-scanner = "0.31" wayland-server = "0.31" wayland-backend = "0.3" bitflags = "2" +rustix = { version = "1", features = ["net"] } # DRM/KMS and hardware input depend on Linux kernel interfaces; compile only on Linux. [target.'cfg(target_os = "linux")'.dependencies] diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index 4fdadef..d1a5532 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -1,13 +1,17 @@ #[cfg(unix)] use std::{ - collections::HashMap, - io::{self, Read, Write}, + collections::{HashMap, HashSet, VecDeque}, + io, + os::fd::OwnedFd, os::unix::net::{UnixListener, UnixStream}, path::PathBuf, + sync::Arc, }; #[cfg(unix)] -use smithay::reexports::calloop::{Interest, Mode, PostAction, generic::Generic}; +use smithay::reexports::calloop::{Interest, Mode, PostAction, channel, generic::Generic}; +#[cfg(unix)] +use smithay::reexports::wayland_server::backend::{ClientId, DisconnectReason}; #[cfg(unix)] use weft_ipc_types::{ @@ -15,15 +19,28 @@ use weft_ipc_types::{ }; #[cfg(unix)] -use crate::state::WeftCompositorState; +use crate::state::{SessionBinding, WeftClientState, WeftCompositorState}; + +/// Descriptors received ahead of the messages that claim them; more than +/// this means the peer is not following the protocol. +#[cfg(unix)] +const MAX_PENDING_FDS: usize = 16; #[cfg(unix)] pub struct WeftAppdIpc { pub socket_path: PathBuf, read_buf: Vec, write_stream: Option, - /// pid → (app_id, session_id) for Wayland clients whose surface has not yet arrived. - pub pending_pids: HashMap, + /// Descriptors received with appd messages, in arrival order. Each + /// `AttachClient` frame is sent with exactly one, attached to its first + /// bytes, so it has arrived by the time the frame is decoded. + fds: VecDeque, + /// The Wayland client of each appd session. + sessions: HashMap, + /// Sessions whose first toplevel was reported to appd. + reported: HashSet, + /// Where clients report their session's disconnection. + disconnected: Option>, } #[cfg(unix)] @@ -33,12 +50,15 @@ impl WeftAppdIpc { socket_path, read_buf: Vec::new(), write_stream: None, - pending_pids: HashMap::new(), + fds: VecDeque::new(), + sessions: HashMap::new(), + reported: HashSet::new(), + disconnected: None, } } - #[allow(dead_code)] pub fn send(&mut self, msg: &CompositorToAppd) { + use std::io::Write; let Some(stream) = &mut self.write_stream else { return; }; @@ -52,6 +72,13 @@ impl WeftAppdIpc { } } + /// Reports a session's first toplevel to appd. + pub fn surface_created(&mut self, session_id: u64) { + if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) { + self.send(&CompositorToAppd::SurfaceReady { session_id }); + } + } + fn try_decode_frames(&mut self) -> Vec { let mut out = Vec::new(); loop { @@ -83,17 +110,35 @@ impl WeftAppdIpc { out } - pub fn on_read(&mut self, stream: &mut UnixStream) -> (Vec, bool) { + /// Reads what is available, keeping descriptors that arrive with the + /// data, and returns the complete messages and whether appd hung up. + pub fn on_read(&mut self, stream: &UnixStream) -> (Vec, bool) { + use rustix::net::{RecvAncillaryBuffer, RecvAncillaryMessage, RecvFlags, recvmsg}; + use std::mem::MaybeUninit; let mut buf = [0u8; 8192]; let mut eof = false; loop { - match stream.read(&mut buf) { - Ok(0) => { + let mut space = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmRights(4))]; + let mut control = RecvAncillaryBuffer::new(&mut space); + let received = recvmsg( + stream, + &mut [io::IoSliceMut::new(&mut buf)], + &mut control, + RecvFlags::CMSG_CLOEXEC | RecvFlags::DONTWAIT, + ); + for message in control.drain() { + if let RecvAncillaryMessage::ScmRights(fds) = message { + self.fds.extend(fds); + } + } + match received { + Ok(msg) if msg.bytes == 0 => { eof = true; break; } - Ok(n) => self.read_buf.extend_from_slice(&buf[..n]), - Err(e) if e.kind() == io::ErrorKind::WouldBlock => break, + Ok(msg) => self.read_buf.extend_from_slice(&buf[..msg.bytes]), + Err(rustix::io::Errno::AGAIN) => break, + Err(rustix::io::Errno::INTR) => continue, Err(e) => { tracing::warn!(?e, "appd IPC stream read error"); eof = true; @@ -101,10 +146,15 @@ impl WeftAppdIpc { } } } + if self.fds.len() > MAX_PENDING_FDS { + tracing::warn!("appd sent descriptors without messages; disconnecting"); + eof = true; + } let messages = self.try_decode_frames(); if eof { self.write_stream = None; self.read_buf.clear(); + self.fds.clear(); } (messages, eof) } @@ -113,18 +163,21 @@ impl WeftAppdIpc { #[cfg(unix)] fn handle_message(state: &mut WeftCompositorState, msg: AppdToCompositor) { match msg { - AppdToCompositor::AppSurfaceCreated { - app_id, - session_id, - pid, - } => { - tracing::debug!(app_id, session_id, pid, "AppSurfaceCreated"); - if let Some(ipc) = &mut state.appd_ipc { - ipc.pending_pids.insert(pid, (app_id, session_id)); - } + AppdToCompositor::AttachClient { session_id, app_id } => { + attach_client(state, session_id, app_id); } AppdToCompositor::AppSurfaceDestroyed { session_id } => { - tracing::debug!(session_id, "AppSurfaceDestroyed"); + let Some(ipc) = state.appd_ipc.as_mut() else { + return; + }; + ipc.reported.remove(&session_id); + if let Some(client) = ipc.sessions.remove(&session_id) { + tracing::info!(session_id, "closing the client of an ended session"); + state + .display_handle + .backend_handle() + .kill_client(client, DisconnectReason::ConnectionClosed); + } } AppdToCompositor::AppFocusRequest { session_id } => { tracing::debug!(session_id, "AppFocusRequest"); @@ -132,6 +185,54 @@ fn handle_message(state: &mut WeftCompositorState, msg: AppdToCompositor) { } } +/// Makes the descriptor that came with an `AttachClient` message a Wayland +/// client bound to `session_id`. +#[cfg(unix)] +fn attach_client(state: &mut WeftCompositorState, session_id: u64, app_id: String) { + let Some(ipc) = state.appd_ipc.as_mut() else { + return; + }; + let Some(fd) = ipc.fds.pop_front() else { + tracing::warn!( + session_id, + "AttachClient arrived without a connection; ignored" + ); + return; + }; + if !weft_ipc_types::package::is_valid_app_id(&app_id) { + tracing::warn!(session_id, "AttachClient with an invalid app ID; ignored"); + return; + } + if ipc.sessions.contains_key(&session_id) { + tracing::warn!( + session_id, + "session already has a client; connection refused" + ); + return; + } + let Some(disconnected) = ipc.disconnected.clone() else { + return; + }; + let stream = UnixStream::from(fd); + let data = Arc::new(WeftClientState { + session: Some(SessionBinding { + session_id, + app_id: app_id.clone(), + disconnected, + }), + ..WeftClientState::default() + }); + match state.display_handle.insert_client(stream, data) { + Ok(client) => { + tracing::info!(session_id, %app_id, "client bound to session"); + if let Some(ipc) = state.appd_ipc.as_mut() { + ipc.sessions.insert(session_id, client.id()); + } + } + Err(e) => tracing::warn!(session_id, ?e, "cannot add the session's client"), + } +} + #[cfg(unix)] pub fn compositor_socket_path() -> PathBuf { if let Ok(p) = std::env::var("WEFT_COMPOSITOR_SOCKET") { @@ -168,6 +269,25 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { tracing::info!(path = %socket_path.display(), "compositor IPC socket open"); + // Clients bound to a session report their disconnection here. + let (sender, receiver) = channel::channel::(); + if let Some(ipc) = state.appd_ipc.as_mut() { + ipc.disconnected = Some(sender); + } + state + .loop_handle + .insert_source(receiver, |event, _, state| { + if let channel::Event::Msg(session_id) = event + && let Some(ipc) = state.appd_ipc.as_mut() + { + ipc.sessions.remove(&session_id); + ipc.reported.remove(&session_id); + tracing::info!(session_id, "session client disconnected"); + ipc.send(&CompositorToAppd::ClientDisconnected { session_id }); + } + }) + .map_err(|e| anyhow::anyhow!("insert session disconnect source: {e}"))?; + let handle = state.loop_handle.clone(); state .loop_handle @@ -177,6 +297,18 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { loop { match listener.accept() { Ok((stream, _addr)) => { + // One appd at a time: a second connection while + // one is live is refused rather than replacing it. + if state + .appd_ipc + .as_ref() + .is_some_and(|ipc| ipc.write_stream.is_some()) + { + tracing::warn!( + "second compositor IPC connection refused; weft-appd is connected" + ); + continue; + } tracing::info!("weft-appd connected to compositor IPC"); let write_clone = match stream.try_clone() { Ok(c) => c, @@ -188,6 +320,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { if let Some(ipc) = &mut state.appd_ipc { ipc.write_stream = Some(write_clone); ipc.read_buf.clear(); + ipc.fds.clear(); } stream.set_nonblocking(true).ok(); let _ = handle.insert_source( @@ -225,3 +358,70 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { Ok(()) } + +#[cfg(all(test, unix))] +mod tests { + use super::*; + use rustix::net::{SendAncillaryBuffer, SendAncillaryMessage, SendFlags, sendmsg}; + use std::mem::MaybeUninit; + use std::os::fd::AsFd; + + fn send_with_fd(stream: &UnixStream, bytes: &[u8], fd: &impl AsFd) { + let mut space = [MaybeUninit::uninit(); rustix::cmsg_space!(ScmRights(1))]; + let mut control = SendAncillaryBuffer::new(&mut space); + let fds = [fd.as_fd()]; + assert!(control.push(SendAncillaryMessage::ScmRights(&fds))); + let sent = sendmsg( + stream, + &[io::IoSlice::new(bytes)], + &mut control, + SendFlags::empty(), + ) + .unwrap(); + assert_eq!(sent, bytes.len()); + } + + #[test] + fn a_connection_arrives_with_the_message_that_claims_it() { + let (appd, compositor) = UnixStream::pair().unwrap(); + compositor.set_nonblocking(true).unwrap(); + let (shell, client) = UnixStream::pair().unwrap(); + let frame = frame_encode(&AppdToCompositor::AttachClient { + session_id: 7, + app_id: "org.weft.demo.counter".into(), + }) + .unwrap(); + send_with_fd(&appd, &frame, &client); + drop(client); + + let mut ipc = WeftAppdIpc::new(PathBuf::from("/nonexistent")); + let (messages, eof) = ipc.on_read(&compositor); + assert!(!eof); + assert!(matches!( + messages.as_slice(), + [AppdToCompositor::AttachClient { session_id: 7, .. }] + )); + // The received descriptor is the other end of the shell's connection. + let received = UnixStream::from(ipc.fds.pop_front().expect("no descriptor")); + use std::io::{Read, Write}; + (&shell).write_all(b"hello").unwrap(); + let mut buf = [0u8; 5]; + (&received).read_exact(&mut buf).unwrap(); + assert_eq!(&buf, b"hello"); + } + + #[test] + fn descriptors_without_messages_end_the_connection() { + let (appd, compositor) = UnixStream::pair().unwrap(); + compositor.set_nonblocking(true).unwrap(); + let (_shell, client) = UnixStream::pair().unwrap(); + let mut ipc = WeftAppdIpc::new(PathBuf::from("/nonexistent")); + for _ in 0..=MAX_PENDING_FDS { + send_with_fd(&appd, &[0], &client); + } + let (messages, eof) = ipc.on_read(&compositor); + assert!(messages.is_empty()); + assert!(eof); + assert!(ipc.fds.is_empty()); + } +} diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index 82960da..b0dac77 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -53,11 +53,27 @@ use smithay::{ #[derive(Default)] pub struct WeftClientState { pub compositor_state: CompositorClientState, + /// The appd session this client's connection was created for. Clients + /// that connect through the display socket have none. + pub session: Option, +} + +/// The appd session a client belongs to, established by weft-appd handing +/// the compositor the client's connection, never by the client itself. +pub struct SessionBinding { + pub session_id: u64, + pub app_id: String, + /// Reports the session's disconnection to the event loop. + pub disconnected: smithay::reexports::calloop::channel::Sender, } impl ClientData for WeftClientState { fn initialized(&self, _client_id: ClientId) {} - fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) {} + fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) { + if let Some(binding) = &self.session { + let _ = binding.disconnected.send(binding.session_id); + } + } } /// Accumulated state for a multi-touch swipe gesture in progress. @@ -241,8 +257,16 @@ impl XdgShellHandler for WeftCompositorState { fn new_toplevel(&mut self, surface: ToplevelSurface) { surface.send_configure(); + let session = surface.wl_surface().client().and_then(|client| { + client + .get_data::() + .and_then(|data| data.session.as_ref().map(|s| s.session_id)) + }); let window = Window::new_wayland_window(surface); self.space.map_element(window, (0, 0), false); + if let (Some(session_id), Some(ipc)) = (session, self.appd_ipc.as_mut()) { + ipc.surface_created(session_id); + } } fn new_popup(&mut self, surface: PopupSurface, positioner: PositionerState) { @@ -466,7 +490,7 @@ impl GlobalDispatch for WeftCompositorState { impl Dispatch for WeftCompositorState { fn request( state: &mut Self, - _client: &Client, + client: &Client, _resource: &ZweftShellManagerV1, request: zweft_shell_manager_v1::Request, _data: &(), @@ -486,6 +510,14 @@ impl Dispatch for WeftCompositorState { width, height, } => { + // An application session's windows carry the identity appd + // bound to its connection, and only the trusted shell, which + // connects through the display socket, may take shell roles. + let session = client + .get_data::() + .and_then(|data| data.session.as_ref()); + let shell_role = matches!(role.as_str(), "panel" | "overlay"); + let app_id = session.map_or(app_id, |s| s.app_id.clone()); let is_panel = role == "panel"; let window = data_init.init( id, @@ -497,6 +529,18 @@ impl Dispatch for WeftCompositorState { closed: std::sync::atomic::AtomicBool::new(false), }, ); + if let Some(session) = session + && shell_role + { + window.post_error( + crate::protocols::server::zweft_shell_window_v1::Error::RoleNotPermitted, + format!( + "session {} ({}) may not create shell windows", + session.session_id, session.app_id + ), + ); + return; + } if is_panel { let (ox, oy, ow, oh) = state .space diff --git a/crates/weft-ipc-types/src/lib.rs b/crates/weft-ipc-types/src/lib.rs index 0590c77..c7d74f8 100644 --- a/crates/weft-ipc-types/src/lib.rs +++ b/crates/weft-ipc-types/src/lib.rs @@ -10,11 +10,16 @@ pub const MAX_FRAME_LEN: usize = 4 * 1024 * 1024; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum AppdToCompositor { - AppSurfaceCreated { - app_id: String, + /// Hands the compositor its end of the Wayland connection weft-appd + /// created for the session's app shell. The frame is sent with exactly + /// one file descriptor attached (SCM_RIGHTS): the compositor end of that + /// connection. Holding the connection is the session's identity; no + /// client-supplied ID or process ID is trusted for it. + AttachClient { session_id: u64, - pid: u32, + app_id: String, }, + /// The session ended; the compositor closes its client and windows. AppSurfaceDestroyed { session_id: u64, }, @@ -27,8 +32,10 @@ pub enum AppdToCompositor { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum CompositorToAppd { + /// The session's client mapped its first toplevel. SurfaceReady { session_id: u64 }, - ClientDisconnected { pid: u32 }, + /// The session's Wayland client disconnected. + ClientDisconnected { session_id: u64 }, } /// Encode a message as a length-prefixed MessagePack frame. @@ -111,23 +118,17 @@ mod tests { use super::*; #[test] - fn roundtrip_appd_to_compositor_surface_created() { - let msg = AppdToCompositor::AppSurfaceCreated { + fn roundtrip_appd_to_compositor_attach_client() { + let msg = AppdToCompositor::AttachClient { app_id: "com.example.app".into(), session_id: 42, - pid: 1234, }; let frame = frame_encode(&msg).unwrap(); let decoded: AppdToCompositor = frame_decode(&frame).unwrap(); match decoded { - AppdToCompositor::AppSurfaceCreated { - app_id, - session_id, - pid, - } => { + AppdToCompositor::AttachClient { app_id, session_id } => { assert_eq!(app_id, "com.example.app"); assert_eq!(session_id, 42); - assert_eq!(pid, 1234); } _ => panic!("unexpected variant"), } @@ -168,11 +169,11 @@ mod tests { #[test] fn roundtrip_compositor_to_appd_disconnected() { - let msg = CompositorToAppd::ClientDisconnected { pid: 5678 }; + let msg = CompositorToAppd::ClientDisconnected { session_id: 5678 }; let frame = frame_encode(&msg).unwrap(); let decoded: CompositorToAppd = frame_decode(&frame).unwrap(); match decoded { - CompositorToAppd::ClientDisconnected { pid } => assert_eq!(pid, 5678), + CompositorToAppd::ClientDisconnected { session_id } => assert_eq!(session_id, 5678), _ => panic!("unexpected variant"), } } diff --git a/docs/architecture.md b/docs/architecture.md index ee92a31..33f0f8a 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell @@ -16,7 +16,7 @@ System UI host. Renders one WebView pointing at `system-ui.html` using the embed ### weft-app-shell -Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signals READY. Takes ``, `` and `--ui `, the UI document weft-appd resolved from the package; it never looks for the package itself. Injects the `weftIpc` WebSocket bridge into the page. The bridge authenticates with the per-session token from `WEFT_BRIDGE_TOKEN` and can only exchange messages with its own session. It is installed only in top-level documents inside the application's UI directory, and top-level navigation outside that directory is denied; framed documents never receive the bridge. Every resource the page loads (subresources, `fetch()`, XHR, workers and WebSocket handshakes) is checked by the shell: the page may load files inside its UI directory, also after symbolic links are resolved, inline `data:` and `blob:` resources, `about:blank` and the session bridge; any other local file or network destination, and every redirect, fails with a network error. Loads Servo does not attribute to the webview, such as `navigator.sendBeacon()`, are all refused. Network access belongs to the Wasm component, under its fetch grants. Servo's privileged `navigator.servo` interface, which can change engine preferences, is not exposed to `about:blank` or `about:srcdoc` documents (a fork patch; see `crates/weft-servo-shell/SERVO_PIN.md`). Registers with the compositor as window type `application`. Exits when the appd session ends. +Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signals READY. Takes ``, `` and `--ui `, the UI document weft-appd resolved from the package; it never looks for the package itself. Injects the `weftIpc` WebSocket bridge into the page. The bridge authenticates with the per-session token from `WEFT_BRIDGE_TOKEN` and can only exchange messages with its own session. It is installed only in top-level documents inside the application's UI directory, and top-level navigation outside that directory is denied; framed documents never receive the bridge. Every resource the page loads (subresources, `fetch()`, XHR, workers and WebSocket handshakes) is checked by the shell: the page may load files inside its UI directory, also after symbolic links are resolved, inline `data:` and `blob:` resources, `about:blank` and the session bridge; any other local file or network destination, and every redirect, fails with a network error. Loads Servo does not attribute to the webview, such as `navigator.sendBeacon()`, are all refused. Network access belongs to the Wasm component, under its fetch grants. Servo's privileged `navigator.servo` interface, which can change engine preferences, is not exposed to `about:blank` or `about:srcdoc` documents (a fork patch; see `crates/weft-servo-shell/SERVO_PIN.md`). Registers with the compositor as window type `application`, over the one Wayland connection weft-appd created for its session and passed as standard input (`WAYLAND_SOCKET=0`). Exits when the appd session ends. ### weft-appd diff --git a/docs/security.md b/docs/security.md index 09a6b4a..d17bd78 100644 --- a/docs/security.md +++ b/docs/security.md @@ -30,7 +30,9 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF ## Wayland Surface Isolation -Each app registers its surface with the compositor via `zweft_shell_manager_v1`. The compositor enforces that each surface belongs to the session that created it. The app cannot render outside its assigned surface slot. +Each app shell has exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd. The compositor accepts one appd connection at a time. + +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user can open. ## JavaScript Engine (SpiderMonkey) diff --git a/protocol/weft-shell-unstable-v1.xml b/protocol/weft-shell-unstable-v1.xml index 339eb11..f208b62 100644 --- a/protocol/weft-shell-unstable-v1.xml +++ b/protocol/weft-shell-unstable-v1.xml @@ -65,6 +65,11 @@ object identity; no separate window_id is exchanged. The compositor will emit configure on the new object to communicate the effective initial geometry and state. + + For a client that the compositor associated with an application + session, the session's application identifier replaces the app_id + argument, and the panel and overlay roles are refused with the + role_not_permitted error on the new window. @@ -114,6 +119,8 @@ + diff --git a/tests/frame/check_counter.py b/tests/frame/check_counter.py index aa8ecdb..ed4a8e1 100644 --- a/tests/frame/check_counter.py +++ b/tests/frame/check_counter.py @@ -169,9 +169,16 @@ def run(args, desktop, xdotool): appd = AppdClient(port, token) appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0}) try: - appd.wait_for(lambda m: m.get("type") == "APP_READY", STARTUP_TIMEOUT) + ready = appd.wait_for(lambda m: m.get("type") == "APP_READY", STARTUP_TIMEOUT) except TimeoutError: raise AssertionError("no APP_READY") + session_id = ready["session_id"] + + # The app's only Wayland connection is the one appd handed the + # compositor for this session. + bound = f"client bound to session session_id={session_id} app_id={APP_ID}" + if bound not in desktop.log_text("compositor"): + raise AssertionError(f"the compositor did not bind the app's client: {bound!r}") def capture_card(): deadline = time.monotonic() + 10 @@ -213,6 +220,15 @@ def run(args, desktop, xdotool): if states[3] != states[1]: raise AssertionError("count after ArrowDown does not match the count after one ArrowUp") + # Ending the session closes its client in the compositor. + appd.send({"type": "TERMINATE_APP", "session_id": session_id}) + deadline = time.monotonic() + 15 + closed = f"closing the client of an ended session session_id={session_id}" + while closed not in desktop.log_text("compositor"): + if time.monotonic() > deadline: + raise AssertionError(f"the compositor did not close the session's client: {closed!r}") + time.sleep(0.3) + def subprocess_run(xdotool, display, arguments): import subprocess From 4c778330a37d63279f5cb5d31af617d5cb2c0500 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:15:01 +0000 Subject: [PATCH 16/61] fix(compositor): keep shell-level interfaces from sessions and harden the appd connection Session clients could still create layer-shell surfaces, including full-screen overlays with exclusive keyboard input, and bind the input method, which bypassed the refusal of shell roles. Both globals are now hidden from clients bound to a session. The compositor IPC connection is tracked by its read source, so a write error can no longer let a second peer feed the same buffers; a failed write shuts the connection down. An undecodable or oversized frame ends the connection, since descriptors could otherwise pair with the wrong messages, and the descriptor limit counts only descriptors no message claims, so a burst of launches is not taken for abuse. When appd's connection ends, the compositor closes the clients of the sessions it attached. weft-appd notices when the compositor closes the connection, resends a message whose write failed after reconnecting, and refuses a launch while the compositor is not connected instead of starting an app shell that can never connect. --- crates/weft-appd/src/compositor_client.rs | 67 +++++++++-- crates/weft-appd/src/main.rs | 84 +++++++++++++- crates/weft-appd/src/runtime.rs | 3 + crates/weft-compositor/src/appd_ipc.rs | 128 ++++++++++++++++++---- crates/weft-compositor/src/state.rs | 15 ++- docs/security.md | 4 +- 6 files changed, 265 insertions(+), 36 deletions(-) diff --git a/crates/weft-appd/src/compositor_client.rs b/crates/weft-appd/src/compositor_client.rs index 01bb1ac..84d6b81 100644 --- a/crates/weft-appd/src/compositor_client.rs +++ b/crates/weft-appd/src/compositor_client.rs @@ -1,5 +1,7 @@ use std::os::fd::OwnedFd; use std::path::PathBuf; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; use tokio::io::AsyncWriteExt; use tokio::sync::mpsc; @@ -19,7 +21,27 @@ impl From for Outbound { } } -pub type CompositorSender = mpsc::Sender; +/// Queues messages for the compositor and tells whether it is connected. +#[derive(Clone)] +pub struct CompositorSender { + tx: mpsc::Sender, + connected: Arc, +} + +impl CompositorSender { + pub fn new(tx: mpsc::Sender, connected: Arc) -> Self { + Self { tx, connected } + } + + /// Whether the compositor IPC connection is currently up. + pub fn is_connected(&self) -> bool { + self.connected.load(Ordering::Acquire) + } + + pub fn try_send(&self, outbound: Outbound) -> Result<(), Outbound> { + self.tx.try_send(outbound).map_err(|e| e.into_inner()) + } +} /// Resolve the compositor IPC socket path. /// @@ -37,16 +59,24 @@ pub fn socket_path() -> Option { /// Spawn the compositor IPC client task and return a sender for outbound messages. /// -/// The task connects to `socket_path`, retrying every 2 s on failure. If the -/// connection drops while sending, it waits 500 ms then reconnects. Incoming +/// The task connects to `socket_path`, retrying every 2 s on failure. When +/// the connection drops, whether noticed by a failed write or by the +/// compositor closing it, it waits 500 ms and reconnects; a message whose +/// write failed is sent again on the new connection. Incoming /// `CompositorToAppd` frames are decoded and logged. pub fn spawn(socket_path: PathBuf) -> CompositorSender { let (tx, rx) = mpsc::channel::(32); - tokio::spawn(run_client(socket_path, rx)); - tx + let connected = Arc::new(AtomicBool::new(false)); + tokio::spawn(run_client(socket_path, rx, connected.clone())); + CompositorSender::new(tx, connected) } -async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver) { +async fn run_client( + socket_path: PathBuf, + mut rx: mpsc::Receiver, + connected: Arc, +) { + let mut unsent: Option = None; loop { let stream = loop { match tokio::net::UnixStream::connect(&socket_path).await { @@ -61,12 +91,22 @@ async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver) { }; let (read_half, mut write_half) = stream.into_split(); - - tokio::spawn(read_unix_incoming(read_half)); + let mut reader = tokio::spawn(read_unix_incoming(read_half)); + connected.store(true, Ordering::Release); loop { - let Some(outbound) = rx.recv().await else { - return; + let outbound = match unsent.take() { + Some(outbound) => outbound, + None => tokio::select! { + next = rx.recv() => match next { + Some(outbound) => outbound, + None => return, + }, + _ = &mut reader => { + tracing::warn!("compositor closed the IPC connection; reconnecting"); + break; + } + }, }; let frame = match frame_encode(&outbound.msg) { Ok(frame) => frame, @@ -75,14 +115,17 @@ async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver) { continue; } }; - if send_frame(&mut write_half, &frame, outbound.fd) + if send_frame(&mut write_half, &frame, outbound.fd.as_ref()) .await .is_err() { tracing::warn!("compositor IPC write failed; reconnecting"); + unsent = Some(outbound); break; } } + connected.store(false, Ordering::Release); + reader.abort(); tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; } @@ -93,7 +136,7 @@ async fn run_client(socket_path: PathBuf, mut rx: mpsc::Receiver) { async fn send_frame( stream: &mut tokio::net::unix::OwnedWriteHalf, frame: &[u8], - fd: Option, + fd: Option<&OwnedFd>, ) -> std::io::Result<()> { use rustix::net::{SendAncillaryBuffer, SendAncillaryMessage, SendFlags, sendmsg}; use std::mem::MaybeUninit; diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index a8c75ac..e367f39 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -1241,6 +1241,85 @@ mod tests { assert!(!session_ipc_socket_path(session_id).unwrap().exists()); } + #[tokio::test(flavor = "current_thread")] + async fn without_a_connected_compositor_the_app_shell_is_not_started() { + use std::os::unix::fs::PermissionsExt; + let _env = env_lock().lock().await; + use_test_runtime_dir(); + let dir = std::env::temp_dir().join(format!("weft_test_nocomp_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let app = dir.join("store/org.example.nocomp"); + write_test_package(&app, "org.example.nocomp", ""); + // Each child records that it started. + let log = dir.join("started.log"); + let child = dir.join("child.sh"); + std::fs::write( + &child, + format!( + "#!/bin/sh\necho started >> '{}'\necho READY $WEFT_READY_TOKEN\nexec sleep 1\n", + log.display() + ), + ) + .unwrap(); + std::fs::set_permissions(&child, std::fs::Permissions::from_mode(0o755)).unwrap(); + let vars = [ + ("WEFT_RUNTIME_BIN", child.clone().into_os_string()), + ("WEFT_APP_SHELL_BIN", child.clone().into_os_string()), + ("WEFT_DISABLE_CGROUP", "1".into()), + ("WEFT_APP_STORE", dir.join("store").into_os_string()), + ]; + let prior: Vec<_> = vars + .iter() + .map(|(k, _)| (*k, std::env::var_os(k))) + .collect(); + for (key, value) in &vars { + // SAFETY: env_lock is held on a current_thread runtime. + unsafe { std::env::set_var(key, value) }; + } + + let registry = make_registry(); + let (tx, mut compositor) = tokio::sync::mpsc::channel(8); + registry.lock().await.compositor_tx = Some(compositor_client::CompositorSender::new( + tx, + std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)), + )); + let mut rx = registry.lock().await.subscribe(); + let _ = dispatch( + Request::LaunchApp { + app_id: "org.example.nocomp".into(), + surface_id: 0, + }, + ®istry, + ) + .await; + let stopped = tokio::time::timeout(std::time::Duration::from_secs(10), async { + while !matches!( + rx.recv().await, + Ok(Response::AppState { + state: AppStateKind::Stopped, + .. + }) + ) {} + }) + .await; + let started = std::fs::read_to_string(&log).unwrap_or_default(); + + for (key, value) in prior { + // SAFETY: as above. + unsafe { + match value { + Some(v) => std::env::set_var(key, v), + None => std::env::remove_var(key), + } + } + } + let _ = std::fs::remove_dir_all(&dir); + assert!(stopped.is_ok(), "session did not stop"); + // Only the runtime started; no app shell, and nothing was queued. + assert_eq!(started.lines().count(), 1, "{started}"); + assert!(compositor.try_recv().is_err()); + } + #[tokio::test(flavor = "current_thread")] async fn the_app_shell_connects_only_through_its_session_connection() { use std::io::BufRead; @@ -1280,7 +1359,10 @@ mod tests { let registry = make_registry(); let (tx, mut compositor) = tokio::sync::mpsc::channel(8); - registry.lock().await.compositor_tx = Some(tx); + registry.lock().await.compositor_tx = Some(compositor_client::CompositorSender::new( + tx, + std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)), + )); let mut rx = registry.lock().await.subscribe(); let ack = dispatch( Request::LaunchApp { diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 1106155..95e8fbd 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -464,6 +464,9 @@ fn attach_client( session_id: u64, app_id: &str, ) -> Result { + if !tx.is_connected() { + return Err("weft-compositor is not connected, so the app cannot show a window".to_owned()); + } let (shell, compositor) = std::os::unix::net::UnixStream::pair() .map_err(|e| format!("cannot create the app's Wayland connection: {e}"))?; tx.try_send(crate::compositor_client::Outbound { diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index d1a5532..2f129d2 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -31,6 +31,9 @@ pub struct WeftAppdIpc { pub socket_path: PathBuf, read_buf: Vec, write_stream: Option, + /// Whether an appd connection's read source is registered. Only one + /// connection is served at a time. + connected: bool, /// Descriptors received with appd messages, in arrival order. Each /// `AttachClient` frame is sent with exactly one, attached to its first /// bytes, so it has arrived by the time the frame is decoded. @@ -50,6 +53,7 @@ impl WeftAppdIpc { socket_path, read_buf: Vec::new(), write_stream: None, + connected: false, fds: VecDeque::new(), sessions: HashMap::new(), reported: HashSet::new(), @@ -65,6 +69,10 @@ impl WeftAppdIpc { match frame_encode(msg) { Ok(frame) => { if stream.write_all(&frame).is_err() { + // A partly written frame breaks the stream's framing: + // end the connection, whose read side then cleans up. + tracing::warn!("compositor IPC write failed; closing the appd connection"); + let _ = stream.shutdown(std::net::Shutdown::Both); self.write_stream = None; } } @@ -79,7 +87,10 @@ impl WeftAppdIpc { } } - fn try_decode_frames(&mut self) -> Vec { + /// Decodes the complete frames in the buffer, and reports whether the + /// stream can no longer be trusted: after an oversized or undecodable + /// frame, descriptors could be paired with the wrong messages. + fn try_decode_frames(&mut self) -> (Vec, bool) { let mut out = Vec::new(); loop { if self.read_buf.len() < 4 { @@ -93,9 +104,7 @@ impl WeftAppdIpc { ]) as usize; if declared_len > MAX_FRAME_LEN { tracing::warn!(declared_len, "appd IPC frame too large; disconnecting"); - self.write_stream = None; - self.read_buf.clear(); - break; + return (out, true); } if self.read_buf.len() < 4 + declared_len { break; @@ -103,11 +112,14 @@ impl WeftAppdIpc { let frame_end = 4 + declared_len; match frame_decode::(&self.read_buf[..frame_end]) { Ok(msg) => out.push(msg), - Err(e) => tracing::warn!(?e, "appd IPC frame decode error"), + Err(e) => { + tracing::warn!(?e, "appd IPC frame decode error; disconnecting"); + return (out, true); + } } self.read_buf.drain(..frame_end); } - out + (out, false) } /// Reads what is available, keeping descriptors that arrive with the @@ -146,18 +158,29 @@ impl WeftAppdIpc { } } } - if self.fds.len() > MAX_PENDING_FDS { + let (messages, broken) = self.try_decode_frames(); + eof |= broken; + let claimed = messages + .iter() + .filter(|m| matches!(m, AppdToCompositor::AttachClient { .. })) + .count(); + if self.fds.len().saturating_sub(claimed) > MAX_PENDING_FDS { tracing::warn!("appd sent descriptors without messages; disconnecting"); eof = true; } - let messages = self.try_decode_frames(); - if eof { - self.write_stream = None; - self.read_buf.clear(); - self.fds.clear(); - } (messages, eof) } + + /// Forgets the appd connection once its read source is gone. The + /// sessions it started are closed: they belong to that appd. + fn disconnect(&mut self) -> Vec { + self.connected = false; + self.write_stream = None; + self.read_buf.clear(); + self.fds.clear(); + self.reported.clear(); + self.sessions.drain().map(|(_, client)| client).collect() + } } #[cfg(unix)] @@ -299,11 +322,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { Ok((stream, _addr)) => { // One appd at a time: a second connection while // one is live is refused rather than replacing it. - if state - .appd_ipc - .as_ref() - .is_some_and(|ipc| ipc.write_stream.is_some()) - { + if state.appd_ipc.as_ref().is_some_and(|ipc| ipc.connected) { tracing::warn!( "second compositor IPC connection refused; weft-appd is connected" ); @@ -319,6 +338,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { }; if let Some(ipc) = &mut state.appd_ipc { ipc.write_stream = Some(write_clone); + ipc.connected = true; ipc.read_buf.clear(); ipc.fds.clear(); } @@ -338,8 +358,20 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { } if eof { tracing::info!( - "weft-appd disconnected from compositor IPC" + "weft-appd disconnected from compositor IPC; \ + closing its sessions' clients" ); + let orphans = state + .appd_ipc + .as_mut() + .map(WeftAppdIpc::disconnect) + .unwrap_or_default(); + for client in orphans { + state.display_handle.backend_handle().kill_client( + client, + DisconnectReason::ConnectionClosed, + ); + } Ok(PostAction::Remove) } else { Ok(PostAction::Continue) @@ -422,6 +454,64 @@ mod tests { let (messages, eof) = ipc.on_read(&compositor); assert!(messages.is_empty()); assert!(eof); + ipc.disconnect(); assert!(ipc.fds.is_empty()); } + + #[test] + fn a_burst_of_attachments_is_not_mistaken_for_abuse() { + let (appd, compositor) = UnixStream::pair().unwrap(); + compositor.set_nonblocking(true).unwrap(); + let mut keep = Vec::new(); + for session_id in 0..(MAX_PENDING_FDS as u64 + 4) { + let (shell, client) = UnixStream::pair().unwrap(); + let frame = frame_encode(&AppdToCompositor::AttachClient { + session_id, + app_id: "org.weft.demo.counter".into(), + }) + .unwrap(); + send_with_fd(&appd, &frame, &client); + // A plain frame between attachments keeps its place. + let plain = frame_encode(&AppdToCompositor::AppFocusRequest { session_id }).unwrap(); + use std::io::Write; + (&appd).write_all(&plain).unwrap(); + keep.push(shell); + } + let mut ipc = WeftAppdIpc::new(PathBuf::from("/nonexistent")); + let mut messages = Vec::new(); + let mut connections = 0; + loop { + let (batch, eof) = ipc.on_read(&compositor); + assert!(!eof, "a legitimate burst ended the connection"); + if batch.is_empty() { + break; + } + // As the handler does, each attachment takes its connection. + for message in &batch { + if matches!(message, AppdToCompositor::AttachClient { .. }) { + ipc.fds + .pop_front() + .expect("an attachment without its connection"); + connections += 1; + } + } + messages.extend(batch); + } + assert_eq!(connections, MAX_PENDING_FDS + 4); + assert_eq!(messages.len(), 2 * (MAX_PENDING_FDS + 4)); + assert!(ipc.fds.is_empty()); + } + + #[test] + fn an_undecodable_frame_ends_the_connection() { + let (appd, compositor) = UnixStream::pair().unwrap(); + compositor.set_nonblocking(true).unwrap(); + use std::io::Write; + let mut frame = 3u32.to_le_bytes().to_vec(); + frame.extend_from_slice(&[0xc1, 0xc1, 0xc1]); + (&appd).write_all(&frame).unwrap(); + let mut ipc = WeftAppdIpc::new(PathBuf::from("/nonexistent")); + let (_, eof) = ipc.on_read(&compositor); + assert!(eof); + } } diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index b0dac77..f02ff23 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -67,6 +67,14 @@ pub struct SessionBinding { pub disconnected: smithay::reexports::calloop::channel::Sender, } +/// Whether `client` connected through the display socket rather than +/// through a connection weft-appd created for an application session. +pub fn outside_sessions(client: &Client) -> bool { + client + .get_data::() + .is_none_or(|data| data.session.is_none()) +} + impl ClientData for WeftClientState { fn initialized(&self, _client_id: ClientId) {} fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) { @@ -140,7 +148,10 @@ impl WeftCompositorState { ) -> Self { let compositor_state = CompositorState::new::(&display_handle); let xdg_shell_state = XdgShellState::new::(&display_handle); - let layer_shell_state = WlrLayerShellState::new::(&display_handle); + // Layer surfaces and input methods are shell-level: an application + // session's client is not offered them. + let layer_shell_state = + WlrLayerShellState::new_with_filter::(&display_handle, outside_sessions); let shm_state = ShmState::new::(&display_handle, vec![]); let dmabuf_state = DmabufState::new(); let output_manager_state = OutputManagerState::new_with_xdg_output::(&display_handle); @@ -148,7 +159,7 @@ impl WeftCompositorState { let presentation_state = PresentationState::new::(&display_handle, 1); let text_input_state = TextInputManagerState::new::(&display_handle); let input_method_state = - InputMethodManagerState::new::(&display_handle, |_client| true); + InputMethodManagerState::new::(&display_handle, outside_sessions); let pointer_constraints_state = PointerConstraintsState::new::(&display_handle); let cursor_shape_state = CursorShapeManagerState::new::(&display_handle); let weft_shell_state = WeftShellState::new::(&display_handle); diff --git a/docs/security.md b/docs/security.md index d17bd78..3c4310c 100644 --- a/docs/security.md +++ b/docs/security.md @@ -30,9 +30,9 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF ## Wayland Surface Isolation -Each app shell has exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd. The compositor accepts one appd connection at a time. +Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused while appd is not connected to the compositor, rather than starting an app that could never show a window. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. ## JavaScript Engine (SpiderMonkey) From d6a2f94a1da60bd7fcce18ad0dfe8096b9aab39e Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:20:29 +0000 Subject: [PATCH 17/61] fix(compositor): buffer appd-bound frames and ignore stale session disconnects A full socket buffer toward a stalled appd closed the connection and with it every app. Frames for appd are now queued whole and flushed on each send and read, and only a backlog beyond the frame limit or a write error ends the connection. A disconnect report is applied only when it comes from the session's current client, so a client closed with an earlier appd cannot remove the mapping of a new session that reused its ID. A failure to watch a new appd connection no longer leaves the compositor refusing every later one. weft-appd refuses a launch with 503 before anything starts while the compositor is not connected, and waits up to 10 s for the connection before restoring the previous session. --- crates/weft-appd/src/main.rs | 45 +++++---- crates/weft-compositor/src/appd_ipc.rs | 123 ++++++++++++++++++++++--- crates/weft-compositor/src/state.rs | 6 +- docs/security.md | 2 +- 4 files changed, 142 insertions(+), 34 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index e367f39..92a4c72 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -218,6 +218,14 @@ async fn run() -> anyhow::Result<()> { } if let Some(app_ids) = load_session() { + // Apps need the compositor connection; give it a moment to come up. + let compositor = registry.lock().await.compositor_tx.clone(); + if let Some(compositor) = compositor { + let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10); + while !compositor.is_connected() && tokio::time::Instant::now() < deadline { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + } + } tracing::info!(count = app_ids.len(), "restoring previous session"); for app_id in app_ids { let _ = dispatch( @@ -438,8 +446,20 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { // The package is resolved and its capabilities granted before a // session exists; a package this host cannot satisfy is refused, // not started. + let compositor_down = registry + .lock() + .await + .compositor_tx + .as_ref() + .is_some_and(|tx| !tx.is_connected()); let launch = if !weft_ipc_types::package::is_valid_app_id(&app_id) { Err(grants::Refusal::new(400, "invalid app ID")) + } else if compositor_down { + // An app started now could never show a window. + Err(grants::Refusal::new( + 503, + "weft-compositor is not connected", + )) } else if std::env::var("WEFT_RUNTIME_BIN").is_ok() { resolve_launch(app_id.clone()).await.map(Some) } else { @@ -1242,7 +1262,7 @@ mod tests { } #[tokio::test(flavor = "current_thread")] - async fn without_a_connected_compositor_the_app_shell_is_not_started() { + async fn without_a_connected_compositor_nothing_is_started() { use std::os::unix::fs::PermissionsExt; let _env = env_lock().lock().await; use_test_runtime_dir(); @@ -1283,8 +1303,7 @@ mod tests { tx, std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)), )); - let mut rx = registry.lock().await.subscribe(); - let _ = dispatch( + let refused = dispatch( Request::LaunchApp { app_id: "org.example.nocomp".into(), surface_id: 0, @@ -1292,16 +1311,7 @@ mod tests { ®istry, ) .await; - let stopped = tokio::time::timeout(std::time::Duration::from_secs(10), async { - while !matches!( - rx.recv().await, - Ok(Response::AppState { - state: AppStateKind::Stopped, - .. - }) - ) {} - }) - .await; + tokio::time::sleep(std::time::Duration::from_millis(500)).await; let started = std::fs::read_to_string(&log).unwrap_or_default(); for (key, value) in prior { @@ -1314,9 +1324,12 @@ mod tests { } } let _ = std::fs::remove_dir_all(&dir); - assert!(stopped.is_ok(), "session did not stop"); - // Only the runtime started; no app shell, and nothing was queued. - assert_eq!(started.lines().count(), 1, "{started}"); + assert!( + matches!(refused, Response::Error { code: 503, .. }), + "{refused:?}" + ); + // Nothing started and nothing was queued for the compositor. + assert_eq!(started, ""); assert!(compositor.try_recv().is_err()); } diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index 2f129d2..bcf658c 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -43,7 +43,10 @@ pub struct WeftAppdIpc { /// Sessions whose first toplevel was reported to appd. reported: HashSet, /// Where clients report their session's disconnection. - disconnected: Option>, + disconnected: Option>, + /// Bytes for appd that did not fit in the socket buffer, sent before + /// anything else so frames stay whole. + out_buf: Vec, } #[cfg(unix)] @@ -58,28 +61,59 @@ impl WeftAppdIpc { sessions: HashMap::new(), reported: HashSet::new(), disconnected: None, + out_buf: Vec::new(), } } pub fn send(&mut self, msg: &CompositorToAppd) { - use std::io::Write; - let Some(stream) = &mut self.write_stream else { - return; - }; match frame_encode(msg) { Ok(frame) => { - if stream.write_all(&frame).is_err() { - // A partly written frame breaks the stream's framing: - // end the connection, whose read side then cleans up. - tracing::warn!("compositor IPC write failed; closing the appd connection"); - let _ = stream.shutdown(std::net::Shutdown::Both); - self.write_stream = None; + if self.write_stream.is_some() { + self.out_buf.extend_from_slice(&frame); + self.flush(); } } Err(e) => tracing::warn!(?e, "failed to encode compositor IPC message"), } } + /// Writes what appd's socket accepts now; the rest waits for the next + /// send or read. A backlog appd never drains, or a write error, ends the + /// connection, whose read side then cleans up. + fn flush(&mut self) { + use std::io::Write; + let Some(stream) = &mut self.write_stream else { + self.out_buf.clear(); + return; + }; + while !self.out_buf.is_empty() { + match stream.write(&self.out_buf) { + Ok(0) => break, + Ok(n) => { + self.out_buf.drain(..n); + } + Err(e) if e.kind() == io::ErrorKind::WouldBlock => break, + Err(e) if e.kind() == io::ErrorKind::Interrupted => {} + Err(e) => { + tracing::warn!( + ?e, + "compositor IPC write failed; closing the appd connection" + ); + let _ = stream.shutdown(std::net::Shutdown::Both); + self.write_stream = None; + self.out_buf.clear(); + return; + } + } + } + if self.out_buf.len() > MAX_FRAME_LEN { + tracing::warn!("weft-appd is not reading compositor IPC; closing the connection"); + let _ = stream.shutdown(std::net::Shutdown::Both); + self.write_stream = None; + self.out_buf.clear(); + } + } + /// Reports a session's first toplevel to appd. pub fn surface_created(&mut self, session_id: u64) { if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) { @@ -176,6 +210,7 @@ impl WeftAppdIpc { fn disconnect(&mut self) -> Vec { self.connected = false; self.write_stream = None; + self.out_buf.clear(); self.read_buf.clear(); self.fds.clear(); self.reported.clear(); @@ -293,15 +328,18 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { tracing::info!(path = %socket_path.display(), "compositor IPC socket open"); // Clients bound to a session report their disconnection here. - let (sender, receiver) = channel::channel::(); + let (sender, receiver) = channel::channel::<(u64, ClientId)>(); if let Some(ipc) = state.appd_ipc.as_mut() { ipc.disconnected = Some(sender); } state .loop_handle .insert_source(receiver, |event, _, state| { - if let channel::Event::Msg(session_id) = event + // Only the session's current client: a client closed when its + // appd went away may report after a new appd reused the ID. + if let channel::Event::Msg((session_id, client)) = event && let Some(ipc) = state.appd_ipc.as_mut() + && ipc.sessions.get(&session_id) == Some(&client) { ipc.sessions.remove(&session_id); ipc.reported.remove(&session_id); @@ -343,7 +381,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { ipc.fds.clear(); } stream.set_nonblocking(true).ok(); - let _ = handle.insert_source( + let registered = handle.insert_source( Generic::new(stream, Interest::READ, Mode::Edge), |_, stream, state| { // Safety: calloop wraps the fd in NoIoDrop to prevent @@ -356,6 +394,9 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { for msg in messages { handle_message(state, msg); } + if let Some(ipc) = state.appd_ipc.as_mut() { + ipc.flush(); + } if eof { tracing::info!( "weft-appd disconnected from compositor IPC; \ @@ -378,6 +419,12 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { } }, ); + if let Err(e) = registered { + tracing::warn!(?e, "cannot watch the appd IPC connection"); + if let Some(ipc) = state.appd_ipc.as_mut() { + ipc.disconnect(); + } + } } Err(e) if e.kind() == io::ErrorKind::WouldBlock => break, Err(e) => tracing::warn!(?e, "accept error on compositor IPC socket"), @@ -502,6 +549,54 @@ mod tests { assert!(ipc.fds.is_empty()); } + #[test] + fn a_slow_appd_gets_whole_frames_later_instead_of_losing_its_apps() { + let (appd, compositor) = UnixStream::pair().unwrap(); + compositor.set_nonblocking(true).unwrap(); + let mut ipc = WeftAppdIpc::new(PathBuf::from("/nonexistent")); + ipc.write_stream = Some(compositor); + ipc.connected = true; + // More than the socket buffer holds while appd is not reading. + let count = 20_000u64; + for session_id in 0..count { + ipc.send(&CompositorToAppd::SurfaceReady { session_id }); + } + assert!( + ipc.write_stream.is_some(), + "a full buffer closed the connection" + ); + assert!(!ipc.out_buf.is_empty()); + + // appd reads; every flush moves more, and every frame arrives whole. + appd.set_nonblocking(true).unwrap(); + let mut received = Vec::new(); + let mut buf = [0u8; 65536]; + loop { + ipc.flush(); + use std::io::Read; + match (&appd).read(&mut buf) { + Ok(n) => received.extend_from_slice(&buf[..n]), + Err(e) if e.kind() == io::ErrorKind::WouldBlock => { + if ipc.out_buf.is_empty() { + break; + } + } + Err(e) => panic!("{e}"), + } + } + let mut frames = 0u64; + while !received.is_empty() { + let len = u32::from_le_bytes(received[..4].try_into().unwrap()) as usize; + let frame: CompositorToAppd = frame_decode(&received[..4 + len]).unwrap(); + assert!( + matches!(frame, CompositorToAppd::SurfaceReady { session_id } if session_id == frames) + ); + received.drain(..4 + len); + frames += 1; + } + assert_eq!(frames, count); + } + #[test] fn an_undecodable_frame_ends_the_connection() { let (appd, compositor) = UnixStream::pair().unwrap(); diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index f02ff23..8c6a303 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -64,7 +64,7 @@ pub struct SessionBinding { pub session_id: u64, pub app_id: String, /// Reports the session's disconnection to the event loop. - pub disconnected: smithay::reexports::calloop::channel::Sender, + pub disconnected: smithay::reexports::calloop::channel::Sender<(u64, ClientId)>, } /// Whether `client` connected through the display socket rather than @@ -77,9 +77,9 @@ pub fn outside_sessions(client: &Client) -> bool { impl ClientData for WeftClientState { fn initialized(&self, _client_id: ClientId) {} - fn disconnected(&self, _client_id: ClientId, _reason: DisconnectReason) { + fn disconnected(&self, client_id: ClientId, _reason: DisconnectReason) { if let Some(binding) = &self.session { - let _ = binding.disconnected.send(binding.session_id); + let _ = binding.disconnected.send((binding.session_id, client_id)); } } } diff --git a/docs/security.md b/docs/security.md index 3c4310c..e16c9b0 100644 --- a/docs/security.md +++ b/docs/security.md @@ -30,7 +30,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF ## Wayland Surface Isolation -Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused while appd is not connected to the compositor, rather than starting an app that could never show a window. +Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, before any process starts, rather than starting an app that could never show a window. Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. From 2e27adc4f65b1801702c46c89269ae90356f6552 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:23:41 +0000 Subject: [PATCH 18/61] fix(compositor): retry queued appd frames until sent and restore sessions in the background Frames queued for a slow appd were sent only on later traffic, so the end of a burst could wait indefinitely. A timer now retries the flush every 20 ms, armed only while frames are queued. weft-appd restores the previous session in its own task, so waiting for the compositor no longer delays clients or shutdown, and the security docs describe the launch refusal and the backlog limit exactly. --- crates/weft-appd/src/main.rs | 41 +++++++++++++++----------- crates/weft-compositor/src/appd_ipc.rs | 41 +++++++++++++++++++++++++- docs/security.md | 2 +- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 92a4c72..eaf99f5 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -218,25 +218,30 @@ async fn run() -> anyhow::Result<()> { } if let Some(app_ids) = load_session() { - // Apps need the compositor connection; give it a moment to come up. - let compositor = registry.lock().await.compositor_tx.clone(); - if let Some(compositor) = compositor { - let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10); - while !compositor.is_connected() && tokio::time::Instant::now() < deadline { - tokio::time::sleep(std::time::Duration::from_millis(100)).await; + // Restoring runs beside the request loops, so waiting for the + // compositor delays neither clients nor shutdown. + let registry = Arc::clone(®istry); + tokio::spawn(async move { + // Apps need the compositor connection; give it a moment. + let compositor = registry.lock().await.compositor_tx.clone(); + if let Some(compositor) = compositor { + let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(10); + while !compositor.is_connected() && tokio::time::Instant::now() < deadline { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + } } - } - tracing::info!(count = app_ids.len(), "restoring previous session"); - for app_id in app_ids { - let _ = dispatch( - crate::ipc::Request::LaunchApp { - app_id, - surface_id: 0, - }, - ®istry, - ) - .await; - } + tracing::info!(count = app_ids.len(), "restoring previous session"); + for app_id in app_ids { + let _ = dispatch( + crate::ipc::Request::LaunchApp { + app_id, + surface_id: 0, + }, + ®istry, + ) + .await; + } + }); } #[cfg(unix)] diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index bcf658c..95f37dc 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -9,7 +9,11 @@ use std::{ }; #[cfg(unix)] -use smithay::reexports::calloop::{Interest, Mode, PostAction, channel, generic::Generic}; +use smithay::reexports::calloop::{ + Interest, LoopHandle, Mode, PostAction, RegistrationToken, channel, + generic::Generic, + timer::{TimeoutAction, Timer}, +}; #[cfg(unix)] use smithay::reexports::wayland_server::backend::{ClientId, DisconnectReason}; @@ -47,6 +51,9 @@ pub struct WeftAppdIpc { /// Bytes for appd that did not fit in the socket buffer, sent before /// anything else so frames stay whole. out_buf: Vec, + /// Retries `flush` while `out_buf` holds bytes; armed only then. + flush_retry: Option, + loop_handle: Option>, } #[cfg(unix)] @@ -62,6 +69,8 @@ impl WeftAppdIpc { reported: HashSet::new(), disconnected: None, out_buf: Vec::new(), + flush_retry: None, + loop_handle: None, } } @@ -71,6 +80,7 @@ impl WeftAppdIpc { if self.write_stream.is_some() { self.out_buf.extend_from_slice(&frame); self.flush(); + self.schedule_flush(); } } Err(e) => tracing::warn!(?e, "failed to encode compositor IPC message"), @@ -114,6 +124,32 @@ impl WeftAppdIpc { } } + /// Retries the flush until appd has taken every queued byte, so the + /// last frames of a burst do not wait for unrelated traffic. + fn schedule_flush(&mut self) { + if self.out_buf.is_empty() || self.flush_retry.is_some() { + return; + } + let Some(handle) = &self.loop_handle else { + return; + }; + let retry = Timer::from_duration(std::time::Duration::from_millis(20)); + self.flush_retry = handle + .insert_source(retry, |_, _, state| { + let Some(ipc) = state.appd_ipc.as_mut() else { + return TimeoutAction::Drop; + }; + ipc.flush(); + if ipc.out_buf.is_empty() { + ipc.flush_retry = None; + TimeoutAction::Drop + } else { + TimeoutAction::ToDuration(std::time::Duration::from_millis(20)) + } + }) + .ok(); + } + /// Reports a session's first toplevel to appd. pub fn surface_created(&mut self, session_id: u64) { if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) { @@ -327,6 +363,9 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { tracing::info!(path = %socket_path.display(), "compositor IPC socket open"); + if let Some(ipc) = state.appd_ipc.as_mut() { + ipc.loop_handle = Some(state.loop_handle.clone()); + } // Clients bound to a session report their disconnection here. let (sender, receiver) = channel::channel::<(u64, ClientId)>(); if let Some(ipc) = state.appd_ipc.as_mut() { diff --git a/docs/security.md b/docs/security.md index e16c9b0..f145533 100644 --- a/docs/security.md +++ b/docs/security.md @@ -30,7 +30,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF ## Wayland Surface Isolation -Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, before any process starts, rather than starting an app that could never show a window. +Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the connection drops between that check and the app shell's start, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. From db042fcb5c16652595065b9d1442ee578cbb502b Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:25:57 +0000 Subject: [PATCH 19/61] fix(appd): keep the previous session when shutdown interrupts its restore The session file is read and removed at startup, and restoring now runs in its own task, so a shutdown during the restore would have saved only the apps launched so far. Until the restore finishes, a shutdown stops it and saves the restored list unchanged. --- crates/weft-appd/src/main.rs | 22 ++++++++++++++++------ docs/security.md | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index eaf99f5..37e706f 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -217,11 +217,13 @@ async fn run() -> anyhow::Result<()> { let _ = sd_notify::notify(false, &[sd_notify::NotifyState::Ready]); } - if let Some(app_ids) = load_session() { - // Restoring runs beside the request loops, so waiting for the - // compositor delays neither clients nor shutdown. + // Restoring runs beside the request loops, so waiting for the + // compositor delays neither clients nor shutdown. Until it finishes, + // the restored list is what a shutdown saves. + let restore = load_session().map(|app_ids| { + let saved = app_ids.clone(); let registry = Arc::clone(®istry); - tokio::spawn(async move { + let task = tokio::spawn(async move { // Apps need the compositor connection; give it a moment. let compositor = registry.lock().await.compositor_tx.clone(); if let Some(compositor) = compositor { @@ -242,7 +244,8 @@ async fn run() -> anyhow::Result<()> { .await; } }); - } + (task, saved) + }); #[cfg(unix)] let mut sigterm = { @@ -288,7 +291,14 @@ async fn run() -> anyhow::Result<()> { } } - save_session(registry.lock().await.running_app_ids()).await; + let app_ids = match restore { + Some((task, saved)) if !task.is_finished() => { + task.abort(); + saved + } + _ => registry.lock().await.running_app_ids(), + }; + save_session(app_ids).await; registry.lock().await.shutdown_all(); tokio::time::sleep(tokio::time::Duration::from_millis(200)).await; let _ = std::fs::remove_file(&socket_path); diff --git a/docs/security.md b/docs/security.md index f145533..81fd57e 100644 --- a/docs/security.md +++ b/docs/security.md @@ -30,7 +30,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF ## Wayland Surface Isolation -Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the connection drops between that check and the app shell's start, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. +Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. From aa67e7f19008e2b1147129f5b6b3cee2355fcb57 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:36:55 +0000 Subject: [PATCH 20/61] fix(compositor): own stacking and focus, and activate app windows on request The system shell's panel was an ordinary toplevel at winit's default size, stacked by map order with the applications, and clicking or the taskbar could never bring an application back to the front. The compositor now sizes the panel to the output and keeps it beneath every application window. A newly mapped, clicked or activated application window is raised, marked activated and given keyboard focus. weft-appd accepts ACTIVATE_APP for a running session and asks the compositor to activate that session's window (AppFocusRequest), and the system UI's taskbar entries send it when chosen. check_activation.py runs the system shell with a solid panel page and two test applications: the panel fills the output beneath them, each launch comes to the front with keyboard focus, ACTIVATE_APP switches between the sessions with focus, and activating a session that is not running is refused. --- crates/weft-appd/src/ipc.rs | 4 + crates/weft-appd/src/main.rs | 69 ++++++ crates/weft-compositor/src/appd_ipc.rs | 9 +- crates/weft-compositor/src/backend/drm.rs | 2 + crates/weft-compositor/src/backend/winit.rs | 1 + crates/weft-compositor/src/input.rs | 11 +- crates/weft-compositor/src/main.rs | 1 + crates/weft-compositor/src/state.rs | 6 +- crates/weft-compositor/src/windows.rs | 141 ++++++++++++ docs/architecture.md | 2 +- docs/building.md | 1 + docs/security.md | 2 +- infra/shell/system-ui.html | 6 + tests/frame/check_activation.py | 227 ++++++++++++++++++++ 14 files changed, 476 insertions(+), 6 deletions(-) create mode 100644 crates/weft-compositor/src/windows.rs create mode 100755 tests/frame/check_activation.py diff --git a/crates/weft-appd/src/ipc.rs b/crates/weft-appd/src/ipc.rs index 84d7e4d..d7a3d7a 100644 --- a/crates/weft-appd/src/ipc.rs +++ b/crates/weft-appd/src/ipc.rs @@ -11,6 +11,10 @@ pub enum Request { TerminateApp { session_id: u64, }, + /// Brings the session's window to the front with keyboard focus. + ActivateApp { + session_id: u64, + }, QueryRunning, QueryAppState { session_id: u64, diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 37e706f..dda6435 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -544,6 +544,30 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { } } } + Request::ActivateApp { session_id } => { + let reg = registry.lock().await; + let state = reg.state(session_id); + if !matches!(state, AppStateKind::Running) { + return Response::Error { + code: 404, + message: format!("session {session_id} is not running"), + }; + } + // The compositor owns stacking and focus; appd asks it to act + // for a session it supervises. + let sent = reg.compositor_tx.as_ref().is_some_and(|tx| { + tx.try_send(weft_ipc_types::AppdToCompositor::AppFocusRequest { session_id }.into()) + .is_ok() + }); + if sent { + Response::AppState { session_id, state } + } else { + Response::Error { + code: 503, + message: "weft-compositor is not connected".to_owned(), + } + } + } Request::QueryRunning => { let sessions = registry.lock().await.running_sessions(); Response::RunningApps { sessions } @@ -1276,6 +1300,51 @@ mod tests { assert!(!session_ipc_socket_path(session_id).unwrap().exists()); } + #[tokio::test(flavor = "current_thread")] + async fn activation_reaches_the_compositor_only_for_running_sessions() { + let registry = make_registry(); + let (tx, mut compositor) = tokio::sync::mpsc::channel(8); + let connected = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)); + registry.lock().await.compositor_tx = Some(compositor_client::CompositorSender::new( + tx, + connected.clone(), + )); + let session_id = registry.lock().await.launch("org.example.active"); + + // Starting, then not found: refused, nothing sent. + let starting = dispatch(Request::ActivateApp { session_id }, ®istry).await; + let absent = dispatch(Request::ActivateApp { session_id: 999 }, ®istry).await; + for refused in [&starting, &absent] { + assert!( + matches!(refused, Response::Error { code: 404, .. }), + "{refused:?}" + ); + } + assert!(compositor.try_recv().is_err()); + + registry + .lock() + .await + .set_state(session_id, AppStateKind::Running); + let activated = dispatch(Request::ActivateApp { session_id }, ®istry).await; + assert!( + matches!(activated, Response::AppState { session_id: s, state: AppStateKind::Running } if s == session_id), + "{activated:?}" + ); + assert!(matches!( + compositor.try_recv().map(|out| out.msg), + Ok(weft_ipc_types::AppdToCompositor::AppFocusRequest { session_id: s }) if s == session_id + )); + + // Without the compositor connection the request is refused. + drop(compositor); + let unsent = dispatch(Request::ActivateApp { session_id }, ®istry).await; + assert!( + matches!(unsent, Response::Error { code: 503, .. }), + "{unsent:?}" + ); + } + #[tokio::test(flavor = "current_thread")] async fn without_a_connected_compositor_nothing_is_started() { use std::os::unix::fs::PermissionsExt; diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index 95f37dc..715d794 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -274,7 +274,14 @@ fn handle_message(state: &mut WeftCompositorState, msg: AppdToCompositor) { } } AppdToCompositor::AppFocusRequest { session_id } => { - tracing::debug!(session_id, "AppFocusRequest"); + if state.activate_session(session_id) { + tracing::info!(session_id, "session activated"); + } else { + tracing::info!( + session_id, + "activation requested for a session with no window" + ); + } } } } diff --git a/crates/weft-compositor/src/backend/drm.rs b/crates/weft-compositor/src/backend/drm.rs index 93bb258..71e93fb 100644 --- a/crates/weft-compositor/src/backend/drm.rs +++ b/crates/weft-compositor/src/backend/drm.rs @@ -550,6 +550,7 @@ fn connector_connected( tracing::info!(?name, "output connected"); let (pw, ph) = (wl_mode.size.w, wl_mode.size.h); state.weft_shell_state.reconfigure_panels(0, 0, pw, ph); + state.fit_panels(); state.weft_shell_state.retain_alive_panels(); render_output(state, node, crtc); } @@ -578,6 +579,7 @@ fn connector_disconnected( .map(|g| (g.size.w, g.size.h)) .unwrap_or((0, 0)); state.weft_shell_state.reconfigure_panels(0, 0, pw, ph); + state.fit_panels(); state.weft_shell_state.retain_alive_panels(); } diff --git a/crates/weft-compositor/src/backend/winit.rs b/crates/weft-compositor/src/backend/winit.rs index 7575211..ede398e 100644 --- a/crates/weft-compositor/src/backend/winit.rs +++ b/crates/weft-compositor/src/backend/winit.rs @@ -124,6 +124,7 @@ pub fn run() -> anyhow::Result<()> { state .weft_shell_state .reconfigure_panels(0, 0, size.w, size.h); + state.fit_panels(); state.weft_shell_state.retain_alive_panels(); } WinitEvent::Input(input_event) => { diff --git a/crates/weft-compositor/src/input.rs b/crates/weft-compositor/src/input.rs index 7f2ac6e..ef7d72d 100644 --- a/crates/weft-compositor/src/input.rs +++ b/crates/weft-compositor/src/input.rs @@ -122,10 +122,17 @@ fn handle_pointer_button( let button = event.button_code(); let button_state = event.state(); - // On press: focus the surface under the pointer. + // On press: activate the window under the pointer, which raises an + // application window and focuses it. if button_state == ButtonState::Pressed { let pointer_location = state.pointer_location; - if let Some((surface, _loc)) = surface_under(state, pointer_location) { + let window = state + .space + .element_under(pointer_location) + .map(|(window, _)| window.clone()); + if let Some(window) = window { + state.activate_window(&window); + } else if let Some((surface, _loc)) = surface_under(state, pointer_location) { if let Some(keyboard) = state.seat.get_keyboard() { keyboard.set_focus(state, Some(surface), serial); } diff --git a/crates/weft-compositor/src/main.rs b/crates/weft-compositor/src/main.rs index 84334c4..850116f 100644 --- a/crates/weft-compositor/src/main.rs +++ b/crates/weft-compositor/src/main.rs @@ -5,6 +5,7 @@ mod backend; mod input; mod protocols; mod state; +mod windows; fn main() -> anyhow::Result<()> { tracing_subscriber::fmt() diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index 8c6a303..4308bfa 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -274,7 +274,10 @@ impl XdgShellHandler for WeftCompositorState { .and_then(|data| data.session.as_ref().map(|s| s.session_id)) }); let window = Window::new_wayland_window(surface); - self.space.map_element(window, (0, 0), false); + self.space.map_element(window.clone(), (0, 0), false); + // A new window comes to the front with focus; the shell's panel is + // recognised when it registers and is then kept beneath. + self.activate_window(&window); if let (Some(session_id), Some(ipc)) = (session, self.appd_ipc.as_mut()) { ipc.surface_created(session_id); } @@ -570,6 +573,7 @@ impl Dispatch for WeftCompositorState { ), ); state.weft_shell_state.add_panel(window); + state.fit_panels(); } else { window.configure(x, y, width, height, 0); } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs new file mode 100644 index 0000000..f99708d --- /dev/null +++ b/crates/weft-compositor/src/windows.rs @@ -0,0 +1,141 @@ +//! Stacking, focus and activation of toplevel windows. +//! +//! The compositor decides the order: the trusted shell's panel, which +//! fills the output, stays beneath every application window; a newly mapped +//! application window, a clicked one and one appd asks to activate is raised +//! to the top, marked activated and given keyboard focus. + +use smithay::desktop::Window; +use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; +use smithay::reexports::wayland_server::Resource; +use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; +use smithay::utils::SERIAL_COUNTER; +use smithay::wayland::seat::WaylandFocus; + +use crate::protocols::WeftShellWindowData; +use crate::state::{WeftClientState, WeftCompositorState}; + +impl WeftCompositorState { + /// Whether `surface` backs a window the trusted shell registered as its + /// panel. + pub fn is_panel_surface(&self, surface: &WlSurface) -> bool { + self.weft_shell_state.panels().any(|panel| { + panel + .data::() + .and_then(|data| data.surface.as_ref()) + .is_some_and(|s| s == surface) + }) + } + + fn is_panel(&self, window: &Window) -> bool { + window + .wl_surface() + .is_some_and(|surface| self.is_panel_surface(&surface)) + } + + /// Raises `window` above the other application windows, marks it + /// activated and gives it keyboard focus. A panel is focused but stays + /// beneath the applications. + pub fn activate_window(&mut self, window: &Window) { + let panel = self.is_panel(window); + if !panel { + self.space.raise_element(window, true); + } + self.send_pending_configures(); + if let (Some(keyboard), Some(surface)) = (self.seat.get_keyboard(), window.wl_surface()) { + keyboard.set_focus( + self, + Some(surface.into_owned()), + SERIAL_COUNTER.next_serial(), + ); + } + } + + /// Activates the topmost window of `session_id`; returns whether the + /// session has one. + pub fn activate_session(&mut self, session_id: u64) -> bool { + let window = self + .space + .elements() + .rev() + .find(|window| window_session(window) == Some(session_id)) + .cloned(); + match window { + Some(window) => { + self.activate_window(&window); + true + } + None => false, + } + } + + /// Sizes the panels to the output, places them at its origin and keeps + /// them beneath every application window. + pub fn fit_panels(&mut self) { + let Some(geometry) = self + .space + .outputs() + .next() + .and_then(|output| self.space.output_geometry(output)) + else { + return; + }; + let panels: Vec = self + .space + .elements() + .filter(|window| self.is_panel(window)) + .cloned() + .collect(); + if panels.is_empty() { + return; + } + for panel in &panels { + if let Some(toplevel) = panel.toplevel() { + toplevel.with_pending_state(|state| { + state.size = Some(geometry.size); + state.states.set(xdg_toplevel::State::Maximized); + }); + toplevel.send_pending_configure(); + tracing::debug!( + width = geometry.size.w, + height = geometry.size.h, + "panel fitted to the output" + ); + } + self.space.map_element(panel.clone(), geometry.loc, false); + } + // Raising every other window, in its current order, leaves the + // panels at the bottom. + let others: Vec = self + .space + .elements() + .filter(|window| !self.is_panel(window)) + .cloned() + .collect(); + for window in &others { + self.space.raise_element(window, false); + } + } + + /// Sends the configure for every toplevel whose pending state changed, + /// such as its activated state. + fn send_pending_configures(&self) { + for window in self.space.elements() { + if let Some(toplevel) = window.toplevel() { + toplevel.send_pending_configure(); + } + } + } +} + +/// The appd session `window`'s client is bound to, if any. +pub fn window_session(window: &Window) -> Option { + window + .toplevel()? + .wl_surface() + .client()? + .get_data::()? + .session + .as_ref() + .map(|binding| binding.session_id) +} diff --git a/docs/architecture.md b/docs/architecture.md index 33f0f8a..0dc33cd 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a newly mapped, clicked or activated application window is raised, marked activated and given keyboard focus. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/docs/building.md b/docs/building.md index fca337a..fc6fed2 100644 --- a/docs/building.md +++ b/docs/building.md @@ -88,6 +88,7 @@ python3 tests/frame/check_counter.py # Counter round trip through the session python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept python3 tests/frame/check_confinement.py # an app page reaches only its UI files and its session bridge +python3 tests/frame/check_activation.py # panel beneath two apps; launch and ACTIVATE_APP raise and focus ``` ## Demo apps (wasm32-wasip2) diff --git a/docs/security.md b/docs/security.md index 81fd57e..7002382 100644 --- a/docs/security.md +++ b/docs/security.md @@ -32,7 +32,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement, stacking, focus and close are not yet decided by the compositor per session (all toplevels start at the origin); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement and close are not yet decided by the compositor per session (all toplevels start at the origin, and no work area is reserved for the taskbar); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. ## JavaScript Engine (SpiderMonkey) diff --git a/infra/shell/system-ui.html b/infra/shell/system-ui.html index 9653a41..1b98e2f 100644 --- a/infra/shell/system-ui.html +++ b/infra/shell/system-ui.html @@ -399,6 +399,12 @@ }); el.appendChild(span); el.appendChild(close); + // Choosing the entry brings the app's window to the front. + el.addEventListener('click', function () { + if (ws && ws.readyState === WebSocket.OPEN) { + ws.send(JSON.stringify({ type: 'ACTIVATE_APP', session_id: sessionId })); + } + }); var taskbar = document.querySelector('weft-taskbar'); var clock = document.getElementById('clock'); taskbar.insertBefore(el, clock); diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py new file mode 100755 index 0000000..a4bb1e3 --- /dev/null +++ b/tests/frame/check_activation.py @@ -0,0 +1,227 @@ +#!/usr/bin/env python3 +"""Check that the compositor stacks, focuses and activates windows. + +The harness runs weft-servo-shell with a solid green page as the system +panel, and weft-appd with two test applications, one red and one blue, on a +nested desktop (see session.py). Each application page turns a lighter shade +of its colour when it receives a key press. In the presented pixels: + +- the panel fills the compositor output and stays beneath the applications; +- each newly launched application is shown on top and receives keys; +- ACTIVATE_APP, the request the taskbar sends, brings the other session's + window back to the front with keyboard focus; +- ACTIVATE_APP for a session that is not running is refused. + +The applications are unsigned test packages, recorded as development content +in a private data home, using the Counter demo's component. +""" + +import argparse +import shutil +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from check_counter import AppdClient, free_port, read_endpoint # noqa: E402 +from session import ROOT, Desktop, SessionError, bounding_box, missing_tools, parse_ppm # noqa: E402 + +GREEN = (0, 160, 0) +APPS = { + "org.weft.test.red": ((200, 0, 0), (255, 120, 120)), + "org.weft.test.blue": ((0, 0, 200), (120, 120, 255)), +} +STARTUP_TIMEOUT = 120.0 + +PANEL_PAGE = ('') + + +def app_page(color, pressed): + return ('') + + +def make_store(store, home): + for app_id, (color, pressed) in APPS.items(): + package = store / app_id + (package / "ui").mkdir(parents=True) + (package / "wapp.toml").write_text( + f'[package]\nid = "{app_id}"\nname = "Test"\nversion = "0.1.0"\n' + '[runtime]\nmodule = "app.wasm"\n[ui]\nentry = "ui/index.html"\n') + shutil.copy(ROOT / "examples/org.weft.demo.counter/app.wasm", package / "app.wasm") + (package / "ui/index.html").write_text(app_page(color, pressed)) + record = home / "share/weft/owners" / app_id + record.parent.mkdir(parents=True, exist_ok=True) + record.write_text("development\n") + + +def area(screenshot, color): + width, height, pixels = parse_ppm(screenshot) + box = bounding_box(width, height, pixels, color) + return box, width, height + + +def wait_for_color(desktop, color, timeout=20): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + shot = desktop.capture() + box, _, _ = area(shot, color) + if box is not None: + return shot, box + time.sleep(0.3) + return desktop.capture(), None + + +def wait_until_gone(desktop, color, timeout=10): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + box, _, _ = area(desktop.capture(), color) + if box is None: + return True + time.sleep(0.3) + return False + + +def run(args, desktop, store, home): + panel_page = desktop.runtime / "panel.html" + panel_page.write_text(PANEL_PAGE) + desktop.launch_client("shell", [args.shell], {"WEFT_SYSTEM_UI_HTML": str(panel_page)}) + shot, panel = wait_for_color(desktop, GREEN, 60) + if panel is None: + raise AssertionError("the panel was not shown") + width, height, _ = parse_ppm(shot) + # The compositor sizes the panel to its whole output, which reaches the + # right and bottom edges of the nested desktop. + if panel[2] != width - 1 or panel[3] < height - 3: + raise AssertionError(f"the panel does not fill the output: {panel}") + + t = Path(args.target) + desktop.launch_client("appd", [t / "weft-appd"], { + "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), + "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), + "WEFT_APP_STORE": str(store), + "WEFT_DISABLE_CGROUP": "1", + "WEFT_APPD_WS_PORT": str(free_port()), + "HOME": str(home), + "XDG_DATA_HOME": str(home / "share"), + }) + port, token = read_endpoint(desktop.runtime, 30) + appd = AppdClient(port, token) + + def key(name): + subprocess.run([args.xdotool, "search", "--class", "weft-compositor", + "windowfocus", "--sync"], env={"DISPLAY": desktop.display}, + check=True, timeout=20) + subprocess.run([args.xdotool, "key", name], env={"DISPLAY": desktop.display}, + check=True, timeout=20) + + def shown(app_id, pressed=False): + """Waits until `app_id` is the visible application, alone on top.""" + color = APPS[app_id][1 if pressed else 0] + others = [c for other, cs in APPS.items() for c in cs if other != app_id] + deadline = time.monotonic() + 20 + while time.monotonic() < deadline: + shot = desktop.capture() + box, _, _ = area(shot, color) + hidden = all(area(shot, c)[0] is None for c in others) + if box is not None and hidden: + # The panel stays visible beside the application window. + if area(shot, GREEN)[0] is None: + raise AssertionError("an application covered the panel") + return + time.sleep(0.3) + state = "pressed " if pressed else "" + raise AssertionError(f"{app_id} is not shown {state}on top") + + sessions = {} + for app_id in APPS: + appd.send({"type": "LAUNCH_APP", "app_id": app_id, "surface_id": 0}) + try: + ready = appd.wait_for( + lambda m, a=app_id: m.get("type") == "APP_READY" and m.get("app_id") == a, + STARTUP_TIMEOUT) + except TimeoutError: + raise AssertionError(f"{app_id} did not start") + sessions[app_id] = ready["session_id"] + # A newly launched application comes to the front. + shown(app_id) + + red, blue = "org.weft.test.red", "org.weft.test.blue" + # The last launched application has keyboard focus. + key("a") + shown(blue, pressed=True) + + def activate(app_id): + appd.send({"type": "ACTIVATE_APP", "session_id": sessions[app_id]}) + reply = appd.wait_for(lambda m: m.get("type") in ("APP_STATE", "ERROR") + and m.get("session_id", sessions[app_id]) == sessions[app_id], + 10) + if reply.get("type") != "APP_STATE": + raise AssertionError(f"activating {app_id} was refused: {reply}") + + # Activation brings the other session back with keyboard focus. + activate(red) + shown(red) + key("a") + shown(red, pressed=True) + activate(blue) + shown(blue, pressed=True) + + appd.send({"type": "ACTIVATE_APP", "session_id": 999}) + reply = appd.wait_for(lambda m: m.get("type") == "ERROR", 10) + if reply.get("code") != 404: + raise AssertionError(f"activating a session that does not run: {reply}") + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--compositor", type=Path, default=ROOT / "target/debug/weft-compositor") + parser.add_argument("--shell", type=Path, default=ROOT / "target/debug/weft-servo-shell") + parser.add_argument("--target", type=Path, default=ROOT / "target/debug", + help="directory with weft-appd, weft-runtime and weft-app-shell") + parser.add_argument("--output", type=Path, default=ROOT / "target/activation-check") + parser.add_argument("--xdotool", default=shutil.which("xdotool")) + args = parser.parse_args(argv) + if not args.xdotool: + print("missing: xdotool", file=sys.stderr) + return 2 + + missing = missing_tools() + [str(p) for p in (args.compositor, args.shell, + args.target / "weft-appd", + args.target / "weft-runtime", + args.target / "weft-app-shell") + if not p.is_file()] + if missing: + print("missing: " + ", ".join(missing), file=sys.stderr) + return 2 + store = Path(tempfile.mkdtemp(prefix="weft-store-")) + home = Path(tempfile.mkdtemp(prefix="weft-home-")) + try: + make_store(store, home) + with Desktop(args.compositor, args.output, outputs=["appd.log", "activation.ppm"]) \ + as desktop: + try: + run(args, desktop, store, home) + except AssertionError: + (args.output / "activation.ppm").write_bytes(desktop.capture()) + raise + except (AssertionError, SessionError, TimeoutError) as failure: + print(f"activation check failed: {failure}", file=sys.stderr) + print(f"logs and capture in {args.output}", file=sys.stderr) + return 1 + finally: + shutil.rmtree(store, ignore_errors=True) + shutil.rmtree(home, ignore_errors=True) + print("activation check passed") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) From 65c02231a0e384b491d755adb4427fcb1a009241 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:42:42 +0000 Subject: [PATCH 21/61] fix(compositor): restore focus when a window closes and keep later windows from taking it When the focused window closed, keys went to a dead surface until the next click; the topmost remaining application window, or the panel, now gets focus. Only a session's first window takes focus when it maps, so a client cannot take keystrokes meant for another application by creating windows. A panel is fitted and kept beneath however the shell orders its requests, and focusing the panel clears the applications' activated state. ACTIVATE_APP is refused while the compositor is not connected rather than queued. check_activation.py now also ends the focused application and checks that keys reach the remaining one. --- crates/weft-appd/src/main.rs | 10 ++-- crates/weft-compositor/src/state.rs | 9 ++-- crates/weft-compositor/src/windows.rs | 72 +++++++++++++++++++++++++-- docs/architecture.md | 2 +- docs/security.md | 2 +- tests/frame/check_activation.py | 16 ++++-- 6 files changed, 93 insertions(+), 18 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index dda6435..2bf1e61 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -556,8 +556,12 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { // The compositor owns stacking and focus; appd asks it to act // for a session it supervises. let sent = reg.compositor_tx.as_ref().is_some_and(|tx| { - tx.try_send(weft_ipc_types::AppdToCompositor::AppFocusRequest { session_id }.into()) - .is_ok() + tx.is_connected() + && tx + .try_send( + weft_ipc_types::AppdToCompositor::AppFocusRequest { session_id }.into(), + ) + .is_ok() }); if sent { Response::AppState { session_id, state } @@ -1337,7 +1341,7 @@ mod tests { )); // Without the compositor connection the request is refused. - drop(compositor); + connected.store(false, std::sync::atomic::Ordering::Release); let unsent = dispatch(Request::ActivateApp { session_id }, ®istry).await; assert!( matches!(unsent, Response::Error { code: 503, .. }), diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index 4308bfa..f612358 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -266,6 +266,10 @@ impl XdgShellHandler for WeftCompositorState { &mut self.xdg_shell_state } + fn toplevel_destroyed(&mut self, surface: ToplevelSurface) { + self.window_closed(surface.wl_surface()); + } + fn new_toplevel(&mut self, surface: ToplevelSurface) { surface.send_configure(); let session = surface.wl_surface().client().and_then(|client| { @@ -274,10 +278,7 @@ impl XdgShellHandler for WeftCompositorState { .and_then(|data| data.session.as_ref().map(|s| s.session_id)) }); let window = Window::new_wayland_window(surface); - self.space.map_element(window.clone(), (0, 0), false); - // A new window comes to the front with focus; the shell's panel is - // recognised when it registers and is then kept beneath. - self.activate_window(&window); + self.map_new_window(window); if let (Some(session_id), Some(ipc)) = (session, self.appd_ipc.as_mut()) { ipc.surface_created(session_id); } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index f99708d..860a291 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -1,9 +1,10 @@ //! Stacking, focus and activation of toplevel windows. //! //! The compositor decides the order: the trusted shell's panel, which -//! fills the output, stays beneath every application window; a newly mapped -//! application window, a clicked one and one appd asks to activate is raised -//! to the top, marked activated and given keyboard focus. +//! fills the output, stays beneath every application window; a session's +//! first window, a clicked window and one appd asks to activate is raised to +//! the top, marked activated and given keyboard focus. When the focused +//! window closes, the topmost remaining application window gets focus. use smithay::desktop::Window; use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; @@ -37,8 +38,13 @@ impl WeftCompositorState { /// activated and gives it keyboard focus. A panel is focused but stays /// beneath the applications. pub fn activate_window(&mut self, window: &Window) { - let panel = self.is_panel(window); - if !panel { + if self.is_panel(window) { + // The panel takes focus without rising; no application window + // stays marked activated. + for other in self.space.elements() { + other.set_activated(false); + } + } else { self.space.raise_element(window, true); } self.send_pending_configures(); @@ -117,6 +123,62 @@ impl WeftCompositorState { } } + /// Maps a new toplevel. A session's first window comes to the front with + /// keyboard focus; its later windows, and windows of clients outside + /// any session after the first, are shown without taking focus, so a + /// client cannot take keystrokes meant for another application by + /// creating windows. A panel is fitted and kept beneath. + pub fn map_new_window(&mut self, window: Window) { + let session = window_session(&window); + let first = !self + .space + .elements() + .any(|other| window_session(other) == session); + self.space.map_element(window.clone(), (0, 0), false); + if self.is_panel(&window) { + self.fit_panels(); + } else if first { + self.activate_window(&window); + } + } + + /// Unmaps a closed toplevel and, if it had keyboard focus, gives focus + /// to the topmost remaining application window, or the panel. + pub fn window_closed(&mut self, surface: &WlSurface) { + let Some(window) = self + .space + .elements() + .find(|window| window.wl_surface().is_some_and(|s| *s == *surface)) + .cloned() + else { + return; + }; + self.space.unmap_elem(&window); + let had_focus = self + .seat + .get_keyboard() + .and_then(|keyboard| keyboard.current_focus()) + .is_none_or(|focus| focus == *surface); + if !had_focus { + return; + } + let next = self + .space + .elements() + .rev() + .find(|window| !self.is_panel(window)) + .or_else(|| self.space.elements().next()) + .cloned(); + match next { + Some(next) => self.activate_window(&next), + None => { + if let Some(keyboard) = self.seat.get_keyboard() { + keyboard.set_focus(self, None, SERIAL_COUNTER.next_serial()); + } + } + } + } + /// Sends the configure for every toplevel whose pending state changed, /// such as its activated state. fn send_pending_configures(&self) { diff --git a/docs/architecture.md b/docs/architecture.md index 0dc33cd..dd74a4d 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a newly mapped, clicked or activated application window is raised, marked activated and given keyboard focus. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/docs/security.md b/docs/security.md index 7002382..0d5bcc1 100644 --- a/docs/security.md +++ b/docs/security.md @@ -32,7 +32,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement and close are not yet decided by the compositor per session (all toplevels start at the origin, and no work area is reserved for the taskbar); and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement and close are not yet decided by the compositor per session (all toplevels start at the origin, and no work area is reserved for the taskbar, so an application window as large as the output hides it); a session's first window takes keyboard focus when it maps, later ones do not; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. ## JavaScript Engine (SpiderMonkey) diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index a4bb1e3..3d46ee4 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -3,13 +3,14 @@ The harness runs weft-servo-shell with a solid green page as the system panel, and weft-appd with two test applications, one red and one blue, on a -nested desktop (see session.py). Each application page turns a lighter shade -of its colour when it receives a key press. In the presented pixels: +nested desktop (see session.py). Each application page switches between its +colour and a lighter shade on every key press. In the presented pixels: - the panel fills the compositor output and stays beneath the applications; - each newly launched application is shown on top and receives keys; - ACTIVATE_APP, the request the taskbar sends, brings the other session's window back to the front with keyboard focus; +- when the focused application ends, the remaining one gets keyboard focus; - ACTIVATE_APP for a session that is not running is refused. The applications are unsigned test packages, recorded as development content @@ -42,8 +43,9 @@ PANEL_PAGE = ('') @@ -173,6 +175,12 @@ def run(args, desktop, store, home): activate(blue) shown(blue, pressed=True) + # When the focused application ends, keyboard focus returns to the other. + appd.send({"type": "TERMINATE_APP", "session_id": sessions[blue]}) + shown(red, pressed=True) + key("a") + shown(red) + appd.send({"type": "ACTIVATE_APP", "session_id": 999}) reply = appd.wait_for(lambda m: m.get("type") == "ERROR", 10) if reply.get("code") != 404: From 16af8d2a8c03cd3ce4f04f7f453cae297dc6f232 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 15:45:52 +0000 Subject: [PATCH 22/61] fix(compositor): give a session focus on map only once in its lifetime Unmapping a closed window made a session with no mapped window count as new again, so destroying and recreating its only window took keyboard focus from the application the user had activated. The compositor now remembers which sessions already received focus on map until the session ends. --- crates/weft-compositor/src/appd_ipc.rs | 13 +++++++++++++ crates/weft-compositor/src/windows.rs | 26 +++++++++++++++++--------- docs/security.md | 2 +- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index 715d794..f6e85ab 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -46,6 +46,9 @@ pub struct WeftAppdIpc { sessions: HashMap, /// Sessions whose first toplevel was reported to appd. reported: HashSet, + /// Sessions that already received keyboard focus for a mapped window; + /// kept until the session ends, so recreating a window gains nothing. + focused_on_map: HashSet, /// Where clients report their session's disconnection. disconnected: Option>, /// Bytes for appd that did not fit in the socket buffer, sent before @@ -67,6 +70,7 @@ impl WeftAppdIpc { fds: VecDeque::new(), sessions: HashMap::new(), reported: HashSet::new(), + focused_on_map: HashSet::new(), disconnected: None, out_buf: Vec::new(), flush_retry: None, @@ -150,6 +154,12 @@ impl WeftAppdIpc { .ok(); } + /// Whether a window `session_id` maps now may take keyboard focus: only + /// the first one in the session's lifetime. + pub fn take_focus_on_map(&mut self, session_id: u64) -> bool { + self.sessions.contains_key(&session_id) && self.focused_on_map.insert(session_id) + } + /// Reports a session's first toplevel to appd. pub fn surface_created(&mut self, session_id: u64) { if self.sessions.contains_key(&session_id) && self.reported.insert(session_id) { @@ -250,6 +260,7 @@ impl WeftAppdIpc { self.read_buf.clear(); self.fds.clear(); self.reported.clear(); + self.focused_on_map.clear(); self.sessions.drain().map(|(_, client)| client).collect() } } @@ -265,6 +276,7 @@ fn handle_message(state: &mut WeftCompositorState, msg: AppdToCompositor) { return; }; ipc.reported.remove(&session_id); + ipc.focused_on_map.remove(&session_id); if let Some(client) = ipc.sessions.remove(&session_id) { tracing::info!(session_id, "closing the client of an ended session"); state @@ -389,6 +401,7 @@ pub fn setup(state: &mut WeftCompositorState) -> anyhow::Result<()> { { ipc.sessions.remove(&session_id); ipc.reported.remove(&session_id); + ipc.focused_on_map.remove(&session_id); tracing::info!(session_id, "session client disconnected"); ipc.send(&CompositorToAppd::ClientDisconnected { session_id }); } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index 860a291..df39eb4 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -123,17 +123,25 @@ impl WeftCompositorState { } } - /// Maps a new toplevel. A session's first window comes to the front with - /// keyboard focus; its later windows, and windows of clients outside - /// any session after the first, are shown without taking focus, so a - /// client cannot take keystrokes meant for another application by - /// creating windows. A panel is fitted and kept beneath. + /// Maps a new toplevel. The first window a session maps in its lifetime + /// comes to the front with keyboard focus; its later windows, including + /// recreated ones, are shown without taking focus, so a session cannot + /// take keystrokes meant for another application by creating windows. + /// A client outside any session (one connected through the display + /// socket) takes focus only while no other such window is mapped. A + /// panel is fitted and kept beneath. pub fn map_new_window(&mut self, window: Window) { let session = window_session(&window); - let first = !self - .space - .elements() - .any(|other| window_session(other) == session); + let first = match session { + Some(session_id) => self + .appd_ipc + .as_mut() + .is_some_and(|ipc| ipc.take_focus_on_map(session_id)), + None => !self + .space + .elements() + .any(|other| window_session(other).is_none()), + }; self.space.map_element(window.clone(), (0, 0), false); if self.is_panel(&window) { self.fit_panels(); diff --git a/docs/security.md b/docs/security.md index 0d5bcc1..b18519a 100644 --- a/docs/security.md +++ b/docs/security.md @@ -32,7 +32,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement and close are not yet decided by the compositor per session (all toplevels start at the origin, and no work area is reserved for the taskbar, so an application window as large as the output hides it); a session's first window takes keyboard focus when it maps, later ones do not; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement and close are not yet decided by the compositor per session (all toplevels start at the origin, and no work area is reserved for the taskbar, so an application window as large as the output hides it); only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. ## JavaScript Engine (SpiderMonkey) From 5253eaf2518c5f0053e1b6bc79435a47cb39dad6 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:00:38 +0000 Subject: [PATCH 23/61] fix(compositor): reserve the taskbar strip and lay app windows out in the work area Application windows opened at the client's default size at the origin, so one as large as the output hid the taskbar with no way back. zweft_shell_window_v1 version 3 adds set_exclusive_zone: a panel reserves a strip along an output edge, and other windows get the new invalid_exclusive_zone error. weft-servo-shell reserves its 48-pixel taskbar at the bottom (TASKBAR_HEIGHT, matching --taskbar-height in the system UI). The compositor computes the work area as the output less the reserved strips and configures every application window to fill it, again when the output or a reservation changes. The desktop harness now uses a 1400x900 X screen so the nested compositor's whole 1280x800 output is captured. check_activation.py requires both applications to fill the work area and end at the reserved strip; check_notes.py finds the Discard button from the window's right edge and check_confinement.py samples its probes from the measured page instead of a fixed 800x600 window. --- crates/weft-compositor/src/protocols/mod.rs | 10 ++- crates/weft-compositor/src/state.rs | 24 ++++++- crates/weft-compositor/src/windows.rs | 70 ++++++++++++++++++++- crates/weft-servo-shell/src/shell_client.rs | 12 +++- docs/architecture.md | 2 +- docs/building.md | 2 +- docs/security.md | 2 +- infra/shell/system-ui.html | 1 + protocol/weft-shell-unstable-v1.xml | 27 +++++++- tests/frame/check_activation.py | 21 ++++--- tests/frame/check_confinement.py | 21 +++++-- tests/frame/check_notes.py | 19 ++++-- tests/frame/session.py | 11 ++-- 13 files changed, 188 insertions(+), 34 deletions(-) diff --git a/crates/weft-compositor/src/protocols/mod.rs b/crates/weft-compositor/src/protocols/mod.rs index 18922b4..0993fad 100644 --- a/crates/weft-compositor/src/protocols/mod.rs +++ b/crates/weft-compositor/src/protocols/mod.rs @@ -33,6 +33,8 @@ pub struct WeftShellWindowData { pub role: String, pub surface: Option, pub closed: std::sync::atomic::AtomicBool, + /// The edge strip a panel reserved with set_exclusive_zone. + pub exclusive_zone: std::sync::Mutex>, } impl WeftShellState { @@ -41,7 +43,7 @@ impl WeftShellState { D: GlobalDispatch, D: 'static, { - let global = display.create_global::(2, ()); + let global = display.create_global::(3, ()); Self { _global: global, panels: Vec::new(), @@ -97,6 +99,7 @@ mod tests { role: "normal".into(), surface: None, closed: std::sync::atomic::AtomicBool::new(false), + exclusive_zone: std::sync::Mutex::new(None), }; assert_eq!(d.app_id, "com.example.test"); assert_eq!(d.title, "Test Window"); @@ -112,6 +115,7 @@ mod tests { role: String::new(), surface: None, closed: std::sync::atomic::AtomicBool::new(false), + exclusive_zone: std::sync::Mutex::new(None), }; assert!(!d.closed.load(Ordering::Relaxed)); d.closed.store(true, Ordering::Relaxed); @@ -122,14 +126,14 @@ mod tests { fn manager_interface_name_and_version() { let iface = ZweftShellManagerV1::interface(); assert_eq!(iface.name, "zweft_shell_manager_v1"); - assert_eq!(iface.version, 2); + assert_eq!(iface.version, 3); } #[test] fn window_interface_name_and_version() { let iface = ZweftShellWindowV1::interface(); assert_eq!(iface.name, "zweft_shell_window_v1"); - assert_eq!(iface.version, 2); + assert_eq!(iface.version, 3); } #[test] diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index f612358..363783f 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -542,6 +542,7 @@ impl Dispatch for WeftCompositorState { role, surface, closed: std::sync::atomic::AtomicBool::new(false), + exclusive_zone: std::sync::Mutex::new(None), }, ); if let Some(session) = session @@ -585,7 +586,7 @@ impl Dispatch for WeftCompositorState { impl Dispatch for WeftCompositorState { fn request( - _state: &mut Self, + state: &mut Self, _client: &Client, resource: &ZweftShellWindowV1, request: zweft_shell_window_v1::Request, @@ -613,6 +614,27 @@ impl Dispatch for WeftCompositorState { } => { resource.configure(x, y, width, height, 0); } + zweft_shell_window_v1::Request::SetExclusiveZone { edge, size } => { + let is_panel = state + .weft_shell_state + .panels() + .any(|panel| panel == resource); + let edge = edge.into_result().ok(); + match edge { + Some(edge) if is_panel && size >= 0 => { + let zone = (size > 0).then_some((edge, size)); + *data + .exclusive_zone + .lock() + .unwrap_or_else(|p| p.into_inner()) = zone; + state.layout_app_windows(); + } + _ => resource.post_error( + crate::protocols::server::zweft_shell_window_v1::Error::InvalidExclusiveZone, + "only a panel may reserve an edge, with a size of at least 0", + ), + } + } } } } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index df39eb4..c3a3add 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -10,7 +10,7 @@ use smithay::desktop::Window; use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; use smithay::reexports::wayland_server::Resource; use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; -use smithay::utils::SERIAL_COUNTER; +use smithay::utils::{Logical, Rectangle, SERIAL_COUNTER}; use smithay::wayland::seat::WaylandFocus; use crate::protocols::WeftShellWindowData; @@ -121,6 +121,7 @@ impl WeftCompositorState { for window in &others { self.space.raise_element(window, false); } + self.layout_app_windows(); } /// Maps a new toplevel. The first window a session maps in its lifetime @@ -145,8 +146,11 @@ impl WeftCompositorState { self.space.map_element(window.clone(), (0, 0), false); if self.is_panel(&window) { self.fit_panels(); - } else if first { - self.activate_window(&window); + } else { + self.layout_app_windows(); + if first { + self.activate_window(&window); + } } } @@ -187,6 +191,66 @@ impl WeftCompositorState { } } + /// The part of the output that application windows occupy: the output + /// less the strips the panels reserved. + pub fn work_area(&self) -> Option> { + use crate::protocols::server::zweft_shell_window_v1::Edge; + let output = self.space.outputs().next()?; + let mut area = self.space.output_geometry(output)?; + for panel in self.weft_shell_state.panels() { + let Some(data) = panel.data::() else { + continue; + }; + let zone = *data + .exclusive_zone + .lock() + .unwrap_or_else(|p| p.into_inner()); + let Some((edge, size)) = zone else { + continue; + }; + match edge { + Edge::Top => { + let size = size.min(area.size.h); + area.loc.y += size; + area.size.h -= size; + } + Edge::Bottom => area.size.h -= size.min(area.size.h), + Edge::Left => { + let size = size.min(area.size.w); + area.loc.x += size; + area.size.w -= size; + } + Edge::Right => area.size.w -= size.min(area.size.w), + } + } + Some(area) + } + + /// Places every application window to fill the work area, so none + /// covers the panel's reserved strips. + pub fn layout_app_windows(&mut self) { + let Some(area) = self.work_area() else { + return; + }; + let apps: Vec = self + .space + .elements() + .filter(|window| !self.is_panel(window)) + .cloned() + .collect(); + for window in apps { + if let Some(toplevel) = window.toplevel() { + toplevel.with_pending_state(|state| { + state.size = Some(area.size); + state.states.set(xdg_toplevel::State::Maximized); + }); + toplevel.send_pending_configure(); + } + // Re-mapped in stacking order, so the order is kept. + self.space.map_element(window.clone(), area.loc, false); + } + } + /// Sends the configure for every toplevel whose pending state changed, /// such as its activated state. fn send_pending_configures(&self) { diff --git a/crates/weft-servo-shell/src/shell_client.rs b/crates/weft-servo-shell/src/shell_client.rs index 7af2270..67b4f2b 100644 --- a/crates/weft-servo-shell/src/shell_client.rs +++ b/crates/weft-servo-shell/src/shell_client.rs @@ -9,6 +9,11 @@ use crate::protocols::{ client::{zweft_shell_manager_v1, zweft_shell_window_v1}, }; +/// Height in logical pixels of the taskbar strip the system UI draws along +/// the bottom of the output; it matches `--taskbar-height` in +/// `infra/shell/system-ui.html`. +pub const TASKBAR_HEIGHT: i32 = 48; + // ── Window state ────────────────────────────────────────────────────────────── pub struct ShellWindowState { @@ -72,7 +77,7 @@ impl Dispatch for AppData { } = event && interface == "zweft_shell_manager_v1" { - let mgr = registry.bind::(name, version.min(2), qh, ()); + let mgr = registry.bind::(name, version.min(3), qh, ()); state.manager = Some(mgr); } } @@ -213,6 +218,11 @@ impl ShellClient { &qh, (), ); + // The taskbar's strip at the bottom of the output is kept free of + // application windows. + if window.version() >= 3 { + window.set_exclusive_zone(zweft_shell_window_v1::Edge::Bottom, TASKBAR_HEIGHT); + } data.window = Some(window); event_queue diff --git a/docs/architecture.md b/docs/architecture.md index dd74a4d..f8abe0e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/docs/building.md b/docs/building.md index fc6fed2..d13bbd9 100644 --- a/docs/building.md +++ b/docs/building.md @@ -88,7 +88,7 @@ python3 tests/frame/check_counter.py # Counter round trip through the session python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept python3 tests/frame/check_confinement.py # an app page reaches only its UI files and its session bridge -python3 tests/frame/check_activation.py # panel beneath two apps; launch and ACTIVATE_APP raise and focus +python3 tests/frame/check_activation.py # apps fill the work area above the taskbar; launch and ACTIVATE_APP raise and focus ``` ## Demo apps (wasm32-wasip2) diff --git a/docs/security.md b/docs/security.md index b18519a..d23d1cb 100644 --- a/docs/security.md +++ b/docs/security.md @@ -32,7 +32,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; window placement and close are not yet decided by the compositor per session (all toplevels start at the origin, and no work area is reserved for the taskbar, so an application window as large as the output hides it); only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; close is not yet decided by the compositor per session; application windows fill the work area the panel leaves, one in front at a time; only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. ## JavaScript Engine (SpiderMonkey) diff --git a/infra/shell/system-ui.html b/infra/shell/system-ui.html index 1b98e2f..dd019a6 100644 --- a/infra/shell/system-ui.html +++ b/infra/shell/system-ui.html @@ -9,6 +9,7 @@ color-scheme: dark; --surface-bg: rgba(255, 255, 255, 0.07); --surface-border: rgba(255, 255, 255, 0.12); + /* weft-servo-shell reserves this strip (TASKBAR_HEIGHT); keep them equal. */ --taskbar-height: 48px; --text-primary: rgba(255, 255, 255, 0.92); --text-secondary: rgba(255, 255, 255, 0.55); diff --git a/protocol/weft-shell-unstable-v1.xml b/protocol/weft-shell-unstable-v1.xml index f208b62..8ee68df 100644 --- a/protocol/weft-shell-unstable-v1.xml +++ b/protocol/weft-shell-unstable-v1.xml @@ -43,7 +43,7 @@ - + Bound once by weft-servo-shell. The manager owns the shell session. @@ -95,7 +95,7 @@ - + Represents one visual window. The compositor controls the effective @@ -121,6 +121,16 @@ summary="request sent after window_closed was received"/> + + + + + + + + + @@ -155,6 +165,19 @@ + + + Reserves a strip of the given size, in logical pixels, along an + edge of the output for shell chrome drawn by this panel, such as a + taskbar. The compositor places application windows only in the + work area that remains, so they never cover the strip. A size of 0 + releases the reservation; a later request replaces the earlier one. + Only a panel window may reserve a zone. + + + + + Sent by the compositor when the effective geometry or state diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index 3d46ee4..f5b3e1a 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -7,6 +7,8 @@ nested desktop (see session.py). Each application page switches between its colour and a lighter shade on every key press. In the presented pixels: - the panel fills the compositor output and stays beneath the applications; +- application windows fill the work area: the output less the strip the + shell reserves for its taskbar along the bottom, which stays visible; - each newly launched application is shown on top and receives keys; - ACTIVATE_APP, the request the taskbar sends, brings the other session's window back to the front with keyboard focus; @@ -35,6 +37,8 @@ APPS = { "org.weft.test.blue": ((0, 0, 200), (120, 120, 255)), } STARTUP_TIMEOUT = 120.0 +# The strip weft-servo-shell reserves at the bottom (TASKBAR_HEIGHT). +TASKBAR = 48 PANEL_PAGE = ('') @@ -97,10 +101,9 @@ def run(args, desktop, store, home): shot, panel = wait_for_color(desktop, GREEN, 60) if panel is None: raise AssertionError("the panel was not shown") - width, height, _ = parse_ppm(shot) - # The compositor sizes the panel to its whole output, which reaches the - # right and bottom edges of the nested desktop. - if panel[2] != width - 1 or panel[3] < height - 3: + # The compositor sizes the panel to its whole output, larger than a + # toplevel's default size. + if panel[2] - panel[0] + 1 < 1000 or panel[3] - panel[1] + 1 < 700: raise AssertionError(f"the panel does not fill the output: {panel}") t = Path(args.target) @@ -133,9 +136,13 @@ def run(args, desktop, store, home): box, _, _ = area(shot, color) hidden = all(area(shot, c)[0] is None for c in others) if box is not None and hidden: - # The panel stays visible beside the application window. - if area(shot, GREEN)[0] is None: - raise AssertionError("an application covered the panel") + # The application fills the work area: the panel's whole + # width, down to the reserved strip, which stays visible. + if box[0] != panel[0] or box[2] != panel[2] or box[1] != panel[1]: + raise AssertionError(f"{app_id} does not fill the work area: {box}") + if box[3] != panel[3] - TASKBAR: + raise AssertionError( + f"{app_id} does not end at the reserved strip: {box}, panel {panel}") return time.sleep(0.3) state = "pressed " if pressed else "" diff --git a/tests/frame/check_confinement.py b/tests/frame/check_confinement.py index c484905..d1ba9e7 100644 --- a/tests/frame/check_confinement.py +++ b/tests/frame/check_confinement.py @@ -52,8 +52,17 @@ APP_ID = "org.weft.test.confinement" PROBES = ["own-image", "bridge", "outside-image", "other-image", "linked-image", "http-image", "http-fetch", "websocket", "worker-fetch", "srcdoc-internals"] GREEN = (0, 192, 0) -# The app window's page area on the 1024x768 nested desktop. -PAGE_TOP, PAGE_HEIGHT, PAGE_X = 35, 600, 400 +# Where the probe bands are sampled across the app window's page. +PAGE_X = 400 +# The X root behind the nested compositor window. +ROOT_COLOUR = (0, 0, 0) + + +def page_rows(width, height, pixels): + """The first and last rows the app window's page covers at PAGE_X: the + compositor gives the application its whole work area.""" + rows = [y for y in range(height) if pixel(width, pixels, PAGE_X, y) != ROOT_COLOUR] + return (rows[0], rows[-1]) if rows else None def png(): @@ -169,10 +178,14 @@ def run(args, desktop, store, other_dir): deadline = time.monotonic() + 15 while True: - width, _, pixels = parse_ppm(desktop.capture()) + width, height, pixels = parse_ppm(desktop.capture()) + rows = page_rows(width, height, pixels) + if rows is None: + raise AssertionError("the test app's page is not on screen") + top, bottom = rows colours = {} for i, name in enumerate(PROBES): - y = PAGE_TOP + int((i + 0.5) * PAGE_HEIGHT / len(PROBES)) + y = top + int((i + 0.5) * (bottom - top + 1) / len(PROBES)) colours[name] = pixel(width, pixels, PAGE_X, y) if all(c == GREEN for c in colours.values()) or time.monotonic() > deadline: break diff --git a/tests/frame/check_notes.py b/tests/frame/check_notes.py index 2d562ee..a769e4a 100644 --- a/tests/frame/check_notes.py +++ b/tests/frame/check_notes.py @@ -35,14 +35,16 @@ from pathlib import Path sys.dont_write_bytecode = True from check_counter import AppdClient, free_port, read_endpoint # noqa: E402 -from session import ROOT, Desktop, SessionError, missing_tools # noqa: E402 +from session import ROOT, Desktop, SessionError, bounding_box, missing_tools, parse_ppm # noqa: E402 APP_ID = "org.weft.demo.notes" SEED = 'path C:\\new\\table "quoted"\tcaf\u00e9 \u2603' -# The Discard button in the 800x600 Notes window at the desktop's origin. -DISCARD = ("663", "68") +PAGE = (15, 15, 15) # The Notes page's background, #0f0f0f. +# The Discard button, right-aligned in the header: its offset from the +# window's right edge, and its height on the desktop. +DISCARD_FROM_RIGHT, DISCARD_Y = 137, 68 def runtime_pid(): @@ -87,6 +89,11 @@ def run(args, desktop, home, xdotool): if reply.get("type") != "APP_READY": raise AssertionError(f"Notes did not start: {reply}") time.sleep(2) + width, height, pixels = parse_ppm(desktop.capture()) + page = bounding_box(width, height, pixels, PAGE) + if page is None: + raise AssertionError("the Notes page is not on screen") + discard = (str(page[2] + 1 - DISCARD_FROM_RIGHT), str(DISCARD_Y)) def act(*arguments): subprocess.run([xdotool, *arguments], env={"DISPLAY": desktop.display}, @@ -125,7 +132,7 @@ def run(args, desktop, home, xdotool): os.kill(runtime, signal.SIGSTOP) try: - act("mousemove", "--sync", *DISCARD) + act("mousemove", "--sync", *discard) act("click", "1") act("mousemove", "--sync", "100", "300") act("click", "1") @@ -140,7 +147,7 @@ def run(args, desktop, home, xdotool): act("key", "ctrl+s") wait_for_file(notes, other + "!") notes.write_bytes(b"\xff not text") - act("mousemove", "--sync", *DISCARD) + act("mousemove", "--sync", *discard) act("click", "1") time.sleep(2) # The editor stays read-only and nothing can be saved over the file. @@ -151,7 +158,7 @@ def run(args, desktop, home, xdotool): time.sleep(2) if notes.read_bytes() != b"\xff not text": raise AssertionError("a failed load changed the stored notes") - act("mousemove", "--sync", *DISCARD) + act("mousemove", "--sync", *discard) notes.write_text("repaired", encoding="utf-8") act("click", "1") time.sleep(2) diff --git a/tests/frame/session.py b/tests/frame/session.py index 0b45837..c87ecf8 100644 --- a/tests/frame/session.py +++ b/tests/frame/session.py @@ -17,7 +17,8 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[2] -SCREEN = (1024, 768) +# Large enough to show the nested compositor's whole 1280x800 output. +SCREEN = (1400, 900) ANSI_ESCAPE = re.compile(r"\x1b\[[0-9;]*m") REQUIRED_TOOLS = ("Xvfb", "xwd", "convert") @@ -111,9 +112,11 @@ def _stop(process): class Desktop: """Xvfb plus weft-compositor; logs are written to `output/.log`.""" - def __init__(self, compositor, output, outputs=()): - """`outputs` names further files in `output` that this run will write.""" + def __init__(self, compositor, output, outputs=(), screen=SCREEN): + """`outputs` names further files in `output` that this run will write; + `screen` is the X screen size.""" self.compositor = Path(compositor) + self.screen = screen self.output = Path(output) self.own_outputs = ["xvfb.log", "compositor.log", "shell.log", *outputs] self.processes = [] @@ -180,7 +183,7 @@ class Desktop: try: xvfb = subprocess.Popen( ["Xvfb", "-displayfd", str(write_end), "-screen", "0", - f"{SCREEN[0]}x{SCREEN[1]}x24", "-nolisten", "tcp"], + f"{self.screen[0]}x{self.screen[1]}x24", "-nolisten", "tcp"], stdout=log, stderr=subprocess.STDOUT, env=self.env, start_new_session=True, pass_fds=(write_end,)) finally: From 8399914dfab3c508717594a3c9eed4bad53403f7 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:06:19 +0000 Subject: [PATCH 24/61] fix(compositor): show the shell's home on a panel click and release a dead panel's strip With applications filling the work area, the system UI's launcher, drawn in the panel beneath them, could not be seen while any application was open. Clicking the panel now brings it to the front as the shell's home until an application is activated again. A panel that is destroyed, or whose client goes away, releases its reserved strip and the applications are laid out again; dead panels no longer count toward the work area, so a restarted shell does not reserve the strip twice. A work area is never configured with a zero size. The zweft_shell configure for an application window, at creation and in reply to set_geometry, reports the work area the layout gives it, and a panel's reports the output. check_activation.py now clicks the taskbar strip, requires the panel to cover both applications, and activates one back. --- crates/weft-compositor/src/protocols/mod.rs | 7 +++ crates/weft-compositor/src/state.rs | 44 +++++++++++--- crates/weft-compositor/src/windows.rs | 66 +++++++++++++-------- docs/architecture.md | 2 +- docs/security.md | 2 +- tests/frame/check_activation.py | 21 +++++++ tests/frame/check_confinement.py | 3 +- 7 files changed, 110 insertions(+), 35 deletions(-) diff --git a/crates/weft-compositor/src/protocols/mod.rs b/crates/weft-compositor/src/protocols/mod.rs index 0993fad..7ef486f 100644 --- a/crates/weft-compositor/src/protocols/mod.rs +++ b/crates/weft-compositor/src/protocols/mod.rs @@ -54,6 +54,13 @@ impl WeftShellState { self.panels.push(window); } + /// Forgets a panel; returns whether it was one. + pub fn remove_panel(&mut self, window: &ZweftShellWindowV1) -> bool { + let before = self.panels.len(); + self.panels.retain(|panel| panel != window); + self.panels.len() != before + } + pub fn reconfigure_panels(&self, x: i32, y: i32, width: i32, height: i32) { for panel in &self.panels { if panel.is_alive() { diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index 363783f..6372dd0 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -577,7 +577,11 @@ impl Dispatch for WeftCompositorState { state.weft_shell_state.add_panel(window); state.fit_panels(); } else { - window.configure(x, y, width, height, 0); + // The compositor decides application geometry: the work + // area, whatever was requested. + let _ = (x, y, width, height); + let (x, y, w, h) = state.app_geometry(); + window.configure(x, y, w, h, 0); } } } @@ -606,13 +610,24 @@ impl Dispatch for WeftCompositorState { zweft_shell_window_v1::Request::UpdateMetadata { title, role } => { let _ = (title, role); } - zweft_shell_window_v1::Request::SetGeometry { - x, - y, - width, - height, - } => { - resource.configure(x, y, width, height, 0); + zweft_shell_window_v1::Request::SetGeometry { .. } => { + // The request is advisory: the configure reports the + // geometry the compositor's layout gives the window. + let is_panel = state + .weft_shell_state + .panels() + .any(|panel| panel == resource); + let (x, y, w, h) = if is_panel { + state.output_geometry() + } else { + state.app_geometry() + }; + let flags = if is_panel { + u32::from(crate::protocols::server::zweft_shell_window_v1::State::Maximized) + } else { + 0 + }; + resource.configure(x, y, w, h, flags); } zweft_shell_window_v1::Request::SetExclusiveZone { edge, size } => { let is_panel = state @@ -637,4 +652,17 @@ impl Dispatch for WeftCompositorState { } } } + + fn destroyed( + state: &mut Self, + _client: wayland_server::backend::ClientId, + resource: &ZweftShellWindowV1, + _data: &WeftShellWindowData, + ) { + // A panel that goes away, by destroy or with its client, releases + // its reserved strip; applications take the space back. + if state.weft_shell_state.remove_panel(resource) { + state.layout_app_windows(); + } + } } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index c3a3add..35352e4 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -1,10 +1,13 @@ //! Stacking, focus and activation of toplevel windows. //! -//! The compositor decides the order: the trusted shell's panel, which -//! fills the output, stays beneath every application window; a session's -//! first window, a clicked window and one appd asks to activate is raised to -//! the top, marked activated and given keyboard focus. When the focused -//! window closes, the topmost remaining application window gets focus. +//! The compositor decides the order and the geometry. The trusted shell's +//! panel fills the output and starts beneath every application window; +//! application windows fill the work area, the output less the strips the +//! panel reserves. A session's first window, a clicked window (the panel +//! included, which then shows the shell's home over the applications) and +//! one appd asks to activate is raised to the top, marked activated and +//! given keyboard focus. When the focused window closes, the topmost +//! remaining application window gets focus. use smithay::desktop::Window; use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; @@ -20,12 +23,15 @@ impl WeftCompositorState { /// Whether `surface` backs a window the trusted shell registered as its /// panel. pub fn is_panel_surface(&self, surface: &WlSurface) -> bool { - self.weft_shell_state.panels().any(|panel| { - panel - .data::() - .and_then(|data| data.surface.as_ref()) - .is_some_and(|s| s == surface) - }) + self.weft_shell_state + .panels() + .filter(|p| p.is_alive()) + .any(|panel| { + panel + .data::() + .and_then(|data| data.surface.as_ref()) + .is_some_and(|s| s == surface) + }) } fn is_panel(&self, window: &Window) -> bool { @@ -34,19 +40,11 @@ impl WeftCompositorState { .is_some_and(|surface| self.is_panel_surface(&surface)) } - /// Raises `window` above the other application windows, marks it - /// activated and gives it keyboard focus. A panel is focused but stays - /// beneath the applications. + /// Raises `window` to the top, marks it activated and gives it keyboard + /// focus. Activating the panel shows the shell's home, with its launcher, + /// over the applications until one is activated again. pub fn activate_window(&mut self, window: &Window) { - if self.is_panel(window) { - // The panel takes focus without rising; no application window - // stays marked activated. - for other in self.space.elements() { - other.set_activated(false); - } - } else { - self.space.raise_element(window, true); - } + self.space.raise_element(window, true); self.send_pending_configures(); if let (Some(keyboard), Some(surface)) = (self.seat.get_keyboard(), window.wl_surface()) { keyboard.set_focus( @@ -197,7 +195,7 @@ impl WeftCompositorState { use crate::protocols::server::zweft_shell_window_v1::Edge; let output = self.space.outputs().next()?; let mut area = self.space.output_geometry(output)?; - for panel in self.weft_shell_state.panels() { + for panel in self.weft_shell_state.panels().filter(|p| p.is_alive()) { let Some(data) = panel.data::() else { continue; }; @@ -223,9 +221,29 @@ impl WeftCompositorState { Edge::Right => area.size.w -= size.min(area.size.w), } } + // A zone covering the output still leaves applications a size the + // client must honour, never 0, which would let it pick its own. + area.size.w = area.size.w.max(1); + area.size.h = area.size.h.max(1); Some(area) } + /// The output's geometry as (x, y, width, height). + pub fn output_geometry(&self) -> (i32, i32, i32, i32) { + self.space + .outputs() + .next() + .and_then(|output| self.space.output_geometry(output)) + .map_or((0, 0, 0, 0), |g| (g.loc.x, g.loc.y, g.size.w, g.size.h)) + } + + /// The geometry the layout gives application windows, as + /// (x, y, width, height). + pub fn app_geometry(&self) -> (i32, i32, i32, i32) { + self.work_area() + .map_or((0, 0, 0, 0), |a| (a.loc.x, a.loc.y, a.size.w, a.size.h)) + } + /// Places every application window to fill the work area, so none /// covers the panel's reserved strips. pub fn layout_app_windows(&mut self) { diff --git a/docs/architecture.md b/docs/architecture.md index f8abe0e..4289c8e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and kept beneath application windows; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and starts beneath application windows, and clicking it brings it to the front as the shell's home, with its launcher, until an application is activated; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/docs/security.md b/docs/security.md index d23d1cb..d9c2bb3 100644 --- a/docs/security.md +++ b/docs/security.md @@ -32,7 +32,7 @@ For verified read-only package storage, `weft-pack build-image` produces an EROF Each app shell is given exactly one Wayland connection, created for its session. weft-appd creates a connected socket pair, gives one end to the app shell as its standard input with `WAYLAND_SOCKET=0` and no `WAYLAND_DISPLAY`, and sends the other end to the compositor over the compositor IPC socket (`SCM_RIGHTS`) with the session ID and app ID. The compositor adds that end as a client bound to the session; the binding comes from appd holding the connection, never from an ID or process ID the client supplies. A session's `zweft_shell` windows carry the session's app ID whatever the client requests, and the shell-only `panel` and `overlay` roles are refused with a protocol error; layer-shell surfaces and the input-method interface are not offered to session clients at all. When the session ends, the compositor closes its client; when the client disconnects, the compositor reports it to appd; when appd's connection ends, the compositor closes every session client appd attached. The compositor accepts one appd connection at a time. A launch is refused (503) while appd is not connected to the compositor, rather than starting an app that could never show a window; if the compositor connection is lost before the session's connection is handed over, the session is stopped before the app shell starts. Frames for appd that its socket cannot take at once are queued and retried; a backlog beyond 4 MiB, from an appd that stopped reading, ends the connection and with it the sessions' clients. -Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; close is not yet decided by the compositor per session; application windows fill the work area the panel leaves, one in front at a time; only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. +Not yet covered: the compositor IPC socket accepts any process of the user while no appd is connected, so the first connection after a restart is trusted as appd; close is not yet decided by the compositor per session; there is no user placement or resizing, since application windows fill the work area the panel leaves, one in front at a time; only the first window a session maps in its lifetime takes keyboard focus when it maps, and a client connected through the display socket takes focus on map only while no other such window is mapped; and the trusted system shell is identified only by connecting through the display socket, which any process of the user, including an app shell, can open. ## JavaScript Engine (SpiderMonkey) diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index f5b3e1a..cdd99d8 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -12,6 +12,8 @@ colour and a lighter shade on every key press. In the presented pixels: - each newly launched application is shown on top and receives keys; - ACTIVATE_APP, the request the taskbar sends, brings the other session's window back to the front with keyboard focus; +- clicking the taskbar strip brings the panel, the shell's home, over the + applications, and activating an application brings it back; - when the focused application ends, the remaining one gets keyboard focus; - ACTIVATE_APP for a session that is not running is refused. @@ -182,6 +184,25 @@ def run(args, desktop, store, home): activate(blue) shown(blue, pressed=True) + # A click on the taskbar strip shows the shell's home over the + # applications; activating one brings it back. + x, y = (panel[0] + panel[2]) // 2, panel[3] - TASKBAR // 2 + subprocess.run([args.xdotool, "mousemove", "--sync", str(x), str(y)], + env={"DISPLAY": desktop.display}, check=True, timeout=20) + subprocess.run([args.xdotool, "click", "1"], env={"DISPLAY": desktop.display}, + check=True, timeout=20) + deadline = time.monotonic() + 20 + while True: + shot = desktop.capture() + if area(shot, GREEN)[0] == panel and all( + area(shot, c)[0] is None for cs in APPS.values() for c in cs): + break + if time.monotonic() > deadline: + raise AssertionError("clicking the taskbar did not show the shell over the apps") + time.sleep(0.3) + activate(blue) + shown(blue, pressed=True) + # When the focused application ends, keyboard focus returns to the other. appd.send({"type": "TERMINATE_APP", "session_id": sessions[blue]}) shown(red, pressed=True) diff --git a/tests/frame/check_confinement.py b/tests/frame/check_confinement.py index d1ba9e7..4a07c40 100644 --- a/tests/frame/check_confinement.py +++ b/tests/frame/check_confinement.py @@ -60,7 +60,8 @@ ROOT_COLOUR = (0, 0, 0) def page_rows(width, height, pixels): """The first and last rows the app window's page covers at PAGE_X: the - compositor gives the application its whole work area.""" + compositor gives the application its whole work area. No shell runs in + this check, so every row of the output that is not the X root is page.""" rows = [y for y in range(height) if pixel(width, pixels, PAGE_X, y) != ROOT_COLOUR] return (rows[0], rows[-1]) if rows else None From cdc4a9e15dd59d5bf40a54a96c7bd9b8b5c5dfae Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:11:20 +0000 Subject: [PATCH 25/61] fix(compositor): keep the stacking order through layout changes Laying windows out re-mapped every application window, which smithay puts on top, so a session mapping another window, an exclusive zone change or an output resize pushed the shell's home back under the applications while it kept keyboard focus. Placement now re-maps every window in its current order. Panels are lowered only when they register, and a window that maps without taking focus goes beneath the window in front, so a session cannot cover the shell's home or another application by creating windows. A panel's zweft window that is destroyed while its surface lives also removes its window slot. --- crates/weft-compositor/src/backend/drm.rs | 4 +- crates/weft-compositor/src/backend/winit.rs | 2 +- crates/weft-compositor/src/state.rs | 8 +- crates/weft-compositor/src/windows.rs | 104 +++++++++----------- docs/architecture.md | 2 +- tests/frame/check_activation.py | 2 +- 6 files changed, 57 insertions(+), 65 deletions(-) diff --git a/crates/weft-compositor/src/backend/drm.rs b/crates/weft-compositor/src/backend/drm.rs index 71e93fb..51a56c2 100644 --- a/crates/weft-compositor/src/backend/drm.rs +++ b/crates/weft-compositor/src/backend/drm.rs @@ -550,7 +550,7 @@ fn connector_connected( tracing::info!(?name, "output connected"); let (pw, ph) = (wl_mode.size.w, wl_mode.size.h); state.weft_shell_state.reconfigure_panels(0, 0, pw, ph); - state.fit_panels(); + state.layout_app_windows(); state.weft_shell_state.retain_alive_panels(); render_output(state, node, crtc); } @@ -579,7 +579,7 @@ fn connector_disconnected( .map(|g| (g.size.w, g.size.h)) .unwrap_or((0, 0)); state.weft_shell_state.reconfigure_panels(0, 0, pw, ph); - state.fit_panels(); + state.layout_app_windows(); state.weft_shell_state.retain_alive_panels(); } diff --git a/crates/weft-compositor/src/backend/winit.rs b/crates/weft-compositor/src/backend/winit.rs index ede398e..cd11fad 100644 --- a/crates/weft-compositor/src/backend/winit.rs +++ b/crates/weft-compositor/src/backend/winit.rs @@ -124,7 +124,7 @@ pub fn run() -> anyhow::Result<()> { state .weft_shell_state .reconfigure_panels(0, 0, size.w, size.h); - state.fit_panels(); + state.layout_app_windows(); state.weft_shell_state.retain_alive_panels(); } WinitEvent::Input(input_event) => { diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index 6372dd0..8b2977d 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -657,11 +657,15 @@ impl Dispatch for WeftCompositorState { state: &mut Self, _client: wayland_server::backend::ClientId, resource: &ZweftShellWindowV1, - _data: &WeftShellWindowData, + data: &WeftShellWindowData, ) { // A panel that goes away, by destroy or with its client, releases - // its reserved strip; applications take the space back. + // its reserved strip and its window slot; applications take the + // space back. if state.weft_shell_state.remove_panel(resource) { + if let Some(surface) = &data.surface { + state.window_closed(surface); + } state.layout_app_windows(); } } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index 35352e4..ab176e9 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -6,8 +6,10 @@ //! panel reserves. A session's first window, a clicked window (the panel //! included, which then shows the shell's home over the applications) and //! one appd asks to activate is raised to the top, marked activated and -//! given keyboard focus. When the focused window closes, the topmost -//! remaining application window gets focus. +//! given keyboard focus; nothing else changes the order, and a window that +//! maps without taking focus goes beneath the window in front. When the +//! focused window closes, the topmost remaining application window gets +//! focus. use smithay::desktop::Window; use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; @@ -73,10 +75,19 @@ impl WeftCompositorState { } } - /// Sizes the panels to the output, places them at its origin and keeps - /// them beneath every application window. + /// Sizes the panels to the output and keeps them beneath every + /// application window; used when a panel registers. pub fn fit_panels(&mut self) { - let Some(geometry) = self + self.place_windows(true, None); + } + + /// Places every window for the current output and reservations: panels + /// fill the output, application windows the work area. The stacking + /// order is kept, except that `lower_panels` moves the panels beneath + /// the applications, and `below_top` slips that window under the + /// window that was on top. + pub fn place_windows(&mut self, lower_panels: bool, below_top: Option<&Window>) { + let Some(output) = self .space .outputs() .next() @@ -84,42 +95,36 @@ impl WeftCompositorState { else { return; }; - let panels: Vec = self - .space - .elements() - .filter(|window| self.is_panel(window)) - .cloned() - .collect(); - if panels.is_empty() { + let Some(area) = self.work_area() else { return; + }; + let mut order: Vec = self.space.elements().cloned().collect(); + if lower_panels { + let (panels, apps): (Vec, Vec) = + order.into_iter().partition(|window| self.is_panel(window)); + order = panels.into_iter().chain(apps).collect(); } - for panel in &panels { - if let Some(toplevel) = panel.toplevel() { + if let Some(window) = below_top + && let Some(position) = order.iter().position(|w| w == window) + && position + 1 == order.len() + && order.len() > 1 + { + order.swap(position, position - 1); + } + for window in &order { + let panel = self.is_panel(window); + let geometry = if panel { output } else { area }; + if let Some(toplevel) = window.toplevel() { toplevel.with_pending_state(|state| { state.size = Some(geometry.size); state.states.set(xdg_toplevel::State::Maximized); }); toplevel.send_pending_configure(); - tracing::debug!( - width = geometry.size.w, - height = geometry.size.h, - "panel fitted to the output" - ); } - self.space.map_element(panel.clone(), geometry.loc, false); + // Mapping puts a window on top; mapping all of them in `order` + // leaves exactly that order. + self.space.map_element(window.clone(), geometry.loc, false); } - // Raising every other window, in its current order, leaves the - // panels at the bottom. - let others: Vec = self - .space - .elements() - .filter(|window| !self.is_panel(window)) - .cloned() - .collect(); - for window in &others { - self.space.raise_element(window, false); - } - self.layout_app_windows(); } /// Maps a new toplevel. The first window a session maps in its lifetime @@ -144,11 +149,13 @@ impl WeftCompositorState { self.space.map_element(window.clone(), (0, 0), false); if self.is_panel(&window) { self.fit_panels(); - } else { + } else if first { self.layout_app_windows(); - if first { - self.activate_window(&window); - } + self.activate_window(&window); + } else { + // A window that does not take focus does not cover the window + // in front either, the shell's home included. + self.place_windows(false, Some(&window)); } } @@ -244,29 +251,10 @@ impl WeftCompositorState { .map_or((0, 0, 0, 0), |a| (a.loc.x, a.loc.y, a.size.w, a.size.h)) } - /// Places every application window to fill the work area, so none - /// covers the panel's reserved strips. + /// Lays the windows out again after the work area changed, keeping the + /// stacking order. pub fn layout_app_windows(&mut self) { - let Some(area) = self.work_area() else { - return; - }; - let apps: Vec = self - .space - .elements() - .filter(|window| !self.is_panel(window)) - .cloned() - .collect(); - for window in apps { - if let Some(toplevel) = window.toplevel() { - toplevel.with_pending_state(|state| { - state.size = Some(area.size); - state.states.set(xdg_toplevel::State::Maximized); - }); - toplevel.send_pending_configure(); - } - // Re-mapped in stacking order, so the order is kept. - self.space.map_element(window.clone(), area.loc, false); - } + self.place_windows(false, None); } /// Sends the configure for every toplevel whose pending state changed, diff --git a/docs/architecture.md b/docs/architecture.md index 4289c8e..2d995d3 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and starts beneath application windows, and clicking it brings it to the front as the shell's home, with its launcher, until an application is activated; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and starts beneath application windows, and clicking it brings it to the front as the shell's home, with its launcher, until an application is activated or opens its first window; layout changes keep the stacking order, and a window that maps without taking focus goes beneath the window in front; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index cdd99d8..3d69843 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -6,7 +6,7 @@ panel, and weft-appd with two test applications, one red and one blue, on a nested desktop (see session.py). Each application page switches between its colour and a lighter shade on every key press. In the presented pixels: -- the panel fills the compositor output and stays beneath the applications; +- the panel fills the compositor output and starts beneath the applications; - application windows fill the work area: the output less the strip the shell reserves for its taskbar along the bottom, which stays visible; - each newly launched application is shown on top and receives keys; From c8bae1cc4d063785f0b6d4fbea56d88e2035492f Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:14:31 +0000 Subject: [PATCH 26/61] fix(compositor): hand focus on when a registering panel is lowered A shell restarted while applications run took keyboard focus before it registered as the panel and kept it after being lowered beneath them; the window in front now gets it. Without an output, windows are still reordered, so a window that maps without taking focus does not cover the one in front. --- crates/weft-compositor/src/state.rs | 3 ++- crates/weft-compositor/src/windows.rs | 39 +++++++++++++++++++-------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index 8b2977d..cdd8445 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -661,7 +661,8 @@ impl Dispatch for WeftCompositorState { ) { // A panel that goes away, by destroy or with its client, releases // its reserved strip and its window slot; applications take the - // space back. + // space back. The surface's window is not mapped again if it + // registers once more. if state.weft_shell_state.remove_panel(resource) { if let Some(surface) = &data.surface { state.window_closed(surface); diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index ab176e9..4e5f319 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -6,8 +6,9 @@ //! panel reserves. A session's first window, a clicked window (the panel //! included, which then shows the shell's home over the applications) and //! one appd asks to activate is raised to the top, marked activated and -//! given keyboard focus; nothing else changes the order, and a window that -//! maps without taking focus goes beneath the window in front. When the +//! given keyboard focus; apart from a panel being lowered when it +//! registers, nothing else changes the order, and a window that maps +//! without taking focus goes beneath the window in front. When the //! focused window closes, the topmost remaining application window gets //! focus. @@ -79,6 +80,21 @@ impl WeftCompositorState { /// application window; used when a panel registers. pub fn fit_panels(&mut self) { self.place_windows(true, None); + // A shell that took focus before registering as the panel, such as + // one restarted while applications run, hands it to the window that + // is now in front. + let focus_on_panel = self + .seat + .get_keyboard() + .and_then(|keyboard| keyboard.current_focus()) + .is_some_and(|focus| self.is_panel_surface(&focus)); + let front = self.space.elements().last().cloned(); + if focus_on_panel + && let Some(front) = front + && !self.is_panel(&front) + { + self.activate_window(&front); + } } /// Places every window for the current output and reservations: panels @@ -87,17 +103,14 @@ impl WeftCompositorState { /// the applications, and `below_top` slips that window under the /// window that was on top. pub fn place_windows(&mut self, lower_panels: bool, below_top: Option<&Window>) { - let Some(output) = self + // Without an output the order still changes; windows keep their + // size and place until one is mapped. + let output = self .space .outputs() .next() - .and_then(|output| self.space.output_geometry(output)) - else { - return; - }; - let Some(area) = self.work_area() else { - return; - }; + .and_then(|output| self.space.output_geometry(output)); + let area = self.work_area(); let mut order: Vec = self.space.elements().cloned().collect(); if lower_panels { let (panels, apps): (Vec, Vec) = @@ -113,7 +126,11 @@ impl WeftCompositorState { } for window in &order { let panel = self.is_panel(window); - let geometry = if panel { output } else { area }; + let Some(geometry) = (if panel { output } else { area }) else { + let location = self.space.element_location(window).unwrap_or_default(); + self.space.map_element(window.clone(), location, false); + continue; + }; if let Some(toplevel) = window.toplevel() { toplevel.with_pending_state(|state| { state.size = Some(geometry.size); From 828984be4bdf34dc8bb670c3c9014d0d5c09dd60 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:22:59 +0000 Subject: [PATCH 27/61] fix(compositor): close the focused window on Alt+F4 and let the shells exit without crashing There was no compositor-owned way to close an application window. Alt+F4 is now taken by the compositor: it sends xdg_toplevel close to the focused application window, never the panel, and the key does not reach the client. Closing exposed a crash in both Servo hosts: their shell-protocol client, created on winit's Wayland display, was dropped after that display was torn down, so wl_proxy_destroy touched freed memory and the process ended with SIGSEGV. The hosts now release Servo and the shell client in winit's exiting handler, while the display is alive. check_activation.py closes the focused application with Alt+F4 and requires its session to stop with the app shell exiting with status 0, the window to disappear, and Alt+F4 with the panel focused to leave the panel in place. --- crates/weft-app-shell/src/embedder.rs | 8 ++++++++ crates/weft-compositor/src/input.rs | 17 +++++++++++++++-- crates/weft-compositor/src/windows.rs | 25 +++++++++++++++++++++++++ crates/weft-servo-shell/src/embedder.rs | 8 ++++++++ docs/architecture.md | 2 +- tests/frame/check_activation.py | 25 +++++++++++++++++++++++++ 6 files changed, 82 insertions(+), 3 deletions(-) diff --git a/crates/weft-app-shell/src/embedder.rs b/crates/weft-app-shell/src/embedder.rs index 5f7529f..1cffa8d 100644 --- a/crates/weft-app-shell/src/embedder.rs +++ b/crates/weft-app-shell/src/embedder.rs @@ -431,6 +431,14 @@ impl App { } impl ApplicationHandler for App { + /// Releases Servo and the shell protocol objects while winit's Wayland + /// display is still connected; dropped later, the shell client's + /// proxies would be destroyed on a display that is gone. + fn exiting(&mut self, _event_loop: &ActiveEventLoop) { + self.shut_down(); + self.shell_client = None; + } + fn resumed(&mut self, event_loop: &ActiveEventLoop) { if self.window.is_some() { return; diff --git a/crates/weft-compositor/src/input.rs b/crates/weft-compositor/src/input.rs index ef7d72d..bf64e6d 100644 --- a/crates/weft-compositor/src/input.rs +++ b/crates/weft-compositor/src/input.rs @@ -48,14 +48,27 @@ fn handle_keyboard(state: &mut WeftCompositorState, event: B::K let key_state = event.state(); if let Some(keyboard) = state.seat.get_keyboard() { - keyboard.input::<(), _>( + // Alt+F4 belongs to the compositor: it asks the focused application + // window to close, and the key never reaches a client. + let close = keyboard.input::( state, event.key_code(), key_state, serial, time, - |_state, _mods, _keysym| FilterResult::Forward, + |_state, mods, keysym| { + if mods.alt + && keysym.modified_sym() == smithay::input::keyboard::keysyms::KEY_F4.into() + { + FilterResult::Intercept(key_state == smithay::backend::input::KeyState::Pressed) + } else { + FilterResult::Forward + } + }, ); + if close == Some(true) { + state.close_focused_window(); + } } } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index 4e5f319..7bfd476 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -58,6 +58,31 @@ impl WeftCompositorState { } } + /// Asks the focused application window to close. Its client decides + /// how to end; for an app shell that ends the session. The panel is not + /// closed this way. + pub fn close_focused_window(&mut self) { + let Some(focus) = self + .seat + .get_keyboard() + .and_then(|keyboard| keyboard.current_focus()) + else { + return; + }; + if self.is_panel_surface(&focus) { + return; + } + let window = self + .space + .elements() + .find(|window| window.wl_surface().is_some_and(|s| *s == focus)) + .cloned(); + if let Some(toplevel) = window.as_ref().and_then(|w| w.toplevel()) { + tracing::info!(session = ?window.as_ref().and_then(window_session), "closing the focused window"); + toplevel.send_close(); + } + } + /// Activates the topmost window of `session_id`; returns whether the /// session has one. pub fn activate_session(&mut self, session_id: u64) -> bool { diff --git a/crates/weft-servo-shell/src/embedder.rs b/crates/weft-servo-shell/src/embedder.rs index db30713..1c22d03 100644 --- a/crates/weft-servo-shell/src/embedder.rs +++ b/crates/weft-servo-shell/src/embedder.rs @@ -346,6 +346,14 @@ impl App { } impl ApplicationHandler for App { + /// Releases Servo and the shell protocol objects while winit's Wayland + /// display is still connected; dropped later, the shell client's + /// proxies would be destroyed on a display that is gone. + fn exiting(&mut self, _event_loop: &ActiveEventLoop) { + self.shut_down(); + self.shell_client = None; + } + fn resumed(&mut self, event_loop: &ActiveEventLoop) { if self.window.is_some() { return; diff --git a/docs/architecture.md b/docs/architecture.md index 2d995d3..d850dbe 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,7 +8,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web ### weft-compositor -Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and starts beneath application windows, and clicking it brings it to the front as the shell's home, with its launcher, until an application is activated or opens its first window; layout changes keep the stacking order, and a window that maps without taking focus goes beneath the window in front; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. +Smithay-based Wayland compositor. Implements the `zweft-shell-unstable-v1` protocol extension, which allows shell clients (servo-shell, app-shell) to register their surfaces as typed shell slots. Each application session's Wayland connection is handed to the compositor by weft-appd, which binds the client to that session (see `security.md`). The compositor owns stacking and focus: the system shell's panel is sized to the output and starts beneath application windows, and clicking it brings it to the front as the shell's home, with its launcher, until an application is activated or opens its first window; layout changes keep the stacking order, and a window that maps without taking focus goes beneath the window in front; a session's first window, a clicked window and an activated one are raised, marked activated and given keyboard focus, and when the focused window closes the topmost remaining application window gets focus. Alt+F4 is the compositor's: it asks the focused application window to close (never the panel), and the app shell then exits and its session ends. A session's later windows are shown without taking focus. The panel reserves a strip along an output edge for the taskbar with `set_exclusive_zone` (weft-servo-shell reserves 48 logical pixels at the bottom), and the compositor lays every application window out to fill the remaining work area. weft-appd's `ACTIVATE_APP` request, which the taskbar sends, asks the compositor to activate a running session's window. Supports DRM/KMS and winit (software/dev) backends. ### weft-servo-shell diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index 3d69843..f1ba160 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -15,6 +15,8 @@ colour and a lighter shade on every key press. In the presented pixels: - clicking the taskbar strip brings the panel, the shell's home, over the applications, and activating an application brings it back; - when the focused application ends, the remaining one gets keyboard focus; +- Alt+F4 asks the focused application window to close; its app shell exits + cleanly and the session ends; Alt+F4 does not close the shell's panel; - ACTIVATE_APP for a session that is not running is refused. The applications are unsigned test packages, recorded as development content @@ -209,6 +211,29 @@ def run(args, desktop, store, home): key("a") shown(red) + # Alt+F4 closes the focused application window, which ends its session; + # with the panel focused, it closes nothing. + key("alt+F4") + try: + appd.wait_for(lambda m: m.get("type") == "APP_STATE" and m.get("state") == "stopped" + and m.get("session_id") == sessions[red], 20) + except TimeoutError: + raise AssertionError("Alt+F4 did not end the focused application's session") + # The app shell ended by closing, with exit status 0, not by a crash. + clean = (f'stopping session session_id={sessions[red]} ' + 'reason="app shell exited (Ok(ExitStatus(unix_wait_status(0))))"') + if clean not in desktop.log_text("appd"): + raise AssertionError("the closed app shell did not exit cleanly; see appd.log") + deadline = time.monotonic() + 20 + while any(area(desktop.capture(), c)[0] is not None for cs in APPS.values() for c in cs): + if time.monotonic() > deadline: + raise AssertionError("the closed application is still shown") + time.sleep(0.3) + key("alt+F4") + time.sleep(2) + if area(desktop.capture(), GREEN)[0] != panel: + raise AssertionError("Alt+F4 closed or moved the shell's panel") + appd.send({"type": "ACTIVATE_APP", "session_id": 999}) reply = appd.wait_for(lambda m: m.get("type") == "ERROR", 10) if reply.get("code") != 404: From 102c20d2b40973bcebd7347c184d4711289df981 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:29:22 +0000 Subject: [PATCH 28/61] fix(shells): release the rendering context and window before the display goes away The software rendering path (softbuffer) and the EGL path keep their own Wayland objects on winit's display, so dropping the rendering context after the event loop could destroy proxies on a disconnected display just like the shell client did. Both hosts now release the rendering context and the window in winit's exiting handler as well. The compositor records the keys whose press a shortcut took and takes only those releases, so no client sees a release without its press or keeps a key pressed. check_activation.py watches the panel for six seconds after Alt+F4 instead of sampling it once. --- crates/weft-app-shell/src/embedder.rs | 11 ++++++++--- crates/weft-compositor/src/input.rs | 22 +++++++++++++++------- crates/weft-compositor/src/state.rs | 4 ++++ crates/weft-servo-shell/src/embedder.rs | 11 ++++++++--- tests/frame/check_activation.py | 8 +++++--- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/crates/weft-app-shell/src/embedder.rs b/crates/weft-app-shell/src/embedder.rs index 1cffa8d..74e72ab 100644 --- a/crates/weft-app-shell/src/embedder.rs +++ b/crates/weft-app-shell/src/embedder.rs @@ -431,12 +431,17 @@ impl App { } impl ApplicationHandler for App { - /// Releases Servo and the shell protocol objects while winit's Wayland - /// display is still connected; dropped later, the shell client's - /// proxies would be destroyed on a display that is gone. + /// Releases everything that holds Wayland objects of winit's display + /// (Servo, the rendering context, whose software and EGL paths keep + /// their own proxies on it, the shell protocol client and the window) + /// while that display is still connected; dropped after the event loop, + /// they would be destroyed on a display that is gone. A panic unwinding + /// out of a handler skips this. fn exiting(&mut self, _event_loop: &ActiveEventLoop) { self.shut_down(); + self.rendering_context = None; self.shell_client = None; + self.window = None; } fn resumed(&mut self, event_loop: &ActiveEventLoop) { diff --git a/crates/weft-compositor/src/input.rs b/crates/weft-compositor/src/input.rs index bf64e6d..e5e1721 100644 --- a/crates/weft-compositor/src/input.rs +++ b/crates/weft-compositor/src/input.rs @@ -56,14 +56,22 @@ fn handle_keyboard(state: &mut WeftCompositorState, event: B::K key_state, serial, time, - |_state, mods, keysym| { - if mods.alt - && keysym.modified_sym() == smithay::input::keyboard::keysyms::KEY_F4.into() - { - FilterResult::Intercept(key_state == smithay::backend::input::KeyState::Pressed) - } else { - FilterResult::Forward + |state, mods, keysym| { + // A release is taken only when its press was, so no client + // sees half of a key. + let code = keysym.raw_code().raw(); + if key_state == smithay::backend::input::KeyState::Pressed { + if mods.alt + && keysym.modified_sym() == smithay::input::keyboard::keysyms::KEY_F4.into() + { + state.suppressed_keys.push(code); + return FilterResult::Intercept(true); + } + } else if let Some(i) = state.suppressed_keys.iter().position(|&k| k == code) { + state.suppressed_keys.remove(i); + return FilterResult::Intercept(false); } + FilterResult::Forward }, ); if close == Some(true) { diff --git a/crates/weft-compositor/src/state.rs b/crates/weft-compositor/src/state.rs index cdd8445..79b701c 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -98,6 +98,9 @@ pub struct WeftCompositorState { pub loop_signal: LoopSignal, pub loop_handle: LoopHandle<'static, WeftCompositorState>, pub gesture_state: GestureState, + /// Keycodes whose press a compositor shortcut took, so their release is + /// taken as well and no client sees half of a key. + pub suppressed_keys: Vec, pub compositor_state: CompositorState, pub xdg_shell_state: XdgShellState, @@ -196,6 +199,7 @@ impl WeftCompositorState { dmabuf_global: None, running: true, gesture_state: GestureState::default(), + suppressed_keys: Vec::new(), #[cfg(unix)] appd_ipc: None, #[cfg(target_os = "linux")] diff --git a/crates/weft-servo-shell/src/embedder.rs b/crates/weft-servo-shell/src/embedder.rs index 1c22d03..ffc5ab1 100644 --- a/crates/weft-servo-shell/src/embedder.rs +++ b/crates/weft-servo-shell/src/embedder.rs @@ -346,12 +346,17 @@ impl App { } impl ApplicationHandler for App { - /// Releases Servo and the shell protocol objects while winit's Wayland - /// display is still connected; dropped later, the shell client's - /// proxies would be destroyed on a display that is gone. + /// Releases everything that holds Wayland objects of winit's display + /// (Servo, the rendering context, whose software and EGL paths keep + /// their own proxies on it, the shell protocol client and the window) + /// while that display is still connected; dropped after the event loop, + /// they would be destroyed on a display that is gone. A panic unwinding + /// out of a handler skips this. fn exiting(&mut self, _event_loop: &ActiveEventLoop) { self.shut_down(); + self.rendering_context = None; self.shell_client = None; + self.window = None; } fn resumed(&mut self, event_loop: &ActiveEventLoop) { diff --git a/tests/frame/check_activation.py b/tests/frame/check_activation.py index f1ba160..f011aba 100755 --- a/tests/frame/check_activation.py +++ b/tests/frame/check_activation.py @@ -230,9 +230,11 @@ def run(args, desktop, store, home): raise AssertionError("the closed application is still shown") time.sleep(0.3) key("alt+F4") - time.sleep(2) - if area(desktop.capture(), GREEN)[0] != panel: - raise AssertionError("Alt+F4 closed or moved the shell's panel") + deadline = time.monotonic() + 6 + while time.monotonic() < deadline: + if area(desktop.capture(), GREEN)[0] != panel: + raise AssertionError("Alt+F4 closed or moved the shell's panel") + time.sleep(0.5) appd.send({"type": "ACTIVATE_APP", "session_id": 999}) reply = appd.wait_for(lambda m: m.get("type") == "ERROR", 10) From 80d7451abf30745437299c3be11f0f189a2f75a0 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:36:29 +0000 Subject: [PATCH 29/61] fix(appd): close a running session as a user close would before terminating it TERMINATE_APP, which the taskbar sends, killed the session's processes at once, so an application had no chance to finish, for example to save. A running session is now asked to close through the compositor (AppCloseRequest, which sends xdg_toplevel close to its windows) and the app shell is given five seconds to exit by itself before what remains is terminated. The stop reason records whether the app closed or was terminated after the timeout. TERMINATE_APP replies stopping and the session stays registered until it settles; a starting session is stopped at once, and appd shutting down terminates sessions without a close. check_counter.py requires the compositor to ask the session's window to close and the app shell to exit with status 0 on the close; a unit test requires an app that ignores the close to be terminated after the timeout and not before. --- crates/weft-appd/src/main.rs | 137 +++++++++++++++++++++++-- crates/weft-appd/src/runtime.rs | 37 ++++++- crates/weft-compositor/src/appd_ipc.rs | 4 + crates/weft-compositor/src/windows.rs | 14 +++ crates/weft-ipc-types/src/lib.rs | 4 + docs/architecture.md | 2 +- tests/frame/check_counter.py | 12 ++- 7 files changed, 196 insertions(+), 14 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 2bf1e61..3a13d3c 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -86,10 +86,19 @@ impl SessionRegistry { }) } + /// Asks a session to stop: a running session is closed as a user close + /// would close it, a starting one is stopped at once. The session stays + /// registered, Stopping, until its supervisor settles it. fn terminate(&mut self, session_id: u64) -> bool { - let found = self.sessions.remove(&session_id).is_some(); - self.abort_senders.remove(&session_id); - found + match self.abort_senders.remove(&session_id) { + Some(sender) => { + let _ = sender.send(()); + self.set_state(session_id, AppStateKind::Stopping); + true + } + // A session no supervisor runs has nothing to settle. + None => self.sessions.remove(&session_id).is_some(), + } } pub(crate) fn register_abort(&mut self, session_id: u64) -> tokio::sync::oneshot::Receiver<()> { @@ -532,10 +541,10 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { Request::TerminateApp { session_id } => { let found = registry.lock().await.terminate(session_id); if found { - tracing::info!(session_id, "terminated"); + tracing::info!(session_id, "stop requested"); Response::AppState { session_id, - state: AppStateKind::Stopped, + state: AppStateKind::Stopping, } } else { Response::Error { @@ -915,7 +924,7 @@ mod tests { } #[tokio::test] - async fn dispatch_terminate_known_returns_stopped() { + async fn dispatch_terminate_known_returns_stopping() { let _env = env_lock().lock().await; let reg = make_registry(); let ack = dispatch( @@ -930,11 +939,13 @@ mod tests { Response::LaunchAck { session_id, .. } => session_id, _ => panic!("expected LaunchAck"), }; + // The reply says the session is stopping; Stopped is broadcast once + // its supervisor has settled it. let resp = dispatch(Request::TerminateApp { session_id }, ®).await; assert!(matches!( resp, Response::AppState { - state: AppStateKind::Stopped, + state: AppStateKind::Stopping, .. } )); @@ -1349,6 +1360,115 @@ mod tests { ); } + #[tokio::test(flavor = "current_thread")] + async fn an_app_that_ignores_a_close_is_terminated_after_the_timeout() { + use std::os::unix::fs::PermissionsExt; + let _env = env_lock().lock().await; + use_test_runtime_dir(); + let dir = std::env::temp_dir().join(format!("weft_test_close_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let app = dir.join("store/org.example.close"); + write_test_package(&app, "org.example.close", ""); + // Both children report ready and then ignore everything. + let child = dir.join("child.sh"); + std::fs::write( + &child, + "#!/bin/sh\necho READY $WEFT_READY_TOKEN\nexec sleep 30\n", + ) + .unwrap(); + std::fs::set_permissions(&child, std::fs::Permissions::from_mode(0o755)).unwrap(); + let vars = [ + ("WEFT_RUNTIME_BIN", child.clone().into_os_string()), + ("WEFT_APP_SHELL_BIN", child.clone().into_os_string()), + ("WEFT_DISABLE_CGROUP", "1".into()), + ("WEFT_APP_STORE", dir.join("store").into_os_string()), + ]; + let prior: Vec<_> = vars + .iter() + .map(|(k, _)| (*k, std::env::var_os(k))) + .collect(); + for (key, value) in &vars { + // SAFETY: env_lock is held on a current_thread runtime. + unsafe { std::env::set_var(key, value) }; + } + + let registry = make_registry(); + let (tx, mut compositor) = tokio::sync::mpsc::channel(8); + registry.lock().await.compositor_tx = Some(compositor_client::CompositorSender::new( + tx, + std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)), + )); + let mut rx = registry.lock().await.subscribe(); + let ack = dispatch( + Request::LaunchApp { + app_id: "org.example.close".into(), + surface_id: 0, + }, + ®istry, + ) + .await; + let Response::LaunchAck { session_id, .. } = ack else { + panic!("{ack:?}"); + }; + let ready = tokio::time::timeout(std::time::Duration::from_secs(10), async { + while !matches!(rx.recv().await, Ok(Response::AppReady { .. })) {} + }) + .await; + let started = std::time::Instant::now(); + let reply = dispatch(Request::TerminateApp { session_id }, ®istry).await; + let stopped = tokio::time::timeout(std::time::Duration::from_secs(15), async { + while !matches!( + rx.recv().await, + Ok(Response::AppState { + state: AppStateKind::Stopped, + .. + }) + ) {} + }) + .await; + let elapsed = started.elapsed(); + let mut sent = Vec::new(); + while let Ok(out) = compositor.try_recv() { + sent.push(out.msg); + } + + for (key, value) in prior { + // SAFETY: as above. + unsafe { + match value { + Some(v) => std::env::set_var(key, v), + None => std::env::remove_var(key), + } + } + } + let _ = std::fs::remove_dir_all(&dir); + assert!(ready.is_ok(), "the session did not become ready"); + assert!( + matches!( + reply, + Response::AppState { + state: AppStateKind::Stopping, + .. + } + ), + "{reply:?}" + ); + assert!(stopped.is_ok(), "the session did not stop"); + // The app was asked to close, given the timeout, then terminated. + assert!( + sent.iter().any(|m| matches!( + m, + weft_ipc_types::AppdToCompositor::AppCloseRequest { session_id: s } if *s == session_id + )), + "{sent:?}" + ); + assert!(elapsed >= runtime::CLOSE_TIMEOUT, "{elapsed:?}"); + assert!( + elapsed < runtime::CLOSE_TIMEOUT + std::time::Duration::from_secs(5), + "{elapsed:?}" + ); + } + #[tokio::test(flavor = "current_thread")] async fn without_a_connected_compositor_nothing_is_started() { use std::os::unix::fs::PermissionsExt; @@ -1746,9 +1866,10 @@ mod tests { .await .unwrap(); + // The session stays registered until it settles, then is Stopped. assert!(matches!( registry.lock().await.state(session_id), - AppStateKind::NotFound + AppStateKind::Stopped )); let broadcast = rx.try_recv(); diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 95e8fbd..d0adc53 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -449,7 +449,11 @@ pub(crate) async fn supervise( status = session.app_shell.as_mut().expect("app shell spawned").wait() => { format!("app shell exited ({status:?})") } - _ = &mut abort_rx => "terminate requested".to_owned(), + requested = &mut abort_rx => match requested { + Ok(()) => session.close(session_id).await, + // The sender is dropped only when appd shuts down. + Err(_) => "appd is shutting down".to_owned(), + }, _ = session.relay.as_mut().expect("relay opened").ended() => { "component closed its IPC connection".to_owned() } @@ -457,6 +461,10 @@ pub(crate) async fn supervise( session.settle(®istry, &reason).await } +/// How long an application may take to close after being asked, for +/// example to save, before its processes are terminated. +pub(crate) const CLOSE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); + /// Creates the session's Wayland connection and queues the compositor's end /// for the compositor, returning the app shell's end. fn attach_client( @@ -514,6 +522,33 @@ struct OwnedSession { } impl OwnedSession { + /// Closes a running session as a user close would: the compositor asks + /// its windows to close and the app shell gets `CLOSE_TIMEOUT` to exit by + /// itself. Returns the terminal reason; settling terminates whatever is + /// still running. + async fn close(&mut self, session_id: u64) -> String { + let asked = self.client_attached + && self.compositor_tx.as_ref().is_some_and(|tx| { + tx.is_connected() + && tx + .try_send(AppdToCompositor::AppCloseRequest { session_id }.into()) + .is_ok() + }); + if !asked { + return "terminate requested; the compositor could not ask the app to close".to_owned(); + } + let Some(app_shell) = self.app_shell.as_mut() else { + return "terminate requested".to_owned(); + }; + match tokio::time::timeout(CLOSE_TIMEOUT, app_shell.wait()).await { + Ok(status) => format!("closed on request; app shell exited ({status:?})"), + Err(_) => format!( + "close requested; the app shell did not exit within {} s and was terminated", + CLOSE_TIMEOUT.as_secs() + ), + } + } + async fn settle(self, registry: &Registry, reason: &str) -> anyhow::Result<()> { let session_id = self.session_id; tracing::info!(session_id, reason, "stopping session"); diff --git a/crates/weft-compositor/src/appd_ipc.rs b/crates/weft-compositor/src/appd_ipc.rs index f6e85ab..6421ebf 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -285,6 +285,10 @@ fn handle_message(state: &mut WeftCompositorState, msg: AppdToCompositor) { .kill_client(client, DisconnectReason::ConnectionClosed); } } + AppdToCompositor::AppCloseRequest { session_id } => { + let closed = state.close_session_windows(session_id); + tracing::info!(session_id, windows = closed, "session asked to close"); + } AppdToCompositor::AppFocusRequest { session_id } => { if state.activate_session(session_id) { tracing::info!(session_id, "session activated"); diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs index 7bfd476..2d7b527 100644 --- a/crates/weft-compositor/src/windows.rs +++ b/crates/weft-compositor/src/windows.rs @@ -83,6 +83,20 @@ impl WeftCompositorState { } } + /// Asks every window of `session_id` to close; returns how many. + pub fn close_session_windows(&mut self, session_id: u64) -> usize { + let mut closed = 0; + for window in self.space.elements() { + if window_session(window) == Some(session_id) + && let Some(toplevel) = window.toplevel() + { + toplevel.send_close(); + closed += 1; + } + } + closed + } + /// Activates the topmost window of `session_id`; returns whether the /// session has one. pub fn activate_session(&mut self, session_id: u64) -> bool { diff --git a/crates/weft-ipc-types/src/lib.rs b/crates/weft-ipc-types/src/lib.rs index c7d74f8..0aab42f 100644 --- a/crates/weft-ipc-types/src/lib.rs +++ b/crates/weft-ipc-types/src/lib.rs @@ -26,6 +26,10 @@ pub enum AppdToCompositor { AppFocusRequest { session_id: u64, }, + /// Asks the session's windows to close, as a user close would. + AppCloseRequest { + session_id: u64, + }, } /// Messages sent from weft-compositor to weft-appd. diff --git a/docs/architecture.md b/docs/architecture.md index d850dbe..7b06432 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -20,7 +20,7 @@ Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signal ### weft-appd -Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. +Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd replies `stopping`, asks the compositor to close the session's windows, and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it; the stop reason records which happened, and `stopped` is broadcast once the session has settled. A session still starting is stopped at once, and when appd itself shuts down its sessions are terminated without a close. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. ### weft-runtime diff --git a/tests/frame/check_counter.py b/tests/frame/check_counter.py index ed4a8e1..cb45450 100644 --- a/tests/frame/check_counter.py +++ b/tests/frame/check_counter.py @@ -220,13 +220,17 @@ def run(args, desktop, xdotool): if states[3] != states[1]: raise AssertionError("count after ArrowDown does not match the count after one ArrowUp") - # Ending the session closes its client in the compositor. + # Ending the session closes the app as a user close would: the + # compositor asks its window to close and the app shell exits by itself. appd.send({"type": "TERMINATE_APP", "session_id": session_id}) deadline = time.monotonic() + 15 - closed = f"closing the client of an ended session session_id={session_id}" - while closed not in desktop.log_text("compositor"): + asked = f"session asked to close session_id={session_id} windows=1" + closed = (f'stopping session session_id={session_id} reason="closed on request; ' + 'app shell exited (Ok(ExitStatus(unix_wait_status(0))))"') + while asked not in desktop.log_text("compositor") or closed not in desktop.log_text("appd"): if time.monotonic() > deadline: - raise AssertionError(f"the compositor did not close the session's client: {closed!r}") + raise AssertionError("the session did not close on request; see compositor.log " + "and appd.log") time.sleep(0.3) From 0866befc16b6a1cae6b6848edbf4e108f64146ff Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:41:30 +0000 Subject: [PATCH 30/61] fix(appd): keep a closing session registered until it settles A repeated TERMINATE_APP during the close wait no longer removes the session from the registry. The close wait also watches the runtime and the IPC relay, and reports a failed app shell exit as a failure rather than a close. A session being closed is not saved for restore, only the 32 most recent stopped sessions stay registered, and the taskbar shows an entry as closing until its session stops. --- crates/weft-appd/src/main.rs | 79 ++++++++++++++++++++++++++++++++- crates/weft-appd/src/runtime.rs | 33 ++++++++++---- docs/architecture.md | 2 +- infra/shell/system-ui.html | 15 ++++++- tests/frame/check_counter.py | 3 +- 5 files changed, 120 insertions(+), 12 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 3a13d3c..277c40a 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -27,6 +27,10 @@ struct SessionEntry { bridge_token: Option, } +/// How many stopped sessions stay registered, so their final state can +/// still be queried. +const KEPT_STOPPED: usize = 32; + struct SessionRegistry { next_id: u64, sessions: std::collections::HashMap, @@ -52,6 +56,7 @@ impl Default for SessionRegistry { impl SessionRegistry { fn launch(&mut self, app_id: &str) -> u64 { + self.prune_stopped(); self.next_id += 1; let id = self.next_id; self.sessions.insert( @@ -71,6 +76,24 @@ impl SessionRegistry { id } + /// Forgets the oldest stopped sessions beyond `KEPT_STOPPED`, so the + /// registry does not grow with every launch. + fn prune_stopped(&mut self) { + let mut stopped: Vec = self + .sessions + .iter() + .filter(|(_, e)| matches!(e.state, AppStateKind::Stopped)) + .map(|(&id, _)| id) + .collect(); + if stopped.len() <= KEPT_STOPPED { + return; + } + stopped.sort_unstable(); + for id in &stopped[..stopped.len() - KEPT_STOPPED] { + self.sessions.remove(id); + } + } + pub(crate) fn bridge_token(&self, session_id: u64) -> Option { self.sessions.get(&session_id)?.bridge_token.clone() } @@ -96,6 +119,9 @@ impl SessionRegistry { self.set_state(session_id, AppStateKind::Stopping); true } + // A session already closing keeps closing; it stays registered + // until its supervisor settles it. + None if matches!(self.state(session_id), AppStateKind::Stopping) => true, // A session no supervisor runs has nothing to settle. None => self.sessions.remove(&session_id).is_some(), } @@ -118,10 +144,12 @@ impl SessionRegistry { .collect() } + /// The apps to restore at the next start: a session the user asked to + /// close is not among them. fn running_app_ids(&self) -> Vec { self.sessions .values() - .filter(|e| !matches!(e.state, AppStateKind::Stopped)) + .filter(|e| !matches!(e.state, AppStateKind::Stopped | AppStateKind::Stopping)) .map(|e| e.app_id.clone()) .collect() } @@ -1416,6 +1444,19 @@ mod tests { .await; let started = std::time::Instant::now(); let reply = dispatch(Request::TerminateApp { session_id }, ®istry).await; + // A second request while the app is closing changes nothing: the + // session stays registered and listed until it settles. + let again = dispatch(Request::TerminateApp { session_id }, ®istry).await; + let closing = { + let reg = registry.lock().await; + ( + reg.state(session_id), + reg.running_sessions() + .iter() + .any(|s| s.session_id == session_id), + reg.running_app_ids(), + ) + }; let stopped = tokio::time::timeout(std::time::Duration::from_secs(15), async { while !matches!( rx.recv().await, @@ -1453,6 +1494,24 @@ mod tests { ), "{reply:?}" ); + assert!( + matches!( + again, + Response::AppState { + state: AppStateKind::Stopping, + .. + } + ), + "{again:?}" + ); + assert!( + matches!(closing.0, AppStateKind::Stopping), + "{:?}", + closing.0 + ); + assert!(closing.1, "a closing session is no longer listed"); + // An app the user closed is not restored at the next start. + assert!(closing.2.is_empty(), "{:?}", closing.2); assert!(stopped.is_ok(), "the session did not stop"); // The app was asked to close, given the timeout, then terminated. assert!( @@ -1469,6 +1528,24 @@ mod tests { ); } + #[test] + fn only_the_latest_stopped_sessions_stay_registered() { + let mut reg = SessionRegistry::default(); + let first = reg.launch("org.example.a"); + let running = reg.launch("org.example.b"); + reg.set_state(running, AppStateKind::Running); + reg.set_state(first, AppStateKind::Stopped); + for _ in 0..KEPT_STOPPED { + let id = reg.launch("org.example.c"); + reg.set_state(id, AppStateKind::Stopped); + } + let last = reg.launch("org.example.d"); + assert!(matches!(reg.state(first), AppStateKind::NotFound)); + assert!(matches!(reg.state(running), AppStateKind::Running)); + assert!(matches!(reg.state(last - 1), AppStateKind::Stopped)); + assert_eq!(reg.sessions.len(), KEPT_STOPPED + 2); + } + #[tokio::test(flavor = "current_thread")] async fn without_a_connected_compositor_nothing_is_started() { use std::os::unix::fs::PermissionsExt; diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index d0adc53..8ef2dee 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -537,16 +537,33 @@ impl OwnedSession { if !asked { return "terminate requested; the compositor could not ask the app to close".to_owned(); } - let Some(app_shell) = self.app_shell.as_mut() else { + let (Some(app_shell), Some(runtime), Some(relay)) = ( + self.app_shell.as_mut(), + self.runtime.as_mut(), + self.relay.as_mut(), + ) else { return "terminate requested".to_owned(); }; - match tokio::time::timeout(CLOSE_TIMEOUT, app_shell.wait()).await { - Ok(status) => format!("closed on request; app shell exited ({status:?})"), - Err(_) => format!( - "close requested; the app shell did not exit within {} s and was terminated", - CLOSE_TIMEOUT.as_secs() - ), - } + let wait = async { + tokio::select! { + status = app_shell.wait() => match status { + Ok(status) if status.success() => { + format!("closed on request; app shell exited ({status:?})") + } + status => format!("app shell failed while closing ({status:?})"), + }, + status = runtime.wait() => format!("runtime exited while closing ({status:?})"), + _ = relay.ended() => "component closed its IPC connection while closing".to_owned(), + } + }; + tokio::time::timeout(CLOSE_TIMEOUT, wait) + .await + .unwrap_or_else(|_| { + format!( + "close requested; the app shell did not exit within {} s and was terminated", + CLOSE_TIMEOUT.as_secs() + ) + }) } async fn settle(self, registry: &Registry, reason: &str) -> anyhow::Result<()> { diff --git a/docs/architecture.md b/docs/architecture.md index 7b06432..c600cdc 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -20,7 +20,7 @@ Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signal ### weft-appd -Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd replies `stopping`, asks the compositor to close the session's windows, and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it; the stop reason records which happened, and `stopped` is broadcast once the session has settled. A session still starting is stopped at once, and when appd itself shuts down its sessions are terminated without a close. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. +Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd replies `stopping`, asks the compositor to close the session's windows, and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it; the stop reason records which happened (a clean exit, a failure while closing, or the timeout), and `stopped` is broadcast once the session has settled. A repeated `TERMINATE_APP` while the app is closing changes nothing; the timeout is the only forced termination, and a session being closed is not restored at the next start. appd keeps the final state of the 32 most recently stopped sessions. A session still starting is stopped at once, and when appd itself shuts down its sessions are terminated without a close. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. ### weft-runtime diff --git a/infra/shell/system-ui.html b/infra/shell/system-ui.html index dd019a6..bf28f85 100644 --- a/infra/shell/system-ui.html +++ b/infra/shell/system-ui.html @@ -320,6 +320,8 @@ } else if (msg.type === 'APP_STATE') { if (msg.state === 'stopped') { removeTaskbarEntry(msg.session_id); + } else if (msg.state === 'stopping') { + markTaskbarEntryClosing(msg.session_id); } } else if (msg.type === 'RUNNING_APPS') { msg.sessions.forEach(function (s) { @@ -391,7 +393,7 @@ var close = document.createElement('span'); close.className = 'task-close'; close.textContent = '×'; - close.title = 'Terminate'; + close.title = 'Close'; close.addEventListener('click', function (e) { e.stopPropagation(); if (ws && ws.readyState === WebSocket.OPEN) { @@ -411,6 +413,17 @@ taskbar.insertBefore(el, clock); } + // The app has been asked to close and may take a few seconds, for + // example to save; the entry stays until the session has stopped. + function markTaskbarEntryClosing(sessionId) { + var el = document.getElementById('task-' + sessionId); + if (!el) { return; } + el.style.opacity = '0.5'; + el.title += ' (closing)'; + var close = el.querySelector('.task-close'); + if (close) { close.remove(); } + } + function removeTaskbarEntry(sessionId) { var el = document.getElementById('task-' + sessionId); if (el) { el.remove(); } diff --git a/tests/frame/check_counter.py b/tests/frame/check_counter.py index cb45450..2118376 100644 --- a/tests/frame/check_counter.py +++ b/tests/frame/check_counter.py @@ -225,8 +225,9 @@ def run(args, desktop, xdotool): appd.send({"type": "TERMINATE_APP", "session_id": session_id}) deadline = time.monotonic() + 15 asked = f"session asked to close session_id={session_id} windows=1" + # appd records "closed on request" only when the app shell exits cleanly. closed = (f'stopping session session_id={session_id} reason="closed on request; ' - 'app shell exited (Ok(ExitStatus(unix_wait_status(0))))"') + 'app shell exited') while asked not in desktop.log_text("compositor") or closed not in desktop.log_text("appd"): if time.monotonic() > deadline: raise AssertionError("the session did not close on request; see compositor.log " From e72c55c87cbcc23f03197297435901b52ca07e68 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 16:45:25 +0000 Subject: [PATCH 31/61] fix(appd): tell every client that a session is closing TERMINATE_APP broadcasts the Stopping state, so every system UI marks the entry. A closing taskbar entry is marked once and no longer sends ACTIVATE_APP. The docs describe which stopped sessions are kept. --- crates/weft-appd/src/main.rs | 13 ++++++++++++- docs/architecture.md | 2 +- infra/shell/system-ui.html | 4 +++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 277c40a..8cb687d 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -567,7 +567,18 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { Response::LaunchAck { session_id, app_id } } Request::TerminateApp { session_id } => { - let found = registry.lock().await.terminate(session_id); + let found = { + let mut reg = registry.lock().await; + let found = reg.terminate(session_id); + if found && matches!(reg.state(session_id), AppStateKind::Stopping) { + // Every client, not only the requester, shows the close. + let _ = reg.broadcast().send(Response::AppState { + session_id, + state: AppStateKind::Stopping, + }); + } + found + }; if found { tracing::info!(session_id, "stop requested"); Response::AppState { diff --git a/docs/architecture.md b/docs/architecture.md index c600cdc..d0448bc 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -20,7 +20,7 @@ Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signal ### weft-appd -Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd replies `stopping`, asks the compositor to close the session's windows, and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it; the stop reason records which happened (a clean exit, a failure while closing, or the timeout), and `stopped` is broadcast once the session has settled. A repeated `TERMINATE_APP` while the app is closing changes nothing; the timeout is the only forced termination, and a session being closed is not restored at the next start. appd keeps the final state of the 32 most recently stopped sessions. A session still starting is stopped at once, and when appd itself shuts down its sessions are terminated without a close. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. +Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd replies `stopping`, asks the compositor to close the session's windows, and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it; the stop reason records which happened (a clean exit, a failure while closing, or the timeout), and `stopped` is broadcast once the session has settled. A repeated `TERMINATE_APP` while the app is closing changes nothing; the timeout is the only forced termination, and a session being closed is not restored at the next start. appd keeps the final state of at most 32 stopped sessions, forgetting those launched earliest. A session still starting is stopped at once, and when appd itself shuts down its sessions are terminated without a close. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. ### weft-runtime diff --git a/infra/shell/system-ui.html b/infra/shell/system-ui.html index bf28f85..25c6157 100644 --- a/infra/shell/system-ui.html +++ b/infra/shell/system-ui.html @@ -404,6 +404,7 @@ el.appendChild(close); // Choosing the entry brings the app's window to the front. el.addEventListener('click', function () { + if (el.dataset.closing) { return; } if (ws && ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: 'ACTIVATE_APP', session_id: sessionId })); } @@ -417,7 +418,8 @@ // example to save; the entry stays until the session has stopped. function markTaskbarEntryClosing(sessionId) { var el = document.getElementById('task-' + sessionId); - if (!el) { return; } + if (!el || el.dataset.closing) { return; } + el.dataset.closing = '1'; el.style.opacity = '0.5'; el.title += ' (closing)'; var close = el.querySelector('.task-close'); From 551df84562f0598f73689e8945bbee2a2ae4e940 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:00:08 +0000 Subject: [PATCH 32/61] feat: let an application decline a close to keep unsaved changes A package declares its unsaved-change policy in [ui].close. With "ask", the app shell dispatches a cancelable weftclose event before the window closes and waits up to 2 s for the page. A page that cancels the close keeps running, and the app shell reports this to weft-appd with its readiness token, which returns the session to running. The page closes itself later with window.close(). Notes declares "ask". With unsaved changes it offers Save and close, Close without saving, and Cancel. It closes after a save only once the save is acknowledged. The taskbar shows a declined close as running again. --- crates/weft-app-shell/src/embedder.rs | 114 +++++++++++++++++- crates/weft-appd/src/launch.rs | 6 +- crates/weft-appd/src/main.rs | 123 ++++++++++++++++++++ crates/weft-appd/src/runtime.rs | 122 ++++++++++++++++---- crates/weft-ipc-types/src/manifest.rs | 43 +++++++ docs/architecture.md | 13 ++- docs/building.md | 2 +- examples/org.weft.demo.notes/signature.sig | 2 +- examples/org.weft.demo.notes/ui/index.html | 61 +++++++++- examples/org.weft.demo.notes/wapp.toml | 1 + infra/shell/system-ui.html | 20 ++-- tests/frame/check_notes.py | 127 ++++++++++++++++++++- 12 files changed, 589 insertions(+), 45 deletions(-) diff --git a/crates/weft-app-shell/src/embedder.rs b/crates/weft-app-shell/src/embedder.rs index 74e72ab..987eac2 100644 --- a/crates/weft-app-shell/src/embedder.rs +++ b/crates/weft-app-shell/src/embedder.rs @@ -68,6 +68,33 @@ struct FrameSignals { content: AtomicBool, /// The document has loaded and Servo reports its rendering is up to date. settled: AtomicBool, + /// The page closed itself with `window.close()`. + page_closed: AtomicBool, + /// The page's answer to the pending close request; see `CloseAnswer`. + close_answer: std::sync::atomic::AtomicU8, +} + +/// The page's answer to a close request, as stored in `close_answer`. +#[derive(Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +enum CloseAnswer { + Pending = 0, + Close = 1, + Cancel = 2, +} + +/// How long a page with the `ask` close policy has to answer a close +/// request; a page that does not answer in time is closed. +const CLOSE_ANSWER_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2); + +/// Asks the page whether it may close: dispatches a cancelable `weftclose` +/// event on the top-level window. The page cancels the close by calling +/// `preventDefault()` and closes itself later with `window.close()`. +const ASK_CLOSE_SCRIPT: &str = "window.dispatchEvent(new Event('weftclose', { cancelable: true }))"; + +/// The application's declared close policy, from `WEFT_APP_CLOSE`. +fn close_policy_asks() -> bool { + std::env::var("WEFT_APP_CLOSE").is_ok_and(|v| v == "ask") } struct WeftWebViewDelegate { @@ -87,6 +114,12 @@ impl WebViewDelegate for WeftWebViewDelegate { self.signals.redraw.store(true, Ordering::Relaxed); } + /// The application page called `window.close()`, which Servo allows for + /// a top-level document with a single history entry. + fn notify_closed(&self, _webview: servo::WebView) { + self.signals.page_closed.store(true, Ordering::Relaxed); + } + /// The webview carries the session's bridge, so it stays within the /// application's own UI files. fn request_navigation(&self, _webview: servo::WebView, request: NavigationRequest) { @@ -368,6 +401,10 @@ struct App { modifiers: ModifiersState, cursor_pos: DevicePoint, shell_client: Option, + /// Whether the page is asked before its window closes. + ask_before_close: bool, + /// When the pending close request is closed regardless of the page. + close_deadline: Option, } impl App { @@ -394,9 +431,70 @@ impl App { modifiers: ModifiersState::default(), cursor_pos: DevicePoint::origin(), shell_client: None, + ask_before_close: close_policy_asks(), + close_deadline: None, } } + /// Handles a request to close the window. Under the `ask` policy the + /// page answers first; otherwise, or without a page, the app closes. + fn request_close(&mut self, event_loop: &ActiveEventLoop) { + if self.close_deadline.is_some() { + return; + } + let Some(webview) = self.webview.as_ref().filter(|_| self.ask_before_close) else { + self.shut_down(); + event_loop.exit(); + return; + }; + self.signals + .close_answer + .store(CloseAnswer::Pending as u8, Ordering::Relaxed); + self.close_deadline = Some(std::time::Instant::now() + CLOSE_ANSWER_TIMEOUT); + let signals = Arc::clone(&self.signals); + webview.evaluate_javascript(ASK_CLOSE_SCRIPT, move |result| { + // dispatchEvent returns false when the page cancelled the event. + let answer = match result { + Ok(servo::JSValue::Boolean(false)) => CloseAnswer::Cancel, + Ok(_) => CloseAnswer::Close, + Err(e) => { + tracing::warn!(?e, "the page could not be asked to close; closing"); + CloseAnswer::Close + } + }; + signals.close_answer.store(answer as u8, Ordering::Relaxed); + }); + } + + /// Acts on the page's answer to a pending close request, or on its + /// absence once the answer is due. Returns whether the app is closing. + fn settle_close_request(&mut self) -> bool { + let Some(deadline) = self.close_deadline else { + return false; + }; + let answer = self.signals.close_answer.load(Ordering::Relaxed); + if answer == CloseAnswer::Cancel as u8 { + self.close_deadline = None; + tracing::info!("the page cancelled the close"); + // weft-appd learns that the session keeps running; the token + // keeps page output from forging this report. + if let Ok(token) = std::env::var("WEFT_READY_TOKEN") { + use std::io::Write; + println!("CLOSE_CANCELLED {token}"); + let _ = std::io::stdout().flush(); + } + return false; + } + if answer == CloseAnswer::Close as u8 { + return true; + } + if std::time::Instant::now() >= deadline { + tracing::warn!("the page did not answer the close request in time; closing"); + return true; + } + false + } + /// Paints and presents once Servo has requested its first repaint. /// /// Nothing is presented before that request; the first frames can still be @@ -546,6 +644,17 @@ impl ApplicationHandler for App { if let Some(servo) = &self.servo { servo.spin_event_loop(); } + // The page answers a close request while Servo spins. + if self.signals.page_closed.load(Ordering::Relaxed) || self.settle_close_request() { + self.shut_down(); + event_loop.exit(); + return; + } + // Wake for the close answer's deadline even when nothing else happens. + event_loop.set_control_flow(match self.close_deadline { + Some(deadline) => winit::event_loop::ControlFlow::WaitUntil(deadline), + None => winit::event_loop::ControlFlow::Wait, + }); if self.signals.redraw.swap(false, Ordering::Relaxed) && let Some(w) = &self.window { @@ -605,10 +714,7 @@ impl ApplicationHandler for App { ))); } } - WindowEvent::CloseRequested => { - self.shut_down(); - event_loop.exit(); - } + WindowEvent::CloseRequested => self.request_close(event_loop), _ => {} } } diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 85b5bbc..19efb4f 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf}; -use weft_ipc_types::manifest::{MANIFEST_FILE, Manifest, ManifestError, entry_path}; +use weft_ipc_types::manifest::{ClosePolicy, MANIFEST_FILE, Manifest, ManifestError, entry_path}; use weft_ipc_types::package::{ImageFiles, is_valid_app_id}; use crate::grants::Refusal; @@ -20,6 +20,8 @@ pub(crate) struct LaunchPackage { pub(crate) module: PathBuf, /// The UI document, from `[ui].entry`. pub(crate) ui_entry: PathBuf, + /// What a user close does, from `[ui].close`. + pub(crate) close: ClosePolicy, pub(crate) capabilities: Vec, /// The image the package root is mounted from, held for as long as the /// session runs. @@ -110,6 +112,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result = vars + .iter() + .map(|(k, _)| (*k, std::env::var_os(k))) + .collect(); + for (key, value) in &vars { + // SAFETY: env_lock is held on a current_thread runtime. + unsafe { std::env::set_var(key, value) }; + } + + let registry = make_registry(); + let (tx, _compositor) = tokio::sync::mpsc::channel(8); + registry.lock().await.compositor_tx = Some(compositor_client::CompositorSender::new( + tx, + std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true)), + )); + let mut rx = registry.lock().await.subscribe(); + let ack = dispatch( + Request::LaunchApp { + app_id: "org.example.cancel".into(), + surface_id: 0, + }, + ®istry, + ) + .await; + let Response::LaunchAck { session_id, .. } = ack else { + panic!("{ack:?}"); + }; + let ready = tokio::time::timeout(std::time::Duration::from_secs(10), async { + while !matches!(rx.recv().await, Ok(Response::AppReady { .. })) {} + }) + .await; + let started = std::time::Instant::now(); + dispatch(Request::TerminateApp { session_id }, ®istry).await; + // The session is running again once the genuine report arrives. + let resumed = tokio::time::timeout(std::time::Duration::from_secs(5), async { + loop { + match rx.recv().await { + Ok(Response::AppState { + state: AppStateKind::Running, + .. + }) => return true, + Ok(Response::AppState { + state: AppStateKind::Stopped, + .. + }) => return false, + _ => {} + } + } + }) + .await; + let resumed_after = started.elapsed(); + let state = registry.lock().await.state(session_id); + // A second request asks again; this time nothing declines it. + dispatch(Request::TerminateApp { session_id }, ®istry).await; + let stopped = tokio::time::timeout(std::time::Duration::from_secs(10), async { + while !matches!( + rx.recv().await, + Ok(Response::AppState { + state: AppStateKind::Stopped, + .. + }) + ) {} + }) + .await; + + for (key, value) in prior { + // SAFETY: as above. + unsafe { + match value { + Some(v) => std::env::set_var(key, v), + None => std::env::remove_var(key), + } + } + } + let _ = std::fs::remove_dir_all(&dir); + assert!(ready.is_ok(), "the session did not become ready"); + assert_eq!( + resumed, + Ok(true), + "the cancelled close did not resume the session" + ); + // The forged report one second in did not count. + assert!( + resumed_after >= std::time::Duration::from_millis(1500), + "{resumed_after:?}" + ); + assert!(matches!(state, AppStateKind::Running), "{state:?}"); + assert!(stopped.is_ok(), "the second close did not stop the session"); + } + #[tokio::test(flavor = "current_thread")] async fn an_app_that_ignores_a_close_is_terminated_after_the_timeout() { use std::os::unix::fs::PermissionsExt; diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 8ef2dee..ed86f62 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -116,6 +116,8 @@ const READY_TIMEOUT: Duration = Duration::from_secs(30); /// runtime passes the guest only the variables it names and pages cannot read /// the host process environment. const READY_TOKEN_ENV: &str = "WEFT_READY_TOKEN"; +/// The application's close policy, `immediate` or `ask`, for the app shell. +const CLOSE_POLICY_ENV: &str = "WEFT_APP_CLOSE"; /// Environment variable carrying the session's application bridge credential /// to weft-app-shell. @@ -222,6 +224,7 @@ fn spawn_app_shell( .arg("--ui") .arg(&package.ui_entry) .env(READY_TOKEN_ENV, token) + .env(CLOSE_POLICY_ENV, package.close.as_str()) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::piped()) .kill_on_drop(true) @@ -430,7 +433,13 @@ pub(crate) async fn supervise( Ok(reader) => reader, Err(reason) => return session.settle(®istry, &reason).await, }; - tokio::spawn(drain_stdout(shell_stdout, session_id)); + let (cancel_tx, mut cancelled) = tokio::sync::mpsc::channel(1); + tokio::spawn(watch_app_shell_stdout( + shell_stdout, + session_id, + token.clone(), + cancel_tx, + )); { let mut reg = registry.lock().await; @@ -442,25 +451,76 @@ pub(crate) async fn supervise( } tracing::info!(session_id, %app_id, "app ready"); - let reason = tokio::select! { - status = session.runtime.as_mut().expect("runtime spawned").wait() => { - format!("runtime exited ({status:?})") - } - status = session.app_shell.as_mut().expect("app shell spawned").wait() => { - format!("app shell exited ({status:?})") - } - requested = &mut abort_rx => match requested { - Ok(()) => session.close(session_id).await, - // The sender is dropped only when appd shuts down. - Err(_) => "appd is shutting down".to_owned(), - }, - _ = session.relay.as_mut().expect("relay opened").ended() => { - "component closed its IPC connection".to_owned() + let reason = loop { + tokio::select! { + status = session.runtime.as_mut().expect("runtime spawned").wait() => { + break format!("runtime exited ({status:?})"); + } + status = session.app_shell.as_mut().expect("app shell spawned").wait() => { + break format!("app shell exited ({status:?})"); + } + requested = &mut abort_rx => match requested { + Ok(()) => match session.close(session_id, &mut cancelled).await { + Close::Settle(reason) => break reason, + // The application kept running, as its close policy + // allows; a later request asks again. + Close::Cancelled => { + let mut reg = registry.lock().await; + abort_rx = reg.register_abort(session_id); + reg.set_state(session_id, AppStateKind::Running); + let _ = reg.broadcast().send(Response::AppState { + session_id, + state: AppStateKind::Running, + }); + tracing::info!(session_id, "the application cancelled the close"); + } + }, + // The sender is dropped only when appd shuts down. + Err(_) => break "appd is shutting down".to_owned(), + }, + _ = session.relay.as_mut().expect("relay opened").ended() => { + break "component closed its IPC connection".to_owned(); + } } }; session.settle(®istry, &reason).await } +/// How a close request ended. +enum Close { + /// The session ends, for this reason. + Settle(String), + /// The application declined the close and keeps running. + Cancelled, +} + +/// The app shell's report that its page declined a close, followed by the +/// session's readiness token so page output cannot fake it. +const CLOSE_CANCELLED: &str = "CLOSE_CANCELLED"; + +/// Passes the app shell's close reports on to the supervisor and logs +/// everything else it prints. +async fn watch_app_shell_stdout( + mut reader: BufReader, + session_id: u64, + token: String, + cancelled: tokio::sync::mpsc::Sender<()>, +) { + while let Ok(Some(line)) = read_child_line(&mut reader).await { + if line + .strip_prefix(CLOSE_CANCELLED) + .and_then(|rest| rest.strip_prefix(' ')) + .is_some_and(|t| crate::ws::tokens_match(&token, t)) + { + // One pending report is enough; the supervisor drops stale ones + // before it asks again. + let _ = cancelled.try_send(()); + } else { + tracing::debug!(session_id, stdout = %line, "child stdout"); + } + } +} + /// How long an application may take to close after being asked, for /// example to save, before its processes are terminated. pub(crate) const CLOSE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); @@ -526,7 +586,14 @@ impl OwnedSession { /// its windows to close and the app shell gets `CLOSE_TIMEOUT` to exit by /// itself. Returns the terminal reason; settling terminates whatever is /// still running. - async fn close(&mut self, session_id: u64) -> String { + async fn close( + &mut self, + session_id: u64, + cancelled: &mut tokio::sync::mpsc::Receiver<()>, + ) -> Close { + // A report from an earlier close, for example one the user started + // in the compositor, does not answer this one. + while cancelled.try_recv().is_ok() {} let asked = self.client_attached && self.compositor_tx.as_ref().is_some_and(|tx| { tx.is_connected() @@ -535,34 +602,41 @@ impl OwnedSession { .is_ok() }); if !asked { - return "terminate requested; the compositor could not ask the app to close".to_owned(); + return Close::Settle( + "terminate requested; the compositor could not ask the app to close".to_owned(), + ); } let (Some(app_shell), Some(runtime), Some(relay)) = ( self.app_shell.as_mut(), self.runtime.as_mut(), self.relay.as_mut(), ) else { - return "terminate requested".to_owned(); + return Close::Settle("terminate requested".to_owned()); }; let wait = async { tokio::select! { - status = app_shell.wait() => match status { + status = app_shell.wait() => Close::Settle(match status { Ok(status) if status.success() => { format!("closed on request; app shell exited ({status:?})") } status => format!("app shell failed while closing ({status:?})"), - }, - status = runtime.wait() => format!("runtime exited while closing ({status:?})"), - _ = relay.ended() => "component closed its IPC connection while closing".to_owned(), + }), + status = runtime.wait() => { + Close::Settle(format!("runtime exited while closing ({status:?})")) + } + _ = relay.ended() => Close::Settle( + "component closed its IPC connection while closing".to_owned(), + ), + Some(()) = cancelled.recv() => Close::Cancelled, } }; tokio::time::timeout(CLOSE_TIMEOUT, wait) .await .unwrap_or_else(|_| { - format!( + Close::Settle(format!( "close requested; the app shell did not exit within {} s and was terminated", CLOSE_TIMEOUT.as_secs() - ) + )) }) } diff --git a/crates/weft-ipc-types/src/manifest.rs b/crates/weft-ipc-types/src/manifest.rs index 0115bc4..26a56dc 100644 --- a/crates/weft-ipc-types/src/manifest.rs +++ b/crates/weft-ipc-types/src/manifest.rs @@ -38,6 +38,31 @@ pub struct RuntimeMeta { pub struct UiMeta { /// The UI document, relative to the package root. pub entry: String, + /// What happens when the user asks the application's window to close. + #[serde(default)] + pub close: ClosePolicy, +} + +/// An application's declared unsaved-change policy: what a user close does. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ClosePolicy { + /// The window closes at once; the application keeps no unsaved state. + #[default] + Immediate, + /// The page is asked first and may cancel the close, for example to + /// offer saving unsaved changes; it then closes itself when done. + Ask, +} + +impl ClosePolicy { + /// The value weft-appd passes to the app shell in `WEFT_APP_CLOSE`. + pub fn as_str(self) -> &'static str { + match self { + Self::Immediate => "immediate", + Self::Ask => "ask", + } + } } /// Why a manifest could not be read. @@ -212,6 +237,24 @@ mod tests { let manifest = Manifest::read(&dir).unwrap(); assert_eq!(manifest.package.id, "org.weft.test"); assert_eq!(manifest.capabilities(), ["sys:notifications"]); + assert_eq!(manifest.ui.close, ClosePolicy::Immediate); + std::fs::write( + dir.join(MANIFEST_FILE), + "[package]\nid = \"org.weft.test\"\nname = \"T\"\nversion = \"1.0.0\"\n\ + [runtime]\nmodule = \"app.wasm\"\n[ui]\nentry = \"ui/index.html\"\nclose = \"ask\"\n", + ) + .unwrap(); + assert_eq!(Manifest::read(&dir).unwrap().ui.close, ClosePolicy::Ask); + std::fs::write( + dir.join(MANIFEST_FILE), + "[package]\nid = \"org.weft.test\"\nname = \"T\"\nversion = \"1.0.0\"\n\ + [runtime]\nmodule = \"app.wasm\"\n[ui]\nentry = \"ui/index.html\"\nclose = \"later\"\n", + ) + .unwrap(); + assert!(matches!( + Manifest::read(&dir), + Err(ManifestError::Parse(..)) + )); std::fs::write(dir.join(MANIFEST_FILE), "[package]\nid = \"x\"\n").unwrap(); assert!(matches!( Manifest::read(&dir), diff --git a/docs/architecture.md b/docs/architecture.md index d0448bc..8742547 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -20,7 +20,7 @@ Per-application Servo host. Spawned by `weft-appd` after the Wasm runtime signal ### weft-appd -Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd replies `stopping`, asks the compositor to close the session's windows, and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it; the stop reason records which happened (a clean exit, a failure while closing, or the timeout), and `stopped` is broadcast once the session has settled. A repeated `TERMINATE_APP` while the app is closing changes nothing; the timeout is the only forced termination, and a session being closed is not restored at the next start. appd keeps the final state of at most 32 stopped sessions, forgetting those launched earliest. A session still starting is stopped at once, and when appd itself shuts down its sessions are terminated without a close. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. +Session supervisor. Listens on a Unix socket (MessagePack protocol) and a WebSocket port (JSON). Before a session exists, appd resolves the package once (see [Package resolution](#package-resolution)) and derives its grants from that package's manifest; the runtime and the app shell receive the resolved component and UI document and do not search the stores themselves. For each session: spawns `weft-runtime` and waits for its READY, spawns `weft-app-shell` (`WEFT_APP_SHELL_BIN` is required) and waits for its READY, and only then reports the session running (`APP_READY`). A child reports readiness by printing `READY ` on stdout, where the token is a random per-session value appd passes in `WEFT_READY_TOKEN`; Wasm guests and pages share those stdout streams but cannot read the token, so their output cannot fake readiness. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd replies `stopping`, asks the compositor to close the session's windows, and gives the app shell 5 seconds to exit by itself, for example after saving, before terminating it; the stop reason records which happened (a clean exit, a failure while closing, or the timeout), and `stopped` is broadcast once the session has settled. An application that declares the `ask` close policy may decline the close (see [Closing an application](#closing-an-application)); the session is then `running` again. A repeated `TERMINATE_APP` while the app is closing changes nothing; the timeout is the only forced termination, and a session being closed is not restored at the next start. appd keeps the final state of at most 32 stopped sessions, forgetting those launched earliest. A session still starting is stopped at once, and when appd itself shuts down its sessions are terminated without a close. If either child fails to start, fails to become ready or exits, or the session is terminated, the whole session is stopped: both children, the IPC relay (its task, socket and registered sender), the file portal, the compositor association and any image mount are released. The IPC relay is created with the session in `$XDG_RUNTIME_DIR/weft` (a session cannot start without it); its socket is accessible only to the user and accepts a single connection, which the session's runtime makes before it starts the component. If that connection closes while the session runs, the session is stopped. `IPC_FORWARD` on the Unix socket never waits: it reports an error when the session has no relay or its queue is full. appd applies cgroup resource limits to the runtime via systemd-run when available. ### weft-runtime @@ -121,6 +121,17 @@ The package root must be a directory and its manifest a regular file, neither a A directory install can still be changed by its owner while a session runs, and nothing verifies its content at launch. +## Closing an application + +A user closes an application from the taskbar, which sends `TERMINATE_APP`, or with Alt+F4 in the compositor; both ask the application's window to close. What happens next is the application's declared unsaved-change policy, `[ui].close` in `wapp.toml`: + +- `immediate` (the default): the app shell exits at once. +- `ask`: the app shell dispatches a cancelable `weftclose` event on the page's top-level window and waits up to 2 seconds for it to be handled. If no listener calls `preventDefault()`, or the page does not answer in time, the app shell exits. Otherwise the close is cancelled: the app shell reports this to weft-appd with its readiness token on standard output, so page output cannot forge it, and the session stays running. The page then lets the user decide, and closes itself with `window.close()`, which Servo allows while the top-level document has a single history entry. + +Notes uses `ask`: with unsaved changes it offers "Save and close", which closes only after the save is acknowledged and stays open with the error shown when the save fails, "Close without saving", and Cancel. + +The close timeout of weft-appd still applies to a close that is not declined: an app shell that neither exits nor declines within 5 seconds is terminated, and the stop reason says so. A terminated session, a crash and appd's own shutdown are never reported as a close on request, and say nothing about whether the application saved. + ## App data Each app's private data lives in `$XDG_DATA_HOME/weft/app-data/` (`~/.local/share/weft/app-data/` by default), apart from installed packages. Installing, updating or uninstalling a package never removes it. `weft-appd` creates the directory when a session with an app-data capability starts and makes it accessible only to the user (mode 0700); a symbolic link in its place is refused rather than followed. `weft-appd` and `weft-pack` must run with the same `HOME` and `XDG_DATA_HOME` to agree on this location; the systemd unit allows `weft-appd` to write under `~/.local/share/weft`. diff --git a/docs/building.md b/docs/building.md index d13bbd9..9ca5f22 100644 --- a/docs/building.md +++ b/docs/building.md @@ -85,7 +85,7 @@ same nested desktop and drive the demo apps with real keyboard input: ```sh python3 tests/frame/check_counter.py # Counter round trip through the session bridge -python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves +python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves, asks before closing unsaved text python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept python3 tests/frame/check_confinement.py # an app page reaches only its UI files and its session bridge python3 tests/frame/check_activation.py # apps fill the work area above the taskbar; launch and ACTIVATE_APP raise and focus diff --git a/examples/org.weft.demo.notes/signature.sig b/examples/org.weft.demo.notes/signature.sig index 637f568..2de0ba8 100644 --- a/examples/org.weft.demo.notes/signature.sig +++ b/examples/org.weft.demo.notes/signature.sig @@ -1 +1 @@ -e8c10750b142b9df4405fd33ea0c060e63d44b5293e7f9ffa5b076989146b05e81f716e9b92aaeeb139ce1dc9b13ed26f356385d6a61a19c7f3dcaf6e95d2d03 \ No newline at end of file +6bd7b2df26ef619b20c73aa42ed92438d8b0c8515e35f47fd4be346cd90875b53e83374413a3b6008b2b0a718ba1da28497532f04a54b673ebc074db94ce730c \ No newline at end of file diff --git a/examples/org.weft.demo.notes/ui/index.html b/examples/org.weft.demo.notes/ui/index.html index f99a124..114a4b3 100644 --- a/examples/org.weft.demo.notes/ui/index.html +++ b/examples/org.weft.demo.notes/ui/index.html @@ -80,6 +80,15 @@ font-size: 12px; color: #444; } + #closing { + display: none; + align-items: center; + gap: 8px; + flex-shrink: 0; + font-size: 13px; + } + #closing.shown { display: flex; } + #closing span { flex: 1; } #charcount { font-size: 12px; color: #444; @@ -95,6 +104,12 @@ +
+ These notes have unsaved changes. + + + +