Skip to content

fix: lifecycle guard crashes on referenced scripts with embedded NUL byte paths - #78506

Closed
Duncan822 wants to merge 2 commits into
NousResearch:mainfrom
Duncan822:fix/lifecycle-guard-nul-byte
Closed

fix: lifecycle guard crashes on referenced scripts with embedded NUL byte paths#78506
Duncan822 wants to merge 2 commits into
NousResearch:mainfrom
Duncan822:fix/lifecycle-guard-nul-byte

Conversation

@Duncan822

Copy link
Copy Markdown

Bug

cron/lifecycle_guard.py crashes with ValueError: embedded null byte inside _read_referenced_script when a command references a script whose scanned contents yield a path containing a NUL byte.

Observed on the terminal tool: any command referencing certain scripts (e.g. python /home/user/scripts/bitaxe_thermal_guard.py) fails with:

File "cron/lifecycle_guard.py", line 260, in _read_referenced_script
    descriptor = os.open(path, flags)
ValueError: embedded null byte

Root cause

_iter_referenced_shell_scripts can yield a path containing an embedded NUL byte (binary/decoded content tokenized as a path — the exact case #76762 documents). _contains_unsafe_gateway_action already guards script_path.resolve() with except (OSError, ValueError) (lines 315-320), but then passes the original script_path to _read_referenced_script, whose os.open only catches OSError. os.open raises ValueError for NUL-containing paths, which escapes and crashes the guard (and therefore the tool call).

Fix

Catch ValueError alongside OSError in _read_referenced_script and treat it as "nothing to scan" — consistent with the documented intent that a guarded path must never crash the guard.

Test

  • _read_referenced_script(Path("/path/to/file\x00foo")) now returns (None, False) instead of raising.
  • Normal script scanning unaffected.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cron Cron scheduler and job management tool/terminal Terminal execution and process management duplicate This issue or pull request already exists labels Aug 4, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Duplicate of #77898: both patches catch the same embedded-NUL os.open() ValueError in _read_referenced_script and return the existing no-scan result.

@Duncan822

Copy link
Copy Markdown
Author

Second fix (same root cause, second symptom)

After the first fix, the guard no longer crashes — but it now false-blocks commands that reference a binary in a script position (e.g. /venv/bin/python script.py):

Blocked: command or referenced script cannot restart or stop the gateway...

Cause

_read_referenced_script correctly skips binaries via the NUL-byte check (returns None), but the remote fallback (read_remote_scriptcat <path>) then re-reads the same binary decoded as text (~8 MB with NUL bytes). The recursive scan of that decoded binary explodes to _MAX_REFERENCED_SCRIPT_DEPTH → unconditional True → false block.

Fix

Mirror the local NUL guard in the remote fallback: if the returned text contains \x00, treat it as binary → None (nothing to scan).

Tests

  • python /venv/bin/python script.py → no longer blocked
  • gateway restart command → still blocked (protection intact)
  • NUL path → (None, False), no crash

@Duncan822

Copy link
Copy Markdown
Author

Closing as duplicate of #77898 — the embedded-NUL os.open() ValueError fix is identical there (with a better test).

Important follow-up: while testing this in production I found a second symptom of the same root cause that #77898 does not cover: after the crash is fixed, the guard false-blocks commands that reference a binary in a script position (e.g. /venv/bin/python script.py). The remote fallback (read_remote_scriptcat <path>) re-reads the binary as decoded text (~8 MB with NUL bytes) and the recursive scan explodes to _MAX_REFERENCED_SCRIPT_DEPTH → unconditional block.

I've posted the full fix (mirror the local NUL guard in the remote fallback) as a comment on #77898 with tests. Local copy of both fixes is active here.

@Duncan822 Duncan822 closed this Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cron Cron scheduler and job management duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants