Skip to content

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

fix: preserve sandbox environment precedence

Steven Enamakel2cdba4cc2f55
diff --git a/crates/openhuman-core/src/sandbox/ops.rs b/crates/openhuman-core/src/sandbox/ops.rs
index 9cf37bdd83..ccf0a7ffa9 100644
--- a/crates/openhuman-core/src/sandbox/ops.rs
+++ b/crates/openhuman-core/src/sandbox/ops.rs
@@ -271,9 +271,6 @@ async fn execute_unsandboxed(
let mut cmd = platform_shell::build_tokio_command(command);
cmd.current_dir(working_dir);
cmd.env_clear();
- for (k, v) in extra_env {
- cmd.env(k, v);
- }
for var in SANDBOX_ENV_PASSTHROUGH {
if let Ok(val) = std::env::var(var) {
if val.is_empty() {
@@ -282,6 +279,9 @@ async fn execute_unsandboxed(
cmd.env(var, val);
}
}
+ for (k, v) in extra_env {
+ cmd.env(k, v);
+ }
platform_shell::forward_windows_bootstrap_env(&mut cmd)?;
let result = tokio::time::timeout(timeout, cmd.output()).await;
@@ -403,9 +403,6 @@ async fn execute_local_jail(
let mut cmd = platform_shell::build_std_command(&wrapped);
cmd.current_dir(working_dir);
cmd.env_clear();
- for (k, v) in extra_env {
- cmd.env(k, v);
- }
for var in SANDBOX_ENV_PASSTHROUGH {
if let Ok(val) = std::env::var(var) {
if val.is_empty() {
@@ -414,6 +411,9 @@ async fn execute_local_jail(
cmd.env(var, val);
}
}
+ for (k, v) in extra_env {
+ cmd.env(k, v);
+ }
platform_shell::forward_windows_bootstrap_env_std(&mut cmd)?;
// Keep every Windows spelling of the temporary directory inside this
// per-call grant. `TEMP`/`TMP` are the variables used by Windows tools;
diff --git a/tests/windows_sandbox_env_e2e.rs b/tests/windows_sandbox_env_e2e.rs
index 8fa8699459..c93b50f0ae 100644
--- a/tests/windows_sandbox_env_e2e.rs
+++ b/tests/windows_sandbox_env_e2e.rs
@@ -20,11 +20,16 @@
//! probe driven through `cmd.exe` from JavaScript reports it as present, so a
//! JS harness cannot observe the stripping at all.
+#[cfg(windows)]
use std::collections::HashMap;
+#[cfg(windows)]
use std::time::Duration;
+#[cfg(windows)]
use openhuman_core::agent::harness::definition::SandboxMode;
+#[cfg(windows)]
use openhuman_core::config::RuntimeConfig;
+#[cfg(windows)]
use openhuman_core::sandbox::ops::{execute_in_sandbox, resolve_sandbox_policy};
/// Run `command` through OpenHuman's sandbox execution path under `mode`.
@@ -32,8 +37,12 @@ use openhuman_core::sandbox::ops::{execute_in_sandbox, resolve_sandbox_policy};
async fn run_in_sandbox(
mode: SandboxMode,
command: &str,
-) -> openhuman_core::sandbox::types::SandboxExecResult {
+) -> (
+ openhuman_core::sandbox::types::SandboxExecResult,
+ std::path::PathBuf,
+) {
let tempdir = tempfile::tempdir().expect("tempdir");
+ let root = tempdir.path().to_path_buf();
let policy = resolve_sandbox_policy(
mode,
tempdir.path(),
@@ -41,7 +50,7 @@ async fn run_in_sandbox(
&RuntimeConfig::default(),
false,
);
- execute_in_sandbox(
+ let result = execute_in_sandbox(
&policy,
command,
tempdir.path(),
@@ -49,7 +58,8 @@ async fn run_in_sandbox(
Duration::from_secs(120),
)
.await
- .unwrap_or_else(|e| panic!("execute_in_sandbox({mode:?}) failed to run the command: {e}"))
+ .unwrap_or_else(|e| panic!("execute_in_sandbox({mode:?}) failed to run the command: {e}"));
+ (result, root)
}
#[cfg(windows)]
@@ -100,6 +110,22 @@ fn assert_bootstrap_env_resolved(result: &openhuman_core::sandbox::types::Sandbo
}
}
+#[cfg(windows)]
+fn assert_temp_is_in_scratch(
+ result: &openhuman_core::sandbox::types::SandboxExecResult,
+ root: &std::path::Path,
+) {
+ let temp = bracketed(&result.stdout, "TEMP=").unwrap_or_default();
+ let expected_prefix = root.join("artifacts").join("sandbox-scratch");
+ let normalize = |path: &str| path.replace('/', "\\").to_ascii_lowercase();
+ assert!(
+ normalize(&temp).starts_with(&normalize(&expected_prefix.to_string_lossy())),
+ "local-jail TEMP escaped its per-test scratch tree: TEMP={temp:?}, expected under {:?}; stdout {:?}",
+ expected_prefix,
+ result.stdout
+ );
+}
+
#[cfg(windows)]
fn bracketed(stdout: &str, prefix: &str) -> Option<String> {
let tail = stdout.split(prefix).nth(1)?;
@@ -111,7 +137,7 @@ fn bracketed(stdout: &str, prefix: &str) -> Option<String> {
#[cfg(windows)]
#[tokio::test]
async fn unsandboxed_child_receives_windows_bootstrap_env() {
- let result = run_in_sandbox(SandboxMode::None, env_probe_command()).await;
+ let (result, _) = run_in_sandbox(SandboxMode::None, env_probe_command()).await;
assert_bootstrap_env_resolved(&result);
}
@@ -121,8 +147,9 @@ async fn unsandboxed_child_receives_windows_bootstrap_env() {
#[cfg(windows)]
#[tokio::test]
async fn sandboxed_child_receives_windows_bootstrap_env() {
- let result = run_in_sandbox(SandboxMode::Sandboxed, env_probe_command()).await;
+ let (result, root) = run_in_sandbox(SandboxMode::Sandboxed, env_probe_command()).await;
assert_bootstrap_env_resolved(&result);
+ assert_temp_is_in_scratch(&result, &root);
}
/// The reported Node failure, end to end through OpenHuman's spawn code:
@@ -135,7 +162,7 @@ async fn node_crypto_runs_through_sandbox_path() {
"node is required for node_crypto_runs_through_sandbox_path; missing tooling must not silently pass"
);
- let result = run_in_sandbox(
+ let (result, _) = run_in_sandbox(
SandboxMode::None,
r#"node -e "console.log('SR=' + process.env.SystemRoot); console.log(require('crypto').randomBytes(8).toString('hex'))""#,
)
@@ -174,7 +201,7 @@ async fn powershell_runs_through_sandbox_path() {
"powershell.exe is required for powershell_runs_through_sandbox_path; missing tooling must not silently pass"
);
- let result = run_in_sandbox(
+ let (result, _) = run_in_sandbox(
SandboxMode::None,
"powershell.exe -NoProfile -Command [guid]::NewGuid().ToString()",
)