The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(windows): forward process bootstrap env into sandboxed children
ajabaabaab8b5c208edd8
diff --git a/crates/openhuman-cli/Cargo.toml b/crates/openhuman-cli/Cargo.tomlindex 121e2ef738..73854b9550 100644--- a/crates/openhuman-cli/Cargo.toml+++ b/crates/openhuman-cli/Cargo.toml@@ -209,6 +209,10 @@ path = "../../tests/config_auth_app_state_connectivity_e2e.rs"name = "cwd_jail_e2e"path = "../../tests/cwd_jail_e2e.rs"+[[test]]+name = "windows_sandbox_env_e2e"+path = "../../tests/windows_sandbox_env_e2e.rs"+[[test]]name = "domain_modules_e2e"path = "../../tests/domain_modules_e2e.rs"diff --git a/crates/openhuman-core/src/agent/platform_shell.rs b/crates/openhuman-core/src/agent/platform_shell.rsindex ffc36df4e9..148bc35d47 100644--- a/crates/openhuman-core/src/agent/platform_shell.rs+++ b/crates/openhuman-core/src/agent/platform_shell.rs@@ -29,6 +29,71 @@use std::path::Path;+/// Environment variables a Windows child process needs before it can run at+/// all, beyond the functional allow-list each launcher already forwards.+///+/// Every sanitized spawn path in the core calls `env_clear()` and re-forwards+/// only what it names, so this list is what makes a cleared Windows environment+/// bootable. These are forwarded from the parent environment, never synthesised+/// or hard-coded: a name the parent does not have is simply not set.+///+/// Measured consequences of omitting them (Windows 11, `SystemRoot` absent from+/// an otherwise-valid child environment):+///+/// - `node.exe` aborts during startup with+/// `Assertion failed: ncrypto::CSPRNG(nullptr, 0)` (exit 134), because the+/// OS random provider cannot initialise without a system directory.+/// - `powershell.exe` exits with `Internal Windows PowerShell error. Loading+/// managed Windows PowerShell failed with error 8009001d.`+/// - `cmd.exe` leaves `%SystemRoot%`/`%TEMP%`/`%USERPROFILE%` unexpanded. It+/// does *not* repair them for its own children, so a wrapper shell cannot+/// rescue a stripped environment.+///+/// `COMSPEC` and `PATHEXT` are here because `cmd.exe` needs `PATHEXT` to resolve+/// the `.cmd`/`.bat` shims that npm, npx and the Git toolchain are installed+/// as; `TEMP`/`TMP`/`USERPROFILE`/`APPDATA`/`LOCALAPPDATA` because tooling+/// writes scratch and reads config from them; the `ProgramFiles*` trio because+/// installers and SDK locators probe them.+///+/// Single source of truth: keep every launcher's allow-list a superset of this+/// (enforced by `windows_bootstrap_vars_are_forwarded_everywhere`), and add a+/// launcher to that test rather than inventing a sixth list.+pub const WINDOWS_PROCESS_ENV_VARS: &[&str] = &[+ "SystemRoot",+ "WINDIR",+ "COMSPEC",+ "PATHEXT",+ "TEMP",+ "TMP",+ "USERPROFILE",+ "APPDATA",+ "LOCALAPPDATA",+ "ProgramFiles",+ "ProgramFiles(x86)",+ "ProgramW6432",+];++/// Assert that a launcher's environment allow-list covers every Windows+/// process-bootstrap variable.+///+/// Each launcher guards its own list with this, so a new launcher cannot+/// silently ship a `env_clear()` that produces an unbootable Windows child.+/// The failure this prevents is not a clean error: the child aborts inside the+/// OS crypto provider (`node` → `ncrypto::CSPRNG` assertion, `powershell` →+/// `8009001d`), and the harness reports it as a mysterious exit code rather+/// than a missing environment variable.+#[track_caller]+pub fn assert_forwards_windows_bootstrap(allowlist: &[&str], launcher: &str) {+ for var in WINDOWS_PROCESS_ENV_VARS {+ assert!(+ allowlist.contains(var),+ "{launcher} clears the child environment but does not forward \+ `{var}`; a Windows child spawned without it aborts during crypto \+ init. Add it to the allow-list."+ );+ }+}+/// Whether the Unix arm prefixes `set -o pipefail`.#[derive(Clone, Copy, PartialEq, Eq)]enum PipeFail {diff --git a/crates/openhuman-core/src/sandbox/ops.rs b/crates/openhuman-core/src/sandbox/ops.rsindex d3a091d5e1..d4b7b05e66 100644--- a/crates/openhuman-core/src/sandbox/ops.rs+++ b/crates/openhuman-core/src/sandbox/ops.rs@@ -16,8 +16,51 @@ use std::path::Path;use std::time::Duration;/// Safe environment variables forwarded into sandboxed execution.+///+/// This list is the *entire* environment a sandboxed child gets: both+/// [`execute_unsandboxed`] and [`execute_local_jail`] call `env_clear()` and+/// re-forward only what is named here. The `WINDOWS_PROCESS_ENV_VARS` entries+/// are therefore not decoration — without them a Windows child cannot+/// initialise the OS crypto provider, and it fails in a way that looks nothing+/// like a missing environment variable (`node` aborts with the+/// `ncrypto::CSPRNG` assertion, `powershell` with `8009001d`).+///+/// They were missing when this defect was found, and it survived the first+/// round of fixes because the four tool launchers (`shell`, `node_exec`,+/// `npm_exec`, `python_exec`) each carry their own copy of the allow-list and+/// had already been patched: the built-in `orchestrator` runs with+/// `sandbox_mode = "sandboxed"`, and all four tools divert to+/// [`crate::sandbox`] *before* reaching those lists, so this was the only list+/// that mattered for the main agent. Keep it a superset of+/// [`crate::agent::platform_shell::WINDOWS_PROCESS_ENV_VARS`] —+/// `sandbox_env_forwards_windows_bootstrap_vars` and+/// `tests/windows_sandbox_env_e2e.rs` enforce that.+///+/// On Linux and macOS these names simply do not resolve in the parent+/// environment, so forwarding them is a no-op there.pub const SANDBOX_ENV_PASSTHROUGH: &[&str] = &[- "PATH", "HOME", "TERM", "LANG", "LC_ALL", "LC_CTYPE", "USER", "SHELL", "TMPDIR",+ "PATH",+ "HOME",+ "TERM",+ "LANG",+ "LC_ALL",+ "LC_CTYPE",+ "USER",+ "SHELL",+ "TMPDIR",+ // Windows process bootstrap — see `crate::agent::platform_shell`.+ "SystemRoot",+ "WINDIR",+ "COMSPEC",+ "PATHEXT",+ "TEMP",+ "TMP",+ "USERPROFILE",+ "APPDATA",+ "LOCALAPPDATA",+ "ProgramFiles",+ "ProgramFiles(x86)",+ "ProgramW6432",];/// Resolve a `SandboxPolicy` from the agent's `SandboxMode`, thediff --git a/crates/openhuman-core/src/sandbox/ops_tests.rs b/crates/openhuman-core/src/sandbox/ops_tests.rsindex 50c67ae1f4..ead2a40c7e 100644--- a/crates/openhuman-core/src/sandbox/ops_tests.rs+++ b/crates/openhuman-core/src/sandbox/ops_tests.rs@@ -308,3 +308,29 @@ fn local_status_is_ready_for_a_real_jail() {);}}++// ── Windows child environment ────────────────────────────────────────────────+//+// `execute_unsandboxed` and `execute_local_jail` both call `env_clear()` and+// re-forward only `SANDBOX_ENV_PASSTHROUGH`. On Windows that list has to carry+// the process-bootstrap variables or the child cannot initialise the OS crypto+// provider — `node` dies with `Assertion failed: ncrypto::CSPRNG(nullptr, 0)`+// (exit 134) and `powershell` with `8009001d`. Both read as opaque failures to+// the agent, which is how the original defect shipped: the four tool launchers'+// allow-lists were fixed, the sandbox allow-list — the one the `sandboxed`+// orchestrator actually routes through — was not.++#[test]+fn sandbox_env_forwards_windows_bootstrap_vars() {+ crate::agent::platform_shell::assert_forwards_windows_bootstrap(+ SANDBOX_ENV_PASSTHROUGH,+ "sandbox::ops::SANDBOX_ENV_PASSTHROUGH",+ );+}++// What a real child actually receives is covered end-to-end in+// `tests/windows_sandbox_env_e2e.rs`, which spawns through `execute_in_sandbox`+// and asserts on the child's own environment. It lives there because the probe+// has to be a Rust `CreateProcess` spawn: `node`'s own `child_process.spawn`+// silently injects `SystemRoot`, so a JavaScript probe reports a stripped+// environment as healthy.diff --git a/crates/openhuman-core/src/tools/impl/system/node_exec_tests.rs b/crates/openhuman-core/src/tools/impl/system/node_exec_tests.rsindex 62d4187f49..9615701f33 100644--- a/crates/openhuman-core/src/tools/impl/system/node_exec_tests.rs+++ b/crates/openhuman-core/src/tools/impl/system/node_exec_tests.rs@@ -150,3 +150,14 @@ fn resolve_script_path_targets_action_dir_not_workspace_dir() {resolved.display());}++/// `node_exec` clears the child environment, so its allow-list must carry the+/// Windows process-bootstrap set — without `SystemRoot` a spawned `node.exe`+/// aborts with `Assertion failed: ncrypto::CSPRNG(nullptr, 0)`.+#[test]+fn safe_env_vars_cover_windows_bootstrap() {+ crate::agent::platform_shell::assert_forwards_windows_bootstrap(+ SAFE_ENV_VARS,+ "node_exec::SAFE_ENV_VARS",+ );+}diff --git a/crates/openhuman-core/src/tools/impl/system/python_exec_tests.rs b/crates/openhuman-core/src/tools/impl/system/python_exec_tests.rsindex b8b87da3f8..94bdbe59a2 100644--- a/crates/openhuman-core/src/tools/impl/system/python_exec_tests.rs+++ b/crates/openhuman-core/src/tools/impl/system/python_exec_tests.rs@@ -37,3 +37,13 @@ fn resolve_script_path_rejects_escapes() {std::path::Path::new("/ws/scripts/run.py"));}++/// `python_exec` clears the child environment, so its allow-list must carry the+/// Windows process-bootstrap set — a child without them fails to start.+#[test]+fn safe_env_vars_cover_windows_bootstrap() {+ crate::agent::platform_shell::assert_forwards_windows_bootstrap(+ SAFE_ENV_VARS,+ "python_exec::SAFE_ENV_VARS",+ );+}diff --git a/tests/windows_sandbox_env_e2e.rs b/tests/windows_sandbox_env_e2e.rsnew file mode 100644index 0000000000..408584e94c--- /dev/null+++ b/tests/windows_sandbox_env_e2e.rs@@ -0,0 +1,214 @@+//! Windows child-environment e2e for OpenHuman's own sandbox execution path.+//!+//! Both `sandbox::ops` exec functions call `Command::env_clear()` and re-forward+//! only `SANDBOX_ENV_PASSTHROUGH`, so that list is the entire environment a+//! sandboxed child gets. This test measures what a real child sees when spawned+//! through `execute_in_sandbox` — the same function the `shell`, `node_exec`,+//! `npm_exec` and `python_exec` tools route into whenever the active agent is+//! `SandboxMode::Sandboxed` (which the built-in `orchestrator` is).+//!+//! The defect this pins: the list carried no Windows process-bootstrap+//! variables. A child without `SystemRoot` cannot initialise the OS crypto+//! provider, and the failures are opaque to the agent rather than diagnostic:+//!+//! - `node.exe` aborts at startup — `Assertion failed: ncrypto::CSPRNG(nullptr, 0)`, exit 134.+//! - `powershell.exe` exits with `Internal Windows PowerShell error. Loading+//! managed Windows PowerShell failed with error 8009001d.`+//!+//! Note on method: these must be spawned from Rust. `node`'s own+//! `child_process.spawn` (libuv) injects `SystemRoot` into the child, and a+//! probe driven through `cmd.exe` from JavaScript reports it as present, so a+//! JS harness cannot observe the stripping at all.++use std::collections::HashMap;+use std::time::Duration;++use openhuman_core::agent::harness::definition::SandboxMode;+use openhuman_core::config::RuntimeConfig;+use openhuman_core::sandbox::ops::{+ execute_in_sandbox, resolve_sandbox_policy, SANDBOX_ENV_PASSTHROUGH,+};++/// Run `command` through OpenHuman's sandbox execution path under `mode`.+async fn run_in_sandbox(+ mode: SandboxMode,+ command: &str,+) -> openhuman_core::sandbox::types::SandboxExecResult {+ let tempdir = tempfile::tempdir().expect("tempdir");+ let policy = resolve_sandbox_policy(mode, tempdir.path(), &RuntimeConfig::default(), false);+ execute_in_sandbox(+ &policy,+ command,+ tempdir.path(),+ HashMap::new(),+ Duration::from_secs(120),+ )+ .await+ .unwrap_or_else(|e| panic!("execute_in_sandbox({mode:?}) failed to run the command: {e}"))+}++/// Every allow-list that clears the child environment must cover the Windows+/// bootstrap set. Runs on every OS: the list is platform-independent by design,+/// and on Unix the extra names simply never resolve in the parent environment.+#[test]+fn sandbox_allowlist_covers_windows_bootstrap() {+ openhuman_core::agent::platform_shell::assert_forwards_windows_bootstrap(+ SANDBOX_ENV_PASSTHROUGH,+ "sandbox::ops::SANDBOX_ENV_PASSTHROUGH",+ );+}++#[cfg(windows)]+fn env_probe_command() -> &'static str {+ "echo SR=[%SystemRoot%] TEMP=[%TEMP%] UP=[%USERPROFILE%] PATH=[%PATH%]"+}++/// `cmd.exe` prints `%SystemRoot%` *verbatim* when the variable is absent, which+/// makes non-expansion the assertion. A resolved value is also required, so a+/// child that echoes nothing cannot pass.+#[cfg(windows)]+fn assert_bootstrap_env_resolved(result: &openhuman_core::sandbox::types::SandboxExecResult) {+ assert!(+ result.success(),+ "child exited {} with stderr {:?}",+ result.exit_code,+ result.stderr+ );+ for name in ["SystemRoot", "TEMP", "USERPROFILE", "PATH"] {+ assert!(+ !result.stdout.contains(&format!("%{name}%")),+ "`{name}` reached the child unexpanded, so it is not in the \+ forwarded environment; stdout {:?}",+ result.stdout+ );+ }+ let system_root = bracketed(&result.stdout, "SR=").unwrap_or_default();+ assert!(+ system_root.contains(':'),+ "`SystemRoot` did not resolve to a directory; stdout {:?}",+ result.stdout+ );+ assert!(+ !bracketed(&result.stdout, "UP=")+ .unwrap_or_default()+ .is_empty(),+ "`USERPROFILE` resolved to nothing; stdout {:?}",+ result.stdout+ );+}++#[cfg(windows)]+fn bracketed(stdout: &str, prefix: &str) -> Option<String> {+ let tail = stdout.split(prefix).nth(1)?;+ Some(tail.split(']').next()?.trim().to_string())+}++/// The unsandboxed exec path (`SandboxMode::None`), which shares the+/// allow-list with the jail path and has no OS-jail dependency.+#[cfg(windows)]+#[tokio::test]+async fn unsandboxed_child_receives_windows_bootstrap_env() {+ let result = run_in_sandbox(SandboxMode::None, env_probe_command()).await;+ assert_bootstrap_env_resolved(&result);+}++/// The route the built-in orchestrator actually takes:+/// `SandboxMode::Sandboxed` → `SandboxBackendKind::Local` →+/// `execute_local_jail`.+#[cfg(windows)]+#[tokio::test]+async fn sandboxed_child_receives_windows_bootstrap_env() {+ let result = run_in_sandbox(SandboxMode::Sandboxed, env_probe_command()).await;+ assert_bootstrap_env_resolved(&result);+}++/// The reported Node failure, end to end through OpenHuman's spawn code:+/// `node -e` must reach the crypto provider and print random bytes.+#[cfg(windows)]+#[tokio::test]+async fn node_crypto_runs_through_sandbox_path() {+ if !tool_available("node") {+ eprintln!("skipping node_crypto_runs_through_sandbox_path: `node` is not on PATH");+ return;+ }++ let result = run_in_sandbox(+ SandboxMode::None,+ r#"node -e "console.log('SR=' + process.env.SystemRoot); console.log(require('crypto').randomBytes(8).toString('hex'))""#,+ )+ .await;++ assert!(+ !result.stderr.contains("ncrypto"),+ "node's OS random provider failed to initialise — the child \+ environment is missing Windows bootstrap variables; stderr {:?}",+ result.stderr+ );+ assert!(+ result.success(),+ "node aborted through the sandbox path (exit {}); stderr {:?}",+ result.exit_code,+ result.stderr+ );+ assert!(+ result+ .stdout+ .lines()+ .any(|line| line.trim().len() == 16+ && line.trim().chars().all(|c| c.is_ascii_hexdigit())),+ "no random bytes were printed; stdout {:?}",+ result.stdout+ );+}++/// The reported PowerShell failure: `8009001d` when the child has no system+/// directory to load its crypto provider from.+#[cfg(windows)]+#[tokio::test]+async fn powershell_runs_through_sandbox_path() {+ if !tool_available("powershell") {+ eprintln!("skipping powershell_runs_through_sandbox_path: `powershell` is not on PATH");+ return;+ }++ let result = run_in_sandbox(+ SandboxMode::None,+ "powershell -NoProfile -Command [guid]::NewGuid().ToString()",+ )+ .await;++ assert!(+ !result.stderr.contains("8009001d"),+ "PowerShell could not load managed PowerShell — the child environment \+ is missing Windows bootstrap variables; stderr {:?}",+ result.stderr+ );+ assert!(+ result.success(),+ "powershell failed through the sandbox path (exit {}); stderr {:?}",+ result.exit_code,+ result.stderr+ );+ let guid: String = result+ .stdout+ .trim()+ .chars()+ .filter(|c| c.is_ascii_hexdigit() || *c == '-')+ .collect();+ assert!(+ guid.len() >= 36,+ "no GUID was produced by the child; stdout {:?}",+ result.stdout+ );+}++#[cfg(windows)]+fn tool_available(program: &str) -> bool {+ std::process::Command::new(program)+ .arg("--version")+ .stdout(std::process::Stdio::null())+ .stderr(std::process::Stdio::null())+ .status()+ .map(|s| s.success())+ .unwrap_or(false)+}