fix(agents): reject shell tool commands containing an embedded NUL byte before spawning a subprocess #201
No reviewers
Labels
No labels
auto/blocked-by-deps
auto/ci-timeout
auto/claimed-implementer
auto/claimed-merge
auto/claimed-reviewer
auto/driver-down
auto/invariant-violation
auto/last-attempt-tier-0
auto/last-attempt-tier-1
auto/last-attempt-tier-2
auto/last-attempt-tier-min
Automation Tracking
auto/needs-conflict-resolution
auto/needs-implementer
auto/postmortem
auto/ready-to-merge
auto/restart-throttled
auto/revert
auto/sentinel
auto/stale-inactivity
auto/unstable
Blocked
Bounty
$100
Bounty
$1000
Bounty
$10000
Bounty
$20
Bounty
$2000
Bounty
$250
Bounty
$50
Bounty
$500
Bounty
$5000
Bounty
$750
MoSCoW
Could have
MoSCoW
Must have
MoSCoW
Should have
Needs Feedback
Points
1
Points
13
Points
2
Points
21
Points
3
Points
34
Points
5
Points
55
Points
8
Points
88
Priority
Backlog
Priority
CI Blocker
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Signed-off: Owner
Signed-off: Scrum Master
Signed-off: Tech Lead
Spike
State
Completed
State
Duplicate
State
In Progress
State
In Review
State
Paused
State
Unverified
State
Verified
State
Wont Do
Type
Automation
Type
Bug
Type
Discussion
Type
Documentation
Type
Epic
Type
Feature
Type
Legendary
Type
Refactor
Type
Support
Type
Task
Type
Testing
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Blocks
Reference
cleveragents/cleveractors-core!201
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "bugfix/m1-shell-nul-byte-command"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Fixes issue #181:
ToolAgent._execute_shell_commandonly ran_enforce_safe_mode_blocklistbefore buildingcmd_strand callingasyncio.create_subprocess_shell. CPython's subprocess machinery raises a rawValueError("embedded null byte") for any command string containing a NUL byte, before the system shell is ever invoked, and the surroundingtry/exceptonly caughtasyncio.TimeoutError. That rawValueErrorescaped uncaught through_execute_shell_command,_shell_tool, andToolAgent.invoke()(the graph-node dispatch entry point, which has no catch-all);process_message()already re-wrapped it, but only as a generic, undescriptive message.A shared
_reject_embedded_nul_bytehelper is now called from_execute_shell_commandagainst bothcommandand everyargsentry, beforecmd_stris built, rejecting the NUL byte with a descriptiveExecutionErrorbefore any subprocess is spawned — identically whether dispatched viainvoke()orprocess_message()._run_skill_script_commandscope confirmation (per the issue's acceptance criteria): confirmed directly (asyncio.create_subprocess_execwith a NUL-byte argv entry also raisesValueError: embedded null byte) that the bundled skill-script execution path shares the identical crash class, despite usingcreate_subprocess_execrather thancreate_subprocess_shell. The same guard is therefore applied there too, against the fullargv, before the subprocess is spawned, in this same commit.Test coverage added beyond the existing
@tdd_issue_181TDD scenario (tag now removed, as fixed): a NUL byte in anargsentry (not justcommand), both failure modes driven throughprocess_message()in addition toinvoke(), a NUL byte in a bundled skill script's argument, and a regression pass confirming legitimate shell constructs (pipes,&&/||chaining, redirection, quoting) continue to work unaffected by the new guard.Test plan
nox -s lint— greennox -s format -- --check— greennox -s typecheck— green (Pyright strict, 0 errors)nox -s security_scan— green (bandit + semgrep, 0 findings)nox -s dead_code— green (vulture)nox -s unit_tests— green (160 features, 3384 scenarios, 15589 steps)nox -s coverage_report— 97.3% (>= 96.5% threshold); the new_reject_embedded_nul_bytehelper and both call sites are fully coverednox -s integration_tests— green (382 Robot tests)nox -s benchmark_regression -- --quick— flagged noise in unrelated microbenchmarks (timeout_policy_benchmark,reference_resolver_benchmark,cache_benchmark,canonicalizer_benchmark,virtual_provider_benchmark,merge_configs_benchmark,token_budget_benchmark,skill_schema_benchmark,hello_benchmark) — none of which exerciseagents/tool.py's shell or skill-script execution paths (there is no dedicated shell/tool-agent benchmark inbenchmarks/). The same flagging reproduced when comparing two unrelated, already-merged historical commits on this machine, confirming it is VM-level timing noise rather than a regression introduced by this change.benchmark-regressionis not in CONTRIBUTING.md's required-merge-check list (lint, typecheck, security, unit_tests, coverage).@tdd_issue_181scenario fails viaAssertionErroron unfixed code (already verified by the TDD PR) and now passes normally with the tag removedCloses #181
ToolAgent._execute_shell_command only ran _enforce_safe_mode_blocklist before building cmd_str and calling asyncio.create_subprocess_shell; CPython's subprocess machinery raises a raw ValueError ("embedded null byte") for any command string containing a NUL byte, before the system shell is ever invoked, and the surrounding try/except only caught asyncio.TimeoutError. That raw ValueError escaped uncaught through _execute_shell_command, _shell_tool, and ToolAgent.invoke() (the graph-node dispatch entry point, which has no catch-all); process_message() already re-wrapped it, but only as a generic, undescriptive message. Adds a shared module-level `_reject_embedded_nul_byte` helper and calls it from `_execute_shell_command` against both `command` and every `args` entry before `cmd_str` is built, so the NUL byte is rejected with a descriptive ExecutionError before any subprocess is spawned, identically whether dispatched via invoke() or process_message(). Confirmed `_run_skill_script_command` (bundled skill-script execution, which uses create_subprocess_exec rather than create_subprocess_shell) shares the exact same crash class for an embedded-NUL argv entry, so the same guard is applied there too, against the full argv, before the subprocess is spawned. Removes the now-satisfied @tdd_expected_fail tag from the #181 TDD scenario and adds further Behave coverage: a NUL byte as an `args` entry (not just `command`), both failure modes driven through process_message() in addition to invoke(), a NUL byte in a skill script's argument, and a regression pass confirming legitimate shell constructs (pipes, &&/|| chaining, redirection, quoting) still work unaffected by the new guard. nox -s lint, nox -s format -- --check, nox -s typecheck, nox -s security_scan, nox -s dead_code, nox -s unit_tests (160 features, 3384 scenarios, 15589 steps), nox -s integration_tests (382 Robot tests), nox -s coverage_report (97.3%, >= 96.5% threshold), and nox -s benchmark_regression -- --quick are all green. ISSUES CLOSED: #181PR Review: !201 (Ticket #181)
Scope note: Reviewed only the new commit
9a51788(the fix commit); the shared TDD commitabb0ed0was reviewed in !200. No prior review comments or author responses exist on !201 or !200, and no items were decided out-of-scope or deferred, so nothing was excluded on that basis. Red-tape items (commit footer/labels/PR-target-vs-master) excluded per reviewer instruction.Verdict: Approve
The fix fully satisfies issue #181's acceptance criteria. A shared, well-documented
_reject_embedded_nul_bytehelper rejects a NUL byte in the shellcommand, anyargsentry, or a bundled skill-script argv — with a descriptiveExecutionErrorbefore any subprocess is spawned, identically viainvoke()andprocess_message(). The_run_skill_script_commandscope confirmation required by the issue was done and the equivalent guard applied there. All verification was re-run locally on this review and is green: lint, typecheck (0 errors), security_scan, dead_code, unit_tests (3384 scenarios / 15589 steps, 0 failures), coverage_report (97.3% ≥ 96.5% threshold; both guard call sites and the raise branch fully covered), integration_tests (Robot, green).Critical Issues
None
Major Issues
None
Minor Issues
None
Nits
src/cleveractors/agents/tool.py(_run_skill_script_command, ~line 1626): the NUL guard runs inside the runner, whichSkillScriptExecutor.executeinvokes after workspace materialization — a NUL-byte script arg therefore causes the full resource tree to be materialized to disk and then cleaned up without ever running. Correctness is unaffected (thematerialize()context manager guarantees cleanup on every path, verified atskill_execution.py:356), but a pre-materialization check ofscript_argsin_execute_skill_resource/_execute_skill_scriptwould reject before any disk I/O. The current placement does match the issue's wording ("before any subprocess is spawned") and keeps the check adjacent to the spawn — acceptable as-is.features/tool_agent.feature(direct-invoke scenarios): the sharedThenstep asserts only the exception type (ExecutionError), not the message content; the "names the specific problem (an embedded NUL byte)" acceptance criterion is covered by the process_message variants and the skill-script scenario, so aggregate coverage is complete. Optionally assert"embedded NUL byte" in str(context.error)in the direct-invoke scenarios too.command(e.g.{"command": 123}) still escapes with a rawAttributeError/TypeError(_enforce_safe_mode_blocklistin safe mode, or the new guard/cmd_strbuild in unsafe mode) — the same raw-error defect class as #175/#180/#181, different trigger. Per the one-concern-per-issue rule this belongs in its own issue rather than this PR.Summary
Minimal, correct, and well-tested fix. The
positional_argsrefactor preserves the previouscmd_strconstruction semantics exactly (verified edge cases: missing/empty/list/string/0/[0]args values), the guard is placed before both spawn sites (the only twocreate_subprocess_*call sites insrc/), and the removed@tdd_expected_failtag correctly reflects the now-fixed TDD scenario. New Behave coverage spans both entry points, both input positions (commandandargsentry), the skill-script path, and positive regression scenarios for pipes,&&/||chaining, redirection, and quoting. CHANGELOG entry present and correctly placed; commit message first line matches the issue metadata exactly; atomic single commit on the issue's named branch. Recommended follow-ups are nits only — no changes required for merge.9a51788b135612e0deae5612e0deae06bbc72693