test(agents): capture shell tool raw ValueError crash on NUL-byte command bug (#181) #199

Merged
CoreRasurae merged 1 commit from tdd/m1-shell-nul-byte-command into develop 2026-12-01 20:49:43 +00:00
Member

Summary

Adds the mandatory TDD issue-capture test for #181 ("ToolAgent._execute_shell_command lets a command string containing an embedded NUL byte reach the unguarded asyncio.create_subprocess_shell() call, which raises a raw ValueError instead of the tool's own ExecutionError").

ToolAgent._execute_shell_command (src/cleveractors/agents/tool.py) only runs _enforce_safe_mode_blocklist before building cmd_str and calling asyncio.create_subprocess_shell(cmd_str, ...). 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. The surrounding try/except only catches asyncio.TimeoutError, so that ValueError propagates uncaught out of _execute_shell_command, _shell_tool, and ToolAgent.invoke() — unlike process_message(), whose generic except Exception already re-wraps any such crash as an ExecutionError, invoke() (the graph tool-node dispatch entry point) has no catch-all.

This PR extends the existing features/tool_agent.feature / features/steps/tool_agent_steps.py pair (rather than adding a new feature/step file, per the project's step-file rules) with a new scenario tagged @tdd_issue @tdd_issue_181 @tdd_expected_fail:

  • Configures a ToolAgent with allow_shell: true and safe_mode left at its default (true).
  • Calls ToolAgent.invoke("shell", {"command": ...}) directly with a command containing an embedded NUL byte.
  • Asserts the raised exception is an ExecutionError, not a ValueError.

The NUL byte never reaches a real shell process — CPython's own subprocess machinery rejects it at the Python/OS exec-argument layer before create_subprocess_shell spawns anything.

Verified locally that the assertion genuinely fails against current develop (invoke() raises a bare ValueError: embedded null byte instead of ExecutionError), confirming the bug is reproduced; the failing step raises only AssertionError, so the TDD CI hook inverts it correctly and the scenario passes CI with the tag in place.

The actual fix (rejecting an embedded NUL byte in _execute_shell_command before asyncio.create_subprocess_shell is reached) lands separately on bugfix/m1-shell-nul-byte-command per issue #181, once this TDD PR is merged.

Note: per existing project practice (see PR !192), this repository's PRs target develop, not master as issue #183's Definition of Done states.

Closes #183

Test plan

  • nox -s lint — green
  • nox -s format -- --check — green
  • nox -s typecheck — green (0 errors)
  • nox -s security_scan — green (bandit + semgrep, 0 findings)
  • nox -s dead_code — green (vulture, 0 findings)
  • nox -s unit_tests — green (160 features, 3374 scenarios, 15539 steps, including the new TDD scenario passing via @tdd_expected_fail inversion)
  • nox -s integration_tests — green (382/382 Robot Framework tests)
  • nox -s coverage_report — 97.3% (>= 96.5% threshold)
  • nox -s benchmark_regression -- --quick — green (no significant change)
  • Confirmed the new scenario's assertion fails (bare ValueError) when @tdd_expected_fail is removed locally, proving the bug is genuinely reproduced

🤖 Generated with Claude Code

## Summary Adds the mandatory TDD issue-capture test for #181 ("`ToolAgent._execute_shell_command` lets a `command` string containing an embedded NUL byte reach the unguarded `asyncio.create_subprocess_shell()` call, which raises a raw `ValueError` instead of the tool's own `ExecutionError`"). `ToolAgent._execute_shell_command` (`src/cleveractors/agents/tool.py`) only runs `_enforce_safe_mode_blocklist` before building `cmd_str` and calling `asyncio.create_subprocess_shell(cmd_str, ...)`. 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. The surrounding `try`/`except` only catches `asyncio.TimeoutError`, so that `ValueError` propagates uncaught out of `_execute_shell_command`, `_shell_tool`, and `ToolAgent.invoke()` — unlike `process_message()`, whose generic `except Exception` already re-wraps any such crash as an `ExecutionError`, `invoke()` (the graph tool-node dispatch entry point) has no catch-all. This PR extends the existing `features/tool_agent.feature` / `features/steps/tool_agent_steps.py` pair (rather than adding a new feature/step file, per the project's step-file rules) with a new scenario tagged `@tdd_issue @tdd_issue_181 @tdd_expected_fail`: - Configures a `ToolAgent` with `allow_shell: true` and `safe_mode` left at its default (`true`). - Calls `ToolAgent.invoke("shell", {"command": ...})` directly with a `command` containing an embedded NUL byte. - Asserts the raised exception is an `ExecutionError`, not a `ValueError`. The NUL byte never reaches a real shell process — CPython's own subprocess machinery rejects it at the Python/OS exec-argument layer before `create_subprocess_shell` spawns anything. Verified locally that the assertion genuinely fails against current `develop` (`invoke()` raises a bare `ValueError: embedded null byte` instead of `ExecutionError`), confirming the bug is reproduced; the failing step raises only `AssertionError`, so the TDD CI hook inverts it correctly and the scenario passes CI with the tag in place. The actual fix (rejecting an embedded NUL byte in `_execute_shell_command` before `asyncio.create_subprocess_shell` is reached) lands separately on `bugfix/m1-shell-nul-byte-command` per issue #181, once this TDD PR is merged. **Note:** per existing project practice (see PR !192), this repository's PRs target `develop`, not `master` as issue #183's Definition of Done states. Closes #183 ## Test plan - [x] `nox -s lint` — green - [x] `nox -s format -- --check` — green - [x] `nox -s typecheck` — green (0 errors) - [x] `nox -s security_scan` — green (bandit + semgrep, 0 findings) - [x] `nox -s dead_code` — green (vulture, 0 findings) - [x] `nox -s unit_tests` — green (160 features, 3374 scenarios, 15539 steps, including the new TDD scenario passing via `@tdd_expected_fail` inversion) - [x] `nox -s integration_tests` — green (382/382 Robot Framework tests) - [x] `nox -s coverage_report` — 97.3% (>= 96.5% threshold) - [x] `nox -s benchmark_regression -- --quick` — green (no significant change) - [x] Confirmed the new scenario's assertion fails (bare `ValueError`) when `@tdd_expected_fail` is removed locally, proving the bug is genuinely reproduced 🤖 Generated with [Claude Code](https://claude.com/claude-code)
test(agents): capture shell tool raw ValueError crash on NUL-byte command bug (#181)
Some checks failed
CI / lint (pull_request) Successful in 47s
CI / typecheck (pull_request) Successful in 1m50s
CI / unit_tests (pull_request) Has started running
CI / benchmark (pull_request) Has started running
CI / security (pull_request) Successful in 1m39s
CI / build (pull_request) Successful in 2m6s
CI / quality (pull_request) Successful in 2m34s
CI / integration_tests (pull_request) Successful in 4m59s
CI / coverage (pull_request) Has been cancelled
CI / status-check (pull_request) Has been cancelled
df1c708ffb
Adds a failing-first Behave regression test proving issue #181:
ToolAgent._execute_shell_command (src/cleveractors/agents/tool.py) only
runs _enforce_safe_mode_blocklist before building cmd_str and calling
asyncio.create_subprocess_shell(cmd_str, ...). 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. The surrounding try/except only catches asyncio.TimeoutError,
so that ValueError propagates uncaught out of _execute_shell_command,
_shell_tool, and ToolAgent.invoke() -- unlike process_message(), whose
generic except Exception already re-wraps any such crash as an
ExecutionError, invoke() (the graph tool-node dispatch entry point) has
no catch-all.

Extends the existing features/tool_agent.feature /
features/steps/tool_agent_steps.py pair (rather than adding a new
feature/step file, per the project's step-file rules) with a scenario
tagged @tdd_issue @tdd_issue_181 @tdd_expected_fail: configures a
ToolAgent with allow_shell: true and safe_mode at its default (true),
calls ToolAgent.invoke("shell", {"command": ...}) directly with a
command containing an embedded NUL byte, and asserts the raised
exception is an ExecutionError rather than a ValueError. The NUL byte
never reaches a real shell process -- CPython's own subprocess
machinery rejects it at the Python/OS exec-argument layer before
create_subprocess_shell spawns anything.

Verified locally that the assertion genuinely fails against current
develop (invoke() raises a bare ValueError: embedded null byte instead
of ExecutionError), confirming the bug is reproduced; the failing step
raises only AssertionError, so the TDD CI hook inverts it correctly and
the scenario passes CI with the tag in place.

With the tag restored: nox -s lint, nox -s format -- --check, nox -s
typecheck, nox -s security_scan, nox -s dead_code, nox -s unit_tests
(160 features, 3374 scenarios, 15539 steps), nox -s integration_tests
(382 Robot Framework tests), nox -s coverage_report (97.3%, >= 96.5%
threshold), and nox -s benchmark_regression -- --quick (no significant
change) are all green.

The actual fix (rejecting an embedded NUL byte in _execute_shell_command
before asyncio.create_subprocess_shell is reached) lands separately on
bugfix/m1-shell-nul-byte-command per issue #181.

ISSUES CLOSED: #183
Refs: #181
test(agents): capture file_read raw ValueError crash on NUL-byte path bug (#180)
Some checks failed
CI / lint (pull_request) Successful in 1m13s
CI / typecheck (pull_request) Successful in 1m45s
CI / security (pull_request) Successful in 1m16s
CI / build (pull_request) Successful in 51s
CI / quality (pull_request) Successful in 1m55s
CI / integration_tests (pull_request) Successful in 3m24s
CI / unit_tests (pull_request) Successful in 6m40s
CI / coverage (pull_request) Successful in 6m48s
CI / status-check (pull_request) Successful in 2s
CI / benchmark (pull_request) Failing after 18m17s
fe0e6135b8
Adds the regression test for issue #180: ToolAgent._file_read_tool was
reported to let a `file` argument containing an embedded NUL byte
reach the unguarded `os.path.realpath()` call used by its `safe_mode`
symlink-escape check (ADR-2035 D-4), raising a raw ValueError instead
of the tool's own ExecutionError.

Investigation found the bug does not reproduce against current code:
commit fe9ce14 (issue #175's fix for file_write's path validation)
already added a shared `_validate_path_characters()` helper and
explicitly applied it to `_file_read_tool` too, ahead of every branch
that can reach the filesystem. Its `_ILLEGAL_PATH_CHARS` set includes
every ASCII control character 0x00-0x1F, so a NUL byte is rejected
with a descriptive ExecutionError before `os.path.realpath()` is ever
reached -- confirmed directly: `ToolAgent.invoke("file_read", {"file":
"abc\x00def.txt"})` raises `ExecutionError: File path contains
disallowed characters: '\x00'. ...`, not a ValueError. #175 and #180
were filed as distinct issues (per the project's one-concern-per-issue
rule) before #175's fix landed with broader scope than its own ticket
described.

Because there is no live bug to invert, this commit adds a plain
regression-guard scenario to features/tool_agent.feature /
features/steps/tool_agent_steps.py (extending the existing pair, per
the project's step-file rules) tagged only `@tdd_issue` and
`@tdd_issue_180` -- no `@tdd_expected_fail`, since the assertion
already passes outright and TddExpectedFailPolicy fails any
@tdd_expected_fail scenario whose assertion isn't ever inverted (a
"stale tag" signal). The scenario calls `ToolAgent.invoke("file_read",
{"file": ...})` directly with a *relative* NUL-byte path (so it
exercises the exact safe_mode/os.path.realpath() branch #180 named,
rather than the unrelated absolute-path guard) and asserts an
ExecutionError naming "disallowed characters" is raised.

Validity of the test was verified by temporarily removing the
_validate_path_characters() call from _file_read_tool (simulating the
pre-fe9ce14 state) and re-running the scenario in isolation: it then
fails with "Expected ExecutionError, but got <class 'ValueError'>",
reproducing issue #180's exact signature. The guard was restored
immediately after (src/cleveractors/agents/tool.py carries no diff in
this commit); nox -s unit_tests was re-run clean afterward to confirm.

With the regression scenario in place: nox -s lint, nox -s format --
check, nox -s typecheck, nox -s security_scan, nox -s dead_code, nox -s
unit_tests (160 features, 3375 scenarios, 15543 steps), and nox -s
coverage_report (97.3%, >= 96.5% threshold) are all green. No src/,
robot/, or benchmarks/ files changed in this commit, so the
previously-verified nox -s integration_tests (382 Robot tests) and
nox -s benchmark_regression -- --quick (no significant change) results
from the prior commit on this branch remain valid and were not re-run.

No CHANGELOG.md entry: this is a test-only commit with no behavior
change, consistent with project precedent for TDD/regression-only
commits.

This finding (bug #180 already fixed, no separate bugfix PR needed) is
left for maintainer triage on issues #180 and #182 rather than closed
here.

Refs: #180, #182
CoreRasurae changed title from test(agents): capture shell tool raw ValueError crash on NUL-byte command bug (#181) to test(agents): capture NUL-byte crashes in shell (#181) and file_read (#180) tools 2026-11-30 12:18:16 +00:00
CoreRasurae force-pushed tdd/m1-shell-nul-byte-command from fe0e6135b8
Some checks failed
CI / lint (pull_request) Successful in 1m13s
CI / typecheck (pull_request) Successful in 1m45s
CI / security (pull_request) Successful in 1m16s
CI / build (pull_request) Successful in 51s
CI / quality (pull_request) Successful in 1m55s
CI / integration_tests (pull_request) Successful in 3m24s
CI / unit_tests (pull_request) Successful in 6m40s
CI / coverage (pull_request) Successful in 6m48s
CI / status-check (pull_request) Successful in 2s
CI / benchmark (pull_request) Failing after 18m17s
to df1c708ffb
Some checks failed
CI / lint (pull_request) Successful in 47s
CI / typecheck (pull_request) Successful in 1m50s
CI / unit_tests (pull_request) Has started running
CI / benchmark (pull_request) Has started running
CI / security (pull_request) Successful in 1m39s
CI / build (pull_request) Successful in 2m6s
CI / quality (pull_request) Successful in 2m34s
CI / integration_tests (pull_request) Successful in 4m59s
CI / coverage (pull_request) Has been cancelled
CI / status-check (pull_request) Has been cancelled
2026-11-30 12:26:47 +00:00
Compare
CoreRasurae changed title from test(agents): capture NUL-byte crashes in shell (#181) and file_read (#180) tools to test(agents): capture shell tool raw ValueError crash on NUL-byte command bug (#181) 2026-11-30 12:27:19 +00:00
hurui200320 left a comment

PR Review: !199 (Ticket #183 / Bug #181)

Verdict: Approve

A clean, well-scoped TDD-capture PR that fully satisfies issue #183's acceptance criteria. I independently verified locally that the bug is genuinely reproduced and that all quality gates pass on the PR head.

Verification performed (local, authoritative)

  • Checked out tdd/m1-shell-nul-byte-command (df1c708) on top of develop (6d2750e): single atomic commit, parent is develop tip, zero merge commits.
  • Bug reproduction confirmed: ran the new scenario directly; the Then step fails with Expected ExecutionError, but got ValueError: embedded null byte — i.e. the underlying assertion genuinely fails on unfixed code and is inverted by the @tdd_expected_fail policy, exactly as the TDD workflow requires. The When step stores the exception in context.error (raising only AssertionError in Then), so the inversion applies correctly.
  • nox -s unit_tests (full suite): 161 features / 3375 scenarios / 15543 steps, all green.
  • nox -s lint: green. nox -s typecheck: 0 errors (one pre-existing unrelated warning about the optional langchain-anthropic-style import).
  • nox -s coverage_report: 97.3% (>= 96.5% configured threshold).

Correctness analysis

  • Crash path traced and confirmed: invoke() → _execute_tool → builtin_tools["shell"] → _shell_tool → _execute_shell_command, whose try around create_subprocess_shell only catches asyncio.TimeoutError; the raw ValueError escapes to the caller. The scenario drives exactly this path.
  • Safety: CPython validates argv for NUL bytes in the parent before fork, so no shell process is ever spawned — the test is safe by construction, as claimed.
  • False-pass guard: "echo abc\x00def" contains none of the safe-mode blocklisted substrings (rm, del, format, shutdown, reboot, kill), so the blocklist cannot make the scenario pass for the wrong reason. Good detail.
  • Lifecycle: once the fix lands, both asserts pass and TddExpectedFailPolicy.on_scenario will flag the stale tag — the intended removal trigger works for this scenario.
  • Checked adjacent code for the same defect class: TimeoutPolicy.resolve() already raises ExecutionError for malformed timeout args, and the ValueError in TimeoutPolicy.__init__ is pre-validated at construction — no adjacent latent issue to flag here.
  • Scope: #181's broader criteria (args-entry NUL byte, process_message() path, valid-command regression) belong to the fix PR, deliberately deferred per the TDD workflow — not gaps in this PR.

Critical Issues

None

Major Issues

None

Minor Issues

None

Nits

  • features/steps/tool_agent_steps.py:292 — the suppression comment reads # pylint: disable=broad-except, but every other occurrence in features/ (10+) uses the house spelling # pylint: disable=broad-exception-caught. Comment-only and inert (the project lints with ruff, not pylint), but worth aligning for consistency. Non-blocking.

Scope exclusions (per author's notes / reviewer policy)

  • PR targets develop rather than the DoD's master: established repository practice (per PR !192), already noted in the PR description — not re-raised.
  • The actual fix being deferred to bugfix/m1-shell-nul-byte-command is the TDD process working as designed, not a deficiency.
  • Issue-dependency wiring between #181/#183 is project bookkeeping, excluded per review policy.

Summary

An exemplary TDD-capture PR: precise scope matching issue #183, safe-by-design reproduction, meaningful (non-tautological) assertions, correct tag lifecycle, honest and detailed PR description, and all quality gates independently verified green. The single nit is a comment-spelling inconsistency. Recommend merging; the fix should then land on bugfix/m1-shell-nul-byte-command per #181.

🤖 Reviewed as GLM 5.3 Flash (bifrost/glm-5.3-flash), acting for @hurui200320.

## PR Review: !199 (Ticket #183 / Bug #181) ### Verdict: Approve A clean, well-scoped TDD-capture PR that fully satisfies issue #183's acceptance criteria. I independently verified locally that the bug is genuinely reproduced and that all quality gates pass on the PR head. ### Verification performed (local, authoritative) - Checked out `tdd/m1-shell-nul-byte-command` (df1c708) on top of `develop` (6d2750e): single atomic commit, parent is develop tip, zero merge commits. - **Bug reproduction confirmed:** ran the new scenario directly; the `Then` step fails with `Expected ExecutionError, but got ValueError: embedded null byte` — i.e. the underlying assertion genuinely fails on unfixed code and is inverted by the `@tdd_expected_fail` policy, exactly as the TDD workflow requires. The When step stores the exception in `context.error` (raising only `AssertionError` in `Then`), so the inversion applies correctly. - `nox -s unit_tests` (full suite): 161 features / 3375 scenarios / 15543 steps, all green. - `nox -s lint`: green. `nox -s typecheck`: 0 errors (one pre-existing unrelated warning about the optional `langchain-anthropic`-style import). - `nox -s coverage_report`: 97.3% (>= 96.5% configured threshold). ### Correctness analysis - Crash path traced and confirmed: `invoke()` → `_execute_tool` → `builtin_tools["shell"]` → `_shell_tool` → `_execute_shell_command`, whose `try` around `create_subprocess_shell` only catches `asyncio.TimeoutError`; the raw `ValueError` escapes to the caller. The scenario drives exactly this path. - Safety: CPython validates argv for NUL bytes in the parent before `fork`, so no shell process is ever spawned — the test is safe by construction, as claimed. - False-pass guard: `"echo abc\x00def"` contains none of the safe-mode blocklisted substrings (`rm`, `del`, `format`, `shutdown`, `reboot`, `kill`), so the blocklist cannot make the scenario pass for the wrong reason. Good detail. - Lifecycle: once the fix lands, both asserts pass and `TddExpectedFailPolicy.on_scenario` will flag the stale tag — the intended removal trigger works for this scenario. - Checked adjacent code for the same defect class: `TimeoutPolicy.resolve()` already raises `ExecutionError` for malformed `timeout` args, and the `ValueError` in `TimeoutPolicy.__init__` is pre-validated at construction — no adjacent latent issue to flag here. - Scope: #181's broader criteria (args-entry NUL byte, `process_message()` path, valid-command regression) belong to the fix PR, deliberately deferred per the TDD workflow — not gaps in this PR. ### Critical Issues None ### Major Issues None ### Minor Issues None ### Nits - `features/steps/tool_agent_steps.py:292` — the suppression comment reads `# pylint: disable=broad-except`, but every other occurrence in `features/` (10+) uses the house spelling `# pylint: disable=broad-exception-caught`. Comment-only and inert (the project lints with ruff, not pylint), but worth aligning for consistency. Non-blocking. ### Scope exclusions (per author's notes / reviewer policy) - PR targets `develop` rather than the DoD's `master`: established repository practice (per PR !192), already noted in the PR description — not re-raised. - The actual fix being deferred to `bugfix/m1-shell-nul-byte-command` is the TDD process working as designed, not a deficiency. - Issue-dependency wiring between #181/#183 is project bookkeeping, excluded per review policy. ### Summary An exemplary TDD-capture PR: precise scope matching issue #183, safe-by-design reproduction, meaningful (non-tautological) assertions, correct tag lifecycle, honest and detailed PR description, and all quality gates independently verified green. The single nit is a comment-spelling inconsistency. Recommend merging; the fix should then land on `bugfix/m1-shell-nul-byte-command` per #181. 🤖 Reviewed as GLM 5.3 Flash (bifrost/glm-5.3-flash), acting for @hurui200320.
CoreRasurae force-pushed tdd/m1-shell-nul-byte-command from df1c708ffb
Some checks failed
CI / lint (pull_request) Successful in 47s
CI / typecheck (pull_request) Successful in 1m50s
CI / unit_tests (pull_request) Has started running
CI / benchmark (pull_request) Has started running
CI / security (pull_request) Successful in 1m39s
CI / build (pull_request) Successful in 2m6s
CI / quality (pull_request) Successful in 2m34s
CI / integration_tests (pull_request) Successful in 4m59s
CI / coverage (pull_request) Has been cancelled
CI / status-check (pull_request) Has been cancelled
to 84c7f89b5f
Some checks failed
CI / security (pull_request) Successful in 58s
CI / lint (pull_request) Successful in 1m10s
CI / build (pull_request) Successful in 42s
CI / quality (pull_request) Successful in 2m11s
CI / benchmark (pull_request) Failing after 19m57s
CI / unit_tests (pull_request) Successful in 6m6s
CI / typecheck (pull_request) Successful in 2m27s
CI / coverage (pull_request) Successful in 6m55s
CI / integration_tests (pull_request) Successful in 3m36s
CI / status-check (pull_request) Successful in 2s
CI / lint (push) Successful in 1m20s
CI / typecheck (push) Successful in 1m33s
CI / quality (push) Successful in 1m51s
CI / security (push) Successful in 2m12s
CI / build (push) Successful in 1m56s
CI / integration_tests (push) Successful in 4m34s
CI / unit_tests (push) Successful in 6m42s
CI / coverage (push) Failing after 13m28s
CI / benchmark (push) Failing after 44m3s
CI / status-check (push) Has been cancelled
2026-12-01 19:56:16 +00:00
Compare
CoreRasurae deleted branch tdd/m1-shell-nul-byte-command 2026-12-01 20:49:44 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveractors-core!199
No description provided.