Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(tinybox): assert resolve local grants is in the canonical contract

Steven Enamakelded447536bd7
diff --git a/crates/openhuman-core/src/modules/tinybox_tests.rs b/crates/openhuman-core/src/modules/tinybox_tests.rs
index feb252e903..20d98068a4 100644
--- a/crates/openhuman-core/src/modules/tinybox_tests.rs
+++ b/crates/openhuman-core/src/modules/tinybox_tests.rs
@@ -663,3 +663,9 @@ async fn docker_status_transport_uses_its_bounded_deadline() {
.expect_err("cleanup transport must also stop at its bounded deadline plus grace");
assert!(error.contains("MODULE_CALL_REPORTED"), "{error}");
}
+
+#[test]
+fn resolve_local_grants_is_a_member_of_the_canonical_contract() {
+ assert_eq!(RESOLVE_LOCAL_GRANTS, "ResolveLocalGrants");
+ assert!(contract::METHODS.contains(&RESOLVE_LOCAL_GRANTS));
+}
diff --git a/crates/openhuman-core/src/sandbox/local_grants.rs b/crates/openhuman-core/src/sandbox/local_grants.rs
new file mode 100644
index 0000000000..c9fb30c354
--- /dev/null
+++ b/crates/openhuman-core/src/sandbox/local_grants.rs
@@ -0,0 +1,159 @@
+//! Host adapter for the local jail's filesystem grants.
+//!
+//! TinyBox owns grant discovery (`ResolveLocalGrants`): toolchain homes, git
+//! config includes, path normalisation and the credential-ancestor check. The
+//! host owns the policy: it sends its protected-path floor
+//! (`SecurityPolicy::is_always_forbidden`) with the request and re-applies that
+//! same floor to whatever comes back, so a module that returned a forbidden
+//! path would still not widen the jail.
+
+use std::path::PathBuf;
+
+use super::types::SandboxPolicy;
+use crate::config::{Config, LocalJailConfig};
+use crate::security::SecurityPolicy;
+
+/// Credential directories under `$HOME` that no grant may reach or contain.
+const HOME_CREDENTIAL_DIRS: &[&str] = &[".ssh", ".gnupg", ".aws", ".azure", ".kube"];
+
+/// Paths the jail is granted beyond the workspace root.
+#[derive(Debug, Clone, Default, PartialEq, Eq)]
+pub(crate) struct JailGrants {
+ pub read_only: Vec<PathBuf>,
+ pub read_write: Vec<PathBuf>,
+}
+
+/// The operator home a grant set may be built from. A tenant never reads the
+/// operator's home, so SaaS callers get none and `home` is not consulted.
+pub(crate) fn grant_home(
+ saas: bool,
+ home: impl FnOnce() -> Option<PathBuf>,
+) -> Option<PathBuf> {
+ if saas {
+ None
+ } else {
+ home()
+ }
+}
+
+/// Add the TinyBox-resolved toolchain grants to a local policy.
+///
+/// Explicit mounts already on the policy are kept. Without the `modules`
+/// feature there is no jail to grant for, so the policy is returned as is.
+pub(crate) async fn with_resolved_grants(
+ config: &Config,
+ policy: &SandboxPolicy,
+) -> anyhow::Result<SandboxPolicy> {
+ let mut policy = policy.clone();
+ #[cfg(feature = "modules")]
+ {
+ let home = grant_home(crate::core::runtime::is_saas(), dirs::home_dir);
+ let grants = resolve(config, home.as_deref(), &config.runtime.local_jail).await?;
+ tracing::debug!(
+ read_only = grants.read_only.len(),
+ read_write = grants.read_write.len(),
+ "[sandbox:grants] resolved local jail grants"
+ );
+ merge(&mut policy.read_only_mounts, grants.read_only);
+ merge(&mut policy.read_write_mounts, grants.read_write);
+ }
+ #[cfg(not(feature = "modules"))]
+ let _ = config;
+ Ok(policy)
+}
+
+#[cfg(feature = "modules")]
+fn merge(into: &mut Vec<PathBuf>, extra: Vec<PathBuf>) {
+ for path in extra {
+ if !into.contains(&path) {
+ into.push(path);
+ }
+ }
+}
+
+#[cfg(feature = "modules")]
+async fn resolve(
+ config: &Config,
+ home: Option<&std::path::Path>,
+ jail: &LocalJailConfig,
+) -> anyhow::Result<JailGrants> {
+ let request = grant_request(home, jail);
+ let reply = crate::modules::tinybox::resolve_local_grants(config, request)
+ .await
+ .map_err(|error| anyhow::anyhow!("TinyBox could not resolve local jail grants: {error}"))?;
+ Ok(admit_module_grants(reply, jail.allow_proc))
+}
+
+/// Build the `ResolveLocalGrants` request: the operator's grant configuration
+/// plus the host's protected-path floor.
+#[cfg(feature = "modules")]
+pub(crate) fn grant_request(
+ home: Option<&std::path::Path>,
+ jail: &LocalJailConfig,
+) -> crate::modules::tinybox::contract::ResolveLocalGrantsRequest {
+ use crate::modules::tinybox::contract::{
+ LocalGrantConfig, ProtectedPaths, ResolveLocalGrantsRequest,
+ };
+ use crate::security::policy::{
+ ALWAYS_FORBIDDEN_PREFIXES, SENSITIVE_COMPONENTS, SENSITIVE_FILES, SENSITIVE_PAIRS,
+ };
+
+ let owned = |items: &[&str]| items.iter().map(|s| (*s).to_owned()).collect::<Vec<_>>();
+ ResolveLocalGrantsRequest {
+ home: home.map(|h| h.display().to_string()),
+ config: LocalGrantConfig {
+ toolchain_homes: jail.toolchain_homes,
+ extra_read_only: jail.extra_read_only.clone(),
+ extra_read_write: jail.extra_read_write.clone(),
+ allow_proc: jail.allow_proc,
+ },
+ protected: ProtectedPaths {
+ components: owned(SENSITIVE_COMPONENTS)
+ .into_iter()
+ .chain(owned(SENSITIVE_FILES))
+ .collect(),
+ component_pairs: SENSITIVE_PAIRS
+ .iter()
+ .map(|[a, b]| [(*a).to_owned(), (*b).to_owned()])
+ .collect(),
+ prefixes: owned(ALWAYS_FORBIDDEN_PREFIXES),
+ credential_roots: home
+ .map(|h| {
+ HOME_CREDENTIAL_DIRS
+ .iter()
+ .map(|dir| h.join(dir).display().to_string())
+ .collect()
+ })
+ .unwrap_or_default(),
+ },
+ }
+}
+
+/// Second gate: drop every returned path the host floor forbids. `/proc` is
+/// the one sanctioned exception, and only when `allow_proc` is set.
+#[cfg(feature = "modules")]
+pub(crate) fn admit_module_grants(
+ reply: crate::modules::tinybox::contract::LocalGrants,
+ allow_proc: bool,
+) -> JailGrants {
+ let admit = |raw: String| -> Option<PathBuf> {
+ let path = PathBuf::from(raw);
+ let sanctioned_proc = allow_proc && path == std::path::Path::new("/proc");
+ if !sanctioned_proc && SecurityPolicy::is_always_forbidden(&path) {
+ tracing::warn!(
+ path = %path.display(),
+ "[sandbox:grants] refused module grant: host floor forbids it"
+ );
+ return None;
+ }
+ Some(path)
+ };
+ JailGrants {
+ read_only: reply.read_only.into_iter().filter_map(admit).collect(),
+ read_write: reply.read_write.into_iter().filter_map(admit).collect(),
+ }
+}
+
+#[cfg(test)]
+#[path = "local_grants_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/sandbox/local_grants_tests.rs b/crates/openhuman-core/src/sandbox/local_grants_tests.rs
new file mode 100644
index 0000000000..53168feb8f
--- /dev/null
+++ b/crates/openhuman-core/src/sandbox/local_grants_tests.rs
@@ -0,0 +1,80 @@
+use super::*;
+
+#[test]
+fn saas_never_consults_the_operator_home() {
+ let home = grant_home(true, || panic!("a tenant must not read the operator's home"));
+ assert!(home.is_none());
+}
+
+#[test]
+fn single_user_keeps_the_operator_home() {
+ let home = grant_home(false, || Some(PathBuf::from("/home/op")));
+ assert_eq!(home, Some(PathBuf::from("/home/op")));
+}
+
+#[cfg(feature = "modules")]
+mod with_module {
+ use super::*;
+ use crate::modules::tinybox::contract::LocalGrants;
+
+ #[test]
+ fn a_forbidden_path_returned_by_the_module_is_refused() {
+ let reply = LocalGrants {
+ read_only: vec![
+ "/home/op/.ssh".into(),
+ "/home/op/.cargo/credentials.toml".into(),
+ "/etc".into(),
+ "/home/op/.rustup".into(),
+ ],
+ read_write: vec!["/home/op/.aws/sso".into(), "/home/op/.cargo/registry".into()],
+ };
+ let grants = admit_module_grants(reply, false);
+ assert_eq!(grants.read_only, [PathBuf::from("/home/op/.rustup")]);
+ assert_eq!(grants.read_write, [PathBuf::from("/home/op/.cargo/registry")]);
+ }
+
+ #[test]
+ fn proc_is_admitted_only_through_allow_proc() {
+ let reply = || LocalGrants {
+ read_only: vec!["/proc".into(), "/proc/1/environ".into()],
+ read_write: Vec::new(),
+ };
+ assert!(admit_module_grants(reply(), false).read_only.is_empty());
+ assert_eq!(
+ admit_module_grants(reply(), true).read_only,
+ [PathBuf::from("/proc")]
+ );
+ }
+
+ #[test]
+ fn request_carries_config_and_the_host_floor() {
+ let jail = LocalJailConfig {
+ toolchain_homes: false,
+ extra_read_only: vec!["~/a".into()],
+ extra_read_write: vec!["~/b".into()],
+ allow_proc: true,
+ };
+ let request = grant_request(Some(std::path::Path::new("/home/op")), &jail);
+ assert_eq!(request.home.as_deref(), Some("/home/op"));
+ assert!(!request.config.toolchain_homes && request.config.allow_proc);
+ assert_eq!(request.config.extra_read_only, ["~/a"]);
+ assert_eq!(request.config.extra_read_write, ["~/b"]);
+ assert!(request.protected.components.contains(&".ssh".to_owned()));
+ assert!(request.protected.components.contains(&".netrc".to_owned()));
+ assert!(request
+ .protected
+ .component_pairs
+ .contains(&[".cargo".to_owned(), "credentials.toml".to_owned()]));
+ assert!(request.protected.prefixes.contains(&"/etc".to_owned()));
+ assert!(request
+ .protected
+ .credential_roots
+ .contains(&"/home/op/.ssh".to_owned()));
+ }
+
+ #[test]
+ fn no_home_means_no_credential_roots() {
+ let request = grant_request(None, &LocalJailConfig::default());
+ assert!(request.home.is_none() && request.protected.credential_roots.is_empty());
+ }
+}