Skip to content

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

test(sandbox): add integration tests for Landlock jail behaviour

Steven Enamakel9f28baa00748
diff --git a/crates/openhuman-core/src/sandbox/ops_tests.rs b/crates/openhuman-core/src/sandbox/ops_tests.rs
index 56c577d662..98eec50296 100644
--- a/crates/openhuman-core/src/sandbox/ops_tests.rs
+++ b/crates/openhuman-core/src/sandbox/ops_tests.rs
@@ -465,3 +465,143 @@ fn local_status_is_inactive_for_the_unsupported_backend() {
commands through the no-op fallback; `Ready` would claim confinement that is absent"
);
}
+
+// ── tinybox#23: with a real jail, everyday commands still work ───────────────
+//
+// These run a real Landlock jail and skip (loudly) where the kernel has none,
+// so they never fail a host that cannot confine.
+
+#[cfg(target_os = "linux")]
+fn landlock_in_force() -> bool {
+ let backend = cwd_jail::default_backend();
+ let ok = backend.name() == "landlock" && backend.is_available();
+ if !ok {
+ eprintln!(
+ "SKIP: no Landlock on this host (backend = {})",
+ backend.name()
+ );
+ }
+ ok
+}
+
+#[cfg(target_os = "linux")]
+fn host_has(program: &str) -> bool {
+ std::process::Command::new("sh")
+ .arg("-c")
+ .arg(format!("command -v {program}"))
+ .output()
+ .map(|o| o.status.success())
+ .unwrap_or(false)
+}
+
+#[cfg(target_os = "linux")]
+#[tokio::test]
+async fn landlock_jail_runs_cargo_and_mktemp_but_blocks_writes_outside() {
+ if !landlock_in_force() {
+ return;
+ }
+ let action = tempfile::tempdir().unwrap();
+ let state = tempfile::tempdir().unwrap();
+ let outside = tempfile::tempdir().unwrap();
+ let policy = local_policy(action.path(), state.path());
+
+ if host_has("cargo") {
+ let r = run_local(&policy, "cargo --version").await;
+ assert!(r.success(), "cargo failed under the jail: {}", r.stderr);
+ assert!(r.stdout.starts_with("cargo "), "stdout: {}", r.stdout);
+ } else {
+ eprintln!("SKIP cargo: not installed on this host");
+ }
+
+ // `/tmp` is not granted; `mktemp` lands in the per-call TMPDIR scratch dir.
+ let r = run_local(&policy, "mktemp").await;
+ assert!(r.success(), "mktemp failed under the jail: {}", r.stderr);
+ assert!(
+ r.stdout
+ .trim()
+ .starts_with(sandbox_scratch_root(state.path()).to_str().unwrap()),
+ "mktemp should land in the scratch dir, got {:?}",
+ r.stdout
+ );
+ assert!(
+ entries(&sandbox_scratch_root(state.path())).is_empty(),
+ "per-call scratch dir left behind"
+ );
+
+ // The jail still confines: writing outside the root fails.
+ let target = outside.path().join("pwned");
+ let r = run_local(&policy, &format!("echo x > '{}'", target.display())).await;
+ assert!(!r.success(), "write outside the root must fail");
+ assert!(!target.exists(), "the jail let a write escape");
+
+ // ...while the workspace root itself stays writable.
+ let r = run_local(&policy, "echo ok > inside.txt").await;
+ assert!(r.success(), "stderr: {}", r.stderr);
+ assert!(action.path().join("inside.txt").exists());
+}
+
+#[cfg(target_os = "linux")]
+#[tokio::test]
+async fn landlock_jail_denies_proc_unless_the_toggle_is_on() {
+ if !landlock_in_force() {
+ return;
+ }
+ let action = tempfile::tempdir().unwrap();
+ let state = tempfile::tempdir().unwrap();
+ let policy = local_policy(action.path(), state.path());
+ let denied = run_local(&policy, "cat /proc/self/environ > /dev/null").await;
+ assert!(!denied.success(), "/proc must be off by default");
+
+ let config = RuntimeConfig {
+ local_jail: crate::config::LocalJailConfig {
+ allow_proc: true,
+ ..Default::default()
+ },
+ ..RuntimeConfig::default()
+ };
+ let policy = resolve_sandbox_policy(
+ SandboxMode::Sandboxed,
+ action.path(),
+ state.path(),
+ &config,
+ false,
+ );
+ let allowed = run_local(&policy, "cat /proc/self/environ > /dev/null").await;
+ assert!(allowed.success(), "stderr: {}", allowed.stderr);
+}
+
+#[cfg(target_os = "linux")]
+#[tokio::test]
+async fn landlock_jail_cannot_read_the_users_ssh_directory() {
+ if !landlock_in_force() {
+ return;
+ }
+ let Some(ssh) = dirs::home_dir().map(|h| h.join(".ssh")).filter(|p| p.is_dir()) else {
+ eprintln!("SKIP: no ~/.ssh on this host");
+ return;
+ };
+ let action = tempfile::tempdir().unwrap();
+ let state = tempfile::tempdir().unwrap();
+ let policy = local_policy(action.path(), state.path());
+ let r = run_local(&policy, &format!("ls '{}'", ssh.display())).await;
+ assert!(!r.success(), "~/.ssh must stay unreachable from the jail");
+}
+
+#[tokio::test]
+async fn local_handle_status_matches_the_backend_actually_in_force() {
+ let action = tempfile::tempdir().unwrap();
+ let state = tempfile::tempdir().unwrap();
+ let handle = create_sandbox_backend(&local_policy(action.path(), state.path())).await;
+ let name = handle.backend_id.clone().unwrap();
+ let unconfined = name == cwd_jail::NOOP_BACKEND_NAME
+ || name == cwd_jail::detect::UNSUPPORTED_BACKEND_NAME;
+ assert_eq!(
+ handle.status,
+ if unconfined {
+ SandboxStatus::Inactive
+ } else {
+ SandboxStatus::Ready
+ },
+ "backend = {name}"
+ );
+}