Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cron/lifecycle_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ def _read_referenced_script(path: Path) -> tuple[Optional[str], bool]:
flags = os.O_RDONLY | getattr(os, "O_NONBLOCK", 0)
try:
descriptor = os.open(path, flags)
except OSError:
except (OSError, ValueError):
# ValueError: an embedded NUL byte in the path itself — the token
# came straight from the command string, not from file contents, so
# the #76762 binary-content skip never sees it. No such file can
# exist on POSIX; there is nothing to read.
return None, False
try:
metadata = os.fstat(descriptor)
Expand Down
19 changes: 19 additions & 0 deletions tests/hermes_cli/test_gateway_restart_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,25 @@ def test_absolute_path_binary_does_not_crash_guard(self):
)
assert result is False

def test_nul_byte_in_command_string_does_not_crash_guard(self):
"""A NUL byte in the command string itself must not crash the guard.

#76762 covered NUL bytes read out of a referenced binary's contents,
but a path token containing a literal NUL can also arrive straight
from the command string (e.g. an agent emitting "\\x00" inside a
JSON-encoded command). os.open() raises ValueError — not OSError —
for such a path, which escaped _read_referenced_script's handler and
crashed the guard. A NUL-bearing path can never exist on POSIX, so
it reads as "nothing to scan".
"""
from cron.lifecycle_guard import (
contains_gateway_lifecycle_command_or_referenced_script,
)
result = contains_gateway_lifecycle_command_or_referenced_script(
"bash /tmp/junk\x00fragment.sh"
)
assert result is False

def test_shell_script_reference_walk_still_works(self, tmp_path):
"""The referenced-script walk still applies to real shell scripts:
a .sh script that itself invokes a lifecycle command is caught."""
Expand Down
Loading