fix(appd): resolve each launch to one package and fail closed on images
The runtime, the app shell and grant derivation each searched the
package stores and parsed wapp.toml on their own, so they could use
different roots; the runtime ignored [runtime].module, the image loader
looked for companion files the builder never wrote, and a failed image
mount fell back to the mutable directory install.
weft-appd now resolves a launch once: a verified image wins over a
directory in the same store, and when any image file exists the image
must mount or the launch is refused. The manifest must declare the
requested ID, and both entries must be plain relative paths to regular
files without links. The runtime receives --module and the app shell
--ui; neither looks for the package. Grants come from the same
manifest, and the mount belongs to the session.
The manifest parser and the image file names move to weft-ipc-types,
shared with weft-pack, whose check now applies the same entry rules.
Mapper devices are named after their per-launch mountpoint, so
concurrent sessions no longer collide.
2026-10-09 10:03:40 +00:00
|
|
|
//! The package a session runs, resolved once before anything is started.
|
|
|
|
|
//!
|
|
|
|
|
//! 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.
|
|
|
|
|
|
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
|
|
use weft_ipc_types::manifest::{MANIFEST_FILE, Manifest, entry_path};
|
|
|
|
|
use weft_ipc_types::package::{ImageFiles, is_valid_app_id};
|
|
|
|
|
|
|
|
|
|
use crate::grants::Refusal;
|
|
|
|
|
use crate::mount::Mount;
|
|
|
|
|
|
|
|
|
|
pub(crate) struct LaunchPackage {
|
|
|
|
|
pub(crate) app_id: String,
|
|
|
|
|
pub(crate) version: String,
|
|
|
|
|
pub(crate) root: PathBuf,
|
|
|
|
|
/// The Wasm component, from `[runtime].module`.
|
|
|
|
|
pub(crate) module: PathBuf,
|
|
|
|
|
/// The UI document, from `[ui].entry`.
|
|
|
|
|
pub(crate) ui_entry: PathBuf,
|
|
|
|
|
pub(crate) capabilities: Vec<String>,
|
|
|
|
|
/// The image the package root is mounted from, held for as long as the
|
|
|
|
|
/// session runs.
|
|
|
|
|
pub(crate) image: Option<Mount>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Resolves `app_id` in the package stores, in order. In each store a
|
|
|
|
|
/// verified image takes precedence over a directory install. When any image
|
|
|
|
|
/// file is present for the ID, the image must mount: a missing or damaged
|
|
|
|
|
/// image never falls back to a directory, which nothing verifies.
|
|
|
|
|
pub(crate) fn resolve(app_id: &str) -> Result<LaunchPackage, Refusal> {
|
|
|
|
|
if !is_valid_app_id(app_id) {
|
|
|
|
|
return Err(Refusal::new(400, "invalid app ID"));
|
|
|
|
|
}
|
|
|
|
|
for store in crate::app_store_roots() {
|
|
|
|
|
let files = ImageFiles::in_store(&store, app_id);
|
|
|
|
|
if files.any_present() {
|
|
|
|
|
let image = crate::mount::mount(app_id, &files).map_err(|e| {
|
|
|
|
|
Refusal::new(500, format!("the verified image cannot be mounted: {e}"))
|
|
|
|
|
})?;
|
|
|
|
|
let root = image.root().to_path_buf();
|
|
|
|
|
return from_root(app_id, root, Some(image));
|
|
|
|
|
}
|
|
|
|
|
let dir = store.join(app_id);
|
|
|
|
|
if std::fs::symlink_metadata(dir.join(MANIFEST_FILE)).is_ok() {
|
|
|
|
|
return from_root(app_id, dir, None);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-10-09 10:18:36 +00:00
|
|
|
Err(Refusal::new(
|
|
|
|
|
404,
|
|
|
|
|
format!("package {app_id} is not installed"),
|
|
|
|
|
))
|
fix(appd): resolve each launch to one package and fail closed on images
The runtime, the app shell and grant derivation each searched the
package stores and parsed wapp.toml on their own, so they could use
different roots; the runtime ignored [runtime].module, the image loader
looked for companion files the builder never wrote, and a failed image
mount fell back to the mutable directory install.
weft-appd now resolves a launch once: a verified image wins over a
directory in the same store, and when any image file exists the image
must mount or the launch is refused. The manifest must declare the
requested ID, and both entries must be plain relative paths to regular
files without links. The runtime receives --module and the app shell
--ui; neither looks for the package. Grants come from the same
manifest, and the mount belongs to the session.
The manifest parser and the image file names move to weft-ipc-types,
shared with weft-pack, whose check now applies the same entry rules.
Mapper devices are named after their per-launch mountpoint, so
concurrent sessions no longer collide.
2026-10-09 10:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-09 10:18:36 +00:00
|
|
|
fn from_root(app_id: &str, root: PathBuf, image: Option<Mount>) -> Result<LaunchPackage, Refusal> {
|
fix(appd): resolve each launch to one package and fail closed on images
The runtime, the app shell and grant derivation each searched the
package stores and parsed wapp.toml on their own, so they could use
different roots; the runtime ignored [runtime].module, the image loader
looked for companion files the builder never wrote, and a failed image
mount fell back to the mutable directory install.
weft-appd now resolves a launch once: a verified image wins over a
directory in the same store, and when any image file exists the image
must mount or the launch is refused. The manifest must declare the
requested ID, and both entries must be plain relative paths to regular
files without links. The runtime receives --module and the app shell
--ui; neither looks for the package. Grants come from the same
manifest, and the mount belongs to the session.
The manifest parser and the image file names move to weft-ipc-types,
shared with weft-pack, whose check now applies the same entry rules.
Mapper devices are named after their per-launch mountpoint, so
concurrent sessions no longer collide.
2026-10-09 10:03:40 +00:00
|
|
|
let manifest = Manifest::read(&root).map_err(|e| Refusal::new(403, e.to_string()))?;
|
|
|
|
|
if manifest.package.id != app_id {
|
|
|
|
|
return Err(Refusal::new(
|
|
|
|
|
403,
|
|
|
|
|
format!(
|
|
|
|
|
"the package in {} declares ID '{}'",
|
|
|
|
|
root.display(),
|
|
|
|
|
manifest.package.id
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
let entry = |field: &str, value: &str| {
|
|
|
|
|
entry_path(&root, value).map_err(|e| Refusal::new(403, format!("{field}: {e}")))
|
|
|
|
|
};
|
|
|
|
|
let module = entry("runtime.module", &manifest.runtime.module)?;
|
|
|
|
|
let ui_entry = entry("ui.entry", &manifest.ui.entry)?;
|
|
|
|
|
Ok(LaunchPackage {
|
|
|
|
|
app_id: app_id.to_owned(),
|
|
|
|
|
version: manifest.package.version.clone(),
|
|
|
|
|
capabilities: manifest.capabilities().to_vec(),
|
|
|
|
|
module,
|
|
|
|
|
ui_entry,
|
|
|
|
|
root,
|
|
|
|
|
image,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LaunchPackage {
|
|
|
|
|
/// A package for supervisor tests whose children ignore their files.
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
pub(crate) fn unresolved(app_id: &str) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
app_id: app_id.to_owned(),
|
|
|
|
|
version: "0.0.0".to_owned(),
|
|
|
|
|
root: PathBuf::from("/nonexistent"),
|
|
|
|
|
module: PathBuf::from("/nonexistent/app.wasm"),
|
|
|
|
|
ui_entry: PathBuf::from("/nonexistent/ui/index.html"),
|
|
|
|
|
capabilities: Vec::new(),
|
|
|
|
|
image: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn root(&self) -> &Path {
|
|
|
|
|
&self.root
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
const ID: &str = "org.weft.test.resolve";
|
|
|
|
|
|
|
|
|
|
/// 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 {
|
2026-10-09 10:18:36 +00:00
|
|
|
let store =
|
|
|
|
|
std::env::temp_dir().join(format!("weft_appd_launch_{name}_{}", std::process::id()));
|
fix(appd): resolve each launch to one package and fail closed on images
The runtime, the app shell and grant derivation each searched the
package stores and parsed wapp.toml on their own, so they could use
different roots; the runtime ignored [runtime].module, the image loader
looked for companion files the builder never wrote, and a failed image
mount fell back to the mutable directory install.
weft-appd now resolves a launch once: a verified image wins over a
directory in the same store, and when any image file exists the image
must mount or the launch is refused. The manifest must declare the
requested ID, and both entries must be plain relative paths to regular
files without links. The runtime receives --module and the app shell
--ui; neither looks for the package. Grants come from the same
manifest, and the mount belongs to the session.
The manifest parser and the image file names move to weft-ipc-types,
shared with weft-pack, whose check now applies the same entry rules.
Mapper devices are named after their per-launch mountpoint, so
concurrent sessions no longer collide.
2026-10-09 10:03:40 +00:00
|
|
|
let _ = std::fs::remove_dir_all(&store);
|
|
|
|
|
let package = store.join(ID);
|
|
|
|
|
std::fs::create_dir_all(package.join("ui")).unwrap();
|
|
|
|
|
std::fs::write(package.join("app.wasm"), b"\0asm\x01\0\0\0").unwrap();
|
|
|
|
|
std::fs::write(package.join("ui/index.html"), b"<p>").unwrap();
|
|
|
|
|
std::fs::write(
|
|
|
|
|
package.join(MANIFEST_FILE),
|
|
|
|
|
format!(
|
|
|
|
|
"[package]\nid = \"{manifest_id}\"\nname = \"T\"\nversion = \"1.2.3\"\n\
|
|
|
|
|
capabilities = [\"sys:notifications\"]\n\
|
|
|
|
|
[runtime]\nmodule = \"{module}\"\n[ui]\nentry = \"ui/index.html\"\n"
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
// SAFETY: callers hold env_lock, which serialises environment changes.
|
|
|
|
|
unsafe { std::env::set_var("WEFT_APP_STORE", &store) };
|
|
|
|
|
store
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn finish(store: &Path) {
|
|
|
|
|
// SAFETY: as in `store`.
|
|
|
|
|
unsafe {
|
|
|
|
|
std::env::remove_var("WEFT_APP_STORE");
|
|
|
|
|
std::env::remove_var("WEFT_MOUNT_HELPER");
|
|
|
|
|
}
|
|
|
|
|
let _ = std::fs::remove_dir_all(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn refusal(result: Result<LaunchPackage, Refusal>) -> Refusal {
|
|
|
|
|
match result {
|
|
|
|
|
Ok(package) => panic!("resolved {}", package.root.display()),
|
|
|
|
|
Err(refusal) => refusal,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_directory_install_resolves_to_its_own_files() {
|
|
|
|
|
let _env = crate::tests::env_lock().blocking_lock();
|
|
|
|
|
let store = store("dir", ID, "app.wasm");
|
|
|
|
|
let package = resolve(ID).unwrap();
|
|
|
|
|
finish(&store);
|
|
|
|
|
assert_eq!(package.root, store.join(ID));
|
|
|
|
|
assert_eq!(package.module, store.join(ID).join("app.wasm"));
|
|
|
|
|
assert_eq!(package.ui_entry, store.join(ID).join("ui/index.html"));
|
|
|
|
|
assert_eq!(package.version, "1.2.3");
|
|
|
|
|
assert_eq!(package.capabilities, ["sys:notifications"]);
|
|
|
|
|
assert!(package.image.is_none());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_package_declaring_another_id_is_refused() {
|
|
|
|
|
let _env = crate::tests::env_lock().blocking_lock();
|
|
|
|
|
let store = store("id", "org.weft.test.other", "app.wasm");
|
|
|
|
|
let refused = refusal(resolve(ID));
|
|
|
|
|
finish(&store);
|
|
|
|
|
assert_eq!(refused.code, 403);
|
2026-10-09 10:18:36 +00:00
|
|
|
assert!(
|
|
|
|
|
refused.message.contains("org.weft.test.other"),
|
|
|
|
|
"{}",
|
|
|
|
|
refused.message
|
|
|
|
|
);
|
fix(appd): resolve each launch to one package and fail closed on images
The runtime, the app shell and grant derivation each searched the
package stores and parsed wapp.toml on their own, so they could use
different roots; the runtime ignored [runtime].module, the image loader
looked for companion files the builder never wrote, and a failed image
mount fell back to the mutable directory install.
weft-appd now resolves a launch once: a verified image wins over a
directory in the same store, and when any image file exists the image
must mount or the launch is refused. The manifest must declare the
requested ID, and both entries must be plain relative paths to regular
files without links. The runtime receives --module and the app shell
--ui; neither looks for the package. Grants come from the same
manifest, and the mount belongs to the session.
The manifest parser and the image file names move to weft-ipc-types,
shared with weft-pack, whose check now applies the same entry rules.
Mapper devices are named after their per-launch mountpoint, so
concurrent sessions no longer collide.
2026-10-09 10:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn entries_outside_the_package_are_refused() {
|
|
|
|
|
let _env = crate::tests::env_lock().blocking_lock();
|
|
|
|
|
let store = store("entry", ID, "../escape.wasm");
|
|
|
|
|
std::fs::write(store.join("escape.wasm"), b"\0asm").unwrap();
|
|
|
|
|
let refused = refusal(resolve(ID));
|
|
|
|
|
finish(&store);
|
|
|
|
|
assert_eq!(refused.code, 403);
|
2026-10-09 10:18:36 +00:00
|
|
|
assert!(
|
|
|
|
|
refused.message.contains("runtime.module"),
|
|
|
|
|
"{}",
|
|
|
|
|
refused.message
|
|
|
|
|
);
|
fix(appd): resolve each launch to one package and fail closed on images
The runtime, the app shell and grant derivation each searched the
package stores and parsed wapp.toml on their own, so they could use
different roots; the runtime ignored [runtime].module, the image loader
looked for companion files the builder never wrote, and a failed image
mount fell back to the mutable directory install.
weft-appd now resolves a launch once: a verified image wins over a
directory in the same store, and when any image file exists the image
must mount or the launch is refused. The manifest must declare the
requested ID, and both entries must be plain relative paths to regular
files without links. The runtime receives --module and the app shell
--ui; neither looks for the package. Grants come from the same
manifest, and the mount belongs to the session.
The manifest parser and the image file names move to weft-ipc-types,
shared with weft-pack, whose check now applies the same entry rules.
Mapper devices are named after their per-launch mountpoint, so
concurrent sessions no longer collide.
2026-10-09 10:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn missing_packages_and_invalid_ids_are_refused() {
|
|
|
|
|
let _env = crate::tests::env_lock().blocking_lock();
|
|
|
|
|
let store = store("missing", ID, "app.wasm");
|
|
|
|
|
let missing = refusal(resolve("org.weft.test.absent"));
|
|
|
|
|
let invalid = refusal(resolve("../etc"));
|
|
|
|
|
finish(&store);
|
|
|
|
|
assert_eq!(missing.code, 404);
|
|
|
|
|
assert_eq!(invalid.code, 400);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn an_image_that_cannot_be_used_never_falls_back_to_the_directory() {
|
|
|
|
|
let _env = crate::tests::env_lock().blocking_lock();
|
|
|
|
|
crate::tests::use_test_runtime_dir();
|
|
|
|
|
let store = store("image", ID, "app.wasm");
|
|
|
|
|
let files = ImageFiles::in_store(&store, ID);
|
|
|
|
|
|
|
|
|
|
// Only the image: its companions are missing.
|
|
|
|
|
std::fs::write(&files.image, b"image").unwrap();
|
|
|
|
|
// SAFETY: as in `store`.
|
|
|
|
|
unsafe { std::env::set_var("WEFT_MOUNT_HELPER", "/bin/true") };
|
|
|
|
|
let partial = refusal(resolve(ID));
|
|
|
|
|
|
|
|
|
|
// All three files, but the helper fails to mount them.
|
|
|
|
|
std::fs::write(&files.hash_tree, b"tree").unwrap();
|
|
|
|
|
std::fs::write(&files.root_hash, "ab".repeat(32)).unwrap();
|
|
|
|
|
unsafe { std::env::set_var("WEFT_MOUNT_HELPER", "/bin/false") };
|
|
|
|
|
let failed = refusal(resolve(ID));
|
|
|
|
|
|
|
|
|
|
// The helper is not installed.
|
|
|
|
|
unsafe { std::env::set_var("WEFT_MOUNT_HELPER", "/nonexistent/weft-mount-helper") };
|
|
|
|
|
let absent = refusal(resolve(ID));
|
|
|
|
|
|
|
|
|
|
// A root hash that is not one.
|
|
|
|
|
std::fs::write(&files.root_hash, "not a hash").unwrap();
|
|
|
|
|
unsafe { std::env::set_var("WEFT_MOUNT_HELPER", "/bin/true") };
|
|
|
|
|
let malformed = refusal(resolve(ID));
|
|
|
|
|
|
|
|
|
|
let mountpoints = std::env::var_os("XDG_RUNTIME_DIR")
|
|
|
|
|
.map(|dir| PathBuf::from(dir).join("weft/mnt"))
|
|
|
|
|
.and_then(|dir| std::fs::read_dir(dir).ok())
|
|
|
|
|
.map_or(0, Iterator::count);
|
|
|
|
|
finish(&store);
|
|
|
|
|
|
|
|
|
|
for refused in [&partial, &failed, &absent, &malformed] {
|
|
|
|
|
assert_eq!(refused.code, 500, "{}", refused.message);
|
2026-10-09 10:18:36 +00:00
|
|
|
assert!(
|
|
|
|
|
refused.message.contains("verified image"),
|
|
|
|
|
"{}",
|
|
|
|
|
refused.message
|
|
|
|
|
);
|
fix(appd): resolve each launch to one package and fail closed on images
The runtime, the app shell and grant derivation each searched the
package stores and parsed wapp.toml on their own, so they could use
different roots; the runtime ignored [runtime].module, the image loader
looked for companion files the builder never wrote, and a failed image
mount fell back to the mutable directory install.
weft-appd now resolves a launch once: a verified image wins over a
directory in the same store, and when any image file exists the image
must mount or the launch is refused. The manifest must declare the
requested ID, and both entries must be plain relative paths to regular
files without links. The runtime receives --module and the app shell
--ui; neither looks for the package. Grants come from the same
manifest, and the mount belongs to the session.
The manifest parser and the image file names move to weft-ipc-types,
shared with weft-pack, whose check now applies the same entry rules.
Mapper devices are named after their per-launch mountpoint, so
concurrent sessions no longer collide.
2026-10-09 10:03:40 +00:00
|
|
|
}
|
|
|
|
|
assert!(partial.message.contains(".app.hash"), "{}", partial.message);
|
|
|
|
|
assert_eq!(mountpoints, 0, "a failed mount left its mountpoint behind");
|
|
|
|
|
}
|
|
|
|
|
}
|