mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
fix(pack): install only trusted packages and bind each app ID to its owner
weft-pack install copied any package into the store: signatures were never checked, no key was trusted for anything, any publisher could install over another's app ID and reach its retained data, and the signing inventory followed symbolic links, so a package could sign and install files from outside itself. Installation now requires a signature by a key in the trust store ($XDG_CONFIG_HOME/weft/trusted-keys and /etc/weft/trusted-keys, or $WEFT_TRUSTED_KEYS alone). Unsigned or untrusted content installs only with --dev, recorded as development content. The package is copied into the store, checked and verified on that copy, and renamed into place, so the verified bytes are the installed bytes. The first installation of an app ID records its owner beside the app data; later installations must have the same owner, so neither another publisher nor a development build can take the ID over. The record outlives uninstall while app data remains. The content digest moves to weft_ipc_types::trust and refuses links, special files and non-UTF-8 paths; it keeps the existing inventory format, and both committed demo signatures still verify. Catalogs list only directories named after their package ID. The security guide described a signature over wapp.toml and app.wasm only; it now describes the full inventory and the trust and ownership rules.
This commit is contained in:
parent
5c82b540e6
commit
88c2e9f6a5
11 changed files with 880 additions and 180 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
|
@ -10519,9 +10519,12 @@ dependencies = [
|
||||||
name = "weft-ipc-types"
|
name = "weft-ipc-types"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"ed25519-dalek",
|
||||||
|
"hex",
|
||||||
"rmp-serde",
|
"rmp-serde",
|
||||||
"rustix 1.1.4",
|
"rustix 1.1.4",
|
||||||
"serde",
|
"serde",
|
||||||
|
"sha2",
|
||||||
"toml 0.8.23",
|
"toml 0.8.23",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web
|
||||||
|
|
||||||
**Runtime** — `weft-runtime` runs WASI Component Model binaries under Wasmtime 30 (`--features wasmtime-runtime`). Provides `weft:app/notify`, `weft:app/ipc`, `weft:app/fetch`, `weft:app/notifications`, and `weft:app/clipboard` host imports. Preopens filesystem paths according to declared capabilities.
|
**Runtime** — `weft-runtime` runs WASI Component Model binaries under Wasmtime 30 (`--features wasmtime-runtime`). Provides `weft:app/notify`, `weft:app/ipc`, `weft:app/fetch`, `weft:app/notifications`, and `weft:app/clipboard` host imports. Preopens filesystem paths according to declared capabilities.
|
||||||
|
|
||||||
**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. App data is stored apart from packages and survives uninstall.
|
**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall.
|
||||||
|
|
||||||
**File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking.
|
**File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -634,6 +634,11 @@ fn scan_installed_apps() -> Vec<AppInfo> {
|
||||||
let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else {
|
let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
// Only a directory named after the ID its manifest declares is an
|
||||||
|
// installed package; staging copies and stray directories are not.
|
||||||
|
if entry.file_name().to_str() != Some(m.package.id.as_str()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if seen.insert(m.package.id.clone()) {
|
if seen.insert(m.package.id.clone()) {
|
||||||
apps.push(AppInfo {
|
apps.push(AppInfo {
|
||||||
app_id: m.package.id,
|
app_id: m.package.id,
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,9 @@ path = "src/lib.rs"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
rmp-serde = "1"
|
rmp-serde = "1"
|
||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
|
sha2 = "0.10"
|
||||||
|
hex = "0.4"
|
||||||
|
ed25519-dalek = "2"
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
pub mod capability;
|
pub mod capability;
|
||||||
pub mod manifest;
|
pub mod manifest;
|
||||||
pub mod package;
|
pub mod package;
|
||||||
|
pub mod trust;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -180,13 +180,13 @@ pub fn migrate_app_data(legacy: &Path, target: &Path) -> Result<Migration, Migra
|
||||||
/// Renames `from` to `to`, failing with `AlreadyExists` instead of replacing
|
/// Renames `from` to `to`, failing with `AlreadyExists` instead of replacing
|
||||||
/// an existing `to`, even one created after the caller checked for it.
|
/// an existing `to`, even one created after the caller checked for it.
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(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<()> {
|
||||||
use rustix::fs::{CWD, RenameFlags, renameat_with};
|
use rustix::fs::{CWD, RenameFlags, renameat_with};
|
||||||
renameat_with(CWD, from, CWD, to, RenameFlags::NOREPLACE).map_err(std::io::Error::from)
|
renameat_with(CWD, from, CWD, to, RenameFlags::NOREPLACE).map_err(std::io::Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "linux"))]
|
#[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() {
|
if std::fs::symlink_metadata(to).is_ok() {
|
||||||
return Err(std::io::ErrorKind::AlreadyExists.into());
|
return Err(std::io::ErrorKind::AlreadyExists.into());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
400
crates/weft-ipc-types/src/trust.rs
Normal file
400
crates/weft-ipc-types/src/trust.rs
Normal file
|
|
@ -0,0 +1,400 @@
|
||||||
|
//! Package content identity, publisher keys and the trust decisions made
|
||||||
|
//! before a package is installed or launched.
|
||||||
|
//!
|
||||||
|
//! A package is signed over its content digest: the SHA-256 of a canonical
|
||||||
|
//! inventory listing every file except the root `signature.sig`, one
|
||||||
|
//! `<relative path>\t<hex sha-256>\n` line per file, sorted by path. Paths use
|
||||||
|
//! `/` and must be UTF-8. Symbolic links and special files are refused, so the
|
||||||
|
//! signed bytes are exactly the files a package holds.
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
|
||||||
|
/// The signature file at the root of a signed package.
|
||||||
|
pub const SIGNATURE_FILE: &str = "signature.sig";
|
||||||
|
|
||||||
|
/// Why a package's content or trust could not be established.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum TrustError {
|
||||||
|
/// The package holds a symbolic link.
|
||||||
|
Link(PathBuf),
|
||||||
|
/// The package holds something other than directories and regular files.
|
||||||
|
NotAFile(PathBuf),
|
||||||
|
/// A path in the package is not UTF-8.
|
||||||
|
NonUtf8(PathBuf),
|
||||||
|
/// A key, signature or owner record is malformed.
|
||||||
|
Malformed(PathBuf, String),
|
||||||
|
Io(PathBuf, std::io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for TrustError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Link(p) => write!(f, "{} is a symbolic link", p.display()),
|
||||||
|
Self::NotAFile(p) => write!(f, "{} is not a regular file or directory", p.display()),
|
||||||
|
Self::NonUtf8(p) => write!(f, "{} is not a UTF-8 path", p.display()),
|
||||||
|
Self::Malformed(p, why) => write!(f, "{}: {why}", p.display()),
|
||||||
|
Self::Io(p, e) => write!(f, "{}: {e}", p.display()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for TrustError {}
|
||||||
|
|
||||||
|
fn io(path: &Path) -> impl FnOnce(std::io::Error) -> TrustError + '_ {
|
||||||
|
move |e| TrustError::Io(path.to_path_buf(), e)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The SHA-256 content digest a package signature covers.
|
||||||
|
pub fn content_digest(package_root: &Path) -> Result<[u8; 32], TrustError> {
|
||||||
|
let mut entries = Vec::new();
|
||||||
|
collect(package_root, package_root, &mut entries)?;
|
||||||
|
entries.sort();
|
||||||
|
let mut inventory = String::new();
|
||||||
|
for (path, hash) in &entries {
|
||||||
|
inventory.push_str(path);
|
||||||
|
inventory.push('\t');
|
||||||
|
inventory.push_str(&hex::encode(hash));
|
||||||
|
inventory.push('\n');
|
||||||
|
}
|
||||||
|
Ok(Sha256::digest(inventory.as_bytes()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn collect(
|
||||||
|
root: &Path,
|
||||||
|
dir: &Path,
|
||||||
|
entries: &mut Vec<(String, [u8; 32])>,
|
||||||
|
) -> Result<(), TrustError> {
|
||||||
|
for entry in std::fs::read_dir(dir).map_err(io(dir))? {
|
||||||
|
let path = entry.map_err(io(dir))?.path();
|
||||||
|
let relative = path
|
||||||
|
.strip_prefix(root)
|
||||||
|
.expect("read_dir yields children of the directory it reads")
|
||||||
|
.to_str()
|
||||||
|
.ok_or_else(|| TrustError::NonUtf8(path.clone()))?
|
||||||
|
.to_owned();
|
||||||
|
let kind = std::fs::symlink_metadata(&path)
|
||||||
|
.map_err(io(&path))?
|
||||||
|
.file_type();
|
||||||
|
if kind.is_symlink() {
|
||||||
|
return Err(TrustError::Link(path));
|
||||||
|
} else if kind.is_dir() {
|
||||||
|
collect(root, &path, entries)?;
|
||||||
|
} else if !kind.is_file() {
|
||||||
|
return Err(TrustError::NotAFile(path));
|
||||||
|
} else if relative != SIGNATURE_FILE {
|
||||||
|
let bytes = std::fs::read(&path).map_err(io(&path))?;
|
||||||
|
entries.push((relative, Sha256::digest(&bytes).into()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A publisher's Ed25519 public key.
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct PublisherKey(VerifyingKey);
|
||||||
|
|
||||||
|
impl fmt::Debug for PublisherKey {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "PublisherKey({})", self.to_hex())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PublisherKey {
|
||||||
|
/// Parses 64 hex digits, the format `weft-pack generate-key` writes.
|
||||||
|
pub fn from_hex(text: &str) -> Result<Self, String> {
|
||||||
|
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<Self, TrustError> {
|
||||||
|
let text = std::fs::read_to_string(path).map_err(io(path))?;
|
||||||
|
Self::from_hex(&text).map_err(|why| TrustError::Malformed(path.to_path_buf(), why))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The key as 64 lowercase hex digits; it also identifies the publisher.
|
||||||
|
pub fn to_hex(&self) -> String {
|
||||||
|
hex::encode(self.0.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether `signature` signs `digest` with this key.
|
||||||
|
pub fn signed(&self, digest: &[u8; 32], signature: &Signature) -> bool {
|
||||||
|
self.0.verify(digest, signature).is_ok()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads the package's signature, or `None` when the package is unsigned.
|
||||||
|
pub fn read_signature(package_root: &Path) -> Result<Option<Signature>, 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<PublisherKey>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<PathBuf> {
|
||||||
|
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<PathBuf> = 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<Self, TrustError> {
|
||||||
|
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<PathBuf> = entries
|
||||||
|
.map(|e| e.map(|e| e.path()).map_err(io(dir)))
|
||||||
|
.collect::<Result<_, _>>()?;
|
||||||
|
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<Option<PublisherKey>, TrustError> {
|
||||||
|
let Some(signature) = read_signature(package_root)? else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
let digest = content_digest(package_root)?;
|
||||||
|
Ok(self
|
||||||
|
.keys
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.find(|k| k.signed(&digest, &signature)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Who an application ID belongs to on this host. The record outlives the
|
||||||
|
/// package while the app's data remains, so another publisher cannot take
|
||||||
|
/// over the ID, and the data with it, by reusing it.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Owner {
|
||||||
|
/// Installed from packages signed by this trusted key.
|
||||||
|
Verified(PublisherKey),
|
||||||
|
/// Installed as development content, without a trusted signature.
|
||||||
|
Development,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Owner {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Verified(key) => write!(f, "publisher {}", key.to_hex()),
|
||||||
|
Self::Development => f.write_str("development installation"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Where the owner of `app_id` is recorded, beside the app's data.
|
||||||
|
pub fn owner_record_path(data_home: &Path, app_id: &str) -> PathBuf {
|
||||||
|
data_home.join("weft/owners").join(app_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads an owner record: `verified <key>` or `development`.
|
||||||
|
pub fn read_owner(record: &Path) -> Result<Option<Owner>, 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 <key>' or 'development'")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Records the owner of an app ID. An existing record is never replaced.
|
||||||
|
pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> {
|
||||||
|
use std::io::Write;
|
||||||
|
let parent = record.parent().expect("owner records live in a directory");
|
||||||
|
std::fs::create_dir_all(parent).map_err(io(parent))?;
|
||||||
|
let text = match owner {
|
||||||
|
Owner::Verified(key) => format!("verified {}\n", key.to_hex()),
|
||||||
|
Owner::Development => "development\n".to_owned(),
|
||||||
|
};
|
||||||
|
let mut file = std::fs::OpenOptions::new()
|
||||||
|
.write(true)
|
||||||
|
.create_new(true)
|
||||||
|
.open(record)
|
||||||
|
.map_err(io(record))?;
|
||||||
|
file.write_all(text.as_bytes()).map_err(io(record))?;
|
||||||
|
file.sync_all().map_err(io(record))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use ed25519_dalek::{Signer, SigningKey};
|
||||||
|
|
||||||
|
fn dir(name: &str) -> PathBuf {
|
||||||
|
let dir = std::env::temp_dir().join(format!("weft_trust_{name}_{}", std::process::id()));
|
||||||
|
let _ = std::fs::remove_dir_all(&dir);
|
||||||
|
std::fs::create_dir_all(dir.join("ui")).unwrap();
|
||||||
|
std::fs::write(dir.join("wapp.toml"), b"[package]\n").unwrap();
|
||||||
|
std::fs::write(dir.join("ui/index.html"), b"<p>").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"<p>changed").unwrap();
|
||||||
|
assert_eq!(store(&[key]).signer(&dir).unwrap(), None);
|
||||||
|
std::fs::write(dir.join("ui/index.html"), b"<p>").unwrap();
|
||||||
|
std::fs::write(dir.join("ui/extra.js"), b"").unwrap();
|
||||||
|
assert_eq!(store(&[key]).signer(&dir).unwrap(), None);
|
||||||
|
std::fs::remove_dir_all(&dir).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn only_trusted_keys_are_signers_and_unsigned_packages_have_none() {
|
||||||
|
let dir = dir("untrusted");
|
||||||
|
assert_eq!(store(&[]).signer(&dir).unwrap(), None);
|
||||||
|
let key = sign(&dir, 2);
|
||||||
|
let other = PublisherKey(SigningKey::from_bytes(&[3; 32]).verifying_key());
|
||||||
|
assert_eq!(store(&[other]).signer(&dir).unwrap(), None);
|
||||||
|
assert_eq!(store(&[other, key]).signer(&dir).unwrap(), Some(key));
|
||||||
|
std::fs::remove_dir_all(&dir).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
#[test]
|
||||||
|
fn links_and_special_files_have_no_content_digest() {
|
||||||
|
let dir = dir("links");
|
||||||
|
std::os::unix::fs::symlink("/etc/hostname", dir.join("ui/linked")).unwrap();
|
||||||
|
assert!(matches!(content_digest(&dir), Err(TrustError::Link(_))));
|
||||||
|
std::fs::remove_file(dir.join("ui/linked")).unwrap();
|
||||||
|
std::os::unix::fs::symlink(dir.join("ui"), dir.join("view")).unwrap();
|
||||||
|
assert!(matches!(content_digest(&dir), Err(TrustError::Link(_))));
|
||||||
|
std::fs::remove_file(dir.join("view")).unwrap();
|
||||||
|
rustix::fs::mknodat(
|
||||||
|
rustix::fs::CWD,
|
||||||
|
dir.join("fifo"),
|
||||||
|
rustix::fs::FileType::Fifo,
|
||||||
|
rustix::fs::Mode::from_raw_mode(0o600),
|
||||||
|
0,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(content_digest(&dir), Err(TrustError::NotAFile(_))));
|
||||||
|
std::fs::remove_dir_all(&dir).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trust_stores_load_pub_files_and_refuse_malformed_keys() {
|
||||||
|
let dir = dir("store");
|
||||||
|
let keys = dir.join("keys");
|
||||||
|
std::fs::create_dir_all(&keys).unwrap();
|
||||||
|
let key = PublisherKey(SigningKey::from_bytes(&[4; 32]).verifying_key());
|
||||||
|
std::fs::write(keys.join("a.pub"), key.to_hex()).unwrap();
|
||||||
|
std::fs::write(keys.join("notes.txt"), "not a key").unwrap();
|
||||||
|
let loaded = TrustStore::load(&[keys.clone(), dir.join("missing")]).unwrap();
|
||||||
|
assert_eq!(loaded.keys(), [key]);
|
||||||
|
std::fs::write(keys.join("b.pub"), "zz").unwrap();
|
||||||
|
assert!(matches!(
|
||||||
|
TrustStore::load(&[keys]),
|
||||||
|
Err(TrustError::Malformed(..))
|
||||||
|
));
|
||||||
|
std::fs::remove_dir_all(&dir).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn owner_records_round_trip_and_are_never_replaced() {
|
||||||
|
let dir = dir("owners");
|
||||||
|
let key = PublisherKey(SigningKey::from_bytes(&[5; 32]).verifying_key());
|
||||||
|
let verified = owner_record_path(&dir, "org.weft.test.a");
|
||||||
|
let development = owner_record_path(&dir, "org.weft.test.b");
|
||||||
|
assert_eq!(read_owner(&verified).unwrap(), None);
|
||||||
|
write_owner(&verified, Owner::Verified(key)).unwrap();
|
||||||
|
write_owner(&development, Owner::Development).unwrap();
|
||||||
|
assert_eq!(read_owner(&verified).unwrap(), Some(Owner::Verified(key)));
|
||||||
|
assert_eq!(read_owner(&development).unwrap(), Some(Owner::Development));
|
||||||
|
assert!(write_owner(&verified, Owner::Development).is_err());
|
||||||
|
assert_eq!(read_owner(&verified).unwrap(), Some(Owner::Verified(key)));
|
||||||
|
std::fs::write(&development, "someone else\n").unwrap();
|
||||||
|
assert!(read_owner(&development).is_err());
|
||||||
|
std::fs::remove_dir_all(&dir).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ use std::path::{Path, PathBuf};
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use weft_ipc_types::manifest::{Manifest, entry_path};
|
use weft_ipc_types::manifest::{Manifest, entry_path};
|
||||||
use weft_ipc_types::package::ImageFiles;
|
use weft_ipc_types::package::ImageFiles;
|
||||||
|
use weft_ipc_types::trust::{Owner, PublisherKey, TrustStore};
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
|
@ -19,8 +20,14 @@ fn main() -> anyhow::Result<()> {
|
||||||
print_info(&manifest);
|
print_info(&manifest);
|
||||||
}
|
}
|
||||||
Some("install") => {
|
Some("install") => {
|
||||||
let dir = args.get(2).context("usage: weft-pack install <dir>")?;
|
let usage = "usage: weft-pack install <dir|archive> [--dev]";
|
||||||
install_package(Path::new(dir))?;
|
let dir = args.get(2).context(usage)?;
|
||||||
|
let mode = match args.get(3).map(String::as_str) {
|
||||||
|
None => InstallMode::Verified,
|
||||||
|
Some("--dev") => InstallMode::Development,
|
||||||
|
Some(other) => anyhow::bail!("unexpected argument '{other}'; {usage}"),
|
||||||
|
};
|
||||||
|
install_package(Path::new(dir), mode)?;
|
||||||
}
|
}
|
||||||
Some("uninstall") => {
|
Some("uninstall") => {
|
||||||
let app_id = args.get(2).context("usage: weft-pack uninstall <app_id>")?;
|
let app_id = args.get(2).context("usage: weft-pack uninstall <app_id>")?;
|
||||||
|
|
@ -86,7 +93,7 @@ fn main() -> anyhow::Result<()> {
|
||||||
.find(|w| w[0] == "--key")
|
.find(|w| w[0] == "--key")
|
||||||
.map(|w| &w[1])
|
.map(|w| &w[1])
|
||||||
.context("missing --key <pubkeyfile>")?;
|
.context("missing --key <pubkeyfile>")?;
|
||||||
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 {
|
if ok {
|
||||||
println!("OK");
|
println!("OK");
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -98,7 +105,12 @@ fn main() -> anyhow::Result<()> {
|
||||||
eprintln!("usage:");
|
eprintln!("usage:");
|
||||||
eprintln!(" weft-pack check <dir> validate a package directory");
|
eprintln!(" weft-pack check <dir> validate a package directory");
|
||||||
eprintln!(" weft-pack info <dir> print package metadata");
|
eprintln!(" weft-pack info <dir> print package metadata");
|
||||||
eprintln!(" weft-pack install <dir> install package to app store");
|
eprintln!(
|
||||||
|
" weft-pack install <dir> [--dev] install a package signed by a trusted key"
|
||||||
|
);
|
||||||
|
eprintln!(
|
||||||
|
" (--dev: unsigned development content)"
|
||||||
|
);
|
||||||
eprintln!(" weft-pack uninstall <app_id> remove installed package");
|
eprintln!(" weft-pack uninstall <app_id> remove installed package");
|
||||||
eprintln!(" weft-pack list list installed packages");
|
eprintln!(" weft-pack list list installed packages");
|
||||||
eprintln!(
|
eprintln!(
|
||||||
|
|
@ -128,6 +140,12 @@ fn check_package(dir: &Path) -> anyhow::Result<String> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 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 `<package>/data`; the name stays
|
// Earlier versions kept app data in `<package>/data`; the name stays
|
||||||
// reserved so package content is never mistaken for user data.
|
// reserved so package content is never mistaken for user data.
|
||||||
if std::fs::symlink_metadata(dir.join("data")).is_ok() {
|
if std::fs::symlink_metadata(dir.join("data")).is_ok() {
|
||||||
|
|
@ -229,12 +247,22 @@ fn resolve_install_root() -> anyhow::Result<PathBuf> {
|
||||||
anyhow::bail!("cannot determine install root: HOME and WEFT_APP_STORE are both unset")
|
anyhow::bail!("cannot determine install root: HOME and WEFT_APP_STORE are both unset")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn install_package(path: &Path) -> anyhow::Result<()> {
|
/// How a package is admitted to the store.
|
||||||
let root = resolve_install_root()?;
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
install_package_to(path, &root)
|
enum InstallMode {
|
||||||
|
/// The package must be signed by a key in the trust store.
|
||||||
|
Verified,
|
||||||
|
/// Unsigned or untrusted content, chosen explicitly by a developer and
|
||||||
|
/// recorded as development content.
|
||||||
|
Development,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> {
|
fn install_package(path: &Path, mode: InstallMode) -> anyhow::Result<()> {
|
||||||
|
let root = resolve_install_root()?;
|
||||||
|
install_package_to(path, &root, mode)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> {
|
||||||
let dir_buf;
|
let dir_buf;
|
||||||
let dir: &Path = if path.extension().is_some_and(|e| e == "zst" || e == "tar")
|
let dir: &Path = if path.extension().is_some_and(|e| e == "zst" || e == "tar")
|
||||||
|| path.to_string_lossy().ends_with(".app.tar.zst")
|
|| path.to_string_lossy().ends_with(".app.tar.zst")
|
||||||
|
|
@ -280,12 +308,78 @@ fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> {
|
||||||
dest.display()
|
dest.display()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
copy_dir(dir, &dest)
|
|
||||||
.with_context(|| format!("copy {} -> {}", dir.display(), dest.display()))?;
|
// The package is copied into the store first and every decision is made
|
||||||
println!("installed {} -> {}", app_id, dest.display());
|
// on that copy, so the bytes that are checked and trusted are the bytes
|
||||||
|
// that become installed. Only then does one rename make it available.
|
||||||
|
std::fs::create_dir_all(store_root)
|
||||||
|
.with_context(|| format!("create {}", store_root.display()))?;
|
||||||
|
let staging = store_root.join(format!(
|
||||||
|
".staging-{app_id}-{}",
|
||||||
|
hex::encode(rand::random::<[u8; 8]>())
|
||||||
|
));
|
||||||
|
let staged = copy_dir(dir, &staging)
|
||||||
|
.with_context(|| format!("copy {} -> {}", dir.display(), staging.display()))
|
||||||
|
.and_then(|()| admit(&staging, app_id, mode))
|
||||||
|
.and_then(|owner| {
|
||||||
|
weft_ipc_types::package::rename_no_replace(&staging, &dest)
|
||||||
|
.with_context(|| format!("move {} -> {}", staging.display(), dest.display()))?;
|
||||||
|
Ok(owner)
|
||||||
|
});
|
||||||
|
let owner = match staged {
|
||||||
|
Ok(owner) => owner,
|
||||||
|
Err(e) => {
|
||||||
|
let _ = std::fs::remove_dir_all(&staging);
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
println!("installed {} -> {} ({owner})", app_id, dest.display());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks the staged copy of a package and establishes who owns its app ID:
|
||||||
|
/// the trusted publisher that signed it, or, only when asked, development.
|
||||||
|
/// The first installation of an ID records its owner, and every later one
|
||||||
|
/// must have the same owner.
|
||||||
|
fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result<Owner> {
|
||||||
|
use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner};
|
||||||
|
check_package(staged)?;
|
||||||
|
let manifest = load_manifest(staged)?;
|
||||||
|
anyhow::ensure!(
|
||||||
|
manifest.package.id == app_id,
|
||||||
|
"the package changed while it was copied"
|
||||||
|
);
|
||||||
|
let owner = match mode {
|
||||||
|
InstallMode::Development => Owner::Development,
|
||||||
|
InstallMode::Verified => {
|
||||||
|
let dirs = TrustStore::directories();
|
||||||
|
let signer = TrustStore::load(&dirs)?.signer(staged)?;
|
||||||
|
Owner::Verified(signer.with_context(|| {
|
||||||
|
format!(
|
||||||
|
"{app_id} is not signed by a trusted key (trusted keys are read from {}); \
|
||||||
|
install it with --dev to use it as development content",
|
||||||
|
dirs.iter()
|
||||||
|
.map(|d| d.display().to_string())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
)
|
||||||
|
})?)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let data_home = weft_ipc_types::package::data_home()
|
||||||
|
.context("cannot locate the data home to record who owns the app ID")?;
|
||||||
|
let record = owner_record_path(&data_home, app_id);
|
||||||
|
match read_owner(&record)? {
|
||||||
|
Some(existing) if existing == owner => {}
|
||||||
|
Some(existing) => anyhow::bail!(
|
||||||
|
"{app_id} belongs to {existing}; this package is {owner}. Its app data stays with \
|
||||||
|
its owner, so another publisher or a development build cannot take the ID over"
|
||||||
|
),
|
||||||
|
None => write_owner(&record, owner)?,
|
||||||
|
}
|
||||||
|
Ok(owner)
|
||||||
|
}
|
||||||
|
|
||||||
fn uninstall_package(app_id: &str) -> anyhow::Result<()> {
|
fn uninstall_package(app_id: &str) -> anyhow::Result<()> {
|
||||||
let root = resolve_install_root()?;
|
let root = resolve_install_root()?;
|
||||||
uninstall_package_from(app_id, &root)
|
uninstall_package_from(app_id, &root)
|
||||||
|
|
@ -326,6 +420,14 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()>
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
// The owner record keeps the app ID, and the data with it, for its owner
|
||||||
|
// while the data remains; with no data left, the ID is free again.
|
||||||
|
if let Some(data_home) = weft_ipc_types::package::data_home()
|
||||||
|
&& std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(&data_home, app_id))
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
let _ = std::fs::remove_file(weft_ipc_types::trust::owner_record_path(&data_home, app_id));
|
||||||
|
}
|
||||||
println!("uninstalled {}", app_id);
|
println!("uninstalled {}", app_id);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
@ -416,6 +518,11 @@ fn list_installed() {
|
||||||
let Ok(m) = Manifest::read(&entry.path()) else {
|
let Ok(m) = Manifest::read(&entry.path()) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
// Only a directory named after the ID its manifest declares is an
|
||||||
|
// installed package; staging copies and stray directories are not.
|
||||||
|
if entry.file_name().to_str() != Some(m.package.id.as_str()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if seen.insert(m.package.id.clone()) {
|
if seen.insert(m.package.id.clone()) {
|
||||||
pkgs.push((m.package.id, m.package.name, m.package.version));
|
pkgs.push((m.package.id, m.package.name, m.package.version));
|
||||||
}
|
}
|
||||||
|
|
@ -431,17 +538,26 @@ fn list_installed() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Copies a package directory. Packages hold only directories and regular
|
||||||
|
/// files, so a symbolic link or special file stops the copy rather than
|
||||||
|
/// being followed.
|
||||||
fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> {
|
fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> {
|
||||||
std::fs::create_dir_all(dst)?;
|
std::fs::create_dir(dst).with_context(|| format!("create {}", dst.display()))?;
|
||||||
for entry in std::fs::read_dir(src)? {
|
for entry in std::fs::read_dir(src)? {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
let src_path = entry.path();
|
let src_path = entry.path();
|
||||||
let dst_path = dst.join(entry.file_name());
|
let dst_path = dst.join(entry.file_name());
|
||||||
if src_path.is_dir() {
|
let kind = std::fs::symlink_metadata(&src_path)?.file_type();
|
||||||
|
if kind.is_dir() {
|
||||||
copy_dir(&src_path, &dst_path)?;
|
copy_dir(&src_path, &dst_path)?;
|
||||||
} else {
|
} else if kind.is_file() {
|
||||||
std::fs::copy(&src_path, &dst_path)
|
std::fs::copy(&src_path, &dst_path)
|
||||||
.with_context(|| format!("copy {}", src_path.display()))?;
|
.with_context(|| format!("copy {}", src_path.display()))?;
|
||||||
|
} else {
|
||||||
|
anyhow::bail!(
|
||||||
|
"{} is not a regular file or directory; packages cannot contain links",
|
||||||
|
src_path.display()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -540,47 +656,6 @@ fn unbundle_package(archive: &Path, out_dir: &Path) -> anyhow::Result<()> {
|
||||||
Ok(())
|
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<()> {
|
fn generate_key(output_dir: &Path) -> anyhow::Result<()> {
|
||||||
use ed25519_dalek::SigningKey;
|
use ed25519_dalek::SigningKey;
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
|
|
@ -614,34 +689,20 @@ fn sign_package(dir: &Path, key_file: &Path) -> anyhow::Result<()> {
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| anyhow::anyhow!("signing key must be 32 bytes"))?;
|
.map_err(|_| anyhow::anyhow!("signing key must be 32 bytes"))?;
|
||||||
let signing_key = SigningKey::from_bytes(&key_bytes);
|
let signing_key = SigningKey::from_bytes(&key_bytes);
|
||||||
let hash = canonical_bundle_hash(dir)?;
|
let digest = weft_ipc_types::trust::content_digest(dir)?;
|
||||||
let signature = signing_key.sign(&hash);
|
let signature = signing_key.sign(&digest);
|
||||||
let sig_path = dir.join("signature.sig");
|
let sig_path = dir.join(weft_ipc_types::trust::SIGNATURE_FILE);
|
||||||
std::fs::write(&sig_path, hex::encode(signature.to_bytes()))
|
std::fs::write(&sig_path, hex::encode(signature.to_bytes()))
|
||||||
.with_context(|| format!("write {}", sig_path.display()))?;
|
.with_context(|| format!("write {}", sig_path.display()))?;
|
||||||
println!("signed: {}", sig_path.display());
|
println!("signed: {}", sig_path.display());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn verify_package(dir: &Path, pub_key_file: &Path) -> anyhow::Result<bool> {
|
fn verify_package(dir: &Path, key: &PublisherKey) -> anyhow::Result<bool> {
|
||||||
use ed25519_dalek::{Signature, Verifier, VerifyingKey};
|
let signature = weft_ipc_types::trust::read_signature(dir)?
|
||||||
let pub_hex = std::fs::read_to_string(pub_key_file)
|
.with_context(|| format!("{} is not signed", dir.display()))?;
|
||||||
.with_context(|| format!("read {}", pub_key_file.display()))?;
|
let digest = weft_ipc_types::trust::content_digest(dir)?;
|
||||||
let pub_bytes: [u8; 32] = hex::decode(pub_hex.trim())
|
Ok(key.signed(&digest, &signature))
|
||||||
.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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
@ -704,13 +765,187 @@ mod tests {
|
||||||
|
|
||||||
const APP_DATA: &str = "\"fs:rw:app-data\"";
|
const APP_DATA: &str = "\"fs:rw:app-data\"";
|
||||||
|
|
||||||
|
/// Runs `f` like `with_home`, with only the keys in `home/keys` trusted.
|
||||||
|
fn with_trust<T>(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/<name>.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<Owner> {
|
||||||
|
weft_ipc_types::trust::read_owner(&weft_ipc_types::trust::owner_record_path(
|
||||||
|
&home.join("share"),
|
||||||
|
app_id,
|
||||||
|
))
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn staging_left(store: &Path) -> bool {
|
||||||
|
std::fs::read_dir(store).is_ok_and(|entries| {
|
||||||
|
entries
|
||||||
|
.flatten()
|
||||||
|
.any(|e| e.file_name().to_string_lossy().starts_with(".staging-"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn only_packages_signed_by_a_trusted_key_install_as_verified() {
|
||||||
|
let home = temp_root("verified");
|
||||||
|
let store = home.join("store");
|
||||||
|
let app_id = "org.weft.test.verified";
|
||||||
|
write_package(&home.join("src"), app_id, "");
|
||||||
|
let publisher = key(&home, "publisher", 1, true);
|
||||||
|
let stranger = key(&home, "stranger", 2, false);
|
||||||
|
|
||||||
|
// Unsigned, then signed by a key the host does not trust.
|
||||||
|
let unsigned = with_trust(&home, || {
|
||||||
|
install_package_to(&home.join("src"), &store, InstallMode::Verified)
|
||||||
|
});
|
||||||
|
sign_package(&home.join("src"), &stranger).unwrap();
|
||||||
|
let untrusted = with_trust(&home, || {
|
||||||
|
install_package_to(&home.join("src"), &store, InstallMode::Verified)
|
||||||
|
});
|
||||||
|
for refused in [&unsigned, &untrusted] {
|
||||||
|
let message = format!("{:#}", refused.as_ref().unwrap_err());
|
||||||
|
assert!(message.contains("not signed by a trusted key"), "{message}");
|
||||||
|
}
|
||||||
|
assert!(!store.join(app_id).exists());
|
||||||
|
assert!(!staging_left(&store));
|
||||||
|
assert_eq!(owner_of(&home, app_id), None);
|
||||||
|
|
||||||
|
// Signed by a trusted key.
|
||||||
|
sign_package(&home.join("src"), &publisher).unwrap();
|
||||||
|
with_trust(&home, || {
|
||||||
|
install_package_to(&home.join("src"), &store, InstallMode::Verified)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
assert!(store.join(app_id).join("signature.sig").exists());
|
||||||
|
let key = PublisherKey::from_file(&home.join("keys/publisher.pub")).unwrap();
|
||||||
|
assert_eq!(owner_of(&home, app_id), Some(Owner::Verified(key)));
|
||||||
|
assert!(!staging_left(&store));
|
||||||
|
let _ = std::fs::remove_dir_all(&home);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn content_changed_after_signing_is_refused() {
|
||||||
|
let home = temp_root("tampered");
|
||||||
|
let store = home.join("store");
|
||||||
|
let app_id = "org.weft.test.tampered";
|
||||||
|
write_package(&home.join("src"), app_id, "");
|
||||||
|
sign_package(&home.join("src"), &key(&home, "publisher", 3, true)).unwrap();
|
||||||
|
std::fs::write(home.join("src/ui/index.html"), "<script>changed</script>").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]
|
#[test]
|
||||||
fn uninstall_keeps_app_data_from_the_user_store() {
|
fn uninstall_keeps_app_data_from_the_user_store() {
|
||||||
let home = temp_root("keep");
|
let home = temp_root("keep");
|
||||||
let app_id = "com.example.keep";
|
let app_id = "com.example.keep";
|
||||||
write_package(&home.join("src"), app_id, APP_DATA);
|
write_package(&home.join("src"), app_id, APP_DATA);
|
||||||
let store = home.join(".local/share/weft/apps");
|
let store = home.join(".local/share/weft/apps");
|
||||||
install_package_to(&home.join("src"), &store).unwrap();
|
with_home(&home, || {
|
||||||
|
install_package_to(&home.join("src"), &store, InstallMode::Development)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
let legacy = store.join(app_id).join("data");
|
let legacy = store.join(app_id).join("data");
|
||||||
std::fs::create_dir_all(&legacy).unwrap();
|
std::fs::create_dir_all(&legacy).unwrap();
|
||||||
std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap();
|
std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap();
|
||||||
|
|
@ -730,7 +965,10 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
// A second copy cannot be merged: uninstall refuses and removes nothing.
|
// A second copy cannot be merged: uninstall refuses and removes nothing.
|
||||||
install_package_to(&home.join("src"), &store).unwrap();
|
with_home(&home, || {
|
||||||
|
install_package_to(&home.join("src"), &store, InstallMode::Development)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
std::fs::create_dir_all(&legacy).unwrap();
|
std::fs::create_dir_all(&legacy).unwrap();
|
||||||
assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err());
|
assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err());
|
||||||
assert!(legacy.is_dir() && kept.is_dir());
|
assert!(legacy.is_dir() && kept.is_dir());
|
||||||
|
|
@ -744,7 +982,10 @@ mod tests {
|
||||||
// No app-data capability: the data directory is left and uninstall stops.
|
// No app-data capability: the data directory is left and uninstall stops.
|
||||||
write_package(&home.join("src"), app_id, "");
|
write_package(&home.join("src"), app_id, "");
|
||||||
let store = home.join(".local/share/weft/apps");
|
let store = home.join(".local/share/weft/apps");
|
||||||
install_package_to(&home.join("src"), &store).unwrap();
|
with_home(&home, || {
|
||||||
|
install_package_to(&home.join("src"), &store, InstallMode::Development)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
std::fs::create_dir_all(store.join(app_id).join("data")).unwrap();
|
std::fs::create_dir_all(store.join(app_id).join("data")).unwrap();
|
||||||
std::fs::write(store.join(app_id).join("data/x"), "x").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());
|
assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err());
|
||||||
|
|
@ -757,11 +998,14 @@ mod tests {
|
||||||
// Another store: earlier versions never kept data there, so it stops too.
|
// Another store: earlier versions never kept data there, so it stops too.
|
||||||
write_package(&home.join("src2"), app_id, APP_DATA);
|
write_package(&home.join("src2"), app_id, APP_DATA);
|
||||||
let other = home.join("other-store");
|
let other = home.join("other-store");
|
||||||
install_package_to(&home.join("src2"), &other).unwrap();
|
with_home(&home, || {
|
||||||
|
install_package_to(&home.join("src2"), &other, InstallMode::Development)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
std::fs::create_dir_all(other.join(app_id).join("data")).unwrap();
|
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!(with_home(&home, || uninstall_package_from(app_id, &other)).is_err());
|
||||||
assert!(other.join(app_id).join("data").is_dir());
|
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);
|
let _ = std::fs::remove_dir_all(&home);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -808,15 +1052,21 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn packages_cannot_ship_a_top_level_data_entry() {
|
fn packages_cannot_ship_a_top_level_data_entry() {
|
||||||
let root = temp_root("ships-data");
|
let home = temp_root("packages_cannot_ship_a_top_level_data_entry");
|
||||||
let src = root.join("src");
|
with_home(&home.clone(), move || {
|
||||||
write_package(&src, "com.example.ships", "");
|
let root = temp_root("ships-data");
|
||||||
std::fs::create_dir_all(src.join("data")).unwrap();
|
let src = root.join("src");
|
||||||
std::fs::write(src.join("data/asset.json"), "{}").unwrap();
|
write_package(&src, "com.example.ships", "");
|
||||||
let err = check_package(&src).unwrap_err().to_string();
|
std::fs::create_dir_all(src.join("data")).unwrap();
|
||||||
assert!(err.contains("'data'"), "{err}");
|
std::fs::write(src.join("data/asset.json"), "{}").unwrap();
|
||||||
assert!(install_package_to(&src, &root.join("store")).is_err());
|
let err = check_package(&src).unwrap_err().to_string();
|
||||||
let _ = std::fs::remove_dir_all(&root);
|
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]
|
#[test]
|
||||||
|
|
@ -828,7 +1078,10 @@ mod tests {
|
||||||
std::fs::create_dir_all(store.join(app_id).join("data")).unwrap();
|
std::fs::create_dir_all(store.join(app_id).join("data")).unwrap();
|
||||||
std::fs::write(store.join(app_id).join("data/x"), "x").unwrap();
|
std::fs::write(store.join(app_id).join("data/x"), "x").unwrap();
|
||||||
|
|
||||||
with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap();
|
with_home(&home, || {
|
||||||
|
install_package_to(&home.join("src"), &store, InstallMode::Development)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
assert!(store.join(app_id).join("wapp.toml").exists());
|
assert!(store.join(app_id).join("wapp.toml").exists());
|
||||||
assert!(!store.join(app_id).join("data").exists());
|
assert!(!store.join(app_id).join("data").exists());
|
||||||
let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id);
|
let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id);
|
||||||
|
|
@ -837,7 +1090,10 @@ mod tests {
|
||||||
// An empty directory left behind does not block installation either.
|
// An empty directory left behind does not block installation either.
|
||||||
with_home(&home, || uninstall_package_from(app_id, &store)).unwrap();
|
with_home(&home, || uninstall_package_from(app_id, &store)).unwrap();
|
||||||
std::fs::create_dir_all(store.join(app_id)).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);
|
let _ = std::fs::remove_dir_all(&home);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -970,60 +1226,68 @@ entry = "ui/index.html"
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn install_package_copies_to_store() {
|
fn install_package_copies_to_store() {
|
||||||
use std::fs;
|
let home = temp_root("install_package_copies_to_store");
|
||||||
let id = format!("weft.pack.install{}", std::process::id());
|
with_home(&home.clone(), move || {
|
||||||
let src = std::env::temp_dir().join(format!("weft_pack_install_src_{}", id));
|
use std::fs;
|
||||||
let store = std::env::temp_dir().join(format!("weft_pack_install_store_{}", id));
|
let id = format!("weft.pack.install{}", std::process::id());
|
||||||
let ui_dir = src.join("ui");
|
let src = std::env::temp_dir().join(format!("weft_pack_install_src_{}", id));
|
||||||
let _ = fs::create_dir_all(&ui_dir);
|
let store = std::env::temp_dir().join(format!("weft_pack_install_store_{}", id));
|
||||||
fs::write(src.join("app.wasm"), b"\0asm").unwrap();
|
let ui_dir = src.join("ui");
|
||||||
fs::write(ui_dir.join("index.html"), b"<!DOCTYPE html>").unwrap();
|
let _ = fs::create_dir_all(&ui_dir);
|
||||||
let app_id = format!("com.example.t{}", std::process::id());
|
fs::write(src.join("app.wasm"), b"\0asm").unwrap();
|
||||||
fs::write(
|
fs::write(ui_dir.join("index.html"), b"<!DOCTYPE html>").unwrap();
|
||||||
src.join("wapp.toml"),
|
let app_id = format!("com.example.t{}", std::process::id());
|
||||||
format!(
|
fs::write(
|
||||||
"[package]\nid = \"{app_id}\"\nname = \"Test\"\nversion = \"1.0.0\"\n\n\
|
src.join("wapp.toml"),
|
||||||
[runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n"
|
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:?}");
|
.unwrap();
|
||||||
assert!(store.join(&app_id).join("app.wasm").exists());
|
let result = install_package_to(&src, &store, InstallMode::Development);
|
||||||
assert!(store.join(&app_id).join("wapp.toml").exists());
|
assert!(result.is_ok(), "{result:?}");
|
||||||
assert!(store.join(&app_id).join("ui").join("index.html").exists());
|
assert!(store.join(&app_id).join("app.wasm").exists());
|
||||||
assert!(install_package_to(&src, &store).is_err());
|
assert!(store.join(&app_id).join("wapp.toml").exists());
|
||||||
let _ = fs::remove_dir_all(&src);
|
assert!(store.join(&app_id).join("ui").join("index.html").exists());
|
||||||
let _ = fs::remove_dir_all(&store);
|
assert!(install_package_to(&src, &store, InstallMode::Development).is_err());
|
||||||
|
let _ = fs::remove_dir_all(&src);
|
||||||
|
let _ = fs::remove_dir_all(&store);
|
||||||
|
});
|
||||||
|
let _ = std::fs::remove_dir_all(&home);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn uninstall_package_removes_directory() {
|
fn uninstall_package_removes_directory() {
|
||||||
use std::fs;
|
let home = temp_root("uninstall_package_removes_directory");
|
||||||
let id = format!("weft.pack.uninstall{}", std::process::id());
|
with_home(&home.clone(), move || {
|
||||||
let src = std::env::temp_dir().join(format!("weft_pack_uninstall_src_{}", id));
|
use std::fs;
|
||||||
let store = std::env::temp_dir().join(format!("weft_pack_uninstall_store_{}", id));
|
let id = format!("weft.pack.uninstall{}", std::process::id());
|
||||||
let ui_dir = src.join("ui");
|
let src = std::env::temp_dir().join(format!("weft_pack_uninstall_src_{}", id));
|
||||||
let _ = fs::create_dir_all(&ui_dir);
|
let store = std::env::temp_dir().join(format!("weft_pack_uninstall_store_{}", id));
|
||||||
fs::write(src.join("app.wasm"), b"\0asm").unwrap();
|
let ui_dir = src.join("ui");
|
||||||
fs::write(ui_dir.join("index.html"), b"").unwrap();
|
let _ = fs::create_dir_all(&ui_dir);
|
||||||
let app_id = format!("com.example.u{}", std::process::id());
|
fs::write(src.join("app.wasm"), b"\0asm").unwrap();
|
||||||
fs::write(
|
fs::write(ui_dir.join("index.html"), b"").unwrap();
|
||||||
src.join("wapp.toml"),
|
let app_id = format!("com.example.u{}", std::process::id());
|
||||||
format!(
|
fs::write(
|
||||||
"[package]\nid = \"{app_id}\"\nname = \"U\"\nversion = \"1.0.0\"\n\n\
|
src.join("wapp.toml"),
|
||||||
[runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n"
|
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());
|
.unwrap();
|
||||||
let result = uninstall_package_from(&app_id, &store);
|
install_package_to(&src, &store, InstallMode::Development).unwrap();
|
||||||
assert!(result.is_ok(), "{result:?}");
|
assert!(store.join(&app_id).exists());
|
||||||
assert!(!store.join(&app_id).exists());
|
let result = uninstall_package_from(&app_id, &store);
|
||||||
assert!(uninstall_package_from(&app_id, &store).is_err());
|
assert!(result.is_ok(), "{result:?}");
|
||||||
let _ = fs::remove_dir_all(&src);
|
assert!(!store.join(&app_id).exists());
|
||||||
let _ = fs::remove_dir_all(&store);
|
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]
|
#[test]
|
||||||
|
|
@ -1089,36 +1353,40 @@ entry = "ui/index.html"
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn install_package_from_archive() {
|
fn install_package_from_archive() {
|
||||||
use std::fs;
|
let home = temp_root("install_package_from_archive");
|
||||||
let id = format!("instarch_{}", std::process::id());
|
with_home(&home.clone(), move || {
|
||||||
let src = std::env::temp_dir().join(&id);
|
use std::fs;
|
||||||
let ui = src.join("ui");
|
let id = format!("instarch_{}", std::process::id());
|
||||||
let _ = fs::create_dir_all(&ui);
|
let src = std::env::temp_dir().join(&id);
|
||||||
fs::write(src.join("app.wasm"), b"\0asm\x01\0\0\0").unwrap();
|
let ui = src.join("ui");
|
||||||
fs::write(ui.join("index.html"), b"<!DOCTYPE html>").unwrap();
|
let _ = fs::create_dir_all(&ui);
|
||||||
let app_id = format!("com.example.ia{}", std::process::id());
|
fs::write(src.join("app.wasm"), b"\0asm\x01\0\0\0").unwrap();
|
||||||
fs::write(
|
fs::write(ui.join("index.html"), b"<!DOCTYPE html>").unwrap();
|
||||||
src.join("wapp.toml"),
|
let app_id = format!("com.example.ia{}", std::process::id());
|
||||||
format!(
|
fs::write(
|
||||||
"[package]\nid = \"{app_id}\"\nname = \"IA\"\nversion = \"1.0.0\"\n\n\
|
src.join("wapp.toml"),
|
||||||
[runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n"
|
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();
|
),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let bundle_dir = std::env::temp_dir().join(format!("{id}_bnd"));
|
let bundle_dir = std::env::temp_dir().join(format!("{id}_bnd"));
|
||||||
let _ = fs::create_dir_all(&bundle_dir);
|
let _ = fs::create_dir_all(&bundle_dir);
|
||||||
bundle_package(&src, Some(&bundle_dir)).unwrap();
|
bundle_package(&src, Some(&bundle_dir)).unwrap();
|
||||||
let archive = bundle_dir.join(format!("{app_id}.app.tar.zst"));
|
let archive = bundle_dir.join(format!("{app_id}.app.tar.zst"));
|
||||||
|
|
||||||
let store = std::env::temp_dir().join(format!("{id}_store"));
|
let store = std::env::temp_dir().join(format!("{id}_store"));
|
||||||
let _ = fs::create_dir_all(&store);
|
let _ = fs::create_dir_all(&store);
|
||||||
install_package_to(&archive, &store).unwrap();
|
install_package_to(&archive, &store, InstallMode::Development).unwrap();
|
||||||
assert!(store.join(&app_id).join("app.wasm").exists());
|
assert!(store.join(&app_id).join("app.wasm").exists());
|
||||||
|
|
||||||
let _ = fs::remove_dir_all(&src);
|
let _ = fs::remove_dir_all(&src);
|
||||||
let _ = fs::remove_dir_all(&bundle_dir);
|
let _ = fs::remove_dir_all(&bundle_dir);
|
||||||
let _ = fs::remove_dir_all(&store);
|
let _ = fs::remove_dir_all(&store);
|
||||||
|
});
|
||||||
|
let _ = std::fs::remove_dir_all(&home);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -1181,7 +1449,7 @@ entry = "ui/index.html"
|
||||||
sign_package(&dir, &key_file).unwrap();
|
sign_package(&dir, &key_file).unwrap();
|
||||||
assert!(dir.join("signature.sig").exists());
|
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");
|
assert!(ok, "signature should verify");
|
||||||
|
|
||||||
let _ = fs::remove_dir_all(&dir);
|
let _ = fs::remove_dir_all(&dir);
|
||||||
|
|
@ -1210,7 +1478,11 @@ entry = "ui/index.html"
|
||||||
|
|
||||||
fs::write(dir.join("app.wasm"), b"\0asm\x01\0\0\x01").unwrap();
|
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");
|
assert!(!ok, "tampered bundle should not verify");
|
||||||
|
|
||||||
let _ = fs::remove_dir_all(&dir);
|
let _ = fs::remove_dir_all(&dir);
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ WASI Preview 2 + Component Model execution host (Wasmtime 30). Runs the componen
|
||||||
|
|
||||||
### weft-pack
|
### weft-pack
|
||||||
|
|
||||||
Package management CLI. Subcommands: `check` (validate wapp.toml + wasm module), `sign` (Ed25519 signature), `verify` (verify signature), `generate-key`, `install`, `uninstall`, `list`, `build-image` (EROFS dm-verity), `info`.
|
Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner), `uninstall`, `list`, `build-image` (EROFS dm-verity), `info`.
|
||||||
|
|
||||||
### weft-file-portal
|
### weft-file-portal
|
||||||
|
|
||||||
|
|
@ -102,7 +102,7 @@ Fetch grants name hosts, not addresses: ports are not restricted, and a host tha
|
||||||
app.wasm — WASI Component Model binary
|
app.wasm — WASI Component Model binary
|
||||||
ui/
|
ui/
|
||||||
index.html — entry point served by weft-app-shell
|
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):
|
Package store roots (in priority order):
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,16 @@ weft-pack sign ./examples/org.weft.demo.counter --key ./keys/weft-sign.key
|
||||||
weft-pack verify ./examples/org.weft.demo.counter --key ./keys/weft-sign.pub
|
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 <dir> --dev`, recorded as development content. See `docs/security.md` for the trust store and app ID ownership.
|
||||||
|
|
||||||
## NixOS VM (requires Nix with flakes)
|
## NixOS VM (requires Nix with flakes)
|
||||||
|
|
||||||
Build the VM image:
|
Build the VM image:
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,17 @@ Each app session runs as a separate OS process (`weft-runtime`). When systemd is
|
||||||
|
|
||||||
Apps access the filesystem only through WASI preopened directories. Each capability maps to a specific host path preopened at a fixed guest path. The `weft-file-portal` process applies the same directory grants and access modes, but checks paths lexically: it blocks `..` traversal, not symbolic links. Components cannot currently reach its socket, since the runtime grants them no socket access.
|
Apps access the filesystem only through WASI preopened directories. Each capability maps to a specific host path preopened at a fixed guest path. The `weft-file-portal` process applies the same directory grants and access modes, but checks paths lexically: it blocks `..` traversal, not symbolic links. Components cannot currently reach its socket, since the runtime grants them no socket access.
|
||||||
|
|
||||||
## Package Signing
|
## Package Signing and Trust
|
||||||
|
|
||||||
Packages are signed with Ed25519 (`ed25519-dalek`). The signature covers the SHA-256 hash of `wapp.toml` and `app.wasm`. `weft-pack verify` checks the signature before installation.
|
Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `<path>\t<sha-256>` line per file sorted by path. Every file is covered, including the UI. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule.
|
||||||
|
|
||||||
For verified read-only package storage, `weft-pack build-image` produces an EROFS image protected with dm-verity. Mounting requires the setuid `weft-mount-helper` which calls `veritysetup`.
|
`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed.
|
||||||
|
|
||||||
|
The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/<id>`: the publisher key, or development. Every later installation of that ID must have the same owner, so neither another trusted publisher nor a development build can take over an ID, and with it the app's data. The record survives uninstall while the app's data remains; uninstalling with no data left frees the ID. The demo key in `examples/keys` is a test fixture, not a signing authority.
|
||||||
|
|
||||||
|
Not yet covered: `weft-appd` does not check signatures or owners at launch, so the installed directory is trusted as installed; key revocation, rotation and ownership transfer have no workflow; and capabilities are granted at launch without asking the user, independently of the signature.
|
||||||
|
|
||||||
|
For verified read-only package storage, `weft-pack build-image` produces an EROFS image protected with dm-verity. Mounting requires the setuid `weft-mount-helper` which calls `veritysetup`. The image's root hash is not yet authenticated (see `architecture.md`).
|
||||||
|
|
||||||
## Seccomp
|
## Seccomp
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue