Skip to content

The agent that grows with you

fix: keep reading scripts named inside masked heredoc bodies

teknium156e563d3a414
diff --git a/cron/lifecycle_guard.py b/cron/lifecycle_guard.py
index 5b9f41dc6a5..0ff98ff083e 100644
--- a/cron/lifecycle_guard.py
+++ b/cron/lifecycle_guard.py
@@ -932,10 +932,19 @@ def _contains_unsafe_gateway_action(
if recurse(payload, cwd):
return True
- for script_path in _iter_referenced_shell_scripts(walk_command, cwd=cwd):
+ # Paths named only inside a masked body are still READ: an interpreter body that hands
+ # `/x/restart.sh` to os.system() executes it. Only the fail-closed verdicts (cloud placeholder,
+ # oversized/binary) stay restricted to the masked view — a mere data mention must not trip them.
+ candidates = [(path, True) for path in _iter_referenced_shell_scripts(walk_command, cwd=cwd)]
+ if walk_command != command:
+ candidates += [(path, False) for path in _iter_referenced_shell_scripts(command, cwd=cwd)]
+
+ for script_path, executed in candidates:
# Do not touch a FileProvider path even to discover whether the file is hydrated.
if _on_cloud_path(script_path):
- return True
+ if executed:
+ return True
+ continue
resolved = _resolve_lenient(script_path)
if resolved in visited:
continue
@@ -946,7 +955,9 @@ def _contains_unsafe_gateway_action(
# remainder fails closed exactly like an oversized one.
script_text, unsafe = _read_referenced_script(script_path, max_bytes=budget.bytes_remaining)
if unsafe:
- return True
+ if executed:
+ return True
+ continue
if script_text is None and read_remote_script is not None:
# Local path missing; the remote backend's output crosses the same trust boundary as a
# local read — sanitize identically (binary skip + size fail-closed).
@@ -956,7 +967,9 @@ def _contains_unsafe_gateway_action(
read_remote_script(str(script_path)), max_bytes=budget.bytes_remaining
)
if unsafe:
- return True
+ if executed:
+ return True
+ continue
if not script_text:
continue
# Relative references inside a script resolve against that script's directory, not the cwd.
diff --git a/tests/cron/test_lifecycle_guard_heredoc_walk.py b/tests/cron/test_lifecycle_guard_heredoc_walk.py
index 54cd8ea155a..c53e22f39d7 100644
--- a/tests/cron/test_lifecycle_guard_heredoc_walk.py
+++ b/tests/cron/test_lifecycle_guard_heredoc_walk.py
@@ -25,3 +25,12 @@ def test_unquoted_heredoc_body_path_still_walked(tmp_path):
"""An expansion-capable body is not provably inert: the walk still sees it and fails closed."""
big = _big_file(tmp_path)
assert guard(f"cat > /tmp/x <<EOF\n{big}\nEOF", cwd=str(tmp_path)) is True
+
+
+def test_inert_heredoc_body_script_path_still_read(tmp_path):
+ """Masking hides the body from the *executed* view only: a lifecycle script named inside a
+ Python body is still handed to ``os.system`` at runtime, so its contents must still be read."""
+ script = tmp_path / "restart.sh"
+ script.write_text("#!/bin/sh\nhermes gateway restart\n")
+ command = f"python3 - <<'PY'\nimport os\nos.system('{script}')\nPY"
+ assert guard(command, cwd=str(tmp_path)) is True