Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix(safety): steps run without a shell; say so, and refuse shell syntax - #27

Merged
mkhellat merged 1 commit into
mainfrom
fix/no-shell
Nov 25, 2026
Merged

mkhellat merged 1 commit into
mainfrom
fix/no-shell

Conversation

@mkhellat

Copy link
Copy Markdown
Owner

Command steps run without a shell (the sandbox runs shlex.split then subprocess.run), but the planner was never told. In the maintainer's retest the model planned echo 2+2 | bc, which printed the text 2+2 | bc: echo received | and bc as arguments. The 3B reproduces it in the chat evaluation (#26). Pipes, redirection, &&, ;, command substitution and globbing all silently do the wrong thing.

Change

  • The planner prompt (rule 9) says commands run without a shell, and what to do instead: one program per step, its own options (grep -c, find -name), and file_write to save text. The retry prompt says it in one line.
  • The safety engine refuses a command with an unquoted shell operator or a command substitution, giving the reason: `|` would reach echo as plain text: steps run without a shell. Use one program per step and its own options, or separate steps. Quoting is honoured as a shell would: grep '<div>', echo "a && b" and awk '$1>5' are ordinary arguments.
  • More specific refusals are checked first, so their reasons win: find -exec, the refused arguments.

Review (done locally, before pushing)

  • One finding in the first version, caused by my own spec: it worked on shlex.split words, which drop the quotes, so grep '<div>' f was refused as a redirection. Fixed before commit.
  • The fix is a small quote-aware scanner (_shell_token) over the raw command line. It tracks single and double quotes and backslash escapes like a shell, and treats each unquoted run of | & ; < > as one operator. It also catches $( and backticks outside single quotes, exactly where a shell would expand them.
  • The agent first tried shlex.shlex(punctuation_chars=True) and showed it can't tell '|' from |.
  • Intended consequence: an unquoted & in a URL is refused, because in shell syntax it is an operator; the model has to quote it.

Tests and CI

tests/test_no_shell.py covers the refused cases (including echo a|wc -l, ls>out, cat<in, 2>&1, $(...), backticks, and operators behind timeout) and the allowed quoted cases. The full suite passes (1827), and ruff is clean. CI runs once on this head: it changes the safety engine.

Command steps run without a shell (sandbox: shlex.split, then
subprocess.run), but the planner was never told.  In the maintainer's
retest the model planned `echo 2+2 | bc`, which printed the text
"2+2 | bc": echo received "|" and "bc" as arguments.  Pipes,
redirection, &&, ;, command substitution and globbing all silently do
the wrong thing.

The planner prompt now says commands run without a shell and what to
do instead (one program per step, its own options, file_write to save
text); the retry prompt says it in one line.  The safety engine
refuses a command with an unquoted shell operator or a command
substitution, with the reason: "`|` would reach echo as plain text:
steps run without a shell. ..."  Quoting is honoured as a shell would:
grep '<div>', echo "a && b" and awk '$1>5' are ordinary arguments.
More specific refusals (find -exec, refused arguments) are checked
first, so their reasons win.

* src/codebud/safety.py (_OPERATOR_CHARS, _shell_token)
(shell_syntax_reason): New.
(SafetyEngine._command_refusal): Refuse shell syntax.
* src/codebud/planner.py (Planner._render_prompt): Rule 9.
(Planner._build_retry_prompt): One line on it.
* tests/test_no_shell.py: New file.
* docs/chapters/safety-and-sandbox.texi (The Sandbox): Document it.
* docs/chapters/development.texi: List the test file.
* NEWS: Mention it.
@mkhellat

Copy link
Copy Markdown
Owner Author

Review summary: correct, after one fix made before pushing

One sweep, locally.

Conditions.

  • A command step is refused ⇔ the raw line contains an unquoted run of | & ; < >, or $( or a backtick outside single quotes, unless a more specific refusal (catastrophic, refused arguments, find -exec) fires first.

Finding, fixed: quoted operators (grep '<div>') were refused, because the first version worked on shlex words, which drop quotes. The quote-aware scanner fixes it.

Verified:

  • the scanner's single-quote, double-quote and backslash handling;
  • operators with no spaces (a|wc, ls>out, cat<in), and operators behind wrappers;
  • the planner's rule 9 wording.

1827 tests pass.

Intended consequence: an unquoted & in a URL is refused, as shell syntax would treat it.

Verdict: correct. I'll merge when CI passes.

@mkhellat

Copy link
Copy Markdown
Owner Author

Merging. CI is green on all five jobs at eb2dff9, macOS included.

CI on the merge is skipped, and why. main moved after this branch was cut (#25, #26). I merged this branch onto the current main (f0e7a5b) locally first. The merge was clean, and the merged tree passed the full suite (1850 tests), ruff, and the manual build without warnings. The merge commit therefore carries [skip ci]. I start the docs workflow by hand for the changed manual section.

Measured effect, from scripts/chat_eval.py with this PR combined with #26 and the coming answer-after-steps PR: the 3B's echo 2+2 | bc is now refused with the reason, instead of printing 2+2 | bc. The 3B still writes pipes despite the rule, so the next PR sends a refused shell syntax back to the planner once, with the reason.

@mkhellat
mkhellat merged commit aba82d2 into main Nov 25, 2026
7 checks passed
@mkhellat
mkhellat deleted the fix/no-shell branch November 25, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant