fix(runtime): enforce effective capability grants per session
Every host import was registered and callable whatever the package
declared, read-only directory capabilities were preopened read-write,
and appd launched packages whose capabilities it could not satisfy. The
fetch import also could not be linked: the host returned a tuple where
the WIT declares the response record, so any component importing
weft:app/fetch failed to instantiate.
One capability vocabulary now lives in weft_ipc_types::capability and
is used by weft-pack, weft-appd and weft-runtime. weft-appd derives a
session's grants once, before the session exists, and refuses the launch
with error 403 when a capability is unknown, unsupported (hw:gpu:*) or
unsatisfiable (documents without an XDG documents directory). The
documents directory now comes from user-dirs.dirs instead of a guessed
~/Documents.
The runtime receives grants explicitly (--grant CAP, --preopen
HOST::GUEST::ro|rw), preopens read-only directories read-only, and checks
the grant on every notifications, clipboard and fetch call. Fetch accepts
only http and https to granted hosts, returns redirects and HTTP error
statuses as responses instead of following or failing them, and bounds
connect time, total time and response size. The file portal takes
read-only directories with --allow-read and refuses writes there.
tests/runtime/check_grants.py runs the real runtime on a probe component
under each kind of grant and checks the outcome of every operation; it
fails when read-only data is writable or an import ignores its grant.
The appd dispatch tests that read WEFT_RUNTIME_BIN now hold the env lock
shared with the tests that set it.
2026-10-09 08:08:58 +00:00
|
|
|
//! 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")?;
|
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
|
|
|
// Guest paths are fixed names without `::`; splitting from the right
|
|
|
|
|
// keeps a host path containing `::` whole.
|
fix(runtime): enforce effective capability grants per session
Every host import was registered and callable whatever the package
declared, read-only directory capabilities were preopened read-write,
and appd launched packages whose capabilities it could not satisfy. The
fetch import also could not be linked: the host returned a tuple where
the WIT declares the response record, so any component importing
weft:app/fetch failed to instantiate.
One capability vocabulary now lives in weft_ipc_types::capability and
is used by weft-pack, weft-appd and weft-runtime. weft-appd derives a
session's grants once, before the session exists, and refuses the launch
with error 403 when a capability is unknown, unsupported (hw:gpu:*) or
unsatisfiable (documents without an XDG documents directory). The
documents directory now comes from user-dirs.dirs instead of a guessed
~/Documents.
The runtime receives grants explicitly (--grant CAP, --preopen
HOST::GUEST::ro|rw), preopens read-only directories read-only, and checks
the grant on every notifications, clipboard and fetch call. Fetch accepts
only http and https to granted hosts, returns redirects and HTTP error
statuses as responses instead of following or failing them, and bounds
connect time, total time and response size. The file portal takes
read-only directories with --allow-read and refuses writes there.
tests/runtime/check_grants.py runs the real runtime on a probe component
under each kind of grant and checks the outcome of every operation; it
fails when read-only data is writable or an import ignores its grant.
The appd dispatch tests that read WEFT_RUNTIME_BIN now hold the env lock
shared with the tests that set it.
2026-10-09 08:08:58 +00:00
|
|
|
let (host, guest) = rest
|
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
|
|
|
.rsplit_once("::")
|
fix(runtime): enforce effective capability grants per session
Every host import was registered and callable whatever the package
declared, read-only directory capabilities were preopened read-write,
and appd launched packages whose capabilities it could not satisfy. The
fetch import also could not be linked: the host returned a tuple where
the WIT declares the response record, so any component importing
weft:app/fetch failed to instantiate.
One capability vocabulary now lives in weft_ipc_types::capability and
is used by weft-pack, weft-appd and weft-runtime. weft-appd derives a
session's grants once, before the session exists, and refuses the launch
with error 403 when a capability is unknown, unsupported (hw:gpu:*) or
unsatisfiable (documents without an XDG documents directory). The
documents directory now comes from user-dirs.dirs instead of a guessed
~/Documents.
The runtime receives grants explicitly (--grant CAP, --preopen
HOST::GUEST::ro|rw), preopens read-only directories read-only, and checks
the grant on every notifications, clipboard and fetch call. Fetch accepts
only http and https to granted hosts, returns redirects and HTTP error
statuses as responses instead of following or failing them, and bounds
connect time, total time and response size. The file portal takes
read-only directories with --allow-read and refuses writes there.
tests/runtime/check_grants.py runs the real runtime on a probe component
under each kind of grant and checks the outcome of every operation; it
fails when read-only data is writable or an import ignores its grant.
The appd dispatch tests that read WEFT_RUNTIME_BIN now hold the env lock
shared with the tests that set it.
2026-10-09 08:08:58 +00:00
|
|
|
.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
|
|
|
|
|
);
|
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
|
|
|
assert_eq!(Preopen::parse("/a::/b::/data::ro").unwrap().host, "/a::/b");
|
fix(runtime): enforce effective capability grants per session
Every host import was registered and callable whatever the package
declared, read-only directory capabilities were preopened read-write,
and appd launched packages whose capabilities it could not satisfy. The
fetch import also could not be linked: the host returned a tuple where
the WIT declares the response record, so any component importing
weft:app/fetch failed to instantiate.
One capability vocabulary now lives in weft_ipc_types::capability and
is used by weft-pack, weft-appd and weft-runtime. weft-appd derives a
session's grants once, before the session exists, and refuses the launch
with error 403 when a capability is unknown, unsupported (hw:gpu:*) or
unsatisfiable (documents without an XDG documents directory). The
documents directory now comes from user-dirs.dirs instead of a guessed
~/Documents.
The runtime receives grants explicitly (--grant CAP, --preopen
HOST::GUEST::ro|rw), preopens read-only directories read-only, and checks
the grant on every notifications, clipboard and fetch call. Fetch accepts
only http and https to granted hosts, returns redirects and HTTP error
statuses as responses instead of following or failing them, and bounds
connect time, total time and response size. The file portal takes
read-only directories with --allow-read and refuses writes there.
tests/runtime/check_grants.py runs the real runtime on a probe component
under each kind of grant and checks the outcome of every operation; it
fails when read-only data is writable or an import ignores its grant.
The appd dispatch tests that read WEFT_RUNTIME_BIN now hold the env lock
shared with the tests that set it.
2026-10-09 08:08:58 +00:00
|
|
|
for bad in ["/a::/b", "/a", "/a::/b::wx", "::/b::ro", "/a::b::ro"] {
|
|
|
|
|
assert!(Preopen::parse(bad).is_err(), "{bad} accepted");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|