fix: address 4-angle review findings on the guard-total change

- _sanitize_remote_script_text: compare re-encoded BYTES against the cap,
  not characters — a >1MiB multibyte file truncated at the head -c byte
  bound decodes to fewer chars than bytes and would have scanned the
  truncated text instead of failing closed (the exact local/remote
  divergence this PR closes).
- terminal_tool: replace the three hardcoded 1MiB literals with
  lifecycle_guard._MAX_REFERENCED_SCRIPT_BYTES so the budget cannot
  drift; use the redirect-safe 'head -c N < path' form from
  tools/image_source.py so leading-dash paths stay out of argv.
- Public guard wrapper: drop the duplicate depth-0 direct scan — the
  walk already runs it; the except-path now falls back to the pure
  string scans, preserving the direct verdict when the walk crashes.
This commit is contained in:
kshitij
2026-08-06 16:03:35 +05:30
committed by kshitij
parent c8d48b8b13
commit c135b88d2d
2 changed files with 24 additions and 19 deletions
+14 -13
View File
@@ -336,21 +336,22 @@ def _sanitize_remote_script_text(text: Optional[str]) -> tuple[Optional[str], bo
The recursion boundary must not trust its callbacks: any backend (SSH,
Modal, Daytona, or a future one) can hand back raw binary bytes decoded
as text, or arbitrarily large output. Mirror
``_read_referenced_script``'s semantics — NUL bytes mean binary
``_read_referenced_script``'s semantics exactly — NUL bytes mean binary
(nothing to scan, checked first, #77703), oversized text fails closed
like an oversized local file (#76762) — so remote and local reads can
never diverge again. The size bound here is in characters rather than
bytes: the downstream cost (shlex tokenization of the text) is
per-character, and decoded characters never exceed source bytes, so
the character bound is at least as strict for scan work. Enforced here
rather than inside each callback so the guarantee holds for every
callback, not just the ones we hardened.
never diverge again. The size check re-encodes to compare *bytes*
(matching the local read and the ``head -c`` wire bound): a >1 MiB
multibyte file truncated at the byte cap decodes to fewer characters
than bytes, and a character-count check would scan the truncated text
instead of failing closed. Enforced here rather than inside each
callback so the guarantee holds for every callback, not just the ones
we hardened.
"""
if not text:
return None, False
if "\x00" in text:
return None, False
if len(text) > _MAX_REFERENCED_SCRIPT_BYTES:
if len(text.encode("utf-8", errors="replace")) > _MAX_REFERENCED_SCRIPT_BYTES:
return None, True
return text, False
@@ -441,11 +442,8 @@ def contains_gateway_lifecycle_command_or_referenced_script(
every terminal command until the gateway restarts (#77780, #78256),
which is strictly worse than either verdict.
"""
if contains_gateway_lifecycle_command(command) or contains_launchctl_submit_command(
command
):
return True
try:
# Includes the direct regex/submit scans at depth 0.
return _contains_unsafe_gateway_action(
command,
cwd=cwd,
@@ -459,7 +457,10 @@ def contains_gateway_lifecycle_command_or_referenced_script(
"falling back to direct-scan verdict",
exc_info=True,
)
return False
# Pure string scans of the top-level command — cannot raise.
return contains_gateway_lifecycle_command(
command
) or contains_launchctl_submit_command(command)
+10 -6
View File
@@ -2503,6 +2503,7 @@ def terminal_tool(
# but applies unconditionally (force=True cannot help here).
if os.environ.get("_HERMES_GATEWAY") == "1":
from cron.lifecycle_guard import (
_MAX_REFERENCED_SCRIPT_BYTES,
contains_gateway_lifecycle_command_or_referenced_script,
contains_launchctl_submit_command,
)
@@ -2542,9 +2543,9 @@ def terminal_tool(
local_path = Path(guard_cwd) / local_path
if local_path.is_file():
metadata = local_path.stat()
if stat.S_ISREG(metadata.st_mode) and metadata.st_size <= 1024 * 1024:
if stat.S_ISREG(metadata.st_mode) and metadata.st_size <= _MAX_REFERENCED_SCRIPT_BYTES:
data = local_path.read_bytes()
if len(data) <= 1024 * 1024:
if len(data) <= _MAX_REFERENCED_SCRIPT_BYTES:
if b"\x00" in data:
# Binary (ELF/Mach-O/PE), not a shell script:
# feeding its decoded bytes back into the guard
@@ -2561,12 +2562,15 @@ def terminal_tool(
# file (e.g. a 166MB ELF invoked by absolute path) never
# crosses the wire — `cat` of such a binary previously pinned
# the gateway's tool thread on a superlinear shlex scan for
# 30+ minutes. One byte over the guard's 1 MiB budget is
# enough for lifecycle_guard's sanitizer to fail the
# oversized case closed, mirroring the local-read semantics.
# 30+ minutes. One byte over the guard's budget is enough for
# lifecycle_guard's sanitizer to fail the oversized case
# closed, mirroring the local-read semantics. The `< path`
# redirect keeps leading-dash paths out of argv (same form as
# tools/image_source.py).
try:
result = env.execute(
f"head -c 1048577 {shlex.quote(script_path)}"
f"head -c {_MAX_REFERENCED_SCRIPT_BYTES + 1} "
f"< {shlex.quote(script_path)}"
)
if result.get("returncode", -1) == 0:
output = result.get("output", "")