Repository navigation
Studio: treat bash coproc as a command boundary in the terminal safety scanners - #11369
Conversation
…ety scanners
`coproc` is a bash keyword, not a program: the word behind it is a command the
shell runs asynchronously. The command-position parsers read it as the command
word instead, which demoted the real command to an argument, so `coproc rm -f
victim` came back from `_find_blocked_commands` as nothing at all and from
`_terminal_is_high_risk` as not high risk. `_bash_exec` then handed the original
text to `bash -c` and the file was deleted. The same hole covered `pkill`,
`ssh`, `curl` and `wget`, and in permission mode `auto` it skipped the
confirmation prompt as well.
Adding the keyword to `_SHELL_KEYWORDS_AS_SEP` fixes the unnamed form in all
four parsers that share the set. The named form needs more than that: bash
accepts `coproc NAME compound-command` only before a compound command, so
`coproc JOB { rm ...; }` was already caught by the `{` separator while
`coproc JOB if rm ...; then :; fi` walked straight through. `_is_coproc_name`
tells the two apart on that rule, and command position now carries past a name
to the compound behind it. The raw-text `_BLOCKED_WORD_RE` screen behind the
lexer gets the same boundary.
The invariant the tests assert is parity rather than strictness: `coproc X`
classifies exactly as `X` does, in both directions, so a coprocess that ran
before still runs and nothing new starts prompting. `coproc echo hi`,
`coproc MYJOB { cat train.log; }` and `grep -rn coproc tools.py` are all
unchanged.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex security review |
That pass sees no quoting and no command position, so any occurrence of the word became a boundary: `grep coproc rm file`, `echo coproc rm`, `echo 'coproc rm'` and the valid `coproc echo rm` all came back as blocked `rm` and the terminal refused four commands that run nothing. Bash recognizes the keyword only unquoted at command position (`'coproc' echo rm` is "command not found"), which is exactly the context the raw pass lacks and the token scan has. The token scan is where the boundary belongs, and it already carries every coproc case on its own: the unnamed form, the named compound forms, and the command-substitution form through the existing `_SUBST_CMD_SEP` expression.
…onal name Same family as the raw-text screen: `echo coproc JOB if rm -f x; then :; fi` prints three words, and taking JOB for a coprocess name there made the `if` behind it a boundary and blocked a line that runs nothing. The name rule now fires only when the `coproc` itself follows a separator, a keyword, an assignment prefix, or the start of the command, which is where bash reads it as the keyword.
The previous guard decided from the neighbouring tokens, and that cannot be right in both directions: `echo coproc JOB if rm -f x` prints three words while `time coproc JOB if rm -f x` and `time -p coproc ...` really run the compound and delete the file, because `time` is a reserved word taking a pipeline. Each walker already tracks where a command may start, so the keyword is recognized there and a one-token flag carries the optional name, which drops the neighbour heuristic entirely. `_terminal_is_high_risk` needed one more clause: it carries a wrapper's command position in `prefix_pending` rather than `expect_command`, so `time coproc rm -f x` was hard-blocked but still classified not high risk. Both classifiers now answer for `coproc X` exactly what they answer for `X`, with or without a `time` in front. An external wrapper cannot prefix a coprocess at all (`env coproc JOB if ...` is a syntax error in bash 5.2.21). Those spellings are refused rather than picked apart, which costs nothing: they run nothing either way.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 82f4a3541f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (at_command and token in _SHELL_KEYWORDS_AS_SEP) or ( | ||
| after_coproc and _is_coproc_name(tokens, here) | ||
| ): | ||
| coproc_kw = at_command and token == "coproc" |
There was a problem hiding this comment.
Require an unquoted
coproc before opening a boundary
shlex removes quoting, while quoted only records separator punctuation, so this condition also treats an escaped or quoted command word as the Bash keyword. For example, in 'coproc' find . -exec rm -f victim \;, Bash attempts to invoke a program literally named coproc and passes the remaining words as arguments, but this scan marks find as executable and subsequently hard-blocks rm. Preserve whether the coproc token itself was quoted or escaped before using it as a command boundary.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Accurate about the mechanism, but I am leaving it. Checked against bash 5.2.21: 'coproc' find . -exec rm -f victim \; prints "coproc: command not found" and the file survives, so the line runs nothing and refusing it costs a user nothing. The direction matters here, since this is an over-block, not a bypass.
It is also not specific to this change. quoted_separators has only ever covered separator punctuation, so every entry in _SHELL_KEYWORDS_AS_SEP behaves this way already: 'if' true; then rm -rf x; fi is hard-blocked on main today. Threading quoted-token indexes through all four walkers to recover four lines bash refuses to execute is a much larger change to the most security-sensitive parser in the file than the thing it would buy, and it would be a change to existing behaviour rather than to this PR.
| # This walker carries command position through a wrapper in `prefix_pending` rather than | ||
| # `expect_command`, so the keyword clause above misses `time coproc rm -f x`, which bash runs and | ||
| # which really deletes. | ||
| or (token == "coproc" and prefix_pending) |
There was a problem hiding this comment.
Limit prefix-pending
coproc handling to Bash time
prefix_pending is set for every recognized wrapper, not only the time reserved word mentioned in the comment. Consequently, inputs such as env coproc rm -f victim or timeout 1 coproc rm are analyzed as though Bash executes rm; in reality the wrapper attempts to execute a program named coproc, with rm merely an argument. This produces unnecessary high-risk prompts, and the analogous generic-wrapper state in the blocklist walker can hard-block the same benign shape, so coproc should retain keyword status only after the Bash time prefix.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct that prefix_pending is set for every recognized wrapper, but the consequence is an over-block on lines bash does not run, so I am leaving it. Checked against bash 5.2.21 with a real victim file:
env coproc rm -f victim -> env: 'coproc': No such file or directory (victim survives)
timeout 1 coproc rm -> timeout: failed to run command 'coproc' (victim survives)
time coproc rm -f victim -> victim DELETED
time -p coproc JOB if rm -f victim; then :; fi -> victim DELETED
Only the reserved word executes, which is why the clause exists. Narrowing it to time would trade a refusal of two lines that cannot run for an extra condition in the walker that decides whether a command needs confirmation, and getting that condition subtly wrong fails in the unsafe direction rather than the safe one. The blocklist walker was measured the same way and behaves the same: it refuses env coproc rm -f victim, which deletes nothing.
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Quoting the lookahead made it a lie. shlex hands back the same token for a real
`{` and a quoted `'{'`, so `coproc rm '{' -f victim` satisfied the
compound-command test, `rm` was taken for the coprocess name and skipped, and
nothing blocked it. Bash reads that line as the SIMPLE form and it deletes the
file, verified against 5.2.21; `coproc rm 'if' -f victim` is the same trick
with a word.
Rather than thread quoting state for the lookahead through four walkers, a word
on the blocklist is never read as a name. That is right by construction
whichever way the lookahead was spelled, and it fails in the safe direction:
the only thing it costs is `coproc rm { echo hi; }`, which names a coprocess
after a blocked command and is refused.
A quoted lookahead forges the compound-command test for every word, not only
the blocked ones, and the previous commit only covered the blocked ones. On
main `coproc sort '{' -o victim input` read as potentially unsafe; on this
branch it had stopped doing so, which is a detection this PR took away rather
than one it failed to add.
Two changes close it. The starter set keeps only the WORD starters, since `{`
and `(` are separators already: `coproc JOB { rm ...; }` resolves without ever
reading JOB as a name, so listing them bought nothing and meant trusting a
lookahead a quoted `'{'` can forge. And a word is skipped as a name only when
no classifier judges it, which covers the blocklist, the high-risk set, the
commands with unsafe flags and the wrappers.
Both spellings now answer what the command alone answers. `sort '{' -o victim
input` is still not potentially unsafe, but that is equally true of the bare
command on main: an unquoted `{` between a command and its flags resets the
command being judged, which is not about coprocesses.
Skipping a token was the wrong primitive for a security boundary. shlex has
already dropped the quotes by the time a walker sees the lookahead, so every
round found another word that a forged `'{'` or `'if'` could hide: first `rm`,
then `sort -o`, now `sed` and its `-e` program, which executes and which the
allowlist of judged commands did not cover.
Nothing is skipped now. A name-shaped word after `coproc` is scanned exactly as
any other command word, so it is still blocklisted, still glob-matched, still
recorded as a sed whose program needs screening. The only thing the name rule
does is leave command position OPEN afterwards, so the compound behind it is
scanned too. Over-detection is the worst it can now produce, which is the
direction this file should fail in.
That also removes the need for the allowlist and for the name handling in the
two auto-mode classifiers, where an unrecognized word already ends the scan.
`_terminal_is_potentially_unsafe` now answers for `coproc sort '{' -o victim
input` exactly what it answers for the bare command, which is what it did
before this branch.
The blocklist walker was taught to read the name as a command word and keep command position open behind it; the high-risk walker was left resetting at `coproc` and then spending that position on the name. So `coproc JOB if git clean -fd; then :; fi`, which bash runs and which deletes untracked files, came back not high risk and would not have been held for approval in auto mode. `git clean` is not blocklisted, so nothing else caught it. Same treatment as the other walker: the name is judged like any other command word, and only command position carries past it.
A coprocess may be named after a wrapper, since bash cares only that the word
is a name: `coproc env if git clean -fd; then :; fi` deletes untracked files.
The wrapper branch reached that word first, set a pending prefix and skipped
the rest of the loop, so the `if` behind it became the wrapped command and the
real one was demoted to arguments. The auto-mode gate returned not high risk,
and `git clean` is not blocklisted, so nothing else was going to stop it.
The name is resolved before the wrapper branch now, in all three walkers that
have one. An ordinary wrapper is untouched: `env rm -f victim`, `env -u FOO rm
-f victim`, `timeout 5 rm -f victim` and `env find . -exec rm -rf victim {} +`
all still resolve to the command behind the wrapper.
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Confirmed against main that coproc is only handled in _SUBST_CMD_SEP in tools.py, so the plain and named forms walk past both _find_blocked_commands and _terminal_is_high_risk, and the parity tests show benign coprocesses still run unchanged. Will get this reviewed. |
…ety scanners (unslothai#11369) `coproc` is a bash keyword, not a program, and the word behind it is a command the shell runs asynchronously. The command-position parsers read `coproc` as the command word instead, which demoted the real command to an argument, so `coproc rm -f victim` came back from `_find_blocked_commands` as nothing at all and from `_terminal_is_high_risk` as not high risk. `_bash_exec` then handed the original text to `bash -c` and the file was deleted. The same hole covered `pkill`, `ssh`, `curl` and `wget`, and in permission mode `auto` it skipped the confirmation prompt as well. `coproc` joins `_SHELL_KEYWORDS_AS_SEP`, which fixes the unnamed form in all four command-position walkers that share the set. Each recognizes the keyword from the command-position state it already tracks, so `echo coproc rm` stays an argument list while `time coproc rm -f x` and `time -p coproc ...` are caught, `time` being a reserved word that takes a pipeline. The named form is the subtle half. Bash accepts `coproc NAME compound-command` only before a compound command, so the name is a label and the command is behind it. The name is read as a command word and never skipped: it is still blocklisted, still glob-matched, still recorded as a sed whose program needs screening, and the only thing the name rule does is leave command position open so the compound behind it is scanned too. Skipping was tried first and is the wrong primitive here, because shlex has already dropped the quotes by the time a walker sees the lookahead and a forged `'{'` or `'if'` can make any word look like a name. A wrapper must not consume a name either, since a coprocess may legitimately be called `env`. `_BLOCKED_WORD_RE`, the raw-text screen behind the lexer, is deliberately left alone: with no quoting or command-position context, a `coproc` alternative there refuses `grep coproc rm file` and three other lines that run nothing. The invariant the tests assert is parity, not strictness: `coproc X` classifies exactly as `X` does, in both directions, with or without a `time` or a name in front. No route, tool schema, prompt or UI surface is touched. Classification stays O(terminal tool calls) rather than O(tokens): counted through the production stream, a 500 delta prose generation invokes the classifiers zero times, and one terminal call invokes each once, the same as before. Per call the blocklist walker costs 2.5 us more, 108.3 us against 105.8 us. Tests: `test_sandbox_tools.py` 308 passed before the new cases and 335 after, covering the unnamed blocked forms, the named `if` / `{ }` / `for` compounds, the `time` and `time -p` prefixes, the separator and assignment prefixes, the forged-lookahead forms including the sed program and the wrapper-spelled name, and the benign coprocesses that must stay allowed. Cross-platform, scoped to that file: ubuntu 335 passed, macOS 14 336 passed, windows 334 passed.
What this fixes
coprocis a bash keyword, not a program, and the word behind it is a command the shell runs asynchronously. The command-position parsers instudio/backend/core/inference/tools.pyreadcoprocas the command word instead, which demoted the real command to an argument.Both terminal safety gates then passed a command they exist to refuse:
_find_blocked_commands("coproc rm -f victim")returned an empty set, so the hard blocklist in_bash_execlet it through tobash -cand the file was deleted._terminal_is_high_riskmade the same mistake, so in permission modeautothe call was never held for confirmation.The same hole covered the other entries the blocklist exists for:
pkill,ssh,curl,wget.Before and after
Through the real
execute_tool("terminal", ...)dispatcher against a throwaway victim file, withdisable_sandbox=False. Every line below was run against bash 5.2.21 first, to confirm bash executes what the scanner is being asked to catch.rm -f victimBlocked command(s) for safety: rmcoproc rm -f victimBlocked command(s) for safety: rmcoproc JOB if rm -f victim; then :; fiBlocked command(s) for safety: rmtime coproc JOB if rm -f victim; then :; fiBlocked command(s) for safety: rmcoproc rm '{' -f victimBlocked command(s) for safety: rmcoproc sed 'if' -e '1e rm -f victim' inputBlocked command(s) for safety: rmcoproc pkill -f unsloth[], high riskFalse['pkill'], high riskTruecoproc JOB if git clean -fd; then :; fiFalseTruecoproc env if git clean -fd; then :; fiFalseTruecoproc echo hicoproc MYJOB { cat train.log; }grep coproc rm filegit clean -fdis the one that is not on the blocklist at all, so for that shape the confirmation gate is the only thing standing between a model-generated call and deleted untracked files.The change
coprocjoins_SHELL_KEYWORDS_AS_SEP, which fixes the unnamed form in all four command-position walkers that share the set:_find_blocked_commands,_terminal_is_high_risk,_terminal_is_potentially_unsafeand_exec_scan_layout. Each walker recognizes the keyword from the command-position state it already tracks, soecho coproc rmstays an argument list whiletime coproc rm -f xandtime -p coproc ...are caught,timebeing a reserved word that takes a pipeline.The named form is the subtle half. Bash accepts
coproc NAME compound-commandonly before a compound command, soJOBincoproc JOB if rm -f victim; then :; fiis a label and the command is behind it. The rule here is that the name is read as a command word, never skipped: it is still blocklisted, still glob-matched, still recorded as a sed whose program needs screening, and the only thing the name rule does is leave command position open so the compound behind it is scanned too. Skipping was tried first and is simply the wrong primitive for a security boundary, because shlex has already dropped the quotes by the time a walker sees the lookahead and a forged'{'or'if'can make any word look like a name. Over-detection is now the worst the rule can produce.Two details fall out of that. A wrapper must not consume a name, since a coprocess may legitimately be called
env, and the auto-mode walker carries a wrapper's command position inprefix_pendingrather thanexpect_command._BLOCKED_WORD_RE, the raw-text screen behind the lexer, is deliberately left alone. It sees no quoting and no command position, so acoprocalternative there turns every occurrence of the word into a boundary and refusesgrep coproc rm file,echo coproc rm,echo 'coproc rm'and the validcoproc echo rm, none of which run anything.Behaviour and compatibility
The invariant the tests assert is parity, not strictness:
coproc Xclassifies exactly asXdoes, in both directions, with or without atimeor a name in front. A coprocess that ran before still runs, and nothing that used to run silently starts asking for confirmation. No route, tool schema, prompt or UI surface is touched, so Studio, Desktop and the notebooks are unaffected.What it does refuse that bash would not run at all:
env coproc rm -f victimandtimeout 1 coproc rm, where the wrapper tries to exec a program namedcoprocand fails. Refusing a line that cannot run costs nothing, and the alternative is a narrower condition in the walker that decides whether a command needs approval, where being subtly wrong fails in the unsafe direction.Tests
studio/backend/tests/test_sandbox_tools.pycovers the unnamed blocked forms, the namedif/{ }/forcompound forms, thetimeandtime -pprefixed forms, the separator and assignment prefixed forms, the forged-lookahead forms including the sed program and the wrapper-spelled name, the benign coprocesses that must stay allowed,coprocas an ordinary argument, and a parametrized parity check overrm,ssh,curl,echoandcat.tests/test_sandbox_tools.py: 308 passed before the new cases, 335 after, no regressions.tests/test_sandbox_tools.py tests/test_tool_approvals.py tests/test_markerless_exec_tool_guard.py tests/test_windows_bash_shell.py: 662 passed, 5 skipped.ruff checkclean, and the pinnedruff-format-with-kwargshook leaves both files alone.Cross-platform run
studio/backend/tests/test_sandbox_tools.pyrun on three OSes off the org queue, scoped to that file so a break fails the job rather than passing advisory.Windows is the one that matters beyond a sanity check, because
_BLOCKED_COMMANDSfolds in_BLOCKED_COMMANDS_WINonwin32and_get_shell_cmdpicks a bash only if the host has one. The counts differ by platform because of per-OS skips, not the coproc cases, which run everywhere. The runs cover the final code; the only commit after them is a comment-only pass, AST-verified to touch no code.There are no screenshots to attach: the change is confined to the terminal classifiers and moves no pixel. The only thing a user sees differently is the text of a tool result,
Blocked command(s) for safety: rmin place of silence, which the before/after table above quotes from the real dispatcher.