diff --git a/Cargo.lock b/Cargo.lock index 20cbbdf..04b567d 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", @@ -10511,6 +10513,7 @@ version = "0.1.0" dependencies = [ "anyhow", "base64 0.22.1", + "rustix 1.1.4", "serde", "serde_json", ] @@ -10519,9 +10522,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", ] @@ -10540,6 +10546,7 @@ dependencies = [ "ed25519-dalek", "hex", "rand 0.8.5", + "rustix 1.1.4", "sha2", "tar", "weft-ipc-types", diff --git a/README.md b/README.md index 022b3d0..b8aacee 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ 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 and update, rollback, 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. +**File portal** — `weft-file-portal` is a per-session file proxy for the session's directory grants, with every operation confined beneath the granted directory by the kernel. **Mount helper** — `weft-mount-helper` is a setuid helper for EROFS dm-verity mount/umount via `veritysetup`. diff --git a/crates/weft-app-shell/src/embedder.rs b/crates/weft-app-shell/src/embedder.rs index 5f7529f..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 @@ -431,6 +529,19 @@ impl App { } impl ApplicationHandler for App { + /// 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) { if self.window.is_some() { return; @@ -533,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 { @@ -592,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-app-shell/src/keyutils.rs b/crates/weft-app-shell/src/keyutils.rs index 1665cb5..ba4bdf5 100644 --- a/crates/weft-app-shell/src/keyutils.rs +++ b/crates/weft-app-shell/src/keyutils.rs @@ -57,7 +57,8 @@ fn named_key(n: NamedKey) -> servo::NamedKey { NamedKey::Control => S::Control, NamedKey::Fn => S::Fn, NamedKey::FnLock => S::FnLock, - NamedKey::Meta => S::Meta, + // winit names the Super keys Super; the DOM names them Meta. + NamedKey::Meta | NamedKey::Super => S::Meta, NamedKey::NumLock => S::NumLock, NamedKey::ScrollLock => S::ScrollLock, NamedKey::Shift => S::Shift, 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..84d6b81 100644 --- a/crates/weft-appd/src/compositor_client.rs +++ b/crates/weft-appd/src/compositor_client.rs @@ -1,4 +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; @@ -6,7 +9,39 @@ 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 } + } +} + +/// 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. /// @@ -24,17 +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 -/// `CompositorToAppd` frames are decoded and logged; no behavioural action is -/// taken yet (surface lifecycle hookup happens in a later task). +/// 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 (tx, rx) = mpsc::channel::(32); + 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 { @@ -49,28 +91,85 @@ 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"); + 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, + 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.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; } } +/// Writes `frame`, attaching `fd` to its first bytes so the compositor +/// receives the descriptor together with the message it belongs to. +async fn send_frame( + stream: &mut tokio::net::unix::OwnedWriteHalf, + frame: &[u8], + fd: Option<&OwnedFd>, +) -> 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 +209,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/grants.rs b/crates/weft-appd/src/grants.rs index 0a84f03..0343f99 100644 --- a/crates/weft-appd/src/grants.rs +++ b/crates/weft-appd/src/grants.rs @@ -87,14 +87,9 @@ impl HostDirs { } } -/// Derives grants for the declared capabilities. Every capability is -/// checked before host directories are resolved or the app's data -/// directory is created, so a refused launch leaves nothing behind. -pub(crate) fn derive( - app_id: &str, - declared: &[String], - host_dirs: impl FnOnce() -> Result, -) -> Result { +/// Parses the declared capabilities, refusing unknown ones and those this +/// host does not support, without touching the host. +pub(crate) fn validate(declared: &[String]) -> Result, Refusal> { let mut capabilities = Vec::new(); for text in declared { let capability: Capability = @@ -110,7 +105,18 @@ pub(crate) fn derive( } capabilities.push(capability); } + Ok(capabilities) +} +/// Derives grants for the declared capabilities. Every capability is +/// checked before host directories are resolved or the app's data +/// directory is created, so a refused launch leaves nothing behind. +pub(crate) fn derive( + app_id: &str, + declared: &[String], + host_dirs: impl FnOnce() -> Result, +) -> Result { + let capabilities = validate(declared)?; let mut grants = SessionGrants::default(); let needs_dirs = capabilities .iter() diff --git a/crates/weft-appd/src/ipc.rs b/crates/weft-appd/src/ipc.rs index 84d7e4d..76f2b4b 100644 --- a/crates/weft-appd/src/ipc.rs +++ b/crates/weft-appd/src/ipc.rs @@ -8,8 +8,16 @@ pub enum Request { app_id: String, surface_id: u64, }, + /// Asks a running session to close as a user close would, or with + /// `force`, terminates it without asking. TerminateApp { session_id: u64, + #[serde(default)] + force: bool, + }, + /// Brings the session's window to the front with keyboard focus. + ActivateApp { + session_id: u64, }, QueryRunning, QueryAppState { @@ -211,10 +219,19 @@ mod tests { #[test] fn terminate_app_request_roundtrip() { - let req = Request::TerminateApp { session_id: 42 }; + let req = Request::TerminateApp { + session_id: 42, + force: false, + }; let bytes = rmp_serde::to_vec(&req).unwrap(); let decoded: Request = rmp_serde::from_slice(&bytes).unwrap(); - assert!(matches!(decoded, Request::TerminateApp { session_id: 42 })); + assert!(matches!( + decoded, + Request::TerminateApp { + session_id: 42, + force: false + } + )); } #[test] diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 85b5bbc..d5aaa8b 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -3,11 +3,22 @@ //! 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, 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::manifest::{ClosePolicy, MANIFEST_FILE, Manifest, ManifestError, entry_path}; use weft_ipc_types::package::{ImageFiles, is_valid_app_id}; +use weft_ipc_types::trust::{ + Inventory, Owner, PublisherKey, TrustError, TrustStore, lock_owner, owner_record_path, + read_owner, read_signature, write_owner, +}; use crate::grants::Refusal; use crate::mount::Mount; @@ -20,10 +31,15 @@ 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. pub(crate) image: Option, + /// The lock that keeps an installed revision from being removed while + /// the session runs from it. + pub(crate) pin: Option, } /// Resolves `app_id` in the package stores. @@ -57,16 +73,47 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result active, + Ok(None) => continue, + Err(e) => { + tracing::warn!(app_id, error = %e, "package entry skipped"); + unusable.get_or_insert(match e { + weft_ipc_types::store::StoreError::NotAPackage(_) => { + Refusal::new(403, e.to_string()) + } + weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()), + }); + continue; + } + }; + let dir = active.dir().to_path_buf(); + // A directory left with only app data by an earlier layout is not a + // package. + if std::fs::symlink_metadata(dir.join(MANIFEST_FILE)).is_err() { + continue; } + // The session runs from the revision itself, not the link, so an + // update or rollback that switches the link while it runs leaves it + // on the bytes it started with. From here on a refusal is final: a + // package that is being changed, or that fails its checks, is not + // silently replaced by another store's copy. + let pin = weft_ipc_types::store::pin(&dir).map_err(|e| { + Refusal::new( + 500, + format!("cannot hold {} while it runs: {e}", dir.display()), + ) + })?; + let mut package = from_root(app_id, dir, None)?; + package.pin = Some(pin); + return Ok(package); } - Err(Refusal::new( - 404, - format!("package {app_id} is not installed"), - )) + Err(unusable.unwrap_or_else(|| Refusal::new(404, format!("package {app_id} is not installed")))) } fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result { @@ -85,9 +132,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( @@ -99,6 +151,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result) -> Result 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()), + }; + 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 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!( + "{app_id} belongs to {existing}, not publisher {}", + signer.to_hex() + ), + )); + } + None => {} + } + // Data in the earlier layout counts: it would be moved to this app's + // data directory when the session starts. + 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}"), + )), + 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), + } +} + impl LaunchPackage { /// A package for supervisor tests whose children ignore their files. #[cfg(test)] @@ -125,8 +285,10 @@ impl LaunchPackage { root: PathBuf::from("/nonexistent"), module: PathBuf::from("/nonexistent/app.wasm"), ui_entry: PathBuf::from("/nonexistent/ui/index.html"), + close: ClosePolicy::Immediate, capabilities: Vec::new(), image: None, + pin: None, } } @@ -141,6 +303,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 { @@ -160,8 +326,19 @@ 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(); + 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) }; + 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 } @@ -170,6 +347,12 @@ 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"); + 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); } @@ -239,13 +422,275 @@ mod tests { std::os::unix::fs::symlink(store.join("manifest.toml"), &manifest).unwrap(); let manifest_link = refusal(resolve(ID)); finish(&store); - for refused in [root_link, manifest_link] { + // Only a link to one of the app's own revisions is followed. + assert_eq!(root_link.code, 403, "{}", root_link.message); + assert!( + root_link + .message + .contains("nor a link to one of its revisions"), + "{}", + root_link.message + ); + assert_eq!(manifest_link.code, 403, "{}", manifest_link.message); + assert!( + manifest_link.message.contains("not a link"), + "{}", + manifest_link.message + ); + } + + #[cfg(unix)] + #[test] + fn an_unusable_store_entry_does_not_hide_a_later_store() { + let _env = crate::tests::env_lock().blocking_lock(); + let user = store("unusable_user", ID, "app.wasm"); + let system = std::env::temp_dir().join(format!( + "weft_appd_launch_unusable_system_{}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&system); + std::fs::create_dir_all(&system).unwrap(); + std::fs::rename(user.join(ID), system.join(ID)).unwrap(); + // A link to something other than one of the app's revisions. + std::os::unix::fs::symlink("/tmp", user.join(ID)).unwrap(); + let found = resolve_in(ID, &[user.clone(), system.clone()]).map(|p| p.root); + let alone = refusal(resolve_in(ID, std::slice::from_ref(&user))); + finish(&user); + let _ = std::fs::remove_dir_all(&system); + assert_eq!(found.ok(), Some(system.join(ID))); + // With no other store, its problem is reported, not "not installed". + assert_eq!(alone.code, 403, "{}", alone.message); + } + + #[cfg(unix)] + #[test] + fn a_session_runs_from_its_revision_and_holds_it() { + use weft_ipc_types::store::{link_target, revisions_of, try_claim}; + let _env = crate::tests::env_lock().blocking_lock(); + let store = store("revision", ID, "app.wasm"); + let revision = "ab".repeat(32); + let dir = revisions_of(&store, ID).join(&revision); + std::fs::create_dir_all(dir.parent().unwrap()).unwrap(); + std::fs::rename(store.join(ID), &dir).unwrap(); + std::os::unix::fs::symlink(link_target(ID, &revision), store.join(ID)).unwrap(); + let package = resolve(ID).unwrap(); + let held = try_claim(&dir).unwrap().is_none(); + let root = package.root.clone(); + let module = package.module.clone(); + drop(package); + let released = try_claim(&dir).unwrap().is_some(); + finish(&store); + // The children get the revision's own paths, not the link's. + assert_eq!(root, dir); + assert_eq!(module, dir.join("app.wasm")); + assert!(held, "the revision is not held while the session runs"); + assert!(released, "the revision is still held after the session"); + } + + 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 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!(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 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 legacy = weft_ipc_types::package::legacy_app_data_dir(&store.join("home"), DEMO); + std::fs::create_dir_all(&legacy).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("no recorded owner"), + "{}", + refused.message + ); + 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(); + 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(); + 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] + 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); - assert!( - refused.message.contains("not a link"), - "{}", - refused.message - ); } } diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 433b0e8..e61ae70 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; @@ -35,11 +27,15 @@ 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, broadcast: tokio::sync::broadcast::Sender, - abort_senders: std::collections::HashMap>, + abort_senders: std::collections::HashMap, compositor_tx: Option, ipc_senders: std::collections::HashMap>, } @@ -60,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( @@ -79,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() } @@ -94,14 +109,46 @@ impl SessionRegistry { }) } - fn terminate(&mut self, session_id: u64) -> bool { - let found = self.sessions.remove(&session_id).is_some(); - self.abort_senders.remove(&session_id); - found + /// Asks a session to stop and returns the state to report, or `None` + /// when there is no such session. A running session is asked to close + /// as a user close would, and stays running while the application + /// answers; a forced stop or a session still starting is stopped at + /// once and is Stopping until its supervisor settles it. + fn terminate(&mut self, session_id: u64, force: bool) -> Option { + let state = self.state(session_id); + match self.abort_senders.get(&session_id) { + // Already ending; a forced request still ends it without waiting. + Some(sender) if matches!(state, AppStateKind::Stopping) => { + if force { + let _ = sender.try_send(runtime::Stop::Force); + } + Some(AppStateKind::Stopping) + } + Some(sender) => { + // A close is queued only into an empty queue, so a forced + // request always finds room; one already queued says the + // same as a repeat. + if force { + let _ = sender.try_send(runtime::Stop::Force); + } else if sender.capacity() == runtime::STOP_QUEUE { + let _ = sender.try_send(runtime::Stop::Close); + } + if !force && matches!(state, AppStateKind::Running) { + return Some(AppStateKind::Running); + } + self.set_state(session_id, AppStateKind::Stopping); + Some(AppStateKind::Stopping) + } + // A session no supervisor runs has nothing to settle. + None => self + .sessions + .remove(&session_id) + .map(|_| AppStateKind::Stopped), + } } - pub(crate) fn register_abort(&mut self, session_id: u64) -> tokio::sync::oneshot::Receiver<()> { - let (tx, rx) = tokio::sync::oneshot::channel(); + pub(crate) fn register_abort(&mut self, session_id: u64) -> runtime::StopReceiver { + let (tx, rx) = tokio::sync::mpsc::channel(runtime::STOP_QUEUE); self.abort_senders.insert(session_id, tx); rx } @@ -117,15 +164,17 @@ 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() } - fn state(&self, session_id: u64) -> AppStateKind { + pub(crate) fn state(&self, session_id: u64) -> AppStateKind { self.sessions .get(&session_id) .map(|e| e.state.clone()) @@ -225,19 +274,35 @@ async fn run() -> anyhow::Result<()> { let _ = sd_notify::notify(false, &[sd_notify::NotifyState::Ready]); } - if let Some(app_ids) = load_session() { - 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; - } - } + // 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); + 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 { + 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; + } + }); + (task, saved) + }); #[cfg(unix)] let mut sigterm = { @@ -283,7 +348,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); @@ -446,8 +518,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 { @@ -502,18 +586,57 @@ 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); - if found { - tracing::info!(session_id, "terminated"); - Response::AppState { - session_id, - state: AppStateKind::Stopped, + Request::TerminateApp { session_id, force } => { + let state = { + let mut reg = registry.lock().await; + let before = reg.state(session_id); + let state = reg.terminate(session_id, force); + if matches!(state, Some(AppStateKind::Stopping)) + && !matches!(before, AppStateKind::Stopping) + { + let _ = reg.broadcast().send(Response::AppState { + session_id, + state: AppStateKind::Stopping, + }); } - } else { - Response::Error { + state + }; + match state { + Some(state) => { + tracing::info!(session_id, force, "stop requested"); + Response::AppState { session_id, state } + } + None => Response::Error { code: 1, message: format!("session {session_id} not found"), + }, + } + } + 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.is_connected() + && 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(), } } } @@ -578,6 +701,29 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { } } +/// Refuses a launch while the app declares a capability the user has not +/// approved; a signature, however trusted, approves nothing. +fn check_approval(app_id: &str, declared: &[String]) -> Result<(), grants::Refusal> { + use weft_ipc_types::approval::{approval_path, read_approved, unapproved}; + let data_home = weft_ipc_types::package::data_home().ok_or_else(|| { + grants::Refusal::new(500, "cannot locate the data home holding app approvals") + })?; + let record = approval_path(&data_home, app_id); + let approved = read_approved(&record) + .map_err(|e| grants::Refusal::new(500, format!("cannot read {}: {e}", record.display())))?; + let pending = unapproved(declared, &approved); + if pending.is_empty() { + return Ok(()); + } + Err(grants::Refusal::new( + 403, + format!( + "{app_id} needs approval for {}; approve it with weft-pack approve {app_id}", + pending.join(", ") + ), + )) +} + /// Resolves the package of `app_id` and derives its grants. Resolution may /// mount an image, so it runs off the async workers; a refused launch /// releases the mount the same way. @@ -586,6 +732,10 @@ async fn resolve_launch( ) -> Result<(launch::LaunchPackage, grants::SessionGrants), grants::Refusal> { tokio::task::spawn_blocking(move || { let package = launch::resolve(&app_id)?; + // Unknown and unsupported capabilities are refused as such, then + // unapproved ones, before anything is prepared on the host. + grants::validate(&package.capabilities)?; + check_approval(&app_id, &package.capabilities)?; let grants = grants::derive(&app_id, &package.capabilities, grants::HostDirs::from_env)?; tracing::info!( %app_id, @@ -631,9 +781,24 @@ fn scan_installed_apps() -> Vec { continue; }; for entry in entries.flatten() { - let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else { + // Only an app's active package, named after the ID its manifest + // declares, is installed; staging copies, revisions and stray + // directories or links are not. + let Some(name) = entry.file_name().to_str().map(str::to_owned) else { continue; }; + if !weft_ipc_types::package::is_valid_app_id(&name) { + continue; + } + let Ok(Some(found)) = weft_ipc_types::store::active(&root, &name) else { + continue; + }; + let Ok(m) = weft_ipc_types::manifest::Manifest::read(found.dir()) else { + continue; + }; + if m.package.id != name { + continue; + } if seen.insert(m.package.id.clone()) { apps.push(AppInfo { app_id: m.package.id, @@ -643,6 +808,9 @@ fn scan_installed_apps() -> Vec { } } } + // A stable order, so the launcher's keyboard order does not follow the + // directory listing. + apps.sort_by_cached_key(|app| (app.name.to_lowercase(), app.app_id.clone())); apps } @@ -707,6 +875,42 @@ 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, 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 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() + .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 { + 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); + std::env::set_var("WEFT_TRUSTED_KEYS", &keys); + } } fn make_registry() -> Registry { @@ -778,6 +982,69 @@ mod tests { assert!(result.is_err()); } + #[tokio::test] + async fn a_capability_launches_only_once_approved() { + let _env = env_lock().lock().await; + let store = std::env::temp_dir().join(format!("weft_approve_{}", std::process::id())); + let id = "org.example.notify"; + write_test_package( + &store.join(id), + id, + "capabilities = [\"sys:notifications\"]", + ); + let prior_store = std::env::var("WEFT_APP_STORE").ok(); + let prior_bin = std::env::var("WEFT_RUNTIME_BIN").ok(); + // SAFETY: env_lock is held and the runtime is current_thread. A + // configured runtime makes appd resolve the package. + unsafe { + std::env::set_var("WEFT_APP_STORE", &store); + std::env::set_var("WEFT_RUNTIME_BIN", "/nonexistent/weft-runtime"); + } + let launch = || async { + dispatch( + Request::LaunchApp { + app_id: id.into(), + surface_id: 0, + }, + &make_registry(), + ) + .await + }; + let refused = launch().await; + let data_home = weft_ipc_types::package::data_home().unwrap(); + let record = weft_ipc_types::approval::approval_path(&data_home, id); + weft_ipc_types::approval::write_approved(&record, &["sys:notifications".to_owned()]) + .unwrap(); + let approved = launch().await; + let _ = std::fs::remove_file(&record); + // SAFETY: as above. + unsafe { + match prior_store { + Some(v) => std::env::set_var("WEFT_APP_STORE", v), + None => std::env::remove_var("WEFT_APP_STORE"), + } + match prior_bin { + Some(v) => std::env::set_var("WEFT_RUNTIME_BIN", v), + None => std::env::remove_var("WEFT_RUNTIME_BIN"), + } + } + let _ = std::fs::remove_dir_all(&store); + match refused { + Response::Error { code, message } => { + assert_eq!(code, 403); + assert!( + message.contains("needs approval for sys:notifications"), + "{message}" + ); + } + other => panic!("an unapproved capability launched: {other:?}"), + } + assert!( + matches!(approved, Response::LaunchAck { .. }), + "{approved:?}" + ); + } + #[tokio::test] async fn launch_is_refused_when_capabilities_cannot_be_granted() { let _env = env_lock().lock().await; @@ -860,7 +1127,7 @@ mod tests { } #[tokio::test] - async fn dispatch_terminate_known_returns_stopped() { + async fn dispatch_terminate_forgets_a_stopped_session() { let _env = env_lock().lock().await; let reg = make_registry(); let ack = dispatch( @@ -875,20 +1142,43 @@ mod tests { Response::LaunchAck { session_id, .. } => session_id, _ => panic!("expected LaunchAck"), }; - let resp = dispatch(Request::TerminateApp { session_id }, ®).await; + // Without a configured runtime the session stopped at once; a + // terminate forgets it. + let resp = dispatch( + Request::TerminateApp { + session_id, + force: false, + }, + ®, + ) + .await; + assert!( + matches!( + resp, + Response::AppState { + state: AppStateKind::Stopped, + .. + } + ), + "{resp:?}" + ); assert!(matches!( - resp, - Response::AppState { - state: AppStateKind::Stopped, - .. - } + reg.lock().await.state(session_id), + AppStateKind::NotFound )); } #[tokio::test] async fn dispatch_terminate_unknown_returns_error() { let reg = make_registry(); - let resp = dispatch(Request::TerminateApp { session_id: 999 }, ®).await; + let resp = dispatch( + Request::TerminateApp { + session_id: 999, + force: false, + }, + ®, + ) + .await; assert!(matches!(resp, Response::Error { .. })); } @@ -999,14 +1289,14 @@ mod tests { fn registry_terminate_known_session() { let mut reg = SessionRegistry::default(); let id = reg.launch("com.example.app"); - assert!(reg.terminate(id)); + assert!(reg.terminate(id, false).is_some()); assert!(matches!(reg.state(id), AppStateKind::NotFound)); } #[test] fn registry_terminate_unknown_returns_false() { let mut reg = SessionRegistry::default(); - assert!(!reg.terminate(999)); + assert!(reg.terminate(999, false).is_none()); } #[test] @@ -1021,7 +1311,7 @@ mod tests { assert_eq!(sessions[0].app_id, "com.example.a"); assert_eq!(sessions[1].session_id, id2); assert_eq!(sessions[1].app_id, "com.example.b"); - reg.terminate(id1); + reg.terminate(id1, false); assert_eq!(reg.running_sessions().len(), 1); assert_eq!(reg.running_sessions()[0].session_id, id2); } @@ -1045,23 +1335,42 @@ mod tests { let _env = env_lock().blocking_lock(); let store = std::env::temp_dir().join(format!("weft_appd_scan_{}", std::process::id())); - let app_dir = store.join("com.example.scanner"); - fs::create_dir_all(&app_dir).unwrap(); - fs::write( - app_dir.join("wapp.toml"), - "[package]\nid = \"com.example.scanner\"\nname = \"Scanner\"\nversion = \"1.0.0\"\n\ - [runtime]\nmodule = \"app.wasm\"\n[ui]\nentry = \"ui/index.html\"\n", - ) - .unwrap(); + // Listed by name, ignoring case, then by ID, whatever the directory + // order. + for (id, name) in [ + ("com.example.scanner", "Scanner"), + ("com.example.zeta", "alpha"), + ("com.example.second", "Alpha"), + ("com.example.first", "Alpha"), + ] { + let app_dir = store.join(id); + fs::create_dir_all(&app_dir).unwrap(); + fs::write( + app_dir.join("wapp.toml"), + format!( + "[package]\nid = \"{id}\"\nname = \"{name}\"\nversion = \"1.0.0\"\n\ + [runtime]\nmodule = \"app.wasm\"\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); + } let prior = std::env::var("WEFT_APP_STORE").ok(); unsafe { std::env::set_var("WEFT_APP_STORE", &store) }; let apps = scan_installed_apps(); - assert_eq!(apps.len(), 1); - assert_eq!(apps[0].app_id, "com.example.scanner"); - assert_eq!(apps[0].name, "Scanner"); - assert_eq!(apps[0].version, "1.0.0"); + let ids: Vec<&str> = apps.iter().map(|a| a.app_id.as_str()).collect(); + assert_eq!( + ids, + [ + "com.example.first", + "com.example.second", + "com.example.zeta", + "com.example.scanner" + ] + ); + assert_eq!(apps[3].name, "Scanner"); + assert_eq!(apps[3].version, "1.0.0"); unsafe { match prior { @@ -1249,6 +1558,708 @@ 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. + connected.store(false, std::sync::atomic::Ordering::Release); + let unsent = dispatch(Request::ActivateApp { session_id }, ®istry).await; + assert!( + matches!(unsent, Response::Error { code: 503, .. }), + "{unsent:?}" + ); + } + + #[tokio::test(flavor = "current_thread")] + async fn an_app_that_cancels_a_close_keeps_running() { + 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_cancel_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let app = dir.join("store/org.example.cancel"); + write_test_package(&app, "org.example.cancel", ""); + let runtime = dir.join("runtime.sh"); + std::fs::write( + &runtime, + "#!/bin/sh\necho READY $WEFT_READY_TOKEN\nexec sleep 30\n", + ) + .unwrap(); + // The app shell declines the close once, after a forged report. + let shell = dir.join("shell.sh"); + std::fs::write( + &shell, + "#!/bin/sh\necho READY $WEFT_READY_TOKEN\nsleep 1\n\ + echo CLOSE_CANCELLED 00000000000000000000000000000000\nsleep 1\n\ + echo CLOSE_CANCELLED $WEFT_READY_TOKEN\nexec sleep 30\n", + ) + .unwrap(); + for script in [&runtime, &shell] { + std::fs::set_permissions(script, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + let vars = [ + ("WEFT_RUNTIME_BIN", runtime.clone().into_os_string()), + ("WEFT_APP_SHELL_BIN", shell.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.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, + force: false, + }, + ®istry, + ) + .await; + // Once the genuine report arrives, the declining window is brought + // to the front, and the session never left Running. + let resumed = tokio::time::timeout(std::time::Duration::from_secs(5), async { + loop { + match compositor.recv().await.map(|out| out.msg) { + Some(weft_ipc_types::AppdToCompositor::AppFocusRequest { session_id: s }) + if s == session_id => + { + return true; + } + Some(_) => {} + None => return false, + } + } + }) + .await; + let mut broadcasts = Vec::new(); + while let Ok(message) = rx.try_recv() { + broadcasts.push(message); + } + let resumed_after = started.elapsed(); + let state = registry.lock().await.state(session_id); + // A forced stop does not ask, so nothing can decline it. + let forced_at = std::time::Instant::now(); + let forced = dispatch( + Request::TerminateApp { + session_id, + force: true, + }, + ®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!( + !broadcasts + .iter() + .any(|m| matches!(m, Response::AppState { .. })), + "{broadcasts:?}" + ); + assert!( + matches!( + forced, + Response::AppState { + state: AppStateKind::Stopping, + .. + } + ), + "{forced:?}" + ); + assert!(stopped.is_ok(), "a forced stop did not stop the session"); + assert!( + forced_at.elapsed() < std::time::Duration::from_secs(3), + "{:?}", + forced_at.elapsed() + ); + } + + #[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, + force: false, + }, + ®istry, + ) + .await; + // While the application is asked, the session is still running; a + // second request changes nothing and does not ask again. + let again = dispatch( + Request::TerminateApp { + session_id, + force: false, + }, + ®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, + 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::Running, + .. + } + ), + "{reply:?}" + ); + assert!( + matches!( + again, + Response::AppState { + state: AppStateKind::Running, + .. + } + ), + "{again:?}" + ); + assert!( + matches!(closing.0, AppStateKind::Running), + "{:?}", + closing.0 + ); + assert!( + closing.1, + "a session being asked to close is no longer listed" + ); + assert_eq!(closing.2, ["org.example.close"]); + assert!(stopped.is_ok(), "the session did not stop"); + // The app was asked to close, given the timeout, then terminated. + assert_eq!( + sent.iter() + .filter(|m| matches!( + m, + weft_ipc_types::AppdToCompositor::AppCloseRequest { session_id: s } if *s == session_id + )) + .count(), + 1, + "{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 a_forced_stop_ends_a_close_in_progress() { + 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_force_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let app = dir.join("store/org.example.force"); + write_test_package(&app, "org.example.force", ""); + // 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, 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.force".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; + // The close is under way: the session is running while it is asked. + let asked = dispatch( + Request::TerminateApp { + session_id, + force: false, + }, + ®istry, + ) + .await; + // Repeats fill no queue a forced stop then cannot enter. + for _ in 0..3 { + dispatch( + Request::TerminateApp { + session_id, + force: false, + }, + ®istry, + ) + .await; + } + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + let forced_at = std::time::Instant::now(); + let reply = dispatch( + Request::TerminateApp { + session_id, + force: true, + }, + ®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 elapsed = forced_at.elapsed(); + drop(compositor); + + 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!( + matches!( + asked, + Response::AppState { + state: AppStateKind::Running, + .. + } + ), + "{asked:?}" + ); + assert!(stopped.is_ok(), "the forced stop did not stop the session"); + // The forced stop does not wait out the close timeout. + assert!(elapsed < std::time::Duration::from_secs(3), "{elapsed:?}"); + } + + #[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; + 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 refused = dispatch( + Request::LaunchApp { + app_id: "org.example.nocomp".into(), + surface_id: 0, + }, + ®istry, + ) + .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 { + // 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!( + 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()); + } + + #[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(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.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; @@ -1402,6 +2413,11 @@ mod tests { Ok(Ok(Response::AppReady { session_id: sid, .. })) if sid == session_id )); + let stopping = tokio::time::timeout(std::time::Duration::from_millis(200), rx.recv()).await; + assert!(matches!( + stopping, + Ok(Ok(Response::AppState { session_id: sid, state: AppStateKind::Stopping })) if sid == session_id + )); let stopped = tokio::time::timeout(std::time::Duration::from_millis(200), rx.recv()).await; assert!(matches!( stopped, @@ -1446,7 +2462,7 @@ mod tests { let session_id = registry.lock().await.launch("test.abort.startup"); let abort_rx = registry.lock().await.register_abort(session_id); - registry.lock().await.terminate(session_id); + registry.lock().await.terminate(session_id, false); use_test_runtime_dir(); @@ -1461,9 +2477,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 7536a99..e0e1860 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use std::time::Duration; -use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}; +use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}; use weft_ipc_types::AppdToCompositor; use crate::Registry; @@ -33,6 +33,11 @@ impl Drop for IpcRelay { } } +/// The longest message from a component, its line break included: the +/// runtime refuses longer ones, so a longer line ends the relay. +#[cfg(unix)] +const MAX_RELAY_FRAME: usize = 64 * 1024 + 2; + /// Listens on `socket_path` for the session's component and relays /// newline-delimited messages between it and the returned sender (towards /// the component) and the broadcast channel (from the component). @@ -69,14 +74,29 @@ pub(crate) fn spawn_ipc_relay( let (reader, writer) = tokio::io::split(stream); let mut reader = BufReader::new(reader); let mut writer = BufWriter::new(writer); + // The frame being read lives outside the loop: `read_until` keeps + // what it has read when the other branch wins, so a message from the + // page never splits a message from the component. + let mut frame = Vec::new(); loop { - let mut line = String::new(); + let mut limited = (&mut reader).take((MAX_RELAY_FRAME + 1 - frame.len()) as u64); tokio::select! { - n = reader.read_line(&mut line) => { + n = limited.read_until(b'\n', &mut frame) => { match n { Ok(0) | Err(_) => break, + Ok(_) if frame.len() > MAX_RELAY_FRAME => { + tracing::warn!(session_id, "IPC relay: message over the size limit"); + break; + } + // The rest of the message is still to come. + Ok(_) if !frame.ends_with(b"\n") => {} Ok(_) => { - let payload = line.trim_end().to_owned(); + let line = std::mem::take(&mut frame); + let Ok(text) = String::from_utf8(line) else { + tracing::warn!(session_id, "IPC relay: message is not UTF-8"); + break; + }; + let payload = text.trim_end_matches(['\n', '\r']).to_owned(); let _ = broadcast.send(Response::IpcMessage { session_id, payload }); } } @@ -120,6 +140,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. @@ -197,8 +219,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. @@ -210,7 +250,7 @@ fn spawn_app_shell( .arg("--ui") .arg(&package.ui_entry) .env(READY_TOKEN_ENV, token) - .stdin(std::process::Stdio::null()) + .env(CLOSE_POLICY_ENV, package.close.as_str()) .stdout(std::process::Stdio::piped()) .stderr(std::process::Stdio::piped()) .kill_on_drop(true) @@ -249,12 +289,27 @@ fn spawn_file_portal( Some((socket, child)) } +/// A request to end a session, from `TERMINATE_APP`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Stop { + /// Close as a user close would; the application may decline. + Close, + /// Terminate without asking. + Force, +} + +/// Repeated requests carry nothing new, so a few queued ones are enough. +pub(crate) const STOP_QUEUE: usize = 2; +pub(crate) type StopSender = tokio::sync::mpsc::Sender; +/// Ends when appd shuts down and drops the sender. +pub(crate) type StopReceiver = tokio::sync::mpsc::Receiver; + pub(crate) async fn supervise( session_id: u64, mut package: LaunchPackage, grants: crate::grants::SessionGrants, registry: Registry, - abort_rx: tokio::sync::oneshot::Receiver<()>, + abort_rx: StopReceiver, compositor_tx: Option, ) -> anyhow::Result<()> { let mut abort_rx = abort_rx; @@ -317,10 +372,6 @@ pub(crate) async fn supervise( cmd.arg("--ipc-socket").arg(&ipc_socket_path); cmd.arg("--module").arg(&package.module); - if let Some((ref sock, _)) = portal { - cmd.env("WEFT_FILE_PORTAL_SOCKET", sock); - } - for dir in &grants.dirs { cmd.arg("--preopen").arg(dir.preopen_arg()); } @@ -336,7 +387,7 @@ pub(crate) async fn supervise( relay: Some(relay), image, compositor_tx, - surface_announced: false, + client_attached: false, }; let mut runtime = match cmd.spawn() { @@ -347,23 +398,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"), @@ -383,16 +417,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"), @@ -417,10 +470,22 @@ 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; + // A stop requested while starting can still be queued here, or lose + // the race with READY; the session is then stopped, not shown. + if matches!(reg.state(session_id), AppStateKind::Stopping) || abort_rx.try_recv().is_ok() { + drop(reg); + return session.settle(®istry, "startup aborted").await; + } reg.set_state(session_id, AppStateKind::Running); let _ = reg.broadcast().send(Response::AppReady { session_id, @@ -429,21 +494,111 @@ 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:?})") - } - _ = &mut abort_rx => "terminate requested".to_owned(), - _ = session.relay.as_mut().expect("relay opened").ended() => { - "component closed its IPC connection".to_owned() + // Whether the user asked the application to close, so that a clean exit + // afterwards, also one the application makes after declining, is a + // close on request. + let mut close_asked = false; + 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 match status { + Ok(status) if close_asked && status.success() => { + format!("closed on request; app shell exited ({status:?})") + } + status => format!("app shell exited ({status:?})"), + }; + } + stop = abort_rx.recv() => match stop { + Some(Stop::Force) => break "terminated by force".to_owned(), + Some(Stop::Close) => { + close_asked = true; + match session.close(session_id, &mut cancelled, &mut abort_rx).await { + Close::Settle(reason) => break reason, + // The application keeps running, as its close + // policy allows; its window comes to the front so + // the user sees why. A later request asks again. + Close::Cancelled => { + tracing::info!(session_id, "the application declined the close"); + session.focus(session_id); + } + } + } + // The senders are dropped only when appd shuts down. + None => 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 { + // Earlier output without a trailing newline can precede the + // report, as it can precede READY; the token alone establishes it. + if line.rsplit_once(' ').is_some_and(|(head, t)| { + head.ends_with(CLOSE_CANCELLED) && 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); + +/// 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 { + 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 { + 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, @@ -473,19 +628,112 @@ 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 { + /// 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, + cancelled: &mut tokio::sync::mpsc::Receiver<()>, + stops: &mut StopReceiver, + ) -> Close { + // A report already read from an earlier close, for example one the + // user started in the compositor, does not answer this one. One + // still in the pipe can: the session then reads as declined while + // the app shell is still asking, and the app shell's own answer + // timeout still closes a page that does not answer. + while cancelled.try_recv().is_ok() {} + 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 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 Close::Settle("terminate requested".to_owned()); + }; + let wait = async { + loop { + tokio::select! { + status = app_shell.wait() => break 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() => { + break Close::Settle(format!("runtime exited while closing ({status:?})")); + } + _ = relay.ended() => break Close::Settle( + "component closed its IPC connection while closing".to_owned(), + ), + Some(()) = cancelled.recv() => break Close::Cancelled, + stop = stops.recv() => match stop { + // Asking again while the application answers changes + // nothing. + Some(Stop::Close) => {} + Some(Stop::Force) => { + break Close::Settle("terminated by force while closing".to_owned()); + } + None => break Close::Settle("appd is shutting down".to_owned()), + }, + } + } + }; + tokio::time::timeout(CLOSE_TIMEOUT, wait) + .await + .unwrap_or_else(|_| { + Close::Settle(format!( + "close requested; the app shell did not exit within {} s and was terminated", + CLOSE_TIMEOUT.as_secs() + )) + }) + } + + /// Brings the session's window to the front with keyboard focus. + fn focus(&self, session_id: u64) { + if self.client_attached + && let Some(tx) = &self.compositor_tx + { + let _ = tx.try_send(AppdToCompositor::AppFocusRequest { session_id }.into()); + } + } + async fn settle(self, registry: &Registry, reason: &str) -> anyhow::Result<()> { let session_id = self.session_id; tracing::info!(session_id, reason, "stopping session"); + { + let mut reg = registry.lock().await; + if !matches!(reg.state(session_id), AppStateKind::Stopping) { + reg.set_state(session_id, AppStateKind::Stopping); + let _ = reg.broadcast().send(Response::AppState { + session_id, + state: AppStateKind::Stopping, + }); + } + } 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!( @@ -526,7 +774,7 @@ async fn kill_child(child: Option) { async fn wait_for_child_ready( stdout: tokio::process::ChildStdout, token: &str, - abort_rx: &mut tokio::sync::oneshot::Receiver<()>, + abort_rx: &mut StopReceiver, ) -> Result, String> { tokio::select! { r = tokio::time::timeout(READY_TIMEOUT, wait_for_ready(stdout, token)) => match r { @@ -534,7 +782,8 @@ async fn wait_for_child_ready( Ok(Err(e)) => Err(format!("did not become ready: {e}")), Err(_) => Err(format!("not ready after {}s", READY_TIMEOUT.as_secs())), }, - _ = abort_rx => Err("startup aborted".to_owned()), + // Any stop request, or appd shutting down, ends a startup. + _ = abort_rx.recv() => Err("startup aborted".to_owned()), } } @@ -572,6 +821,62 @@ async fn drain_stderr(stderr: tokio::process::ChildStderr, session_id: u64, proc mod tests { use super::SYSTEMD_SCOPE_ARGS; + fn relay_socket(name: &str) -> std::path::PathBuf { + std::env::temp_dir().join(format!("weft-relay-{name}-{}.sock", std::process::id())) + } + + #[tokio::test] + async fn a_message_from_the_page_does_not_split_one_from_the_component() { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + let path = relay_socket("split"); + let (broadcast, mut messages) = tokio::sync::broadcast::channel(16); + let (to_component, mut relay) = super::spawn_ipc_relay(7, path.clone(), broadcast).unwrap(); + let mut component = tokio::net::UnixStream::connect(&path).await.unwrap(); + component.write_all(b"{\"half\":").await.unwrap(); + // The relay starts reading the component's message, then the page's + // message wins the select. + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + to_component.send("from the page".to_owned()).await.unwrap(); + let mut received = vec![0; 14]; + component.read_exact(&mut received).await.unwrap(); + assert_eq!(received, b"from the page\n"); + component.write_all(b"\"whole\"}\n").await.unwrap(); + let message = tokio::time::timeout(std::time::Duration::from_secs(5), messages.recv()) + .await + .unwrap() + .unwrap(); + assert!( + matches!(&message, crate::ipc::Response::IpcMessage { session_id: 7, payload } if payload == "{\"half\":\"whole\"}"), + "{message:?}" + ); + + // A message at the limit, ending in a carriage return and line + // feed, arrives whole. + let mut at_limit = vec![b'y'; super::MAX_RELAY_FRAME - 2]; + at_limit.extend_from_slice(b"\r\n"); + component.write_all(&at_limit).await.unwrap(); + let message = tokio::time::timeout(std::time::Duration::from_secs(5), messages.recv()) + .await + .unwrap() + .unwrap(); + assert!( + matches!(&message, crate::ipc::Response::IpcMessage { payload, .. } if payload.len() == super::MAX_RELAY_FRAME - 2), + "a message at the limit did not arrive whole" + ); + + // A line longer than any message ends the relay, even when its line + // break comes in the same read. + let mut over = vec![b'x'; super::MAX_RELAY_FRAME]; + over.push(b'\n'); + component.write_all(&over).await.unwrap(); + tokio::time::timeout(std::time::Duration::from_secs(5), relay.ended()) + .await + .expect("the relay did not end on an overlong line"); + assert!(messages.try_recv().is_err()); + drop(relay); + assert!(!path.exists()); + } + /// systemd-run checks its options and their combinations before it /// connects to the service manager. Run against an empty runtime /// directory and bus address, it must get as far as the connection 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..6421ebf 100644 --- a/crates/weft-compositor/src/appd_ipc.rs +++ b/crates/weft-compositor/src/appd_ipc.rs @@ -1,13 +1,21 @@ #[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, LoopHandle, Mode, PostAction, RegistrationToken, channel, + generic::Generic, + timer::{TimeoutAction, Timer}, +}; +#[cfg(unix)] +use smithay::reexports::wayland_server::backend::{ClientId, DisconnectReason}; #[cfg(unix)] use weft_ipc_types::{ @@ -15,15 +23,40 @@ 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, + /// 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. + fds: VecDeque, + /// The Wayland client of each appd session. + 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 + /// 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)] @@ -33,26 +66,111 @@ impl WeftAppdIpc { socket_path, read_buf: Vec::new(), write_stream: None, - pending_pids: HashMap::new(), + connected: false, + fds: VecDeque::new(), + sessions: HashMap::new(), + reported: HashSet::new(), + focused_on_map: HashSet::new(), + disconnected: None, + out_buf: Vec::new(), + flush_retry: None, + loop_handle: None, } } - #[allow(dead_code)] pub fn send(&mut self, msg: &CompositorToAppd) { - let Some(stream) = &mut self.write_stream else { - return; - }; match frame_encode(msg) { Ok(frame) => { - if stream.write_all(&frame).is_err() { - self.write_stream = None; + 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"), } } - fn try_decode_frames(&mut self) -> Vec { + /// 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(); + } + } + + /// 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(); + } + + /// 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) { + self.send(&CompositorToAppd::SurfaceReady { session_id }); + } + } + + /// 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 { @@ -66,9 +184,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; @@ -76,24 +192,45 @@ 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) } - 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,37 +238,118 @@ impl WeftAppdIpc { } } } - let messages = self.try_decode_frames(); - if eof { - self.write_stream = None; - self.read_buf.clear(); + 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; } (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.out_buf.clear(); + self.read_buf.clear(); + self.fds.clear(); + self.reported.clear(); + self.focused_on_map.clear(); + self.sessions.drain().map(|(_, client)| client).collect() + } } #[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); + 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 + .display_handle + .backend_handle() + .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 } => { - 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" + ); + } } } } +/// 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 +386,32 @@ 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() { + ipc.disconnected = Some(sender); + } + state + .loop_handle + .insert_source(receiver, |event, _, state| { + // 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); + ipc.focused_on_map.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 +421,14 @@ 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.connected) { + 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, @@ -187,10 +439,12 @@ 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(); } 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 @@ -203,16 +457,37 @@ 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" + "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) } }, ); + 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"), @@ -225,3 +500,176 @@ 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); + 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 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(); + 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/backend/drm.rs b/crates/weft-compositor/src/backend/drm.rs index 93bb258..51a56c2 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.layout_app_windows(); 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.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 7575211..cd11fad 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.layout_app_windows(); 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..28069b0 100644 --- a/crates/weft-compositor/src/input.rs +++ b/crates/weft-compositor/src/input.rs @@ -6,7 +6,7 @@ use smithay::{ TouchEvent, }, input::{ - keyboard::FilterResult, + keyboard::{FilterResult, KeyboardTarget}, pointer::{AxisFrame, ButtonEvent, MotionEvent}, }, utils::{Logical, Point, SERIAL_COUNTER}, @@ -42,20 +42,97 @@ pub fn process_input_event(state: &mut WeftCompositorState, eve } } +/// What the compositor does with a key instead of forwarding it. +enum Shortcut { + /// Alt+F4: close the focused application window. + Close, + /// A Super key tap: show the shell. + Shell, + /// Nothing: a suppressed key. + None, +} + fn handle_keyboard(state: &mut WeftCompositorState, event: B::KeyboardKeyEvent) { + use smithay::backend::input::KeyState; + use smithay::input::keyboard::keysyms; + let serial = SERIAL_COUNTER.next_serial(); let time = event.time_msec(); let key_state = event.state(); + let Some(keyboard) = state.seat.get_keyboard() else { + return; + }; - if let Some(keyboard) = state.seat.get_keyboard() { - keyboard.input::<(), _>( - state, - event.key_code(), - key_state, - serial, - time, - |_state, _mods, _keysym| FilterResult::Forward, - ); + // Alt+F4 and the Super keys belong to the compositor. Alt+F4 asks the + // focused application window to close. Super's key events are never + // delivered to an application: tapped alone, without another key or a + // button, it brings the shell's panel to the front with keyboard focus + // and is then delivered to the shell, which opens or closes its + // launcher. Held, it still sets the Super modifier for other keys. + let (filtered, mods_changed) = keyboard.input_intercept::<_, _>( + state, + event.key_code(), + key_state, + |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(); + let sym = keysym.modified_sym(); + if key_state == KeyState::Pressed { + if mods.alt && sym == keysyms::KEY_F4.into() { + state.super_tap = None; + state.suppressed_keys.push(code); + return FilterResult::Intercept(Shortcut::Close); + } + if sym == keysyms::KEY_Super_L.into() || sym == keysyms::KEY_Super_R.into() { + state.super_tap = (!mods.ctrl && !mods.alt && !mods.shift).then_some(code); + state.suppressed_keys.push(code); + return FilterResult::Intercept(Shortcut::None); + } + state.super_tap = None; + } else if let Some(i) = state.suppressed_keys.iter().position(|&k| k == code) { + state.suppressed_keys.remove(i); + if state.super_tap.take() == Some(code) { + return FilterResult::Intercept(Shortcut::Shell); + } + return FilterResult::Intercept(Shortcut::None); + } + FilterResult::Forward + }, + ); + let shortcut = match filtered { + FilterResult::Forward => { + keyboard.input_forward( + state, + event.key_code(), + key_state, + serial, + time, + mods_changed, + ); + return; + } + FilterResult::Intercept(shortcut) => shortcut, + }; + // The focused client follows the modifiers even when the key that + // changed them is taken, so none stays held there. + if mods_changed && let Some(focus) = keyboard.current_focus() { + let seat = state.seat.clone(); + focus.modifiers(&seat, state, keyboard.modifier_state(), serial); + } + match shortcut { + Shortcut::Close => state.close_focused_window(), + Shortcut::Shell => { + if state.activate_panel() { + // The tap itself, for the shell; the keyboard's own state + // already saw it. + let code = event.key_code(); + keyboard.input_forward(state, code, KeyState::Pressed, serial, time, false); + let release = SERIAL_COUNTER.next_serial(); + keyboard.input_forward(state, code, KeyState::Released, release, time, false); + } + } + Shortcut::None => {} } } @@ -122,10 +199,19 @@ 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. A held Super key is then no + // longer a tap. if button_state == ButtonState::Pressed { + state.super_tap = None; 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); } @@ -193,6 +279,7 @@ fn handle_pointer_axis( } fn handle_touch_down(state: &mut WeftCompositorState, event: B::TouchDownEvent) { + state.super_tap = None; let serial = SERIAL_COUNTER.next_serial(); let output = state.space.outputs().next().cloned(); if let Some(output) = output { 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/protocols/mod.rs b/crates/weft-compositor/src/protocols/mod.rs index 18922b4..7ef486f 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(), @@ -52,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() { @@ -97,6 +106,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 +122,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 +133,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 82960da..9eb8ba5 100644 --- a/crates/weft-compositor/src/state.rs +++ b/crates/weft-compositor/src/state.rs @@ -53,11 +53,35 @@ 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<(u64, ClientId)>, +} + +/// 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) {} + fn disconnected(&self, client_id: ClientId, _reason: DisconnectReason) { + if let Some(binding) = &self.session { + let _ = binding.disconnected.send((binding.session_id, client_id)); + } + } } /// Accumulated state for a multi-touch swipe gesture in progress. @@ -74,6 +98,12 @@ 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, + /// The Super key held down while no other key was pressed since; its + /// release is a tap that opens the shell. + pub super_tap: Option, pub compositor_state: CompositorState, pub xdg_shell_state: XdgShellState, @@ -124,7 +154,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); @@ -132,7 +165,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); @@ -169,6 +202,8 @@ impl WeftCompositorState { dmabuf_global: None, running: true, gesture_state: GestureState::default(), + suppressed_keys: Vec::new(), + super_tap: None, #[cfg(unix)] appd_ipc: None, #[cfg(target_os = "linux")] @@ -239,10 +274,22 @@ 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| { + 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); + self.map_new_window(window); + 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 +513,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 +533,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, @@ -495,8 +550,21 @@ 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 + && 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 @@ -515,8 +583,13 @@ 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); } } } @@ -525,7 +598,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, @@ -545,14 +618,64 @@ 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 + .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", + ), + } } } } + + 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 and its window slot; applications take the + // 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); + } + state.layout_app_windows(); + } + } } diff --git a/crates/weft-compositor/src/windows.rs b/crates/weft-compositor/src/windows.rs new file mode 100644 index 0000000..ff0b306 --- /dev/null +++ b/crates/weft-compositor/src/windows.rs @@ -0,0 +1,362 @@ +//! Stacking, focus and activation of toplevel windows. +//! +//! 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), one +//! appd asks to activate and the panel when a Super key is tapped are +//! raised to the top, marked activated and 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. + +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::{Logical, Rectangle, 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() + .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 { + window + .wl_surface() + .is_some_and(|surface| self.is_panel_surface(&surface)) + } + + /// 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) { + 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 shell's panel; returns whether one is registered. With + /// several, the first registered is the shell's. + pub fn activate_panel(&mut self) -> bool { + let panel = self + .weft_shell_state + .panels() + .filter(|p| p.is_alive()) + .find_map(|panel| { + let surface = panel + .data::() + .and_then(|data| data.surface.clone())?; + self.space + .elements() + .find(|window| window.wl_surface().is_some_and(|s| *s == surface)) + .cloned() + }); + match panel { + Some(panel) => { + self.activate_window(&panel); + true + } + None => false, + } + } + + /// 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(); + } + } + + /// 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 { + 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 and keeps them beneath every + /// 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 + /// 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>) { + // 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)); + let area = self.work_area(); + 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(); + } + 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 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); + state.states.set(xdg_toplevel::State::Maximized); + }); + toplevel.send_pending_configure(); + } + // 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); + } + } + + /// 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 = 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(); + } else if first { + self.layout_app_windows(); + 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)); + } + } + + /// 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()); + } + } + } + } + + /// 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().filter(|p| p.is_alive()) { + 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), + } + } + // 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)) + } + + /// Lays the windows out again after the work area changed, keeping the + /// stacking order. + pub fn layout_app_windows(&mut self) { + self.place_windows(false, None); + } + + /// 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/crates/weft-file-portal/Cargo.toml b/crates/weft-file-portal/Cargo.toml index 5e8c337..1b6ecbc 100644 --- a/crates/weft-file-portal/Cargo.toml +++ b/crates/weft-file-portal/Cargo.toml @@ -13,3 +13,6 @@ anyhow = "1.0" serde = { version = "1", features = ["derive"] } serde_json = "1" base64 = "0.22" + +[target.'cfg(target_os = "linux")'.dependencies] +rustix = { version = "1", features = ["fs"] } diff --git a/crates/weft-file-portal/src/main.rs b/crates/weft-file-portal/src/main.rs index 822e535..5becfcb 100644 --- a/crates/weft-file-portal/src/main.rs +++ b/crates/weft-file-portal/src/main.rs @@ -1,17 +1,37 @@ -#[cfg(unix)] -use std::io::{BufRead, BufReader, Write}; -use std::path::{Path, PathBuf}; +//! A per-session file proxy for the directories a session is granted. +//! +//! Requests name absolute host paths inside a granted directory. Each +//! granted directory is opened once at startup, and every operation resolves +//! the rest of the path relative to that descriptor with `openat2` and +//! `RESOLVE_BENEATH`, so the kernel keeps it inside the directory while it +//! runs: `..`, absolute symbolic links, links that point outside and links +//! replaced during the operation cannot leave it. Paths must name their +//! location directly, without `.` or `..` components. Writes replace a file +//! atomically and never create directories. + +#[cfg(target_os = "linux")] +use std::io::{BufRead, BufReader, Read, Write}; +use std::path::{Component, Path, PathBuf}; use serde::{Deserialize, Serialize}; -#[cfg(unix)] -use std::sync::Arc; - -#[cfg(unix)] -use anyhow::Context; - -#[cfg(unix)] -use std::os::unix::net::{UnixListener, UnixStream}; +/// The largest file read or written, in bytes. +const MAX_FILE: usize = 16 * 1024 * 1024; +/// The longest request line: a write of the largest file, base64-encoded, +/// with room for its path. +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +const MAX_LINE: usize = MAX_FILE / 3 * 4 + 64 * 1024; +/// The most entries one listing returns. +const MAX_ENTRIES: usize = 10_000; +/// The most connections served at once. +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +const MAX_CONNECTIONS: usize = 4; +/// How long a connection may stay silent before it is closed. +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +const IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60); +/// The prefix of the temporary files writes create, never listed. +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +const TEMPORARY_PREFIX: &str = ".weft-portal-"; #[derive(Deserialize)] #[serde(tag = "op", rename_all = "snake_case")] @@ -38,8 +58,13 @@ impl Response { } } -#[cfg(unix)] +#[cfg(target_os = "linux")] fn main() -> anyhow::Result<()> { + use anyhow::Context; + use std::os::unix::net::UnixListener; + use std::sync::Arc; + use std::sync::atomic::{AtomicUsize, Ordering}; + let args: Vec = std::env::args().collect(); if args.len() < 2 { eprintln!( @@ -49,32 +74,59 @@ fn main() -> anyhow::Result<()> { } let socket_path = &args[1]; - let allowed = Arc::new(parse_allowed(&args[2..])); + let mut roots = Vec::new(); + for grant in parse_allowed(&args[2..]) { + match Root::open(grant) { + Ok(root) => roots.push(root), + Err((path, e)) => eprintln!("cannot open granted directory {}: {e}", path.display()), + } + } + let roots = Arc::new(roots); if Path::new(socket_path).exists() { std::fs::remove_file(socket_path) .with_context(|| format!("remove stale socket {socket_path}"))?; } - let listener = UnixListener::bind(socket_path).with_context(|| format!("bind {socket_path}"))?; + let active = Arc::new(AtomicUsize::new(0)); for stream in listener.incoming() { - match stream { - Ok(s) => { - let allowed = Arc::clone(&allowed); - std::thread::spawn(move || handle_connection(s, &allowed)); + let mut stream = match stream { + Ok(s) => s, + Err(e) => { + eprintln!("accept error: {e}"); + continue; } - Err(e) => eprintln!("accept error: {e}"), + }; + if active.fetch_add(1, Ordering::SeqCst) >= MAX_CONNECTIONS { + active.fetch_sub(1, Ordering::SeqCst); + let _ = stream.write_all(b"{\"error\":\"too many connections\"}\n"); + continue; + } + // The slot is released however the connection ends, a panic + // included. + struct Slot(Arc); + impl Drop for Slot { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::SeqCst); + } + } + let slot = Slot(Arc::clone(&active)); + let roots = Arc::clone(&roots); + if let Err(e) = std::thread::Builder::new().spawn(move || { + let _slot = slot; + handle_connection(stream, &roots); + }) { + eprintln!("cannot serve a connection: {e}"); } } - Ok(()) } -#[cfg(not(unix))] +#[cfg(not(target_os = "linux"))] fn main() -> anyhow::Result<()> { - anyhow::bail!("weft-file-portal requires a Unix platform") + anyhow::bail!("weft-file-portal requires Linux") } /// A directory the session may use: `--allow` grants reading and writing, @@ -85,7 +137,6 @@ struct Allowed { writable: bool, } -#[cfg_attr(not(any(unix, test)), allow(dead_code))] fn parse_allowed(args: &[String]) -> Vec { let mut allowed = Vec::new(); let mut i = 0; @@ -109,33 +160,105 @@ fn parse_allowed(args: &[String]) -> Vec { allowed } -#[cfg_attr(not(any(unix, test)), allow(dead_code))] -fn normalize_path(path: &Path) -> PathBuf { - use std::path::Component; - let mut out = PathBuf::new(); - for c in path.components() { - match c { - Component::ParentDir => { - out.pop(); - } - Component::CurDir => {} - other => out.push(other), +/// The part of `path` inside the granted directory `root`, if `path` lies +/// in it and names its location directly: an absolute path of plain +/// components only, without `.`, `..`, empty components or a trailing `/`. +/// The text is checked as given, since path parsing drops `.` and repeated +/// separators. +fn relative_to<'a>(path: &'a Path, root: &Path) -> Option<&'a Path> { + let text = path.to_str()?; + let direct = text.starts_with('/') + && text[1..] + .split('/') + .all(|segment| !matches!(segment, "" | "." | "..")); + if !direct { + return None; + } + let rest = path.strip_prefix(root).ok()?; + rest.components() + .all(|c| matches!(c, Component::Normal(_))) + .then_some(rest) +} + +/// A granted directory, held open for the life of the portal. +#[cfg(target_os = "linux")] +struct Root { + path: PathBuf, + dir: rustix::fd::OwnedFd, + writable: bool, +} + +#[cfg(target_os = "linux")] +impl Root { + fn open(grant: Allowed) -> Result { + use rustix::fs::{Mode, OFlags}; + match rustix::fs::open( + &grant.root, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC, + Mode::empty(), + ) { + Ok(dir) => Ok(Self { + path: grant.root, + dir, + writable: grant.writable, + }), + Err(e) => Err((grant.root, e.into())), } } - out } -/// Whether `path` lies in an allowed directory, and for a write, in one -/// granted read-write. -fn is_allowed(path: &Path, allowed: &[Allowed], write: bool) -> bool { - let norm = normalize_path(path); - allowed +/// The granted directory holding `path` and the path inside it, for an +/// operation that writes when `write` is set. +#[cfg(target_os = "linux")] +fn locate<'a, 'p>( + roots: &'a [Root], + path: &'p str, + write: bool, +) -> Result<(&'a Root, &'p Path), Response> { + let requested = Path::new(path); + roots .iter() - .any(|a| norm.starts_with(&a.root) && (a.writable || !write)) + .filter(|root| root.writable || !write) + .find_map(|root| relative_to(requested, &root.path).map(|rest| (root, rest))) + .ok_or_else(|| Response::err(format!("access denied: {path}"))) } -#[cfg(unix)] -fn handle_connection(stream: UnixStream, allowed: &[Allowed]) { +/// Opens `rest` inside `root`, never leaving it. +#[cfg(target_os = "linux")] +fn open_beneath( + root: &Root, + rest: &Path, + flags: rustix::fs::OFlags, +) -> std::io::Result { + use rustix::fs::{Mode, OFlags, ResolveFlags}; + let rest = if rest.as_os_str().is_empty() { + Path::new(".") + } else { + rest + }; + rustix::fs::openat2( + &root.dir, + rest, + flags | OFlags::CLOEXEC | OFlags::NOCTTY, + Mode::empty(), + ResolveFlags::BENEATH | ResolveFlags::NO_MAGICLINKS, + ) + .map_err(|e| { + // The kernel reports a resolution that would leave the directory as + // EXDEV. + if e == rustix::io::Errno::XDEV { + std::io::Error::new( + std::io::ErrorKind::PermissionDenied, + "access denied: the path leads out of the granted directory", + ) + } else { + e.into() + } + }) +} + +#[cfg(target_os = "linux")] +fn handle_connection(stream: std::os::unix::net::UnixStream, roots: &[Root]) { let mut writer = match stream.try_clone() { Ok(s) => s, Err(e) => { @@ -143,22 +266,33 @@ fn handle_connection(stream: UnixStream, allowed: &[Allowed]) { return; } }; - let reader = BufReader::new(stream); - - for line in reader.lines() { - let line = match line { - Ok(l) => l, - Err(_) => break, - }; - if line.is_empty() { + let _ = stream.set_read_timeout(Some(IDLE_TIMEOUT)); + let _ = stream.set_write_timeout(Some(IDLE_TIMEOUT)); + let mut reader = BufReader::new(stream); + let mut line = Vec::new(); + loop { + line.clear(); + match (&mut reader) + .take(MAX_LINE as u64 + 3) + .read_until(b'\n', &mut line) + { + Ok(0) | Err(_) => break, + Ok(_) => {} + } + let content = line.strip_suffix(b"\n").unwrap_or(&line); + if content.strip_suffix(b"\r").unwrap_or(content).len() > MAX_LINE { + let _ = writer.write_all(b"{\"error\":\"request too long\"}\n"); + break; + } + let text = String::from_utf8_lossy(&line); + let text = text.trim_end_matches(['\n', '\r']); + if text.is_empty() { continue; } - - let response = match serde_json::from_str::(&line) { - Ok(req) => handle_request(req, allowed), + let response = match serde_json::from_str::(text) { + Ok(req) => handle_request(req, roots), Err(e) => Response::err(format!("bad request: {e}")), }; - let mut out = serde_json::to_string(&response) .unwrap_or_else(|_| r#"{"error":"serialize"}"#.to_string()); out.push('\n'); @@ -168,246 +302,453 @@ fn handle_connection(stream: UnixStream, allowed: &[Allowed]) { } } -#[cfg_attr(not(any(unix, test)), allow(dead_code))] -fn handle_request(req: Request, allowed: &[Allowed]) -> Response { - match req { +#[cfg(target_os = "linux")] +fn handle_request(req: Request, roots: &[Root]) -> Response { + let result = match req { Request::Read { path } => { - let p = PathBuf::from(&path); - if !is_allowed(&p, allowed, false) { - return Response::err(format!("access denied: {path}")); - } - match std::fs::read(&p) { - Ok(data) => Response::OkData { - data_b64: base64::Engine::encode( - &base64::engine::general_purpose::STANDARD, - &data, - ), - }, - Err(e) => Response::err(e), - } + locate(roots, &path, false).and_then(|(root, rest)| read(root, rest)) } - Request::Write { path, data_b64 } => { - let p = PathBuf::from(&path); - if !is_allowed(&p, allowed, true) { - return Response::err(format!("access denied: {path}")); - } + Request::Write { path, data_b64 } => locate(roots, &path, true).and_then(|(root, rest)| { let data = - match base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &data_b64) - { - Ok(d) => d, - Err(e) => return Response::err(format!("bad base64: {e}")), - }; - if let Some(Err(e)) = p.parent().map(std::fs::create_dir_all) { - return Response::err(e); - } - match std::fs::write(&p, &data) { - Ok(()) => Response::Ok, - Err(e) => Response::err(e), - } - } + base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &data_b64) + .map_err(|e| Response::err(format!("bad base64: {e}")))?; + write(root, rest, &data) + }), Request::List { path } => { - let p = PathBuf::from(&path); - if !is_allowed(&p, allowed, false) { - return Response::err(format!("access denied: {path}")); - } - match std::fs::read_dir(&p) { - Ok(entries) => { - let mut names = Vec::new(); - for entry in entries.flatten() { - if let Some(name) = entry.file_name().to_str() { - names.push(name.to_string()); - } - } - names.sort(); - Response::OkEntries { entries: names } - } - Err(e) => Response::err(e), - } + locate(roots, &path, false).and_then(|(root, rest)| list(root, rest)) } - } + }; + result.unwrap_or_else(|response| response) } -#[cfg(test)] +#[cfg(target_os = "linux")] +fn read(root: &Root, rest: &Path) -> Result { + use rustix::fs::{FileType, OFlags}; + let fd = open_beneath(root, rest, OFlags::RDONLY | OFlags::NONBLOCK).map_err(Response::err)?; + let stat = rustix::fs::fstat(&fd).map_err(Response::err)?; + if FileType::from_raw_mode(stat.st_mode) != FileType::RegularFile { + return Err(Response::err("not a regular file")); + } + let mut data = Vec::new(); + std::fs::File::from(fd) + .take(MAX_FILE as u64 + 1) + .read_to_end(&mut data) + .map_err(Response::err)?; + if data.len() > MAX_FILE { + return Err(Response::err(format!("file larger than {MAX_FILE} bytes"))); + } + Ok(Response::OkData { + data_b64: base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &data), + }) +} + +#[cfg(target_os = "linux")] +fn list(root: &Root, rest: &Path) -> Result { + use rustix::fs::OFlags; + let fd = open_beneath(root, rest, OFlags::RDONLY | OFlags::DIRECTORY).map_err(Response::err)?; + let mut entries = rustix::fs::Dir::read_from(&fd).map_err(Response::err)?; + let mut names = Vec::new(); + while let Some(entry) = entries.read() { + let entry = entry.map_err(Response::err)?; + let Ok(name) = entry.file_name().to_str() else { + continue; + }; + if name == "." || name == ".." || name.starts_with(TEMPORARY_PREFIX) { + continue; + } + if names.len() == MAX_ENTRIES { + return Err(Response::err(format!("more than {MAX_ENTRIES} entries"))); + } + names.push(name.to_owned()); + } + names.sort(); + Ok(Response::OkEntries { entries: names }) +} + +/// Replaces the file `rest` in `root` with `data`: the data is written to a +/// new file in the same directory, flushed and renamed over the name, so +/// the file holds either the old or the new content. +#[cfg(target_os = "linux")] +fn write(root: &Root, rest: &Path, data: &[u8]) -> Result { + use rustix::fs::{Mode, OFlags}; + if data.len() > MAX_FILE { + return Err(Response::err(format!("file larger than {MAX_FILE} bytes"))); + } + let name = rest + .file_name() + .ok_or_else(|| Response::err("a write needs a file name"))?; + let parent = open_beneath( + root, + rest.parent().unwrap_or(Path::new("")), + OFlags::RDONLY | OFlags::DIRECTORY, + ) + .map_err(Response::err)?; + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + // Unique within this portal by the counter, and across restarts by the + // time of its first write, so a file left by an earlier instance does + // not collide. + static STARTED: std::sync::OnceLock = std::sync::OnceLock::new(); + let started = STARTED.get_or_init(|| { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_nanos()) + }); + let temporary = format!( + "{TEMPORARY_PREFIX}{}-{started}-{}", + std::process::id(), + NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + ); + // A replaced file keeps its permission bits; a new one is private. + let mode = match rustix::fs::statat(&parent, name, rustix::fs::AtFlags::SYMLINK_NOFOLLOW) { + Ok(stat) + if rustix::fs::FileType::from_raw_mode(stat.st_mode) + == rustix::fs::FileType::RegularFile => + { + stat.st_mode & 0o777 + } + _ => 0o600, + }; + let file = rustix::fs::openat( + &parent, + temporary.as_str(), + OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::from_raw_mode(0o600), + ) + .map_err(Response::err)?; + let _ = rustix::fs::fchmod(&file, Mode::from_raw_mode(mode)); + let mut file = std::fs::File::from(file); + let written = file + .write_all(data) + .and_then(|()| file.sync_all()) + .and_then(|()| { + rustix::fs::renameat(&parent, temporary.as_str(), &parent, name) + .map_err(std::io::Error::from) + }); + if let Err(e) = written { + let _ = rustix::fs::unlinkat(&parent, temporary.as_str(), rustix::fs::AtFlags::empty()); + return Err(Response::err(e)); + } + let _ = rustix::fs::fsync(&parent); + Ok(Response::Ok) +} + +#[cfg(all(test, target_os = "linux"))] mod tests { use super::*; + use std::fs; - fn rw(root: impl Into) -> Allowed { - Allowed { - root: root.into(), - writable: true, + fn grant(root: &Path, writable: bool) -> Vec { + vec![ + Root::open(Allowed { + root: root.to_path_buf(), + writable, + }) + .unwrap(), + ] + } + + fn temp(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("wfp_{name}_{}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(dir.join("granted")).unwrap(); + dir + } + + fn path(p: &Path) -> String { + p.to_string_lossy().into_owned() + } + + fn denied(resp: &Response) -> bool { + matches!(resp, Response::Err { .. }) + } + + fn read_req(p: &Path) -> Request { + Request::Read { path: path(p) } + } + + fn write_req(p: &Path, data: &[u8]) -> Request { + Request::Write { + path: path(p), + data_b64: base64::Engine::encode(&base64::engine::general_purpose::STANDARD, data), } } - fn ro(root: impl Into) -> Allowed { - Allowed { - root: root.into(), - writable: false, - } - } - - #[test] - fn allowed_path_accepted() { - let allowed = vec![rw("/tmp/weft-test-allowed")]; - assert!(is_allowed( - Path::new("/tmp/weft-test-allowed/file.txt"), - &allowed, - false - )); - } - - #[test] - fn disallowed_path_rejected() { - let allowed = vec![rw("/tmp/weft-test-allowed")]; - assert!(!is_allowed(Path::new("/etc/passwd"), &allowed, false)); - } - - #[test] - fn dotdot_traversal_blocked() { - let allowed = vec![rw("/tmp/weft-test-allowed")]; - assert!(!is_allowed( - Path::new("/tmp/weft-test-allowed/../etc/passwd"), - &allowed, - false - )); - } - - #[test] - fn empty_allowlist_rejects_all() { - assert!(!is_allowed(Path::new("/tmp/anything"), &[], false)); - } - #[test] fn parse_allowed_extracts_paths() { let args: Vec = vec![ "--allow".into(), "/tmp/a".into(), - "--allow".into(), - "/tmp/b".into(), + "--allow-read".into(), + "/tmp/c".into(), ]; - let result = parse_allowed(&args); - assert_eq!(result, vec![rw("/tmp/a"), rw("/tmp/b")]); - let args: Vec = vec!["--allow-read".into(), "/tmp/c".into()]; - assert_eq!(parse_allowed(&args), vec![ro("/tmp/c")]); + assert_eq!( + parse_allowed(&args), + vec![ + Allowed { + root: "/tmp/a".into(), + writable: true + }, + Allowed { + root: "/tmp/c".into(), + writable: false + }, + ] + ); } #[test] - fn handle_request_read_denied() { - let resp = handle_request( - Request::Read { - path: "/etc/shadow".into(), - }, - &[rw("/tmp/safe")], + fn paths_must_name_a_place_inside_a_granted_directory_directly() { + let root = Path::new("/srv/granted"); + assert_eq!( + relative_to(Path::new("/srv/granted/a/b"), root), + Some(Path::new("a/b")) ); - let json = serde_json::to_string(&resp).unwrap(); - assert!(json.contains("access denied")); - } - - #[test] - fn handle_request_read_roundtrip() { - use std::fs; - let dir = std::env::temp_dir().join(format!("wfp_test_{}", std::process::id())); - let _ = fs::create_dir_all(&dir); - let file = dir.join("hello.txt"); - fs::write(&file, b"hello world").unwrap(); - - let allowed = vec![rw(dir.clone())]; - let resp = handle_request( - Request::Read { - path: file.to_string_lossy().into(), - }, - &allowed, + assert_eq!( + relative_to(Path::new("/srv/granted"), root), + Some(Path::new("")) ); - let json = serde_json::to_string(&resp).unwrap(); - assert!(json.contains("data_b64")); - - if let Response::OkData { data_b64 } = resp { - let decoded = - base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &data_b64) - .unwrap(); - assert_eq!(decoded, b"hello world"); - } else { - panic!("expected OkData"); + for outside in [ + "/srv/grantedx/a", + "/srv/granted/../x", + "/srv/granted/a/../../x", + "/etc", + "/srv/granted/a/./b", + "/srv/granted/a/.", + "/srv/granted/a/", + "/srv/granted//a", + "srv/granted/a", + ] { + assert_eq!(relative_to(Path::new(outside), root), None, "{outside}"); } - - let _ = fs::remove_dir_all(&dir); } #[test] - fn handle_request_list() { - use std::fs; - let dir = std::env::temp_dir().join(format!("wfp_list_{}", std::process::id())); - let _ = fs::create_dir_all(&dir); - fs::write(dir.join("b.txt"), b"").unwrap(); - fs::write(dir.join("a.txt"), b"").unwrap(); - - let allowed = vec![rw(dir.clone())]; - let resp = handle_request( + fn reads_writes_and_lists_inside_the_directory() { + let dir = temp("roundtrip"); + let granted = dir.join("granted"); + let roots = grant(&granted, true); + assert!(matches!( + handle_request(write_req(&granted.join("b.txt"), b"hello"), &roots), + Response::Ok + )); + fs::write(granted.join("a.txt"), b"").unwrap(); + match handle_request(read_req(&granted.join("b.txt")), &roots) { + Response::OkData { data_b64 } => assert_eq!( + base64::Engine::decode(&base64::engine::general_purpose::STANDARD, data_b64) + .unwrap(), + b"hello" + ), + _ => panic!("expected data"), + } + match handle_request( Request::List { - path: dir.to_string_lossy().into(), + path: path(&granted), }, - &allowed, - ); - if let Response::OkEntries { entries } = resp { - assert_eq!(entries, vec!["a.txt", "b.txt"]); - } else { - panic!("expected OkEntries"); + &roots, + ) { + Response::OkEntries { entries } => assert_eq!(entries, ["a.txt", "b.txt"]), + _ => panic!("expected entries"), } - let _ = fs::remove_dir_all(&dir); } #[test] - fn handle_request_write_creates_parent_dirs() { - use std::fs; - let dir = std::env::temp_dir().join(format!("wfp_write_{}", std::process::id())); + fn links_cannot_lead_out_of_the_directory() { + let dir = temp("links"); + let granted = dir.join("granted"); + fs::write(dir.join("secret.txt"), b"secret").unwrap(); + fs::create_dir(granted.join("sub")).unwrap(); + std::os::unix::fs::symlink("/", granted.join("root")).unwrap(); + std::os::unix::fs::symlink("../secret.txt", granted.join("up")).unwrap(); + std::os::unix::fs::symlink(&dir, granted.join("parent")).unwrap(); + let roots = grant(&granted, true); + for escape in [ + granted.join("root/etc/passwd"), + granted.join("up"), + granted.join("parent/secret.txt"), + // The check this replaced normalised `link/..` away and then + // followed the link. + granted.join("root/../secret.txt"), + ] { + assert!( + denied(&handle_request(read_req(&escape), &roots)), + "{}", + escape.display() + ); + if escape != granted.join("up") { + assert!( + denied(&handle_request(write_req(&escape, b"x"), &roots)), + "{}", + escape.display() + ); + } + } + // A write to a name that is a link replaces the link, inside the + // directory, rather than writing where it points. + match handle_request(read_req(&granted.join("root/etc/passwd")), &roots) { + Response::Err { error } => assert!(error.contains("leads out"), "{error}"), + _ => panic!("read through a link out of the directory"), + } + assert!(matches!( + handle_request(write_req(&granted.join("up"), b"x"), &roots), + Response::Ok + )); + assert!(fs::symlink_metadata(granted.join("up")).unwrap().is_file()); + assert_eq!(fs::read(dir.join("secret.txt")).unwrap(), b"secret"); + // A link that stays inside is followed. + std::os::unix::fs::symlink("sub", granted.join("inside")).unwrap(); + assert!(matches!( + handle_request(write_req(&granted.join("inside/f"), b"ok"), &roots), + Response::Ok + )); + assert_eq!(fs::read(granted.join("sub/f")).unwrap(), b"ok"); let _ = fs::remove_dir_all(&dir); - let nested = dir.join("sub").join("deep").join("file.txt"); - let data = base64::Engine::encode( - &base64::engine::general_purpose::STANDARD, - b"nested content", + } + + #[test] + fn a_directory_swapped_for_a_link_mid_operation_cannot_lead_out() { + let dir = temp("race"); + let granted = dir.join("granted"); + fs::create_dir(dir.join("outside")).unwrap(); + fs::write(dir.join("outside/f"), b"secret").unwrap(); + fs::create_dir(granted.join("real")).unwrap(); + fs::write(granted.join("real/f"), b"inside").unwrap(); + let roots = grant(&granted, true); + // `swap` is a link, replaced in a loop, alternately to the inside + // directory and out of the granted one, absolute and relative. + std::os::unix::fs::symlink("real", granted.join("swap")).unwrap(); + let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); + let swapper = { + let (granted, outside, stop) = (granted.clone(), dir.join("outside"), stop.clone()); + std::thread::spawn(move || { + let targets = [PathBuf::from("real"), outside, PathBuf::from("../outside")]; + for target in targets.iter().cycle() { + if stop.load(std::sync::atomic::Ordering::Relaxed) { + break; + } + let _ = std::os::unix::fs::symlink(target, granted.join("next")); + let _ = fs::rename(granted.join("next"), granted.join("swap")); + } + }) + }; + let (mut inside, mut refused) = (0, 0); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); + while (inside < 50 || refused < 50) && std::time::Instant::now() < deadline { + match handle_request(read_req(&granted.join("swap/f")), &roots) { + Response::OkData { data_b64 } => { + let data = base64::Engine::decode( + &base64::engine::general_purpose::STANDARD, + data_b64, + ) + .unwrap(); + assert_eq!(data, b"inside", "a read left the directory"); + inside += 1; + } + Response::Err { error } if error.contains("leads out") => refused += 1, + _ => {} + } + let _ = handle_request(write_req(&granted.join("swap/f"), b"inside"), &roots); + } + stop.store(true, std::sync::atomic::Ordering::Relaxed); + swapper.join().unwrap(); + assert_eq!(fs::read(dir.join("outside/f")).unwrap(), b"secret"); + assert!( + inside >= 50 && refused >= 50, + "the race was not exercised: {inside} reads inside, {refused} refused" ); - let allowed = vec![rw(dir.clone())]; - let resp = handle_request( - Request::Write { - path: nested.to_string_lossy().into(), - data_b64: data, - }, - &allowed, - ); - assert!(matches!(resp, Response::Ok), "expected Ok response"); - assert_eq!(fs::read(&nested).unwrap(), b"nested content"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn a_replaced_file_keeps_its_mode() { + use std::os::unix::fs::PermissionsExt; + let dir = temp("mode"); + let granted = dir.join("granted"); + fs::write(granted.join("shared.txt"), b"old").unwrap(); + fs::set_permissions( + granted.join("shared.txt"), + fs::Permissions::from_mode(0o644), + ) + .unwrap(); + let roots = grant(&granted, true); + assert!(matches!( + handle_request(write_req(&granted.join("shared.txt"), b"new"), &roots), + Response::Ok + )); + let mode = fs::metadata(granted.join("shared.txt")) + .unwrap() + .permissions() + .mode(); + assert_eq!(mode & 0o777, 0o644); let _ = fs::remove_dir_all(&dir); } #[test] fn read_only_directories_refuse_writes() { - use std::fs; - let dir = std::env::temp_dir().join(format!("wfp_ro_{}", std::process::id())); + let dir = temp("ro"); + let granted = dir.join("granted"); + fs::write(granted.join("kept.txt"), b"original").unwrap(); + let roots = grant(&granted, false); + assert!(denied(&handle_request( + write_req(&granted.join("kept.txt"), b"x"), + &roots + ))); + assert_eq!(fs::read(granted.join("kept.txt")).unwrap(), b"original"); + assert!(matches!( + handle_request(read_req(&granted.join("kept.txt")), &roots), + Response::OkData { .. } + )); let _ = fs::remove_dir_all(&dir); - fs::create_dir_all(&dir).unwrap(); - fs::write(dir.join("kept.txt"), b"original").unwrap(); - let allowed = vec![ro(dir.clone())]; - let data = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, b"x"); - let resp = handle_request( - Request::Write { - path: dir.join("kept.txt").to_string_lossy().into(), - data_b64: data, - }, - &allowed, - ); - assert!( - serde_json::to_string(&resp) - .unwrap() - .contains("access denied") - ); - assert_eq!(fs::read(dir.join("kept.txt")).unwrap(), b"original"); - let resp = handle_request( - Request::Read { - path: dir.join("kept.txt").to_string_lossy().into(), - }, - &allowed, - ); - assert!(matches!(resp, Response::OkData { .. })); + } + + #[test] + fn writes_create_no_directories_and_leave_no_temporary_files() { + let dir = temp("nodirs"); + let granted = dir.join("granted"); + let roots = grant(&granted, true); + assert!(denied(&handle_request( + write_req(&granted.join("missing/f"), b"x"), + &roots + ))); + assert!(!granted.join("missing").exists()); + assert!(denied(&handle_request(write_req(&granted, b"x"), &roots))); + let left: Vec<_> = fs::read_dir(&granted).unwrap().flatten().collect(); + assert!(left.is_empty(), "{left:?}"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn oversized_files_are_refused() { + let dir = temp("size"); + let granted = dir.join("granted"); + fs::File::create(granted.join("big")) + .unwrap() + .set_len(MAX_FILE as u64 + 1) + .unwrap(); + let roots = grant(&granted, true); + assert!(denied(&handle_request( + read_req(&granted.join("big")), + &roots + ))); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn overlong_requests_end_the_connection() { + let dir = temp("line"); + let granted = dir.join("granted"); + let roots = grant(&granted, true); + let (client, server) = std::os::unix::net::UnixStream::pair().unwrap(); + let serve = std::thread::spawn(move || handle_connection(server, &roots)); + let mut writer = client.try_clone().unwrap(); + let sent = std::thread::spawn(move || { + let _ = writer.write_all(&vec![b'x'; MAX_LINE + 64]); + }); + let mut reply = String::new(); + BufReader::new(client).read_line(&mut reply).unwrap(); + serve.join().unwrap(); + let _ = sent.join(); + assert!(reply.contains("request too long"), "{reply}"); let _ = fs::remove_dir_all(&dir); } } 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/approval.rs b/crates/weft-ipc-types/src/approval.rs new file mode 100644 index 0000000..f553a39 --- /dev/null +++ b/crates/weft-ipc-types/src/approval.rs @@ -0,0 +1,144 @@ +//! The capabilities the user approved for an installed app. +//! +//! A package declares capabilities; it gets only those the user approved. +//! `weft-pack` records the approval, `$XDG_DATA_HOME/weft/approvals/`, +//! one capability per line, and weft-appd refuses to launch an app that +//! declares a capability the record does not hold. An app's own private +//! data is a minimal execution resource and needs no approval. Signature +//! verification and approval are separate decisions: a trusted signature +//! approves nothing. + +use std::collections::BTreeSet; +use std::path::{Path, PathBuf}; + +use crate::capability::Capability; + +/// Where the approval record of `app_id` lives. +pub fn approval_path(data_home: &Path, app_id: &str) -> PathBuf { + data_home.join("weft/approvals").join(app_id) +} + +/// Whether a declared capability needs the user's approval. Strings that +/// are not capabilities are left to the capability checks, which refuse +/// them. +pub fn needs_approval(capability: &str) -> bool { + capability + .parse::() + .is_ok_and(|c| !matches!(c, Capability::AppData(_))) +} + +/// The declared capabilities that need approval and are not in `approved`. +pub fn unapproved<'a>(declared: &'a [String], approved: &BTreeSet) -> Vec<&'a str> { + declared + .iter() + .map(String::as_str) + .filter(|c| needs_approval(c) && !approved.contains(*c)) + .collect() +} + +/// The capabilities approved for an app; none when there is no record. +pub fn read_approved(record: &Path) -> std::io::Result> { + match std::fs::read_to_string(record) { + Ok(text) => Ok(text + .lines() + .map(str::trim) + .filter(|l| !l.is_empty()) + .map(str::to_owned) + .collect()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeSet::new()), + Err(e) => Err(e), + } +} + +/// Records exactly the capabilities of `declared` that need approval as the +/// app's approval, replacing an earlier one in one rename. With none to +/// approve, the record is removed. +pub fn write_approved(record: &Path, declared: &[String]) -> std::io::Result<()> { + use std::io::Write; + let approved: BTreeSet<&str> = declared + .iter() + .map(String::as_str) + .filter(|c| needs_approval(c)) + .collect(); + if approved.is_empty() { + return remove_approval(record); + } + let parent = record + .parent() + .expect("approval records live in a directory"); + std::fs::create_dir_all(parent)?; + let temp = parent.join(format!( + ".{}.{}.tmp", + record + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("record"), + std::process::id() + )); + let written = (|| { + let mut file = std::fs::File::create(&temp)?; + for capability in &approved { + writeln!(file, "{capability}")?; + } + file.sync_all()?; + std::fs::rename(&temp, record) + })(); + if written.is_err() { + let _ = std::fs::remove_file(&temp); + } + written?; + std::fs::File::open(parent).and_then(|dir| dir.sync_all()) +} + +/// Removes an app's approval, so a later installation asks again. +pub fn remove_approval(record: &Path) -> std::io::Result<()> { + match std::fs::remove_file(record) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e), + _ => Ok(()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn caps(list: &[&str]) -> Vec { + list.iter().map(|c| (*c).to_owned()).collect() + } + + #[test] + fn private_app_data_needs_no_approval() { + assert!(!needs_approval("fs:rw:app-data")); + assert!(!needs_approval("fs:read:app-data")); + assert!(needs_approval("net:fetch:example.org")); + assert!(needs_approval("sys:notifications")); + assert!(needs_approval("fs:read:xdg-documents")); + // Not a capability: refused elsewhere, not reported as unapproved. + assert!(!needs_approval("sys:everything")); + } + + #[test] + fn approvals_cover_exactly_what_was_approved() { + let dir = std::env::temp_dir().join(format!("weft_approval_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let record = approval_path(&dir, "org.weft.test"); + let declared = caps(&["fs:rw:app-data", "net:fetch:example.org"]); + assert_eq!(read_approved(&record).unwrap(), BTreeSet::new()); + assert_eq!( + unapproved(&declared, &read_approved(&record).unwrap()), + ["net:fetch:example.org"] + ); + write_approved(&record, &declared).unwrap(); + assert!(unapproved(&declared, &read_approved(&record).unwrap()).is_empty()); + // An update that asks for more needs approval for the new capability. + let updated = caps(&["net:fetch:example.org", "sys:clipboard:read"]); + assert_eq!( + unapproved(&updated, &read_approved(&record).unwrap()), + ["sys:clipboard:read"] + ); + // Nothing left to approve removes the record. + write_approved(&record, &caps(&["fs:rw:app-data"])).unwrap(); + assert!(!record.exists()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/weft-ipc-types/src/lib.rs b/crates/weft-ipc-types/src/lib.rs index 0590c77..b633c6b 100644 --- a/crates/weft-ipc-types/src/lib.rs +++ b/crates/weft-ipc-types/src/lib.rs @@ -1,6 +1,9 @@ +pub mod approval; pub mod capability; pub mod manifest; pub mod package; +pub mod store; +pub mod trust; use serde::{Deserialize, Serialize}; @@ -10,25 +13,36 @@ 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, }, 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. #[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 +125,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 +176,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/crates/weft-ipc-types/src/manifest.rs b/crates/weft-ipc-types/src/manifest.rs index 0115bc4..f5f70f5 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. @@ -62,9 +87,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] { @@ -212,6 +247,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/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index be2a170..9da0a2f 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -79,6 +79,39 @@ 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: &Path, + app_id: &str, +) -> std::io::Result> { + 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)), + 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 { @@ -180,13 +213,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/store.rs b/crates/weft-ipc-types/src/store.rs new file mode 100644 index 0000000..49c117b --- /dev/null +++ b/crates/weft-ipc-types/src/store.rs @@ -0,0 +1,238 @@ +//! The layout of a package store and the revisions it holds. +//! +//! Each installed revision of a package is an immutable directory named by +//! its content digest, `/.revisions//`. The active +//! revision is chosen by `/`, a symbolic link to it that one +//! rename replaces, so an update or rollback switches every new launch at +//! once and an interrupted one leaves the previous choice in place. Stores +//! written before revisions existed hold the package directly in +//! `/`; that layout is still read. +//! +//! A session pins the revision it was launched from with a shared lock on +//! the revision directory, held for the session's lifetime. Package +//! operations remove a revision only after taking that lock exclusively, so +//! a live session keeps running on the bytes it started with. + +use std::fs::File; +use std::path::{Path, PathBuf}; + +/// The directory of a store that holds package revisions. +pub const REVISIONS_DIR: &str = ".revisions"; + +/// The directory holding the revisions of `app_id`. +pub fn revisions_of(store_root: &Path, app_id: &str) -> PathBuf { + store_root.join(REVISIONS_DIR).join(app_id) +} + +/// The link target that makes `revision` the active revision of `app_id`, +/// relative to the store root. +pub fn link_target(app_id: &str, revision: &str) -> PathBuf { + Path::new(REVISIONS_DIR).join(app_id).join(revision) +} + +/// Whether `name` is a revision name: the hex content digest of a package. +pub fn is_revision_name(name: &str) -> bool { + name.len() == 64 && name.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) +} + +/// Where the active package of `app_id` is in a store. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Active { + /// A revision, by name, in `.revisions//`. + Revision { name: String, dir: PathBuf }, + /// A package directory installed before revisions existed. + Directory(PathBuf), +} + +impl Active { + /// The package root. + pub fn dir(&self) -> &Path { + match self { + Self::Revision { dir, .. } | Self::Directory(dir) => dir, + } + } +} + +/// Why the active package of a store cannot be determined. +#[derive(Debug)] +pub enum StoreError { + Io(PathBuf, std::io::Error), + /// `/` is neither a package directory nor a link to one + /// of its revisions. + NotAPackage(PathBuf), +} + +impl std::fmt::Display for StoreError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(path, e) => write!(f, "cannot read {}: {e}", path.display()), + Self::NotAPackage(path) => write!( + f, + "{} is neither a package directory nor a link to one of its revisions", + path.display() + ), + } + } +} + +impl std::error::Error for StoreError {} + +/// The active package of `app_id` in `store_root`, if any. A link is +/// followed only when it names one of the app's own revisions, exactly as +/// package operations write it, and that revision is a directory. +pub fn active(store_root: &Path, app_id: &str) -> Result, StoreError> { + let entry = store_root.join(app_id); + let metadata = match std::fs::symlink_metadata(&entry) { + Ok(m) => m, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(StoreError::Io(entry, e)), + }; + if metadata.is_dir() { + return Ok(Some(Active::Directory(entry))); + } + if !metadata.file_type().is_symlink() { + return Err(StoreError::NotAPackage(entry)); + } + let target = std::fs::read_link(&entry).map_err(|e| StoreError::Io(entry.clone(), e))?; + let name = target + .file_name() + .and_then(|n| n.to_str()) + .filter(|n| is_revision_name(n)) + .map(str::to_owned); + let Some(name) = name.filter(|n| target == link_target(app_id, n)) else { + return Err(StoreError::NotAPackage(entry)); + }; + let dir = revisions_of(store_root, app_id).join(&name); + match std::fs::symlink_metadata(&dir) { + Ok(m) if m.is_dir() => Ok(Some(Active::Revision { name, dir })), + Ok(_) => Err(StoreError::NotAPackage(entry)), + Err(e) => Err(StoreError::Io(dir, e)), + } +} + +/// A shared lock on a package directory, held while a session runs from it. +#[derive(Debug)] +pub struct Pin { + _dir: File, +} + +/// Pins the package directory `dir`. The lock is taken on the directory +/// that is opened, and the path must still name that directory afterwards: +/// a revision removed or replaced in between is not pinned. A directory a +/// package operation holds is not waited for, so nobody able to lock a +/// package directory can stall launches; the launch is refused instead. +pub fn pin(dir: &Path) -> std::io::Result { + let file = File::open(dir)?; + match file.try_lock_shared() { + Ok(()) => {} + Err(std::fs::TryLockError::WouldBlock) => { + return Err(std::io::Error::other(format!( + "{} is being changed; try again", + dir.display() + ))); + } + Err(std::fs::TryLockError::Error(e)) => return Err(e), + } + if !same_file(&file, dir)? { + return Err(std::io::Error::other(format!( + "{} changed while it was being pinned", + dir.display() + ))); + } + Ok(Pin { _dir: file }) +} + +/// Takes the exclusive lock on a package directory that no session pins, +/// or returns `None` while one does. +pub fn try_claim(dir: &Path) -> std::io::Result> { + let file = File::open(dir)?; + match file.try_lock() { + Ok(()) => Ok(Some(file)), + Err(std::fs::TryLockError::WouldBlock) => Ok(None), + Err(std::fs::TryLockError::Error(e)) => Err(e), + } +} + +#[cfg(unix)] +fn same_file(file: &File, path: &Path) -> std::io::Result { + use std::os::unix::fs::MetadataExt; + let opened = file.metadata()?; + let named = std::fs::symlink_metadata(path)?; + Ok(opened.dev() == named.dev() && opened.ino() == named.ino()) +} + +#[cfg(not(unix))] +fn same_file(_file: &File, _path: &Path) -> std::io::Result { + Ok(true) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn store(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("weft_store_{name}_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + const REV: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + + #[cfg(unix)] + #[test] + fn only_links_to_the_apps_own_revisions_are_followed() { + use std::os::unix::fs::symlink; + let root = store("links"); + std::fs::create_dir_all(revisions_of(&root, "org.a").join(REV)).unwrap(); + std::fs::create_dir_all(revisions_of(&root, "org.b").join(REV)).unwrap(); + assert!(active(&root, "org.a").unwrap().is_none()); + + symlink(link_target("org.a", REV), root.join("org.a")).unwrap(); + assert_eq!( + active(&root, "org.a").unwrap(), + Some(Active::Revision { + name: REV.to_owned(), + dir: revisions_of(&root, "org.a").join(REV), + }) + ); + // Another app's revision, an absolute target, a non-revision name. + for target in [ + link_target("org.b", REV), + revisions_of(&root, "org.c").join(REV), + PathBuf::from(".revisions/org.c/latest"), + ] { + let _ = std::fs::remove_file(root.join("org.c")); + std::fs::create_dir_all(revisions_of(&root, "org.c").join(REV)).unwrap(); + symlink(&target, root.join("org.c")).unwrap(); + assert!( + matches!(active(&root, "org.c"), Err(StoreError::NotAPackage(_))), + "{}", + target.display() + ); + } + // A package installed before revisions existed. + std::fs::create_dir(root.join("org.d")).unwrap(); + assert_eq!( + active(&root, "org.d").unwrap(), + Some(Active::Directory(root.join("org.d"))) + ); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn a_pinned_directory_cannot_be_claimed() { + let root = store("pin"); + let dir = root.join("rev"); + std::fs::create_dir(&dir).unwrap(); + let pinned = pin(&dir).unwrap(); + assert!(try_claim(&dir).unwrap().is_none()); + // Other sessions can pin it too. + let again = pin(&dir).unwrap(); + drop((pinned, again)); + let claimed = try_claim(&dir).unwrap(); + assert!(claimed.is_some()); + drop(claimed); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs new file mode 100644 index 0000000..0547b5a --- /dev/null +++ b/crates/weft-ipc-types/src/trust.rs @@ -0,0 +1,521 @@ +//! 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 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}; + +use ed25519_dalek::{Signature, 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 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), +} + +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::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()), + } + } +} + +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> { + 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) + } +} + +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(); + 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(); + 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_strict(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); + }; + 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)) + } +} + +/// 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. 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) +} + +/// 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, 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"); + 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 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)) +} + +/// 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"); + 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() + .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::*; + 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(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 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())); + 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()); + // 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(); + } + + #[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/Cargo.toml b/crates/weft-pack/Cargo.toml index c580df4..8890949 100644 --- a/crates/weft-pack/Cargo.toml +++ b/crates/weft-pack/Cargo.toml @@ -17,3 +17,6 @@ rand = "0.8" hex = "0.4" tar = "0.4" zstd = "0.13" + +[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 00ca0fc..ce1fcb5 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,13 +20,35 @@ 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] [--claim-data] [--approve]"; + let dir = args.get(2).context(usage)?; + let mut mode = InstallMode::Verified; + let mut claim_data = false; + let mut approve = false; + for arg in &args[3..] { + match arg.as_str() { + "--dev" => mode = InstallMode::Development, + "--claim-data" => claim_data = true, + "--approve" => approve = true, + other => anyhow::bail!("unexpected argument '{other}'; {usage}"), + } + } + install_package(Path::new(dir), mode, claim_data, approve)?; } Some("uninstall") => { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; uninstall_package(app_id)?; } + Some("approve") => { + let app_id = args + .get(2) + .context("usage: weft-pack approve [...]")?; + approve_package(app_id, &args[3..])?; + } + Some("rollback") => { + let app_id = args.get(2).context("usage: weft-pack rollback ")?; + rollback_package(&resolve_install_root()?, app_id)?; + } Some("list") => { list_installed(); } @@ -86,7 +109,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,8 +121,24 @@ 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] [--claim-data]"); + eprintln!( + " install a package signed by a trusted key" + ); + eprintln!( + " --dev: as unsigned development content" + ); + eprintln!( + " --claim-data: adopt app data with no owner" + ); + eprintln!( + " --approve: approve its declared capabilities" + ); eprintln!(" weft-pack uninstall remove installed package"); + eprintln!(" weft-pack rollback activate the previous revision"); + eprintln!( + " weft-pack approve [...] approve its declared or listed capabilities" + ); eprintln!(" weft-pack list list installed packages"); eprintln!( " weft-pack build-image [--out ] create EROFS image with mkfs.erofs" @@ -128,6 +167,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,63 +274,809 @@ 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<()> { - 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") - { - 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 - }; +fn install_package( + path: &Path, + mode: InstallMode, + claim_data: bool, + approve: bool, +) -> anyhow::Result<()> { + let root = resolve_install_root()?; + install_into(path, &root, mode, claim_data, approve) +} +#[cfg(test)] +fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { + install_into(path, store_root, mode, false, 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, + approve: bool, +) -> anyhow::Result<()> { + create_store(store_root)?; + 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, claim_data, approve); + } + let name = path + .file_name() + .and_then(|n| n.to_str()) + .and_then(|n| n.strip_suffix(".app.tar.zst")) + .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")?; + let _in_use = weft_ipc_types::store::try_claim(&unpacked) + .with_context(|| format!("open {}", unpacked.display()))?; + let result = unbundle_package(path, &unpacked) + .and_then(|()| install_dir(&unpacked.join(name), store_root, mode, claim_data, approve)); + let _ = std::fs::remove_dir_all(&unpacked); + 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. Each new +/// 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() + .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 { + 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)] + { + use rustix::fs::{CWD, Mode, OFlags}; + let opened = rustix::fs::openat( + CWD, + dir, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ); + 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!( + 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 { + 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, + claim_data: bool, + approve: 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(|()| { + // Held while the copy is in use, so the collection of another + // operation never takes it for abandoned. + let in_use = weft_ipc_types::store::try_claim(&staging) + .with_context(|| format!("open {}", staging.display()))?; + install_staged(&staging, store_root, mode, claim_data, approve, in_use) + }); + if result.is_err() { + let _ = std::fs::remove_dir_all(&staging); + } + result +} + +/// Installs the staged copy. `in_use` holds the copy against collection by +/// other operations until it is placed; it is released then, since the copy +/// has become the revision sessions launch from. +fn install_staged( + staging: &Path, + store_root: &Path, + mode: InstallMode, + claim_data: bool, + approve: bool, + in_use: Option, +) -> anyhow::Result<()> { + check_package(staging)?; + let manifest = load_manifest(staging)?; let app_id = &manifest.package.id; let dest = store_root.join(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. - if dest.is_dir() && only_holds_app_data(&dest)? { - if std::fs::symlink_metadata(dest.join("data")).is_ok() && is_user_store(store_root) { - move_app_data_out(app_id, &dest)?; + 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)?; + // A package directory from before revisions existed is replaced by the + // new revision only while no session runs from it, since it is not kept, + // and only once the app data an earlier weft-appd kept inside it has + // been moved to its own location: the update stops rather than replace + // data with the package. A directory that held only data, or nothing, + // is no package to replace. + let legacy = match active(store_root, app_id)? { + Some(Active::Directory(dir)) => { + let claim = weft_ipc_types::store::try_claim(&dir) + .with_context(|| format!("open {}", dir.display()))? + .with_context(|| { + format!("{app_id} is running from {}; close it first", dir.display()) + })?; + keep_legacy_data(app_id, store_root, &dir)?; + let emptied = + std::fs::symlink_metadata(&dir).is_err() || std::fs::remove_dir(&dir).is_ok(); + (!emptied).then_some(claim) } - let _ = std::fs::remove_dir(&dest); + _ => None, + }; + let admission = admit(staging, app_id, mode, &data_home, claim_data)?; + let owner = admission.owner; + let revision = place(staging, store_root, app_id, admission)?; + drop(in_use); + drop(legacy); + collect(store_root, app_id); + println!( + "installed {app_id} -> {} (revision {}, {owner})", + dest.display(), + &revision[..12] + ); + settle_approval(&data_home, app_id, manifest.capabilities(), approve) +} + +/// Records the installed package's capabilities as approved with +/// `approve`; otherwise reports those that still need approval, since the +/// app does not launch until they are approved. +fn settle_approval( + data_home: &Path, + app_id: &str, + declared: &[String], + approve: bool, +) -> anyhow::Result<()> { + use weft_ipc_types::approval::{approval_path, read_approved, unapproved, write_approved}; + let record = approval_path(data_home, app_id); + if approve { + write_approved(&record, declared) + .with_context(|| format!("record the approval of {app_id}"))?; + let approved: Vec<_> = declared + .iter() + .filter(|c| weft_ipc_types::approval::needs_approval(c)) + .collect(); + if !approved.is_empty() { + println!( + "approved for {app_id}: {}", + approved + .iter() + .map(|c| c.as_str()) + .collect::>() + .join(", ") + ); + } + return Ok(()); } - if dest.exists() { - anyhow::bail!( - "package '{}' is already installed at {}; remove it first", - app_id, - dest.display() + let approved = read_approved(&record).with_context(|| format!("read {}", record.display()))?; + // An approval covers what the installed package declares: a capability + // it no longer declares is dropped, so declaring it again later asks + // again. + let kept: std::collections::BTreeSet = declared + .iter() + .filter(|c| approved.contains(*c)) + .cloned() + .collect(); + if kept != approved { + write_approved(&record, &kept.into_iter().collect::>()) + .with_context(|| format!("update the approval of {app_id}"))?; + } + let pending = unapproved(declared, &approved); + if !pending.is_empty() { + println!( + "{app_id} needs approval for {} before it launches; run weft-pack approve {app_id}", + pending.join(", ") ); } - copy_dir(dir, &dest) - .with_context(|| format!("copy {} -> {}", dir.display(), dest.display()))?; - println!("installed {} -> {}", app_id, dest.display()); Ok(()) } +/// Approves capabilities for `app_id`: those listed in `explicit`, or +/// without a list, those its installed package declares. The package is the +/// one weft-appd launches: the first store, in weft-appd's order, holding +/// the app. A verified image, whose manifest weft-pack cannot read, needs +/// the explicit list. +fn approve_package(app_id: &str, explicit: &[String]) -> anyhow::Result<()> { + use weft_ipc_types::approval::needs_approval; + anyhow::ensure!( + weft_ipc_types::package::is_valid_app_id(app_id), + "'{app_id}' is not a valid app ID" + ); + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to record the approval")?; + let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; + let roots = list_installed_roots(); + let image = roots + .iter() + .any(|root| ImageFiles::in_store(root, app_id).any_present()); + if !explicit.is_empty() { + for capability in explicit { + capability + .parse::() + .map_err(|e| anyhow::anyhow!("{e}"))?; + anyhow::ensure!(needs_approval(capability), "{capability} needs no approval"); + } + // Approvals by name add to the app's approval. They are for an + // installed app, and for a package weft-pack can read, only for + // what it declares. + if !image { + let manifest = launched_manifest(&roots, app_id)?; + if let Some(undeclared) = explicit + .iter() + .find(|c| !manifest.capabilities().contains(c)) + { + anyhow::bail!("{app_id} does not declare {undeclared}"); + } + } + let record = weft_ipc_types::approval::approval_path(&data_home, app_id); + let mut approved: Vec = weft_ipc_types::approval::read_approved(&record) + .with_context(|| format!("read {}", record.display()))? + .into_iter() + .collect(); + approved.extend(explicit.iter().cloned()); + return settle_approval(&data_home, app_id, &approved, true); + } + if image { + anyhow::bail!( + "{app_id} is installed as a verified image, whose capabilities weft-pack cannot \ + read; approve them by name: weft-pack approve {app_id} ..." + ); + } + let manifest = launched_manifest(&roots, app_id)?; + if !manifest.capabilities().iter().any(|c| needs_approval(c)) { + println!("{app_id} declares nothing that needs approval"); + } + settle_approval(&data_home, app_id, manifest.capabilities(), true) +} + +/// The manifest of the package of `app_id` that weft-appd launches from a +/// directory or revision: the first of `roots` holding one. +fn launched_manifest(roots: &[PathBuf], app_id: &str) -> anyhow::Result { + let found = roots + .iter() + .find_map(|root| match active(root, app_id) { + Ok(Some(found)) + if std::fs::symlink_metadata( + found.dir().join(weft_ipc_types::manifest::MANIFEST_FILE), + ) + .is_ok() => + { + Some(found) + } + _ => None, + }) + .with_context(|| format!("{app_id} is not installed"))?; + let manifest = load_manifest(found.dir())?; + anyhow::ensure!( + manifest.package.id == app_id, + "{} declares another app ID", + found.dir().display() + ); + Ok(manifest) +} + +/// The active package of `app_id` in `store_root`. +fn active( + store_root: &Path, + app_id: &str, +) -> anyhow::Result> { + Ok(weft_ipc_types::store::active(store_root, app_id)?) +} + +use weft_ipc_types::store::Active; + +/// How long a leftover staging, unpacking or removal directory must be +/// unchanged before it counts as abandoned by an interrupted operation. +const ABANDONED_AFTER: std::time::Duration = std::time::Duration::from_secs(3600); + +/// The revision a store keeps for rollback, a link next to the revisions. +const PREVIOUS_LINK: &str = "previous"; + +/// 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 the active revision of `app_id` and returns +/// the revision's name. The package becomes an immutable revision named by +/// its content digest (identical content is installed once), and one rename +/// of the store's link activates it, so an interruption at any point leaves +/// the previously active revision, or none on a first installation, in +/// place. The revision active before is kept for rollback and for the +/// sessions running from it. If anything fails, the staging copy, a new +/// revision and an owner record this installation created are removed, so +/// a failed first installation leaves no claim on the app ID. +fn place( + staging: &Path, + store_root: &Path, + app_id: &str, + admission: Admission, +) -> anyhow::Result { + let mut created_revision = None; + let result = (|| { + let revision = hex::encode( + weft_ipc_types::trust::content_digest(staging) + .with_context(|| format!("read {}", staging.display()))?, + ); + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + create_store(&revisions)?; + let dir = revisions.join(&revision); + sync_tree(staging)?; + // A revision of the same name is reused only when it still holds this + // content and the same signature file; anything else under the name, + // such as the remains of an interrupted removal, is replaced. + if std::fs::symlink_metadata(&dir).is_ok() { + if same_revision(&dir, staging, &revision) { + let _ = std::fs::remove_dir_all(staging); + return Ok(revision); + } + // The revision may be the active one, as when the same content is + // installed again with another signature file, so it is replaced + // in one exchange: the name always holds a complete revision. + let claim = weft_ipc_types::store::try_claim(&dir) + .with_context(|| format!("open {}", dir.display()))? + .with_context(|| { + format!( + "{} differs from this package but a running session uses it; close \ + the app and install again", + dir.display() + ) + })?; + exchange(staging, &dir) + .with_context(|| format!("replace {} with {}", dir.display(), staging.display()))?; + // The earlier copy now has the staging name. + remove_claimed(store_root, staging); + drop(claim); + } else { + weft_ipc_types::package::rename_no_replace(staging, &dir) + .with_context(|| format!("move {} -> {}", staging.display(), dir.display()))?; + created_revision = Some(dir.clone()); + } + sync_dir(&revisions)?; + sync_dir(&store_root.join(weft_ipc_types::store::REVISIONS_DIR))?; + Ok(revision) + })() + .and_then(|revision| { + activate(store_root, app_id, &revision)?; + Ok(revision) + }); + if result.is_err() { + let _ = std::fs::remove_dir_all(staging); + if let Some(dir) = &created_revision { + let _ = std::fs::remove_dir_all(dir); + } + if let Some(record) = &admission.created_record { + let _ = std::fs::remove_file(record); + } + } + result +} + +/// Makes `revision` the active revision of `app_id`. The revision active +/// before becomes the one kept for rollback. Its link is written first, so +/// an interruption between the two steps keeps both revisions. A package +/// directory from before revisions existed is exchanged with the new link in +/// one step and then removed; the caller holds it so no session runs from it. +/// An error means the switch did not happen: once the link is in place, the +/// activation stands, and what remains to tidy is left to `collect`. +fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<()> { + use weft_ipc_types::store::{link_target, revisions_of}; + let revisions = revisions_of(store_root, app_id); + let current = active(store_root, app_id)?; + let dest = store_root.join(app_id); + let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; + let kept_before = std::fs::read_link(revisions.join(PREVIOUS_LINK)).ok(); + let kept = match ¤t { + Some(Active::Revision { name, .. }) if name != revision => { + replace_link(&revisions, PREVIOUS_LINK, Path::new(name)) + } + Some(Active::Revision { .. }) => Ok(()), + // Nothing earlier of this installation to roll back to; a link left + // by an earlier, interrupted uninstall is dropped. + _ => match std::fs::remove_file(revisions.join(PREVIOUS_LINK)) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => { + Err(e).context("remove the stale rollback link") + } + _ => sync_dir(&revisions), + }, + }; + let switched = kept.and_then(|()| { + match ¤t { + Some(Active::Directory(_)) => exchange(&link, &dest), + _ => std::fs::rename(&link, &dest), + } + .with_context(|| format!("activate {}", dest.display())) + }); + if let Err(e) = switched { + let _ = std::fs::remove_file(&link); + // The revision kept for rollback stays what it was. + if let Some(Active::Revision { name, .. }) = ¤t + && name != revision + { + let _ = match &kept_before { + Some(target) => replace_link(&revisions, PREVIOUS_LINK, target), + None => { + std::fs::remove_file(revisions.join(PREVIOUS_LINK)).map_err(anyhow::Error::from) + } + }; + } + return Err(e); + } + if let Err(e) = sync_dir(store_root) { + eprintln!("{e:#}"); + } + if let Some(Active::Directory(_)) = current { + // The earlier package directory now has the temporary name, which + // `collect` also recognises. + remove_claimed(store_root, &link); + } + Ok(()) +} + +/// Whether the revision directory `dir` holds the content named `revision` +/// and the same signature file as `staging`. +fn same_revision(dir: &Path, staging: &Path, revision: &str) -> bool { + let signature = + |root: &Path| match std::fs::read(root.join(weft_ipc_types::trust::SIGNATURE_FILE)) { + Ok(bytes) => Some(Some(bytes)), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Some(None), + Err(_) => None, + }; + weft_ipc_types::trust::content_digest(dir).is_ok_and(|d| hex::encode(d) == revision) + && signature(dir).is_some() + && signature(dir) == signature(staging) +} + +/// Creates a symbolic link to `target` under a fresh temporary name in +/// `store_root`, named so the collection of `app_id` can recognise it. +fn temporary_link(store_root: &Path, app_id: &str, target: &Path) -> anyhow::Result { + let link = store_root.join(format!( + ".link-{app_id}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + #[cfg(unix)] + std::os::unix::fs::symlink(target, &link) + .with_context(|| format!("create {}", link.display()))?; + #[cfg(not(unix))] + anyhow::bail!("installing packages requires a Unix host"); + Ok(link) +} + +/// Points the link `name` in `dir` at `target`, replacing it in one rename. +fn replace_link(dir: &Path, name: &str, target: &Path) -> anyhow::Result<()> { + let temporary = dir.join(format!( + ".{name}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + #[cfg(unix)] + std::os::unix::fs::symlink(target, &temporary) + .with_context(|| format!("create {}", temporary.display()))?; + if let Err(e) = std::fs::rename(&temporary, dir.join(name)) { + let _ = std::fs::remove_file(&temporary); + return Err(e).with_context(|| format!("update {}", dir.join(name).display())); + } + sync_dir(dir) +} + +#[cfg(target_os = "linux")] +fn exchange(a: &Path, b: &Path) -> std::io::Result<()> { + use rustix::fs::{CWD, RenameFlags, renameat_with}; + renameat_with(CWD, a, CWD, b, RenameFlags::EXCHANGE).map_err(std::io::Error::from) +} + +#[cfg(not(target_os = "linux"))] +fn exchange(_a: &Path, _b: &Path) -> std::io::Result<()> { + Err(std::io::Error::other( + "replacing a package directory requires Linux", + )) +} + +/// Flushes a directory's entries to disk. +fn sync_dir(dir: &Path) -> anyhow::Result<()> { + std::fs::File::open(dir) + .and_then(|d| d.sync_all()) + .with_context(|| format!("sync {}", dir.display())) +} + +/// Flushes every file and directory of a staged package to disk, so a +/// revision that is activated is complete even after a crash. +fn sync_tree(dir: &Path) -> anyhow::Result<()> { + for entry in std::fs::read_dir(dir).with_context(|| format!("read {}", dir.display()))? { + let entry = entry?; + let path = entry.path(); + if entry.file_type()?.is_dir() { + sync_tree(&path)?; + } else { + std::fs::File::open(&path) + .and_then(|f| f.sync_all()) + .with_context(|| format!("sync {}", path.display()))?; + } + } + sync_dir(dir) +} + +/// The revision of `app_id` kept for rollback, if any. +fn previous_revision(store_root: &Path, app_id: &str) -> Option { + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + std::fs::read_link(revisions.join(PREVIOUS_LINK)) + .ok()? + .to_str() + .filter(|name| weft_ipc_types::store::is_revision_name(name)) + .filter(|name| std::fs::symlink_metadata(revisions.join(name)).is_ok_and(|m| m.is_dir())) + .map(str::to_owned) +} + +/// Removes what package operations on `app_id` no longer need, keeping the +/// active revision, the one kept for rollback and every revision a session +/// is running from: other revisions, temporary links, and staging, +/// unpacking and removal directories that interrupted operations left +/// behind. The caller holds the owner lock of `app_id`. Failures are +/// reported and leave the entry for the next operation. +fn collect(store_root: &Path, app_id: &str) { + use weft_ipc_types::store::{is_revision_name, revisions_of, try_claim}; + let revisions = revisions_of(store_root, app_id); + let mut keep = Vec::new(); + if let Ok(Some(Active::Revision { name, .. })) = active(store_root, app_id) { + keep.push(name); + } + keep.extend(previous_revision(store_root, app_id)); + if let Ok(entries) = std::fs::read_dir(&revisions) { + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str() else { continue }; + let path = entry.path(); + if name == PREVIOUS_LINK { + // A link to a revision that is gone keeps nothing. + if previous_revision(store_root, app_id).is_none() { + let _ = std::fs::remove_file(&path); + } + continue; + } + if name.starts_with('.') { + // A link replacement that was interrupted. + if std::fs::symlink_metadata(&path).is_ok_and(|m| m.file_type().is_symlink()) { + let _ = std::fs::remove_file(&path); + } + continue; + } + if !is_revision_name(name) || keep.iter().any(|k| k == name) { + continue; + } + match try_claim(&path) { + Ok(Some(claim)) => { + remove_claimed(store_root, &path); + drop(claim); + } + Ok(None) => println!("kept revision {} for a running session", &name[..12]), + Err(e) => eprintln!("cannot inspect {}: {e}", path.display()), + } + } + } + let _ = std::fs::remove_dir(&revisions); + let link_prefix = format!(".link-{app_id}-"); + if let Ok(entries) = std::fs::read_dir(store_root) { + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str() else { continue }; + let path = entry.path(); + if name.starts_with(&link_prefix) { + // A temporary link of an interrupted activation. + if std::fs::symlink_metadata(&path).is_ok_and(|m| m.file_type().is_symlink()) { + let _ = std::fs::remove_file(&path); + } else if let Ok(Some(claim)) = try_claim(&path) { + // A replaced package directory whose removal was cut short. + remove_claimed(store_root, &path); + drop(claim); + } + } else if [".staging-", ".unpack-", ".trash-"] + .iter() + .any(|p| name.starts_with(p)) + && abandoned(&path) + && let Ok(Some(claim)) = try_claim(&path) + { + // An operation in progress holds its directory. + let _ = std::fs::remove_dir_all(&path); + drop(claim); + } + } + } +} + +/// Whether a temporary directory has been left unchanged long enough to be +/// abandoned. +fn abandoned(path: &Path) -> bool { + std::fs::symlink_metadata(path) + .and_then(|m| m.modified()) + .ok() + .and_then(|t| t.elapsed().ok()) + .is_some_and(|age| age >= ABANDONED_AFTER) +} + +/// Removes a claimed package directory: it is first renamed out of the way, +/// so no launch can find it half removed. +fn remove_claimed(store_root: &Path, dir: &Path) { + let trash = store_root.join(format!(".trash-{}", hex::encode(rand::random::<[u8; 8]>()))); + match std::fs::rename(dir, &trash) { + Ok(()) => { + if let Err(e) = std::fs::remove_dir_all(&trash) { + eprintln!("cannot remove {}: {e}", trash.display()); + } + } + Err(e) => eprintln!("cannot remove {}: {e}", dir.display()), + } +} + +/// Makes the revision kept for rollback the active revision of `app_id` +/// again; the revision it replaces is kept in turn. The kept revision must +/// still satisfy the app's owner record: a revision by a verified publisher +/// is checked against the trust store again, so a revoked key cannot be +/// rolled back to. +fn rollback_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { + anyhow::ensure!( + weft_ipc_types::package::is_valid_app_id(app_id), + "'{app_id}' is not a valid app ID" + ); + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to check who owns the app ID")?; + let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; + let Some(Active::Revision { name: current, .. }) = active(store_root, app_id)? else { + anyhow::bail!("{app_id} has no installed revision to roll back from"); + }; + let previous = previous_revision(store_root, app_id) + .filter(|previous| *previous != current) + .with_context(|| format!("{app_id} has no earlier revision to roll back to"))?; + let dir = weft_ipc_types::store::revisions_of(store_root, app_id).join(&previous); + let record = weft_ipc_types::trust::owner_record_path(&data_home, app_id); + match weft_ipc_types::trust::read_owner(&record)? { + Some(Owner::Development) => {} + Some(Owner::Verified(key)) => { + let signer = TrustStore::load(&TrustStore::directories())?.signer(&dir)?; + anyhow::ensure!( + signer.as_ref() == Some(&key), + "the earlier revision of {app_id} is no longer signed by a trusted key of its owner" + ); + } + None => anyhow::bail!("{app_id} has no recorded owner"), + } + activate(store_root, app_id, &previous)?; + collect(store_root, app_id); + println!("rolled {app_id} back to revision {}", &previous[..12]); + let manifest = load_manifest(&dir)?; + settle_approval(&data_home, app_id, manifest.capabilities(), false) +} + +/// 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 +/// placed, so concurrent installations by different owners cannot both win. +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}; + 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 record = owner_record_path(data_home, app_id); + 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 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. + 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 { + 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) + } + }; + Ok(Admission { + owner, + created_record, + }) +} + fn uninstall_package(app_id: &str) -> anyhow::Result<()> { let root = resolve_install_root()?; uninstall_package_from(app_id, &root) @@ -295,48 +1086,145 @@ 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() { - anyhow::bail!( - "package '{}' is not installed at {}", - app_id, - target.display() - ); - } - // A `data` directory is never removed with the package: packages cannot - // ship one, so it holds app data from an earlier version. It is moved to - // the app data directory when this is the user store and the package - // declares app data (or its manifest cannot be read); otherwise uninstall - // stops and leaves everything in place. - if std::fs::symlink_metadata(target.join("data")).is_ok() { - let may_hold_app_data = load_manifest(&target).map_or(true, |m| declares_app_data(&m)); - if !(may_hold_app_data && is_user_store(store_root)) { + match active(store_root, app_id)? { + None => { + // Revisions left by sessions that ran past an earlier uninstall + // are removed now that nothing uses them; collecting needs the + // owner lock. + if data_home.is_some() { + collect(store_root, app_id); + } + // 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 + && !installed_in_another_store(app_id, store_root) + && release_owner_without_data(home, app_id) + { + println!("released {app_id}, which was not installed"); + return Ok(()); + } anyhow::bail!( - "{} contains a 'data' directory that may hold app data; move it aside before \ - uninstalling", + "package '{}' is not installed at {}", + app_id, target.display() ); } - move_app_data_out(app_id, &target)?; - } - // Moving the data removes a package directory it leaves empty. - match std::fs::remove_dir_all(&target) { - Err(e) if e.kind() != std::io::ErrorKind::NotFound => { - return Err(e).with_context(|| format!("remove {}", target.display())); + // New launches no longer find the app; sessions running from one of + // its revisions keep it until they end and a later operation on the + // app removes it. + Some(Active::Revision { .. }) => { + std::fs::remove_file(&target) + .with_context(|| format!("remove {}", target.display()))?; + sync_dir(store_root)?; + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + let _ = std::fs::remove_file(revisions.join(PREVIOUS_LINK)); + let _ = sync_dir(&revisions); + if data_home.is_some() { + collect(store_root, app_id); + } } - _ => {} + Some(Active::Directory(_)) => uninstall_directory(app_id, store_root, &target)?, + } + // 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 + && !installed_in_another_store(app_id, store_root) + { + release_owner_without_data(home, app_id); + // A later installation asks for approval again. + weft_ipc_types::approval::remove_approval(&weft_ipc_types::approval::approval_path( + home, app_id, + )) + .with_context(|| format!("remove the approval of {app_id}"))?; } println!("uninstalled {}", app_id); 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) - .with_context(|| format!("read {}", package_dir.display()))? - .map(|entry| entry.map(|e| e.file_name())) - .collect::, _>>()?; - Ok(entries.is_empty() || entries == ["data"]) +/// Moves the app data an earlier weft-appd kept in `/data` to the +/// app's data directory, before the package directory `dir` is removed or +/// replaced. A `data` directory is never removed with a package: packages +/// cannot ship one, so it holds app data from an earlier version. It is +/// moved when this is the user store and the package declares app data (or +/// its manifest cannot be read); otherwise this stops and leaves everything +/// in place. +fn keep_legacy_data(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { + if std::fs::symlink_metadata(target.join("data")).is_ok() { + let may_hold_app_data = load_manifest(target).map_or(true, |m| declares_app_data(&m)); + if !(may_hold_app_data && is_user_store(store_root)) { + anyhow::bail!( + "{} contains a 'data' directory that may hold app data; move it aside before \ + uninstalling or updating", + target.display() + ); + } + move_app_data_out(app_id, target)?; + } + Ok(()) +} + +/// Removes a package directory installed before revisions existed, which +/// only happens while no session runs from it. +fn uninstall_directory(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { + let _claim = weft_ipc_types::store::try_claim(target) + .with_context(|| format!("open {}", target.display()))? + .with_context(|| { + format!( + "{app_id} is running from {}; close it first", + target.display() + ) + })?; + keep_legacy_data(app_id, store_root, target)?; + // Moving the data removes a package directory it leaves empty. + match std::fs::remove_dir_all(target) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => { + return Err(e).with_context(|| format!("remove {}", target.display())); + } + _ => {} + } + 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.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| { + matches!(active(root, app_id), Ok(Some(found)) + if Manifest::read(found.dir()).is_ok_and(|m| m.package.id == app_id)) + }) +} + +/// 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 { + // 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() } /// Whether the manifest declares an app-data capability; only such apps @@ -413,11 +1301,39 @@ fn list_installed() { }; let mut pkgs: Vec<(String, String, String)> = Vec::new(); for entry in entries.flatten() { - let Ok(m) = Manifest::read(&entry.path()) else { + // Only an app's active package, named after the ID its manifest + // declares, is installed; staging copies, revisions and stray + // directories or links are not. + let Some(name) = entry.file_name().to_str().map(str::to_owned) else { continue; }; + if !weft_ipc_types::package::is_valid_app_id(&name) { + continue; + } + let Ok(Some(found)) = active(&root, &name) else { + continue; + }; + let Ok(m) = Manifest::read(found.dir()) else { + continue; + }; + if m.package.id != name { + 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)); @@ -431,17 +1347,111 @@ 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. The walk holds each directory open and opens its entries +/// relative to it without following links, checking that each opened entry +/// 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. +#[cfg(unix)] fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { - std::fs::create_dir_all(dst)?; - 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() { - copy_dir(&src_path, &dst_path)?; + 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, 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(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()))?; + 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) + .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 { - std::fs::copy(&src_path, &dst_path) + let executable = found.st_mode & 0o111 != 0; + let mut target = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .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(()) @@ -540,47 +1550,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 +1583,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)] @@ -672,6 +1627,22 @@ mod tests { .unwrap(); } + /// Runs `f` with WEFT_APP_STORE set to `store`. Callers run it inside + /// `with_home`, which holds env_lock. + fn with_store(store: &Path, f: impl FnOnce() -> T) -> T { + let prior = std::env::var_os("WEFT_APP_STORE"); + // SAFETY: the caller holds env_lock through with_home. + unsafe { std::env::set_var("WEFT_APP_STORE", store) }; + let result = f(); + unsafe { + match prior { + Some(v) => std::env::set_var("WEFT_APP_STORE", v), + None => std::env::remove_var("WEFT_APP_STORE"), + } + } + result + } + /// Runs `f` with HOME set to `home` and XDG_DATA_HOME to `home/share`. fn with_home(home: &Path, f: impl FnOnce() -> T) -> T { let _env = env_lock(); @@ -704,13 +1675,1036 @@ 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-")) + }) + } + + /// Writes a package whose UI holds `marker`, so each marker is a + /// different revision. + fn write_revision(src: &Path, app_id: &str, marker: &str) { + let _ = std::fs::remove_dir_all(src); + write_package(src, app_id, ""); + std::fs::write(src.join("ui/index.html"), marker).unwrap(); + } + + fn active_ui(store: &Path, app_id: &str) -> String { + std::fs::read_to_string(store.join(app_id).join("ui/index.html")).unwrap() + } + + fn revision_count(store: &Path, app_id: &str) -> usize { + std::fs::read_dir(weft_ipc_types::store::revisions_of(store, app_id)).map_or(0, |d| { + d.flatten() + .filter(|e| { + weft_ipc_types::store::is_revision_name(&e.file_name().to_string_lossy()) + }) + .count() + }) + } + + fn active_dir(store: &Path, app_id: &str) -> PathBuf { + active(store, app_id).unwrap().unwrap().dir().to_path_buf() + } + + #[test] + fn an_update_keeps_the_previous_revision_and_collects_older_ones() { + let home = temp_root("update"); + let store = home.join("store"); + let app_id = "org.weft.test.update"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + write_revision(&src, app_id, "two"); + install().unwrap(); + assert_eq!(active_ui(&store, app_id), "two"); + assert!(one.is_dir(), "the previous revision is kept for rollback"); + write_revision(&src, app_id, "three"); + install().unwrap(); + assert_eq!(active_ui(&store, app_id), "three"); + assert!( + !one.exists(), + "a revision neither active nor previous is removed" + ); + assert_eq!(revision_count(&store, app_id), 2); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_revision_a_session_runs_from_is_kept_until_it_ends() { + let home = temp_root("pinned"); + let store = home.join("store"); + let app_id = "org.weft.test.pinned"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + let session = weft_ipc_types::store::pin(&one).unwrap(); + for marker in ["two", "three", "four"] { + write_revision(&src, app_id, marker); + install().unwrap(); + } + assert_eq!( + std::fs::read_to_string(one.join("ui/index.html")).unwrap(), + "one", + "the running session keeps its bytes" + ); + // Uninstalling stops new launches but not the session. + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(active(&store, app_id).unwrap().is_none()); + assert!(one.is_dir()); + drop(session); + // Once it has ended, the next operation on the app removes it. + let _ = with_home(&home, || uninstall_package_from(app_id, &store)); + assert!(!one.exists()); + assert_eq!(revision_count(&store, app_id), 0); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_failed_update_keeps_the_active_revision() { + let home = temp_root("failed_update"); + let store = home.join("store"); + let app_id = "org.weft.test.failed"; + let src = home.join("src"); + write_revision(&src, app_id, "good"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + // A broken component, then the same ID under another owner. + write_revision(&src, app_id, "broken"); + std::fs::write(src.join("app.wasm"), b"not wasm").unwrap(); + assert!( + with_home(&home, || install_package_to( + &src, + &store, + InstallMode::Development + )) + .is_err() + ); + // Signed by a trusted publisher, which does not own the ID. + write_revision(&src, app_id, "other owner"); + sign_package(&src, &key(&home, "other", 7, true)).unwrap(); + let refused = with_trust(&home, || { + install_package_to(&src, &store, InstallMode::Verified) + }); + assert!( + refused + .as_ref() + .is_err_and(|e| e.to_string().contains("belongs to")), + "{refused:?}" + ); + assert_eq!(active_ui(&store, app_id), "good"); + assert_eq!(revision_count(&store, app_id), 1); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn rollback_switches_to_the_kept_revision_and_back() { + let home = temp_root("rollback"); + let store = home.join("store"); + let app_id = "org.weft.test.rollback"; + let src = home.join("src"); + let rollback = || with_home(&home, || rollback_package(&store, app_id)); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!( + rollback().is_err(), + "a first installation has nothing to roll back to" + ); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + rollback().unwrap(); + assert_eq!(active_ui(&store, app_id), "one"); + rollback().unwrap(); + assert_eq!(active_ui(&store, app_id), "two"); + assert_eq!(revision_count(&store, app_id), 2); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_package_directory_from_before_revisions_is_replaced_unless_in_use() { + let home = temp_root("legacy_update"); + let store = home.join("store"); + let app_id = "org.weft.test.legacy"; + write_revision(&store.join(app_id), app_id, "old"); + let src = home.join("src"); + write_revision(&src, app_id, "new"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + let session = weft_ipc_types::store::pin(&store.join(app_id)).unwrap(); + assert!( + install().is_err(), + "a running package directory is not replaced" + ); + assert!(matches!( + active(&store, app_id).unwrap(), + Some(Active::Directory(_)) + )); + assert_eq!(active_ui(&store, app_id), "old"); + drop(session); + install().unwrap(); + assert!(matches!( + active(&store, app_id).unwrap(), + Some(Active::Revision { .. }) + )); + assert_eq!(active_ui(&store, app_id), "new"); + let leftovers: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .filter(|n| n != ".revisions" && n.to_str() != Some(app_id)) + .collect(); + assert!(leftovers.is_empty(), "{leftovers:?}"); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn an_update_keeps_app_data_from_a_package_directory() { + let home = temp_root("legacy_data"); + let app_id = "org.weft.test.legacydata"; + let src = home.join("src"); + write_revision(&src, app_id, "new"); + // In the user store, the data an earlier weft-appd kept in the + // package directory moves to the app data directory. + let store = home.join(".local/share/weft/apps"); + write_package(&store.join(app_id), app_id, APP_DATA); + std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); + std::fs::write(store.join(app_id).join("data/notes.txt"), "kept").unwrap(); + weft_ipc_types::trust::write_owner( + &weft_ipc_types::trust::owner_record_path(&home.join("share"), app_id), + Owner::Development, + ) + .unwrap(); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + assert_eq!( + std::fs::read_to_string(kept.join("notes.txt")).unwrap(), + "kept" + ); + assert_eq!(active_ui(&store, app_id), "new"); + + // Elsewhere nothing claims to know what it is: the update stops. + let other = home.join("other-store"); + write_package(&other.join(app_id), app_id, APP_DATA); + std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); + std::fs::write(other.join(app_id).join("data/notes.txt"), "here").unwrap(); + assert!( + with_home(&home, || install_package_to( + &src, + &other, + InstallMode::Development + )) + .is_err() + ); + assert_eq!( + std::fs::read_to_string(other.join(app_id).join("data/notes.txt")).unwrap(), + "here" + ); + assert!(matches!( + active(&other, app_id).unwrap(), + Some(Active::Directory(_)) + )); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn rollback_refuses_a_revision_whose_key_was_withdrawn() { + let home = temp_root("rollback_revoked"); + let store = home.join("store"); + let app_id = "org.weft.test.revoked"; + let src = home.join("src"); + let signing = key(&home, "publisher", 9, true); + for marker in ["one", "two"] { + write_revision(&src, app_id, marker); + sign_package(&src, &signing).unwrap(); + with_trust(&home, || { + install_package_to(&src, &store, InstallMode::Verified) + }) + .unwrap(); + } + std::fs::remove_file(home.join("keys/publisher.pub")).unwrap(); + let refused = with_trust(&home, || rollback_package(&store, app_id)); + assert!( + refused + .as_ref() + .is_err_and(|e| e.to_string().contains("no longer signed")), + "{refused:?}" + ); + assert_eq!(active_ui(&store, app_id), "two"); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_damaged_revision_is_not_reused() { + let home = temp_root("damaged"); + let store = home.join("store"); + let app_id = "org.weft.test.damaged"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + write_revision(&src, app_id, "two"); + install().unwrap(); + // The kept revision is damaged, as by an interrupted removal. + std::fs::remove_file(one.join("app.wasm")).unwrap(); + write_revision(&src, app_id, "one"); + install().unwrap(); + assert_eq!(active_dir(&store, app_id), one); + assert!( + one.join("app.wasm").is_file(), + "the damaged revision was reused" + ); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn the_same_content_with_another_signature_replaces_the_active_revision_in_place() { + let home = temp_root("resigned"); + let store = home.join("store"); + let app_id = "org.weft.test.resigned"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let revision = active_dir(&store, app_id); + sign_package(&src, &key(&home, "dev", 3, false)).unwrap(); + // A session runs from the unsigned copy: it is not replaced under it. + let session = weft_ipc_types::store::pin(&revision).unwrap(); + assert!(install().is_err()); + assert!( + !revision + .join(weft_ipc_types::trust::SIGNATURE_FILE) + .exists() + ); + drop(session); + install().unwrap(); + assert_eq!( + active_dir(&store, app_id), + revision, + "same content, same revision" + ); + assert!( + revision + .join(weft_ipc_types::trust::SIGNATURE_FILE) + .is_file() + ); + assert_eq!(active_ui(&store, app_id), "one"); + assert!(!staging_left(&store)); + let leftovers: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .filter(|n| n.to_string_lossy().starts_with(".trash-")) + .collect(); + assert!(leftovers.is_empty(), "{leftovers:?}"); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] + #[test] + fn a_new_installation_does_not_inherit_a_rollback_link() { + let home = temp_root("stale_previous"); + let store = home.join("store"); + let app_id = "org.weft.test.stale"; + let src = home.join("src"); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let one = active(&store, app_id).unwrap().unwrap(); + let Active::Revision { name, .. } = one else { + panic!() + }; + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + // An uninstall interrupted before it removed the rollback link, with + // the revision still held by a session. + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); + std::fs::create_dir_all(revisions.join(&name)).unwrap(); + std::os::unix::fs::symlink(&name, revisions.join(PREVIOUS_LINK)).unwrap(); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!(with_home(&home, || rollback_package(&store, app_id)).is_err()); + assert_eq!(active_ui(&store, app_id), "two"); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn capabilities_are_approved_only_when_asked_and_again_for_new_ones() { + use weft_ipc_types::approval::{approval_path, read_approved, unapproved}; + let home = temp_root("approval"); + let store = home.join("store"); + let app_id = "org.weft.test.approval"; + let src = home.join("src"); + let record = approval_path(&home.join("share"), app_id); + let pending = |declared: &[&str]| { + let declared: Vec = declared.iter().map(|c| (*c).to_owned()).collect(); + unapproved(&declared, &read_approved(&record).unwrap()) + .into_iter() + .map(str::to_owned) + .collect::>() + }; + write_package(&src, app_id, "\"sys:notifications\", \"fs:rw:app-data\""); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert_eq!(pending(&["sys:notifications"]), ["sys:notifications"]); + with_home(&home, || { + with_store(&store, || approve_package(app_id, &[])) + }) + .unwrap(); + assert!(pending(&["sys:notifications", "fs:rw:app-data"]).is_empty()); + // An update that asks for more is not approved by the earlier answer. + write_package( + &src, + app_id, + "\"sys:notifications\", \"sys:clipboard:read\"", + ); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert_eq!( + pending(&["sys:notifications", "sys:clipboard:read"]), + ["sys:clipboard:read"] + ); + // --approve approves what the installed package declares. + with_home(&home, || { + install_into(&src, &store, InstallMode::Development, false, true) + }) + .unwrap(); + assert!(pending(&["sys:notifications", "sys:clipboard:read"]).is_empty()); + // A capability the package stops declaring is no longer approved. + write_package(&src, app_id, "\"sys:notifications\""); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + write_package( + &src, + app_id, + "\"sys:notifications\", \"sys:clipboard:read\"", + ); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert_eq!( + pending(&["sys:notifications", "sys:clipboard:read"]), + ["sys:clipboard:read"] + ); + // Capabilities can be approved by name, and only valid ones that + // need approval. + with_home(&home, || { + with_store(&store, || { + approve_package(app_id, &["sys:clipboard:read".to_owned()]) + }) + }) + .unwrap(); + // Added to what was approved before. + assert!(pending(&["sys:notifications", "sys:clipboard:read"]).is_empty()); + let undeclared = with_home(&home, || { + with_store(&store, || { + approve_package(app_id, &["sys:clipboard:write".to_owned()]) + }) + }); + assert!( + undeclared.is_err(), + "approved a capability the package does not declare" + ); + for refused in ["fs:rw:app-data", "sys:everything"] { + assert!( + with_home(&home, || with_store(&store, || { + approve_package(app_id, &[refused.to_owned()]) + })) + .is_err() + ); + } + // Uninstalling forgets the approval. + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(!record.exists()); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn leftovers_of_interrupted_operations_are_removed() { + let home = temp_root("leftovers"); + let store = home.join("store"); + let app_id = "org.weft.test.leftovers"; + let src = home.join("src"); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); + // An activation and a link update cut short, an old staging copy and + // a recent one that may belong to an installation in progress. + #[cfg(unix)] + { + std::os::unix::fs::symlink("x", store.join(format!(".link-{app_id}-0"))).unwrap(); + std::os::unix::fs::symlink("x", revisions.join(".previous-0")).unwrap(); + } + let old = store.join(".staging-old"); + std::fs::create_dir(&old).unwrap(); + std::fs::File::open(&old) + .unwrap() + .set_modified(std::time::SystemTime::now() - 2 * ABANDONED_AFTER) + .unwrap(); + let recent = store.join(".staging-recent"); + std::fs::create_dir(&recent).unwrap(); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!(std::fs::symlink_metadata(store.join(format!(".link-{app_id}-0"))).is_err()); + assert!(std::fs::symlink_metadata(revisions.join(".previous-0")).is_err()); + assert!(!old.exists()); + assert!(recent.exists()); + let _ = std::fs::remove_dir_all(&home); + } + + #[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 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 store = home.join("store"); + let app_id = "org.weft.test.placed"; + let staging = store.join(".staging-x"); + write_package(&staging, app_id, ""); + // Something that is not a package where the app's link would go. + std::fs::write(store.join(app_id), "not a package").unwrap(); + let record = home.join("owners").join(app_id); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + + let admission = Admission { + owner: Owner::Development, + created_record: Some(record.clone()), + }; + assert!(place(&staging, &store, app_id, admission).is_err()); + assert!(!staging.exists()); + assert!(!record.exists()); + assert_eq!( + std::fs::read_to_string(store.join(app_id)).unwrap(), + "not a package", + "what was there is untouched" + ); + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); + assert!( + std::fs::read_dir(&revisions).map_or(true, |mut d| d.next().is_none()), + "the new revision is removed" + ); + + // A record that existed before this installation is kept. + write_package(&staging, app_id, ""); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + let admission = Admission { + owner: Owner::Development, + created_record: None, + }; + assert!(place(&staging, &store, app_id, 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_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()); + + // 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); + } + + #[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(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, 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); + 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(); + assert_eq!(mode(&absolute), 0o750); + 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, + false, + ) + }) + .unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + 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"); + 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", "held"] { + std::fs::create_dir_all(home.join(dir)).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( + CWD, + path, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW, + Mode::empty(), + ) + .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(); + copy_tree(&dir, &home.join("pkg/dir"), &home.join("copied")).unwrap(); + assert_eq!( + 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. + 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( + CWD, + home.join("pkg/fifo"), + rustix::fs::FileType::Fifo, + Mode::from_raw_mode(0o600), + 0, + ) + .unwrap(); + 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); + } + #[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(); + // A package directory from before revisions existed, with the data an + // earlier weft-appd kept inside it. + write_package(&store.join(app_id), app_id, APP_DATA); 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 +2724,7 @@ mod tests { } // A second copy cannot be merged: uninstall refuses and removes nothing. - install_package_to(&home.join("src"), &store).unwrap(); + write_package(&store.join(app_id), app_id, APP_DATA); 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()); @@ -742,9 +2736,8 @@ mod tests { let home = temp_root("refuse"); let app_id = "com.example.refuse"; // 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(); + write_package(&store.join(app_id), app_id, ""); 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()); @@ -755,13 +2748,12 @@ mod tests { std::fs::remove_dir_all(store.join(app_id)).unwrap(); // 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(); + write_package(&other.join(app_id), app_id, APP_DATA); 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 +2800,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 +2826,24 @@ 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(); + // 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, + false, + ) + }) + .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 +2852,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 +2988,72 @@ 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()); + // The store links the app to an immutable revision; installing + // the same content again keeps that revision. + let revision = std::fs::read_link(store.join(&app_id)).unwrap(); + assert!(install_package_to(&src, &store, InstallMode::Development).is_ok()); + assert_eq!(std::fs::read_link(store.join(&app_id)).unwrap(), revision); + 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 +3119,45 @@ 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 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); - 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 +3220,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 +3249,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/crates/weft-runtime/src/desktop.rs b/crates/weft-runtime/src/desktop.rs new file mode 100644 index 0000000..afd5709 --- /dev/null +++ b/crates/weft-runtime/src/desktop.rs @@ -0,0 +1,269 @@ +//! The desktop services behind the clipboard and notification imports. +//! +//! Each import runs a helper program (`wl-paste`, `wl-copy`, +//! `notify-send`) with a bounded input, a bounded output and a deadline, +//! after which the helper is killed and the call fails. A notification +//! names the application that sent it, so an app cannot pass one off as +//! the system's. + +use std::io::{Read, Write}; +use std::process::{Command, Stdio}; +use std::time::{Duration, Instant}; + +/// How long a helper may run. +const HELPER_TIMEOUT: Duration = Duration::from_secs(5); +/// The most clipboard text read or written, in bytes. +pub const MAX_CLIPBOARD: usize = 1024 * 1024; +/// The longest notification title and body, in bytes. +pub const MAX_TITLE: usize = 256; +pub const MAX_BODY: usize = 4096; + +/// What a helper's output streams are connected to. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Output { + /// Read: up to the limit from standard output, and standard error for + /// the failure message. + Captured, + /// Standard output discarded and standard error read only when the + /// helper fails, for a helper that leaves a process running which + /// keeps its streams open after succeeding, as `wl-copy` does to + /// serve the selection; it fails, if at all, before that. + ErrorsOnly, +} + +/// Runs `command` with `input` on its standard input and returns at most +/// `max_output` bytes of its standard output, or why it failed. +fn run( + mut command: Command, + input: Option>, + max_output: usize, + output: Output, +) -> Result, String> { + let program = command.get_program().to_string_lossy().into_owned(); + let mut child = command + .stdin(if input.is_some() { + Stdio::piped() + } else { + Stdio::null() + }) + .stdout(match output { + Output::Captured => Stdio::piped(), + Output::ErrorsOnly => Stdio::null(), + }) + .stderr(Stdio::piped()) + .spawn() + .map_err(|e| format!("cannot run {program}: {e}"))?; + if let (Some(mut stdin), Some(input)) = (child.stdin.take(), input) { + std::thread::spawn(move || { + let _ = stdin.write_all(&input); + }); + } + let reader = |stream: Option>, limit: usize| { + std::thread::spawn(move || { + let mut data = Vec::new(); + if let Some(stream) = stream { + let _ = stream.take(limit as u64 + 1).read_to_end(&mut data); + } + data + }) + }; + let stdout = reader( + child + .stdout + .take() + .map(|s| Box::new(s) as Box), + max_output, + ); + let stderr = reader( + child + .stderr + .take() + .map(|s| Box::new(s) as Box), + 4096, + ); + let deadline = Instant::now() + HELPER_TIMEOUT; + let status = loop { + match child.try_wait() { + Ok(Some(status)) => break status, + Ok(None) if Instant::now() < deadline => std::thread::sleep(Duration::from_millis(10)), + Ok(None) => { + let _ = child.kill(); + let _ = child.wait(); + return Err(format!( + "{program} did not finish within {} s", + HELPER_TIMEOUT.as_secs() + )); + } + Err(e) => { + let _ = child.kill(); + let _ = child.wait(); + return Err(e.to_string()); + } + } + }; + let output = stdout.join().unwrap_or_default(); + // Checked first: a helper whose output is cut off at the limit may die + // of the broken pipe. + if output.len() > max_output { + return Err(format!("{program} returned more than {max_output} bytes")); + } + if !status.success() { + let message = String::from_utf8_lossy(&stderr.join().unwrap_or_default()) + .trim() + .to_owned(); + return Err(if message.is_empty() { + format!("{program} exited with {status}") + } else { + format!("{program}: {message}") + }); + } + Ok(output) +} + +pub fn clipboard_read() -> Result { + let mut command = Command::new("wl-paste"); + // Any text type; the content must still be UTF-8. + command.args(["--no-newline", "--type", "text"]); + let data = run(command, None, MAX_CLIPBOARD, Output::Captured)?; + String::from_utf8(data).map_err(|_| "the clipboard does not hold UTF-8 text".to_owned()) +} + +pub fn clipboard_write(text: &str) -> Result<(), String> { + if text.len() > MAX_CLIPBOARD { + return Err(format!("clipboard text exceeds {MAX_CLIPBOARD} bytes")); + } + let mut command = Command::new("wl-copy"); + command.args(["--type", "text/plain;charset=utf-8"]); + run( + command, + Some(text.as_bytes().to_vec()), + 0, + Output::ErrorsOnly, + ) + .map(|_| ()) +} + +/// Checks a notification before it is sent. +pub fn check_notification(title: &str, body: &str, icon: Option<&str>) -> Result<(), String> { + if title.len() > MAX_TITLE { + return Err(format!("notification title exceeds {MAX_TITLE} bytes")); + } + if body.len() > MAX_BODY { + return Err(format!("notification body exceeds {MAX_BODY} bytes")); + } + // Only icon theme names: a path or URL would let the app show any file + // or reach the network through the notification service. + if let Some(icon) = icon + && !is_icon_name(icon) + { + return Err("a notification icon must be an icon theme name".to_owned()); + } + Ok(()) +} + +fn is_icon_name(icon: &str) -> bool { + !icon.is_empty() + && icon.len() <= 64 + && !icon.starts_with('.') + && icon + .bytes() + .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.')) +} + +fn escape_markup(text: &str) -> String { + text.replace('&', "&") + .replace('<', "<") + .replace('>', ">") +} + +/// Sends a notification attributed to `app_id`. +pub fn notify(app_id: &str, title: &str, body: &str, icon: Option<&str>) -> Result<(), String> { + check_notification(title, body, icon)?; + let mut command = Command::new("notify-send"); + command.arg(format!("--app-name={app_id}")); + if let Some(icon) = icon { + command.arg(format!("--icon={icon}")); + } + // Notification servers may render markup in the body, including links + // and images, so it is sent as plain text. + command.arg("--").arg(title).arg(escape_markup(body)); + run(command, None, 0, Output::Captured).map(|_| ()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn notification_bodies_are_plain_text() { + assert_eq!( + escape_markup("a & b"), + "<a href=\"x\">a & b</a>" + ); + } + + #[test] + fn notifications_are_bounded_and_icons_are_theme_names() { + assert!(check_notification("t", "b", None).is_ok()); + assert!(check_notification("t", "b", Some("dialog-information")).is_ok()); + assert!(check_notification(&"t".repeat(MAX_TITLE + 1), "b", None).is_err()); + assert!(check_notification("t", &"b".repeat(MAX_BODY + 1), None).is_err()); + for icon in [ + "/etc/passwd", + "file:///x.png", + "https://x/i.png", + "../x", + "", + "a b", + ] { + assert!(check_notification("t", "b", Some(icon)).is_err(), "{icon}"); + } + } + + #[test] + fn helpers_are_bounded_in_time_and_output() { + let mut slow = Command::new("sleep"); + slow.arg("30"); + let started = Instant::now(); + let error = run(slow, None, 0, Output::Captured).unwrap_err(); + assert!(error.contains("did not finish"), "{error}"); + assert!(started.elapsed() < HELPER_TIMEOUT + Duration::from_secs(2)); + + let mut chatty = Command::new("head"); + // Far more than a pipe holds, so the cut-off kills the writer. + chatty.args(["-c", "10000000", "/dev/zero"]); + assert!( + run(chatty, None, 1000, Output::Captured) + .unwrap_err() + .contains("more than 1000 bytes") + ); + + let echo = Command::new("cat"); + assert_eq!( + run(echo, Some(b"text".to_vec()), 100, Output::Captured).unwrap(), + b"text" + ); + + // A helper that leaves a process behind returns once it exits + // itself when its streams are discarded. + let mut forking = Command::new("sh"); + forking.args(["-c", "sleep 30 & exit 0"]); + let started = Instant::now(); + assert!(run(forking, None, 0, Output::ErrorsOnly).is_ok()); + assert!(started.elapsed() < Duration::from_secs(2)); + + let mut failing = Command::new("sh"); + failing.args(["-c", "echo no display >&2; exit 1"]); + assert_eq!( + run(failing, None, 0, Output::ErrorsOnly).unwrap_err(), + "sh: no display" + ); + + let failing = Command::new("false"); + assert!( + run(failing, None, 0, Output::Captured) + .unwrap_err() + .contains("exited") + ); + } +} diff --git a/crates/weft-runtime/src/grants.rs b/crates/weft-runtime/src/grants.rs index ab60c2d..e98ebcf 100644 --- a/crates/weft-runtime/src/grants.rs +++ b/crates/weft-runtime/src/grants.rs @@ -47,18 +47,31 @@ impl Grants { } #[cfg_attr(not(feature = "net-fetch"), allow(dead_code))] - /// Whether a request to `host` is covered by a fetch grant. `host` is the - /// URL's host in the lowercase form URL parsing produces. - pub fn fetch_host(&self, host: &str) -> Result<(), String> { + /// Whether a request to `host` is covered by a fetch grant, and which + /// addresses it may reach. `host` is the URL's host in the lowercase form + /// URL parsing produces. + pub fn fetch_host(&self, host: &str) -> Result { if self.fetch.is_empty() { return Err("capability net:fetch is not granted".to_owned()); } + // An address named in a grant is reached as declared, wherever it + // is; a name, or any host, reaches only public addresses, so a + // granted name cannot be pointed at this machine or the local + // network through DNS. + if host.parse::().is_ok() + && self + .fetch + .iter() + .any(|scope| matches!(scope, FetchScope::Host(granted) if granted == host)) + { + return Ok(Reach::Declared); + } let covered = self.fetch.iter().any(|scope| match scope { FetchScope::AnyHost => true, FetchScope::Host(granted) => granted == host, }); if covered { - Ok(()) + Ok(Reach::Public) } else { Err(format!("capability net:fetch:{host} is not granted")) } @@ -115,6 +128,86 @@ impl Preopen { } } +/// The addresses a granted request may connect to. +#[cfg_attr(not(feature = "net-fetch"), allow(dead_code))] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Reach { + /// Only public addresses. + Public, + /// The address the grant names, public or not. + Declared, +} + +/// Whether `ip` is a public unicast address: not loopback, unspecified, +/// private, shared (CGNAT), link-local, multicast, broadcast, reserved, +/// documentation or benchmarking space, nor an IPv6 form of one. +#[cfg_attr(not(feature = "net-fetch"), allow(dead_code))] +pub fn is_public(ip: std::net::IpAddr) -> bool { + use std::net::IpAddr; + match ip { + IpAddr::V4(v4) => { + let [a, b, c, _] = v4.octets(); + !(v4.is_unspecified() + || v4.is_loopback() + || v4.is_private() + || v4.is_link_local() + || v4.is_multicast() + || v4.is_broadcast() + || v4.is_documentation() + || a == 0 + || a >= 240 + || (a == 100 && (64..128).contains(&b)) + || (a == 192 && b == 0 && c == 0) + || (a == 198 && (18..20).contains(&b))) + } + IpAddr::V6(v6) => { + let seg = v6.segments(); + let embedded = |high: u16, low: u16| { + IpAddr::V4(std::net::Ipv4Addr::new( + (high >> 8) as u8, + high as u8, + (low >> 8) as u8, + low as u8, + )) + }; + // Forms that carry an IPv4 address are judged by that address: + // mapped and compatible addresses, 6to4 and NAT64. + if let Some(v4) = v6.to_ipv4_mapped() { + return is_public(IpAddr::V4(v4)); + } + // IPv4-translated (::ffff:0:a.b.c.d). + if seg[..6] == [0, 0, 0, 0, 0xffff, 0] { + return is_public(embedded(seg[6], seg[7])); + } + if seg[..6] == [0; 6] && !(seg[6] == 0 && seg[7] <= 1) { + return is_public(embedded(seg[6], seg[7])); + } + if seg[0] == 0x2002 { + return is_public(embedded(seg[1], seg[2])); + } + if seg[..6] == [0x0064, 0xff9b, 0, 0, 0, 0] { + return is_public(embedded(seg[6], seg[7])); + } + !(v6.is_unspecified() + || v6.is_loopback() + || v6.is_multicast() + // Unique local, link-local and the deprecated site-local. + || (seg[0] & 0xfe00) == 0xfc00 + || (seg[0] & 0xffc0) == 0xfe80 + || (seg[0] & 0xffc0) == 0xfec0 + // NAT64 for local use, discard-only. + || (seg[0] == 0x0064 && seg[1] == 0xff9b) + || (seg[0] == 0x0100 && seg[1..4] == [0, 0, 0]) + // Teredo, benchmarking, ORCHID and documentation. + || (seg[0] == 0x2001 && seg[1] == 0) + || (seg[0] == 0x2001 && seg[1] == 0x0002 && seg[2] == 0) + || (seg[0] == 0x2001 && matches!(seg[1] & 0xfff0, 0x0010 | 0x0020)) + || (seg[0] == 0x2001 && seg[1] == 0x0db8) + || (seg[0] == 0x3fff && (seg[1] & 0xf000) == 0)) + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -138,6 +231,73 @@ mod tests { assert!(grants.notifications().is_err()); } + #[test] + fn named_and_wildcard_grants_reach_only_public_addresses() { + let mut grants = Grants::default(); + grants.add("net:fetch:api.example.org").unwrap(); + grants.add("net:fetch:127.0.0.1").unwrap(); + assert_eq!(grants.fetch_host("api.example.org"), Ok(Reach::Public)); + // An address named in a grant is reached as declared. + assert_eq!(grants.fetch_host("127.0.0.1"), Ok(Reach::Declared)); + let mut any = Grants::default(); + any.add("net:fetch:*").unwrap(); + assert_eq!(any.fetch_host("10.0.0.1"), Ok(Reach::Public)); + } + + #[test] + fn only_public_unicast_addresses_are_public() { + // NAT64 and 6to4 forms of a public address are public, so DNS64 + // networks keep working. + for public in [ + "93.184.216.34", + "1.1.1.1", + "2606:4700::1111", + "64:ff9b::5db8:d822", + "2002:5db8:d822::1", + "3ffe::1", + ] { + assert!(is_public(public.parse().unwrap()), "{public}"); + } + for local in [ + "127.0.0.1", + "0.0.0.0", + "10.1.2.3", + "172.16.0.1", + "192.168.1.1", + "169.254.169.254", + "100.64.0.1", + "224.0.0.1", + "255.255.255.255", + "192.0.2.1", + "198.18.0.1", + "240.0.0.1", + "::1", + "::", + "fd00::1", + "fe80::1", + "ff02::1", + "::ffff:127.0.0.1", + "::ffff:10.0.0.1", + "64:ff9b::a00:1", + "64:ff9b:1::1", + "64:ff9b:0:0:1::1", + "::ffff:0:a00:1", + "2001:20::1", + "2001:db8::1", + "fec0::1", + "::7f00:1", + "2002:7f00:1::", + "2002:a00:1::", + "2001::1", + "2001:2::1", + "2001:10::1", + "3fff::1", + "100::1", + ] { + assert!(!is_public(local.parse().unwrap()), "{local}"); + } + } + #[test] fn host_fetch_grants_match_exact_hosts() { let mut grants = Grants::default(); diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index b94f5a4..ae148f8 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -2,6 +2,8 @@ use std::path::PathBuf; use anyhow::Context; +#[cfg(feature = "wasmtime-runtime")] +mod desktop; mod grants; #[cfg(feature = "wasmtime-runtime")] mod ipc; @@ -97,6 +99,7 @@ fn main() -> anyhow::Result<()> { tracing::info!(session_id, %app_id, wasm = %wasm_path.display(), "executing module"); run_module( + app_id, &wasm_path, &preopen, grants, @@ -120,6 +123,7 @@ fn ready_line() -> String { #[cfg(not(feature = "wasmtime-runtime"))] fn run_module( + _app_id: &str, _wasm_path: &std::path::Path, _preopen: &[Preopen], _grants: Grants, @@ -133,6 +137,7 @@ fn run_module( #[cfg(feature = "wasmtime-runtime")] fn run_module( + app_id: &str, wasm_path: &std::path::Path, preopen: &[Preopen], grants: Grants, @@ -245,6 +250,7 @@ fn run_module( .context("define weft:app/fetch#fetch")?; let notify_grants = Arc::clone(&grants); + let notify_app = app_id.to_owned(); linker .instance("weft:app/notifications@0.1.0") .context("define weft:app/notifications instance")? @@ -255,7 +261,7 @@ fn run_module( -> wasmtime::Result<(Result<(), String>,)> { let result = notify_grants .notifications() - .and_then(|()| host_notify(&title, &body, icon.as_deref())); + .and_then(|()| desktop::notify(¬ify_app, &title, &body, icon.as_deref())); Ok((result,)) }, ) @@ -274,7 +280,7 @@ fn run_module( -> wasmtime::Result<(Result,)> { Ok((read_grants .clipboard_read() - .and_then(|()| host_clipboard_read()),)) + .and_then(|()| desktop::clipboard_read()),)) }, ) .context("define weft:app/clipboard#read")?; @@ -287,7 +293,7 @@ fn run_module( -> wasmtime::Result<(Result<(), String>,)> { Ok((write_grants .clipboard_write() - .and_then(|()| host_clipboard_write(&text)),)) + .and_then(|()| desktop::clipboard_write(&text)),)) }, ) .context("define weft:app/clipboard#write")?; @@ -308,10 +314,6 @@ fn run_module( } } - if let Ok(portal_socket) = std::env::var("WEFT_FILE_PORTAL_SOCKET") { - ctx_builder.env("WEFT_FILE_PORTAL_SOCKET", &portal_socket); - } - for dir in preopen { let (dir_perms, file_perms) = match dir.access { Access::Read => (DirPerms::READ, FilePerms::READ), @@ -403,7 +405,14 @@ fn host_fetch( return Err(format!("unsupported URL scheme '{}'", parsed.scheme())); } let host = parsed.host_str().ok_or("URL has no host")?; - grants.fetch_host(host)?; + let reach = grants.fetch_host(host)?; + // Credentials are never taken from a URL; a component that needs them + // sends them in a header to the host it is granted. + if !parsed.username().is_empty() || parsed.password().is_some() { + return Err( + "URLs with credentials are not supported; send an Authorization header".to_owned(), + ); + } // The method is written into the request line as given, so only known // methods are accepted. if !matches!( @@ -424,6 +433,9 @@ fn host_fetch( } } let agent = ureq::AgentBuilder::new() + .resolver(GrantedResolver(reach)) + // A proxy would be connected to instead of the checked address. + .try_proxy_from_env(false) .redirects(0) .timeout_connect(Duration::from_secs(10)) .timeout(Duration::from_secs(30)) @@ -458,6 +470,33 @@ fn host_fetch( }) } +/// Resolves a fetch destination and keeps only the addresses its grant +/// may reach; the connection is made to those addresses alone, so the +/// check cannot be bypassed by a second, different DNS answer. +#[cfg(all(feature = "wasmtime-runtime", feature = "net-fetch"))] +struct GrantedResolver(grants::Reach); + +#[cfg(all(feature = "wasmtime-runtime", feature = "net-fetch"))] +impl ureq::Resolver for GrantedResolver { + fn resolve(&self, netloc: &str) -> std::io::Result> { + use std::net::ToSocketAddrs; + let addresses: Vec<_> = netloc + .to_socket_addrs()? + .filter(|a| self.0 == grants::Reach::Declared || grants::is_public(a.ip())) + .collect(); + if addresses.is_empty() { + return Err(std::io::Error::new( + std::io::ErrorKind::PermissionDenied, + format!( + "{netloc} resolves only to addresses on this machine or a local network, \ + which only a grant naming that address covers" + ), + )); + } + Ok(addresses) + } +} + #[cfg(all(feature = "wasmtime-runtime", not(feature = "net-fetch")))] fn host_fetch( grants: &Grants, @@ -470,55 +509,6 @@ fn host_fetch( Err("this runtime was built without network fetch support".to_owned()) } -#[cfg(feature = "wasmtime-runtime")] -fn host_clipboard_read() -> Result { - let out = std::process::Command::new("wl-paste") - .arg("--no-newline") - .output() - .map_err(|e| e.to_string())?; - if out.status.success() { - String::from_utf8(out.stdout).map_err(|e| e.to_string()) - } else { - Err(String::from_utf8_lossy(&out.stderr).trim().to_owned()) - } -} - -#[cfg(feature = "wasmtime-runtime")] -fn host_clipboard_write(text: &str) -> Result<(), String> { - use std::io::Write; - let mut child = std::process::Command::new("wl-copy") - .stdin(std::process::Stdio::piped()) - .spawn() - .map_err(|e| e.to_string())?; - if let Some(stdin) = child.stdin.as_mut() { - stdin - .write_all(text.as_bytes()) - .map_err(|e| e.to_string())?; - } - let status = child.wait().map_err(|e| e.to_string())?; - if status.success() { - Ok(()) - } else { - Err(format!("wl-copy exited with {status}")) - } -} - -#[cfg(feature = "wasmtime-runtime")] -fn host_notify(title: &str, body: &str, icon: Option<&str>) -> Result<(), String> { - let mut cmd = std::process::Command::new("notify-send"); - if let Some(i) = icon { - cmd.arg("--icon").arg(i); - } - cmd.arg("--").arg(title).arg(body); - cmd.status().map_err(|e| e.to_string()).and_then(|s| { - if s.success() { - Ok(()) - } else { - Err(format!("notify-send exited with {s}")) - } - }) -} - #[cfg(feature = "seccomp")] fn apply_seccomp_filter() -> anyhow::Result<()> { use seccompiler::{BpfProgram, SeccompAction, SeccompFilter, SeccompRule}; @@ -585,6 +575,7 @@ mod tests { #[test] fn engine_disabled_build_refuses_to_run_components() { let err = run_module( + "org.weft.test", std::path::Path::new("app.wasm"), &[], Grants::default(), diff --git a/crates/weft-servo-shell/src/embedder.rs b/crates/weft-servo-shell/src/embedder.rs index db30713..60026fe 100644 --- a/crates/weft-servo-shell/src/embedder.rs +++ b/crates/weft-servo-shell/src/embedder.rs @@ -296,12 +296,25 @@ impl App { self.next_endpoint_check, )); self.endpoint = appd_endpoint(self.ws_port); - let (Some(endpoint), Some(webview)) = (&self.endpoint, &self.webview) else { + let Some(endpoint) = &self.endpoint else { + return; + }; + // The credential goes only to the system UI document. + let call = format!( + "window.weftAppdEndpoint({}, '{}')", + endpoint.port, endpoint.token + ); + self.call_system_ui("weftAppdEndpoint", &call); + } + + /// Runs `call` in the system UI document if it defines the function + /// `name`. Navigation away from the document is denied; the script + /// checks the document again because a load can still be under way when + /// it runs. + fn call_system_ui(&self, name: &str, call: &str) { + let Some(webview) = &self.webview else { return; }; - // The credential goes only to the system UI document. Navigation away - // from it is denied; the script checks the document again because a - // load can still be under way when it runs. if !webview .url() .is_some_and(|url| same_document(url.as_str(), self.url.as_str())) @@ -313,9 +326,8 @@ impl App { format!( "window === window.top && \ location.href.split('#')[0] === {document} && \ - typeof window.weftAppdEndpoint === 'function' && \ - window.weftAppdEndpoint({}, '{}')", - endpoint.port, endpoint.token + typeof window.{name} === 'function' && \ + {call}" ), |_| {}, ); @@ -346,6 +358,19 @@ impl App { } impl ApplicationHandler for App { + /// 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) { if self.window.is_some() { return; @@ -481,6 +506,11 @@ impl ApplicationHandler for App { WindowEvent::ModifiersChanged(mods) => { self.modifiers = mods.state(); } + // Servo does not tell the page when its window loses keyboard + // focus; the system UI closes its launcher then. + WindowEvent::Focused(false) => { + self.call_system_ui("weftShellBlurred", "window.weftShellBlurred()"); + } WindowEvent::KeyboardInput { event, .. } => { if let Some(wv) = &self.webview { let ev = super::keyutils::keyboard_event_from_winit(&event, self.modifiers); diff --git a/crates/weft-servo-shell/src/keyutils.rs b/crates/weft-servo-shell/src/keyutils.rs index 1665cb5..ba4bdf5 100644 --- a/crates/weft-servo-shell/src/keyutils.rs +++ b/crates/weft-servo-shell/src/keyutils.rs @@ -57,7 +57,8 @@ fn named_key(n: NamedKey) -> servo::NamedKey { NamedKey::Control => S::Control, NamedKey::Fn => S::Fn, NamedKey::FnLock => S::FnLock, - NamedKey::Meta => S::Meta, + // winit names the Super keys Super; the DOM names them Meta. + NamedKey::Meta | NamedKey::Super => S::Meta, NamedKey::NumLock => S::NumLock, NamedKey::ScrollLock => S::ScrollLock, NamedKey::Shift => S::Shift, 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 0c4d5eb..49df9b6 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -8,19 +8,19 @@ 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`). 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. The Super keys are the compositor's too and never reach an application: a Super tap, pressed and released without another key or a pointer button, brings the panel (the first registered) to the front with keyboard focus and then delivers the tap to it, so the shell's launcher is reachable from any application; keys pressed while Super is held reach the focused window with the Super modifier, but Super's own key events do not. 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 -System UI host. Renders one WebView pointing at `system-ui.html` using the embedded Servo engine (feature-gated behind `servo-embed`). Connects to the compositor as window type `panel`. Dispatches the `zweft_shell_manager_v1` event queue each frame. Forwards navigation gestures received from the compositor to `weft-appd` over WebSocket. Reads the appd WebSocket port and system token from `$XDG_RUNTIME_DIR/weft/` and hands both to the page through `window.weftAppdEndpoint(port, token)`; the shell hands the token only to the system UI document and denies top-level navigation away from it, and the launcher shows package names as text, never markup. +System UI host. Renders one WebView pointing at `system-ui.html` using the embedded Servo engine (feature-gated behind `servo-embed`). Connects to the compositor as window type `panel`. Dispatches the `zweft_shell_manager_v1` event queue each frame. Forwards navigation gestures received from the compositor to `weft-appd` over WebSocket. Reads the appd WebSocket port and system token from `$XDG_RUNTIME_DIR/weft/` and hands both to the page through `window.weftAppdEndpoint(port, token)`; the shell hands the token only to the system UI document and denies top-level navigation away from it, and the launcher shows package names as text, never markup. The system UI works from the keyboard: a Super tap opens or closes the launcher, which lists installed apps by name and keeps focus while open (Tab moves between apps, Enter opens one, Escape closes it and returns focus to the Apps button, with the shell's home still in front); every taskbar entry and its close button is a button reachable with Tab, and Enter on an entry brings its app back. The shell tells the page through `window.weftShellBlurred()` when it loses keyboard focus, which Servo does not report to the document, and the launcher closes. The page has no transitions or animations, since Servo does not evaluate `prefers-reduced-motion`. ### 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 -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. The two children log to stderr, which appd records at info level; their stdout carries the readiness line, and anything else printed there is recorded only at debug level. 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. The two children log to stderr, which appd records at info level; their stdout carries the readiness line, and anything else printed there is recorded only at debug level. Each wait times out after 30 seconds. `TERMINATE_APP` on a running session closes it as a user close would: appd 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 session stays `running` while the application answers (appd replies with that state), and enters `stopping` only once it is ending. An application that declares the `ask` close policy may decline the close (see [Closing an application](#closing-an-application)); its window is brought to the front and the session keeps running. A repeated request while the application answers changes nothing, and a session being asked to close is still restored at the next start if appd stops meanwhile. `TERMINATE_APP` with `"force": true` terminates the session without asking; the taskbar offers it, as Force quit, for an app still running once it has had time to answer. The stop reason records what happened: a close on request with a clean exit, a failure while closing, the timeout, or a forced stop; `stopped` is broadcast once the session has settled. A session still starting is stopped at once, also when the request races its readiness, and when appd itself shuts down its sessions are terminated without a close. appd keeps the final state of at most 32 stopped sessions, forgetting those launched earliest. 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 @@ -28,11 +28,11 @@ 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; installs or updates, see [Installing and updating](#installing-and-updating)), `uninstall`, `rollback`, `approve` (approve the app's declared capabilities, or those listed; `install --approve` does so at installation), `list`, `build-image` (EROFS dm-verity), `info`. ### weft-file-portal -Per-session file proxy. Runs as a separate process with a path allowlist derived from the session's directory grants; read-only grants (`--allow-read`) refuse writes. Paths are checked lexically, so `..` is blocked but symbolic links are followed. Accepts JSON-lines requests over a Unix socket. Components cannot currently reach that socket, because the runtime grants them no socket access. +Per-session file proxy. Runs as a separate process with the session's directory grants; read-only grants (`--allow-read`) refuse writes. Every operation is resolved beneath the granted directory's descriptor by the kernel (see security.md), with bounded file, listing and request sizes. Accepts JSON-lines requests over a Unix socket. Components cannot currently reach that socket, because the runtime grants them no socket access. ### weft-mount-helper @@ -83,14 +83,16 @@ Each capability-controlled host import (fetch, notifications, clipboard) checks | `fs:read:app-data` / `fs:rw:app-data` | Preopen the app's data directory (see *App data*) as `/data`, read-only or read-write | | `fs:read:xdg-documents` / `fs:rw:xdg-documents` | Preopen the documents directory from the XDG user-dirs configuration (`XDG_DOCUMENTS_DIR`) as `/xdg/documents`; the launch fails when none is configured | | `net:fetch:` | `weft:app/fetch` to that exact host (a lowercase DNS name or dotted IPv4 address) over HTTP or HTTPS, on any port | -| `net:fetch:*` | `weft:app/fetch` to any host | +| `net:fetch:*` | `weft:app/fetch` to any host at a public address | | `sys:notifications` | `weft:app/notifications` | | `sys:clipboard:read` / `sys:clipboard:write` | `weft:app/clipboard#read` / `#write` | | `hw:gpu:compute` / `hw:gpu:render` | Not supported; the launch fails | +Clipboard and notifications run `wl-paste`, `wl-copy` and `notify-send` from the runtime's environment, each bounded to 5 seconds, after which the helper is killed and the call fails. Clipboard text is UTF-8, of any text type, and at most 1 MiB in either direction. A notification's title is at most 256 bytes and its body 4096, sent as plain text with markup escaped; its icon must be an icon theme name, not a path or URL; and it is sent with the app's ID as the application name, so an app cannot pass a notification off as the system's. Where a helper is not installed the call fails with that error. + Fetch does not follow redirects: a redirect is returned to the component as a response, so every destination it requests is checked against its grants. HTTP error statuses are responses too; only policy and transport failures are errors. Only the methods GET, HEAD, POST, PUT, PATCH, DELETE and OPTIONS are accepted, and a component cannot set `Host`, `Content-Length`, `Transfer-Encoding` or other hop-by-hop headers. Connecting times out after 10 seconds and the whole request after 30 seconds, not counting DNS resolution; response bodies are limited to 16 MiB. Fetch requires a runtime built with the `net-fetch` feature. -Fetch grants name hosts, not addresses: ports are not restricted, and a host that resolves to a loopback or private address (or `net:fetch:*`) can reach local services. IPv6 literals cannot be granted. +The runtime resolves a fetch destination itself and connects only to the addresses it allows. A grant for a name, and `net:fetch:*`, reach only public unicast addresses: loopback, unspecified, private, shared (100.64/10), link-local, multicast, broadcast, reserved, documentation and benchmarking addresses, and their IPv6 counterparts (unique local, site-local, Teredo and others), are refused, and an IPv6 address that carries an IPv4 address (mapped, compatible, translated, 6to4, NAT64) is judged by that address, so DNS cannot point a granted name at this machine or the local network. An IPv4 address named in a grant, such as `net:fetch:127.0.0.1`, is reached as declared. Fetch never goes through a proxy. Ports are not restricted. Credentials in a URL are refused; a component sends credentials in a header, and only to a host it is granted, since redirects are not followed. IPv6 literals cannot be granted. `tests/runtime/check_grants.py` runs the real runtime on a probe component (`tests/components/grants-probe`) under each kind of grant. @@ -102,7 +104,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): @@ -115,11 +117,30 @@ Package store roots (in priority order): `weft-appd` resolves a launch once. A verified image in any store root takes precedence over a directory install in any root, so a user's directory cannot shadow a system image; otherwise the first root with a directory install is used: - **Verified image:** `/.app.img` with its dm-verity hash tree `.app.hash` and root hash `.app.roothash`, the names `weft-pack build-image` and `build-verity` produce. When any of the three exists, the image must mount through `weft-mount-helper`; a missing companion, a malformed root hash, a missing helper or a failed mount refuses the launch, and appd never falls back to a directory install. The root hash is read from the store next to the image and is not yet authenticated, so dm-verity detects corruption but not a store whose image, hash tree and root hash were replaced together. The mount belongs to the session and is released when the session stops. -- **Directory install:** `//wapp.toml`. +- **Directory install:** `/`, a link to the active revision `.revisions//` written by `weft-pack`, or, for a store written before revisions, a package directory. A link is followed only in exactly that form, to one of the app's own revisions; any other link is refused (403). The session runs from the revision directory itself and holds a shared lock on it until it stops, so it keeps its bytes while an update or rollback switches the link. The package root must be a directory and its manifest a regular file, neither a symbolic link, and the manifest must declare the requested ID. `[runtime].module` and `[ui].entry` must be relative paths of plain components, without symbolic links, naming regular files inside that root. The component, the UI document and the capabilities all come from that one manifest. A refused launch reports 400 (invalid ID), 404 (not installed), 403 (invalid package, image or image metadata) or 500 (the host cannot read the package or mount the image). -A directory install can still be changed by its owner while a session runs, and nothing verifies its content at launch. +A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch, with the limits security.md describes. + +### Installing and updating + +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. Such a replacement, for the same content with another signature file, is visible under the revision's name at once, even if the installation then fails. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused. + +The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. + +A launch is refused (500, try again) while a package operation holds the revision it would run from, rather than waiting for it. Only sessions started by this weft-appd hold their revisions: a session left from an earlier version, or processes that outlive weft-appd, do not, and a package directory from before revisions such sessions run from can be replaced under them. Package operations on one app are serialised by the owner lock in the data home of the user running them; the store itself is not locked against users with other data homes. + +## 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 declined: the app shell reports this to weft-appd with its readiness token on standard output, so page output cannot forge it, and the session keeps 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. A clean exit after a close request, including one the page makes after declining, is recorded as a close on request. A report still unread when a new request arrives can answer that request early; the app shell still closes a page that does not answer. + +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. An application can decline every close, so a forced stop never asks it: it is the user's way to end an application that will not close. A forced stop, the timeout, 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 @@ -147,5 +168,4 @@ A package directory left empty by the move is removed. Nothing is moved, and `we | `WEFT_APPD_WS_PORT` | `7410` | WebSocket port for weft-appd | | `WEFT_EGL_RENDERING` | — | Set to `1` to use EGL rendering in Servo shell | | `WEFT_DISABLE_CGROUP` | — | Set to disable systemd-run cgroup wrapping | -| `WEFT_FILE_PORTAL_SOCKET` | — | Path forwarded to app runtime for file portal | | `XDG_RUNTIME_DIR` | — | Standard XDG runtime dir (sockets written here) | diff --git a/docs/building.md b/docs/building.md index fca337a..e91e3c7 100644 --- a/docs/building.md +++ b/docs/building.md @@ -85,9 +85,12 @@ 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 +python3 tests/frame/check_system_ui.py # the system UI's launcher and taskbar work from the keyboard alone, reached with a Super tap +python3 tests/frame/check_update.py # updates leave running sessions on their revision; failed updates, rollback and uninstall ``` ## Demo apps (wasm32-wasip2) @@ -125,6 +128,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 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) Build the VM image: diff --git a/docs/security.md b/docs/security.md index 8b8bcf3..06291f9 100644 --- a/docs/security.md +++ b/docs/security.md @@ -8,7 +8,9 @@ WEFT OS enforces a capability-based security model. No capability is granted by `weft-pack check` validates capability strings against the shared vocabulary in `weft_ipc_types::capability`. Unknown capability strings are rejected. At launch, `weft-appd` derives the session's grants from the manifest and refuses to start a package with a capability that is unknown, unsupported or unsatisfiable. `weft-runtime` enforces the grants it is given: read-only directories are preopened read-only, and each host import checks its grant on every call. See the capability table in `architecture.md`. -Installing a package does not yet ask the user to approve its capabilities; every declared, supported capability is granted at launch. +A package gets only the capabilities the user approved. `weft-pack install --approve` approves those the installed package declares. `weft-pack approve ` approves those of the package weft-appd would launch, the first store in weft-appd's order that holds the app, which also covers packages in the system store. `weft-pack approve ...` adds the listed capabilities to the app's approval; for a package weft-pack can read they must be ones it declares. Listing them is the only way to approve an app installed as a verified image, since weft-pack cannot read an image's manifest; for the same reason, approvals for an image are not reduced when it is updated, and uninstalling a directory copy of the app removes them although the image remains. Without approval, `install` and `rollback` list what still needs it. + +The approval is recorded per user, in `$XDG_DATA_HOME/weft/approvals/`; weft-pack and weft-appd must use the same data home, as for owner records. An installation by another user, such as root installing into the system store, approves nothing for this user. weft-appd refuses to launch an app that declares a capability the record does not hold (403, naming the capabilities), after refusing unknown and unsupported capabilities and before preparing anything for the session. An approval covers what the installed package declares: an update or rollback drops approvals for capabilities the package no longer declares, so a capability declared again later, or newly, needs approval again. Uninstalling the app forgets its approval unless another store still holds the app. Approvals are keyed by app ID, not by store or publisher. Private app data (`fs:read:app-data`, `fs:rw:app-data`) is a minimal execution resource and needs no approval. Signature verification and approval are separate decisions: a trusted signature approves nothing. ## Process Isolation @@ -16,13 +18,21 @@ Each app session runs as a separate OS process (`weft-runtime`). When systemd is ## Filesystem Isolation -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. +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. It opens each granted directory once and resolves every request beneath that descriptor with `openat2(RESOLVE_BENEATH)`, so the kernel keeps the operation inside the directory: `..` and links pointing outside are refused, also when a link is swapped in while the operation runs; links that stay inside are followed. This needs Linux 5.6 or later; without `openat2` every operation fails. Requests must name an absolute path without `.`, `..` or empty components; files are limited to 16 MiB, listings to 10,000 entries, requests to one line of about 22 MiB, and the portal to 4 connections at a time, each closed when it sends or accepts no data for 60 seconds. Writes replace a file atomically, keeping its permission bits but not its owner, extended attributes or hard links (a write to a name that is a link replaces the link), and never create directories. A granted directory that cannot be opened when the portal starts is not served. Components cannot currently reach its socket, since the runtime grants them no socket access, and the socket's path is not passed to them. -## 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. 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. -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 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. + +`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. + +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 @@ -30,7 +40,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 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; 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. Such a client registered as a panel would also be raised and focused by a Super tap; the compositor picks the first panel registered. ## JavaScript Engine (SpiderMonkey) diff --git a/examples/org.weft.demo.notes/signature.sig b/examples/org.weft.demo.notes/signature.sig index 637f568..e031251 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 +3c5155af4973c453e5c09b04aa0eaf71e943b6564d9aaa81a5fb023fe3ebea091a793ef493234a5a862b9752acf7cafbb0d11a2ec9c962331606ebde75c7b803 \ 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..034830a 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. + + + +