WEFT_OS/crates/weft-runtime/src/grants.rs
Marco Allegretti 51ca194acb
fix(runtime): close fetch and preopen gaps in grant enforcement
A component with a host-specific fetch grant could reach another origin
by setting its own Host header, and could inject request lines through
the method string, which ureq writes unvalidated. Fetch now accepts only
standard methods and refuses Host, framing and hop-by-hop headers. Fetch
grants accept canonical DNS names and dotted IPv4 addresses only, so a
grant can never be written in a form that silently never matches.

--preopen split host and guest at the first `::`, so a host path
containing `::` widened the preopened directory; it now splits from the
right, and appd refuses host paths that are not UTF-8 or contain `::`.

appd validated capabilities one at a time, creating the data directory
before a later capability was refused, and required HOME even for
packages without filesystem capabilities. It now checks every capability
first, resolves host directories only when needed, validates the app ID
with the rule weft-pack uses (now shared in weft_ipc_types::package), and
reports 400, 404, 403 or 500 according to the cause. user-dirs.dirs is
read as a shell would: the last assignment wins, $HOME must be followed
by a slash, escaped values are ignored and a relative XDG_CONFIG_HOME is
not used.

The grants probe now also overwrites, renames, removes and creates
directories in read-only data, and sends a forged Host header, an
injected method and a file: URL; it fails when directory permissions of
a read-only preopen are widened or the header check is removed. A new
CI job builds the runtime with its engine and fetch support and runs the
check. The mount test that sets WEFT_APP_STORE now holds the env lock.

Package images are still located separately from directory installs;
deriving grants from the verified image is part of resolving one package
descriptor.
2026-10-09 08:25:40 +00:00

176 lines
6.2 KiB
Rust

//! The effective grants weft-appd derived for this session.
//!
//! weft-appd passes each host-import capability as `--grant <capability>` and
//! each filesystem capability as `--preopen HOST::GUEST::ro|rw`. Nothing here
//! reads the package manifest: the runtime enforces what the supervisor
//! granted, not what the package asks for.
use anyhow::Context;
use weft_ipc_types::capability::{Access, Capability, FetchScope};
/// Grants for the host imports in the `weft:app` world.
#[derive(Debug, Default)]
pub struct Grants {
fetch: Vec<FetchScope>,
notifications: bool,
clipboard_read: bool,
clipboard_write: bool,
}
// The checks are used by the import implementations, which only exist with
// the wasmtime-runtime feature.
#[cfg_attr(not(feature = "wasmtime-runtime"), allow(dead_code))]
impl Grants {
/// Adds a `--grant` argument. Filesystem capabilities arrive as preopens
/// and hardware capabilities have no import, so both are refused here.
pub fn add(&mut self, spec: &str) -> anyhow::Result<()> {
match spec.parse::<Capability>()? {
Capability::Fetch(scope) => self.fetch.push(scope),
Capability::Notifications => self.notifications = true,
Capability::ClipboardRead => self.clipboard_read = true,
Capability::ClipboardWrite => self.clipboard_write = true,
other => anyhow::bail!("'{other}' is not granted through --grant"),
}
Ok(())
}
pub fn notifications(&self) -> Result<(), String> {
granted(self.notifications, "sys:notifications")
}
pub fn clipboard_read(&self) -> Result<(), String> {
granted(self.clipboard_read, "sys:clipboard:read")
}
pub fn clipboard_write(&self) -> Result<(), String> {
granted(self.clipboard_write, "sys:clipboard:write")
}
#[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> {
if self.fetch.is_empty() {
return Err("capability net:fetch is not granted".to_owned());
}
let covered = self.fetch.iter().any(|scope| match scope {
FetchScope::AnyHost => true,
FetchScope::Host(granted) => granted == host,
});
if covered {
Ok(())
} else {
Err(format!("capability net:fetch:{host} is not granted"))
}
}
/// Whether any fetch grant exists, before a URL is examined.
pub fn fetch_any(&self) -> Result<(), String> {
granted(!self.fetch.is_empty(), "net:fetch")
}
}
#[cfg_attr(not(feature = "wasmtime-runtime"), allow(dead_code))]
fn granted(present: bool, capability: &str) -> Result<(), String> {
if present {
Ok(())
} else {
Err(format!("capability {capability} is not granted"))
}
}
/// A directory made visible to the component.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Preopen {
pub host: String,
pub guest: String,
pub access: Access,
}
impl Preopen {
/// Parses `HOST::GUEST::ro` or `HOST::GUEST::rw`.
pub fn parse(spec: &str) -> anyhow::Result<Self> {
let (rest, mode) = spec
.rsplit_once("::")
.context("--preopen expects HOST::GUEST::ro|rw")?;
// Guest paths are fixed names without `::`; splitting from the right
// keeps a host path containing `::` whole.
let (host, guest) = rest
.rsplit_once("::")
.context("--preopen expects HOST::GUEST::ro|rw")?;
let access = match mode {
"ro" => Access::Read,
"rw" => Access::ReadWrite,
other => anyhow::bail!("--preopen mode must be ro or rw, not '{other}'"),
};
anyhow::ensure!(
!host.is_empty() && guest.starts_with('/'),
"--preopen needs a host path and an absolute guest path"
);
Ok(Self {
host: host.to_owned(),
guest: guest.to_owned(),
access,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn imports_are_denied_without_grants() {
let grants = Grants::default();
assert!(grants.notifications().is_err());
assert!(grants.clipboard_read().is_err());
assert!(grants.clipboard_write().is_err());
assert!(grants.fetch_any().is_err());
assert!(grants.fetch_host("example.org").is_err());
}
#[test]
fn each_grant_opens_only_its_import() {
let mut grants = Grants::default();
grants.add("sys:clipboard:read").unwrap();
assert!(grants.clipboard_read().is_ok());
assert!(grants.clipboard_write().is_err());
assert!(grants.notifications().is_err());
}
#[test]
fn host_fetch_grants_match_exact_hosts() {
let mut grants = Grants::default();
grants.add("net:fetch:api.example.org").unwrap();
assert!(grants.fetch_any().is_ok());
assert!(grants.fetch_host("api.example.org").is_ok());
assert!(grants.fetch_host("example.org").is_err());
assert!(grants.fetch_host("evil.api.example.org").is_err());
grants.add("net:fetch:*").unwrap();
assert!(grants.fetch_host("example.org").is_ok());
}
#[test]
fn filesystem_and_unknown_grants_are_refused() {
let mut grants = Grants::default();
assert!(grants.add("fs:rw:app-data").is_err());
assert!(grants.add("hw:gpu:compute").is_err());
assert!(grants.add("sys:everything").is_err());
}
#[test]
fn preopens_carry_their_mode() {
let preopen = Preopen::parse("/home/u/data::/data::ro").unwrap();
assert_eq!(preopen.host, "/home/u/data");
assert_eq!(preopen.guest, "/data");
assert_eq!(preopen.access, Access::Read);
assert_eq!(
Preopen::parse("/a::/b::rw").unwrap().access,
Access::ReadWrite
);
assert_eq!(Preopen::parse("/a::/b::/data::ro").unwrap().host, "/a::/b");
for bad in ["/a::/b", "/a", "/a::/b::wx", "::/b::ro", "/a::b::ro"] {
assert!(Preopen::parse(bad).is_err(), "{bad} accepted");
}
}
}