Skip to content

The agent that grows with you

test(code-execution): assert lockdown behaviour, not command text

kshitijk4poorf49c9d738f0f
diff --git a/tests/tools/test_code_execution.py b/tests/tools/test_code_execution.py
index a8a753e06bb..723f4fad853 100644
--- a/tests/tools/test_code_execution.py
+++ b/tests/tools/test_code_execution.py
@@ -265,17 +265,41 @@ class TestRemoteSharedHostLockdown(unittest.TestCase):
self.assertFalse(any(token in c for c in commands),
f"{env_name} token appeared in a remote command line")
run_cmd = next(c for c in commands if "python3 script.py" in c)
- # The env file is sourced inside a subshell so set -a's exports never
- # reach the backend's session-snapshot dump (issue #71296 class); exec
- # keeps the script's exit code as the command's.
- self.assertIn("( set -a", run_cmd)
- self.assertIn(". ./sandbox.env", run_cmd)
- self.assertIn("exec python3 script.py", run_cmd)
self.assertNotIn("HERMES_RPC_TOKEN=", run_cmd)
+ if sys.platform == "win32":
+ return
+ # Behaviour, not command text: replay the recorded setup + launch
+ # commands through a real shell against a private temp root.
+ import shutil
+ import subprocess
+ import tempfile
mkdir_cmd = next(c for c in commands
if "mkdir -p" in c and "hermes_exec_" in c)
- self.assertIn("umask 077", mkdir_cmd)
- self.assertIn("chmod 700", mkdir_cmd)
+ sandbox = next(c.args[1] for c in ship_mock.call_args_list
+ if c.args[1].endswith("sandbox.env")).rsplit("/", 1)[0]
+ root = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, root, True)
+ local = root + sandbox
+ sh = lambda c: subprocess.run(["bash", "-c", c.replace(sandbox, local)],
+ capture_output=True, text=True)
+ self.assertEqual(sh(mkdir_cmd).returncode, 0)
+ for d in (local, f"{local}/rpc"):
+ self.assertEqual(os.stat(d).st_mode & 0o777, 0o700, d)
+ env_ship = next(c for c in ship_mock.call_args_list
+ if c.args[1].endswith("sandbox.env"))
+ token = next(l for l in env_ship.args[2].splitlines()
+ if l.startswith("HERMES_RPC_TOKEN="))
+ token = token.split("=", 1)[1].strip("'\"")
+ with open(f"{local}/sandbox.env", "w") as fh:
+ fh.write(env_ship.args[2])
+ with open(f"{local}/script.py", "w") as fh:
+ fh.write("import os, sys\nprint(os.environ['HERMES_RPC_TOKEN'])\nsys.exit(7)\n")
+ # The env reaches the child, its exit code is the command's, and the
+ # token never leaks into the outer shell (the backend's session
+ # snapshot dump; issue #71296 class).
+ run = sh(run_cmd + ' ; echo "[${HERMES_RPC_TOKEN:-}]"')
+ self.assertEqual(run.stdout.splitlines(), [token, "[]"], run.stderr)
+ self.assertEqual(sh(run_cmd).returncode, 7)
@unittest.skipIf(sys.platform == "win32", "UDS not available on Windows")
diff --git a/tests/tools/test_code_kernel_remote.py b/tests/tools/test_code_kernel_remote.py
index 27be21427df..a0b1eaba884 100644
--- a/tests/tools/test_code_kernel_remote.py
+++ b/tests/tools/test_code_kernel_remote.py
@@ -359,30 +359,28 @@ class TestSharedHostLockdown(RemoteKernelBase):
any(kernel.rpc_token in c for c in env.commands),
"rpc token appeared in a remote command line")
spawn_cmd = next(c for c in env.commands if "nohup" in c)
- # The env file is sourced inside a subshell so set -a's exports never
- # reach the backend's session-snapshot dump (issue #71296 class).
- self.assertIn("( set -a", spawn_cmd)
- self.assertIn(". ./kernel.env", spawn_cmd)
- self.assertIn("rm -f ./kernel.env", spawn_cmd)
self.assertNotIn("HERMES_RPC_TOKEN=", spawn_cmd)
- # Every dir under the shared temp dir is owner-only (mkdir -p's -m
- # applies only to the leaf, so the chmod must name all three; umask 077
- # covers the creation-time window).
mkdir_cmd = next(c for c in env.commands if "mkdir -p" in c)
- self.assertIn("umask 077", mkdir_cmd)
- self.assertIn("chmod 700", mkdir_cmd)
- for d in (kernel.kernel_dir, f"{kernel.kernel_dir}/cells",
- f"{kernel.kernel_dir}/rpc"):
- self.assertIn(d, mkdir_cmd)
+ if sys.platform != "win32":
+ # Behaviour, not command text: replay the recorded dir setup
+ # through a real shell under a private temp root; every dir under
+ # the shared temp dir must come out owner-only.
+ import shutil
+ import subprocess
+ import tempfile
+ root = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, root, True)
+ local = root + kernel.kernel_dir
+ r = subprocess.run(["bash", "-c", mkdir_cmd.replace(kernel.kernel_dir, local)],
+ capture_output=True, text=True)
+ self.assertEqual(r.returncode, 0, r.stderr)
+ for d in (local, f"{local}/cells", f"{local}/rpc"):
+ self.assertEqual(os.stat(d).st_mode & 0o777, 0o700, d)
# Ships write owner-only; on a pipe-capable backend the payload rides
# stdin, so the base64 (which decodes to the token for kernel.env)
# never enters argv either.
ship_cmds = [c for c in env.commands if "base64 -d" in c]
- self.assertTrue(ship_cmds)
- self.assertTrue(all("umask 077" in c for c in ship_cmds))
self.assertTrue(any("kernel.env" in c for c in ship_cmds))
- self.assertFalse(any("echo '" in c for c in ship_cmds),
- "payload echoed into argv on a stdin-capable backend")
import base64
env_ship = next(p for p, c in zip(env.stdin_payloads, env.commands)
if p and "kernel.env" in c)
diff --git a/tests/tools/test_tool_result_storage.py b/tests/tools/test_tool_result_storage.py
index 324477e9850..6d9b228ce32 100644
--- a/tests/tools/test_tool_result_storage.py
+++ b/tests/tools/test_tool_result_storage.py
@@ -1,5 +1,9 @@
"""Tests for tools/tool_result_storage.py -- 3-layer tool result persistence."""
+import os
+import subprocess
+import sys
+
import pytest
from unittest.mock import MagicMock, patch
@@ -40,20 +44,28 @@ class TestGeneratePreview:
# ── _write_to_sandbox ─────────────────────────────────────────────────
class TestWriteToSandbox:
- def test_success(self):
+ def test_success(self, tmp_path):
env = MagicMock()
env.execute.return_value = {"output": "", "returncode": 0}
- result = _write_to_sandbox("hello world", "/tmp/hermes-results/abc.txt", env)
+ remote = str(tmp_path / "shared" / "hermes-results" / "abc.txt")
+ if sys.platform != "win32":
+ # Run the real commands so the owner-only modes are observed, not
+ # pattern-matched.
+ def _bash(cmd, timeout=None, stdin_data=None, **_kw):
+ r = subprocess.run(["bash", "-c", cmd], input=stdin_data or "",
+ capture_output=True, text=True, timeout=timeout)
+ return {"output": r.stdout + r.stderr, "returncode": r.returncode}
+ env.execute.side_effect = _bash
+ result = _write_to_sandbox("hello world", remote, env)
assert result is True
- # First call is the write; a second call round-trip-verifies the
- # persisted size (unparseable probe output = best-effort success).
cmd = env.execute.call_args_list[0][0][0]
- assert "mkdir -p" in cmd
- # The storage dir sits under shared temp and holds tool output that can
- # contain secrets: it must be owner-only at creation, and the umask must
- # cover the cat redirect too.
- assert "umask 077" in cmd
- assert "chmod 700" in cmd
+ if sys.platform != "win32":
+ # The storage dir sits under shared temp and holds tool output that
+ # can contain secrets: dir and archive are owner-only.
+ assert os.stat(os.path.dirname(remote)).st_mode & 0o777 == 0o700
+ assert os.stat(remote).st_mode & 0o777 == 0o600
+ with open(remote, encoding="utf-8") as fh:
+ assert fh.read() == "hello world"
# Content travels through stdin, NOT inside the command string —
# otherwise large content would hit Linux's 128 KB MAX_ARG_STRLEN
# ceiling on `bash -c <cmd>` (#22906).