Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: treat bash `coproc` as a command boundary in the terminal safety scanners (#11369)

Daniel Han6cd44c5cb7c2
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 76490168760..244eb02a63f 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -198,8 +198,36 @@ _BLOCKED_COMMANDS = (
_SHELL_SEPARATORS = frozenset({";", "&&", "||", "|", "&", "\n", "(", ")", "`", "{", "}"})
# Bash keywords starting a new command position. `if`/`while`/`until` are followed by a CONDITION the shell executes,
-# so a command right after them is at command position.
-_SHELL_KEYWORDS_AS_SEP = frozenset({"then", "do", "else", "elif", "if", "while", "until", "!"})
+# so a command right after them is at command position. `coproc` too: read as the command word it left `coproc rm
+# -rf x` scanning as arguments, and that really deletes.
+_SHELL_KEYWORDS_AS_SEP = frozenset(
+ {"then", "do", "else", "elif", "if", "while", "until", "!", "coproc"}
+)
+# Bash takes `coproc NAME ...` only before a COMPOUND command, which is what tells a name from a command: the same
+# position holds the command itself in `coproc rm -f x`. Only the WORD starters are listed: `{` and `(` are already
+# separators, so `coproc JOB { rm ...; }` resolves without reading JOB as a name, and listing them would only mean
+# trusting a lookahead that a quoted `'{'` can forge.
+_COPROC_COMPOUND_STARTERS = frozenset({"if", "while", "until", "for", "case", "select"})
+_COPROC_NAME_RE = re.compile(r"^[A-Za-z_]\w*$")
+
+
+def _is_coproc_name(tokens: "list[str]", index: int) -> bool:
+ """Whether `tokens[index]` is the NAME of a `coproc NAME compound-command`, not a command word.
+
+ Callers gate this on having just consumed the `coproc` KEYWORD, which the neighbouring tokens cannot decide:
+ `echo coproc JOB if rm -f x` prints three words while `time coproc JOB if rm -f x` really deletes. They then
+ READ the name like any other command word rather than skipping it, since shlex has already dropped the quotes
+ and a forged `'{'` would otherwise hide whatever stands there; only command position carries past it.
+ """
+ return (
+ index > 0
+ and tokens[index - 1] == "coproc"
+ and index + 1 < len(tokens)
+ and tokens[index + 1] in _COPROC_COMPOUND_STARTERS
+ and _COPROC_NAME_RE.match(tokens[index]) is not None
+ )
+
+
# Wrappers whose next non-flag argument is the command Bash will exec.
_COMMAND_PREFIXES = frozenset(
{
@@ -1186,6 +1214,7 @@ def _exec_scan_layout(
at_command = True # the next ordinary word is one the shell RUNS
wrapper = "" # a command prefix (env/timeout/sudo) awaiting that word
skip_operand = False # ...and its option's value stands in between
+ coproc_kw = False # the word just consumed was the `coproc` KEYWORD, so a name may follow
index = 0
while index < len(tokens):
token = tokens[index]
@@ -1196,6 +1225,8 @@ def _exec_scan_layout(
continue
here = index
index += 1
+ after_coproc = coproc_kw
+ coproc_kw = False
if _looks_like_separator(token) and here not in quoted:
stops.add(here)
forwarding = in_action = False
@@ -1231,6 +1262,7 @@ def _exec_scan_layout(
in_action = True
continue
if at_command and token in _SHELL_KEYWORDS_AS_SEP:
+ coproc_kw = token == "coproc"
continue # `then find ...` / `do find ...`: still a command position
if skip_operand:
skip_operand = False # a wrapper option's value (env -u NAME)
@@ -1243,14 +1275,15 @@ def _exec_scan_layout(
if wrapper and token.lstrip("-").isdigit():
continue # `timeout 5 find ...`: the wrapper's own operand
base = os.path.basename(token.strip(";&|()`{}")).lower()
- if at_command and base in _COMMAND_PREFIXES:
+ coproc_name_here = after_coproc and _is_coproc_name(tokens, here)
+ if at_command and base in _COMMAND_PREFIXES and not coproc_name_here:
wrapper = base
continue
if at_command and _forwards_exec_flags(base):
# Only a find/fd the shell really RUNS forwards its exec flags. Any token spelled `fd`/`find` used to turn
# one on, so `echo fd -x rm` came back with rm and was refused.
forwarding = True
- at_command = False
+ at_command = coproc_name_here
wrapper = ""
return frozenset(exec_flags), frozenset(stops), frozenset(redirects)
@@ -1311,6 +1344,8 @@ def _is_start_title(token: str) -> bool:
# 0.60s of 3.9s. None only if the set is empty.
_BLOCKED_WORD_RE = (
re.compile(
+ # No `coproc` alternative here, deliberately: with no quoting or command-position context this pass refused
+ # `grep coproc rm file`. The token scan above knows where a command starts and handles the keyword.
r"(?:^|[;&|`\n(]\s*|[$]\(\s*|<\(\s*)"
r"(?:[\w./\\-]*/|[a-zA-Z]:[/\\][\w./\\-]*)?"
r"(" + "|".join(re.escape(w) for w in sorted(_BLOCKED_COMMANDS)) + r")"
@@ -1421,7 +1456,10 @@ def _find_blocked_commands(command: str) -> set[str]:
sed_indexes: "list[int]" = [] # command-position sed words, for the `e` scan below
sed_xargs: "dict[int, int]" = {} # sed word -> the xargs that builds its argv
xargs_index = -1 # an xargs awaiting the command it wraps
+ coproc_kw = False # the word just consumed was the `coproc` KEYWORD, so a name may follow
for token_index, token in enumerate(tokens):
+ after_coproc = coproc_kw
+ coproc_kw = False
if skip_operand:
# `exec -a NAME cmd` and `if exist FILE cmd` both put an operand where the command word would otherwise
# be.
@@ -1443,6 +1481,7 @@ def _find_blocked_commands(command: str) -> set[str]:
if (_looks_like_separator(token) and token_index not in quoted_separators) or (
token in _SHELL_KEYWORDS_AS_SEP and expect_command
):
+ coproc_kw = expect_command and token == "coproc"
expect_command = True
prefix_pending = False
prefix_command = ""
@@ -1473,6 +1512,7 @@ def _find_blocked_commands(command: str) -> set[str]:
# Numeric wrapper arg: `timeout 1 cmd` / `nice -n 5 cmd`.
if prefix_pending and token.lstrip("-").isdigit():
continue
+ coproc_name_here = after_coproc and _is_coproc_name(tokens, token_index)
base = _token_basename(token)
if _is_sed_command(base):
sed_indexes.append(token_index)
@@ -1484,13 +1524,13 @@ def _find_blocked_commands(command: str) -> set[str]:
blocked |= _blocked_matching_glob(base)
# Wrappers (env/time/xargs/sudo) consume one command; the next non-flag, non-numeric token is the real
# command. sudo is also in _BLOCKED_COMMANDS.
- if base in _COMMAND_PREFIXES:
+ if base in _COMMAND_PREFIXES and not coproc_name_here:
if base == "xargs" and xargs_index < 0:
xargs_index = token_index
prefix_pending = True
prefix_command = base
continue
- expect_command = False
+ expect_command = coproc_name_here
prefix_pending = False
prefix_command = ""
xargs_index = -1
@@ -5483,7 +5523,7 @@ def _terminal_is_potentially_unsafe(command: str) -> bool:
current_command = ""
positional_args = 0
pending_flag_value = False
- for token in tokens:
+ for _tok_idx, token in enumerate(tokens):
# Runs of punctuation (";;", ";&") lex as one token; any token made purely of separator characters still
# separates commands.
if (
@@ -7850,12 +7890,19 @@ def _terminal_is_high_risk(command: str, _depth: int = 0) -> bool:
git_glob_pending = False # a git global option (-C repo) precedes its value
chdir_pending = False # a cd/pushd precedes its target directory
xargs_index = -1 # an xargs awaiting the command whose argv it builds
+ coproc_kw = False # the word just consumed was the `coproc` KEYWORD, so a name may follow
for _tok_idx, token in enumerate(tokens):
+ after_coproc = coproc_kw
+ coproc_kw = False
if (
token in _SHELL_SEPARATORS
or (token in _SHELL_KEYWORDS_AS_SEP and expect_command)
+ # This walker carries a wrapper's command position in `prefix_pending`, not `expect_command`, so the
+ # clause above misses `time coproc rm -f x`, which bash runs and which really deletes.
+ or (token == "coproc" and prefix_pending)
or not set(token) - set(";&|()")
):
+ coproc_kw = (expect_command or prefix_pending) and token == "coproc"
expect_command = True
prefix_pending = False
xargs_index = -1
@@ -8138,10 +8185,17 @@ def _terminal_is_high_risk(command: str, _depth: int = 0) -> bool:
stem, ext = os.path.splitext(base)
if ext in {".exe", ".com", ".bat", ".cmd"}:
base = stem
- if (expect_command or prefix_pending) and (
- base in _AUTO_SAFE_WRAPPERS
- or base in _MULTICALL_BINARIES
- or base in _PRIVILEGE_EXEC_WRAPPERS
+ coproc_name_here = after_coproc and _is_coproc_name(tokens, _tok_idx)
+ if (
+ (expect_command or prefix_pending)
+ # A coprocess NAME is not a wrapper, and spending the wrapper on the compound behind it demoted
+ # the real command: `coproc env if git clean -fd; then :; fi` deletes untracked files.
+ and not coproc_name_here
+ and (
+ base in _AUTO_SAFE_WRAPPERS
+ or base in _MULTICALL_BINARIES
+ or base in _PRIVILEGE_EXEC_WRAPPERS
+ )
):
# A wrapper (env/timeout) or a multicall binary (busybox rm) precedes the real command; keep seeking
# it, but track it so its own flags (env -S / -C) are judged in the meantime.
@@ -8350,7 +8404,7 @@ def _terminal_is_high_risk(command: str, _depth: int = 0) -> bool:
for cand in (raw, _expand_param_defaults(raw), _expand_shell_assignments(raw))
):
return True
- expect_command = False
+ expect_command = coproc_name_here
prefix_pending = False
return False
diff --git a/studio/backend/tests/test_sandbox_tools.py b/studio/backend/tests/test_sandbox_tools.py
index 02a2d3610e2..978cddf76d9 100644
--- a/studio/backend/tests/test_sandbox_tools.py
+++ b/studio/backend/tests/test_sandbox_tools.py
@@ -890,6 +890,19 @@ class TestBashBlocklistPosition:
id = "suffixed_duration_wrapper_spent_allowed",
),
pytest.param("coproc echo hi", id = "coproc_benign_allowed"),
+ pytest.param("coproc MYJOB { cat train.log; }", id = "coproc_named_benign_allowed"),
+ # `coproc` is the keyword only unquoted at command position (`'coproc' echo rm` is "command not found"),
+ # so anywhere else it starts nothing and the blocked word behind it is data.
+ pytest.param("grep -rn coproc tools.py", id = "coproc_as_argument_allowed"),
+ pytest.param("grep coproc rm file", id = "coproc_then_blocked_word_as_args_allowed"),
+ pytest.param("echo coproc rm", id = "coproc_then_blocked_word_echoed_allowed"),
+ pytest.param("echo 'coproc rm'", id = "quoted_coproc_payload_allowed"),
+ pytest.param("coproc echo rm", id = "coproc_benign_command_blocked_arg_allowed"),
+ # The optional-name rule needs the same guard: these three words are arguments echo prints.
+ pytest.param(
+ "echo coproc JOB if rm -f victim; then :; fi",
+ id = "coproc_name_shape_as_args_allowed",
+ ),
pytest.param("> out.log echo hi", id = "spaced_redirection_benign_allowed"),
# A substitution that IS the redirection target names a file; nothing runs.
pytest.param("> $(date).log echo hi", id = "subst_as_redirection_target_allowed"),
@@ -1118,6 +1131,56 @@ class TestBashBlocklistPosition:
"coproc $(ls /usr/bin | grep '^rm$') -rf victim",
id = "coproc_subst_blocked",
),
+ # ...and the plain spellings, where reading `coproc` as the command word left the real one as arguments.
+ pytest.param("rm", "coproc rm -f victim", id = "coproc_bare_blocked"),
+ pytest.param("pkill", "coproc pkill -f unsloth", id = "coproc_pkill_blocked"),
+ pytest.param("ssh", "coproc ssh internal-host", id = "coproc_ssh_blocked"),
+ # `coproc NAME compound` only NAMES the coprocess, so command position carries past the name.
+ pytest.param(
+ "rm", "coproc JOB if rm -f victim; then :; fi", id = "coproc_named_if_blocked"
+ ),
+ pytest.param("rm", "coproc JOB { rm -rf victim; }", id = "coproc_named_group_blocked"),
+ pytest.param(
+ "rm",
+ "x=1; coproc JOB if rm -f victim; then :; fi",
+ id = "coproc_named_after_sep_blocked",
+ ),
+ pytest.param(
+ "rm",
+ "FOO=bar coproc JOB if rm -f victim; then :; fi",
+ id = "coproc_named_after_assign_blocked",
+ ),
+ # Quoting forges the lookahead: shlex hands back the same token for `{` and `'{'`, while bash reads
+ # `coproc rm '{' -f victim` as the SIMPLE form and deletes.
+ pytest.param("rm", "coproc rm '{' -f victim", id = "coproc_quoted_brace_blocked"),
+ pytest.param("rm", "coproc rm 'if' -f victim", id = "coproc_quoted_keyword_blocked"),
+ # The name is read, not skipped, so a sed there still has its `e` program screened.
+ pytest.param(
+ "rm",
+ "coproc sed 'if' -e '1e rm -f victim' input",
+ id = "coproc_quoted_keyword_sed_program_blocked",
+ ),
+ pytest.param(
+ "pkill", "coproc pkill '{' -f unsloth", id = "coproc_quoted_brace_pkill_blocked"
+ ),
+ # `time` is a reserved word taking a pipeline, so it prefixes a coprocess and bash runs it (5.2.21);
+ # an external wrapper cannot, `env coproc JOB if ...` being a syntax error.
+ pytest.param("rm", "time coproc rm -f victim", id = "timed_coproc_blocked"),
+ pytest.param(
+ "rm",
+ "time coproc JOB if rm -f victim; then :; fi",
+ id = "timed_named_coproc_blocked",
+ ),
+ pytest.param(
+ "rm",
+ "time -p coproc JOB if rm -f victim; then :; fi",
+ id = "timed_p_named_coproc_blocked",
+ ),
+ pytest.param(
+ "rm",
+ "coproc JOB for f in x; do rm -f victim; done",
+ id = "coproc_named_for_blocked",
+ ),
# The two laundering routes the site fixes left behind: an arm runs a variable just
# as readily as a substitution, and bash concatenates `${x}m` into one command word.
pytest.param(
@@ -1670,6 +1733,40 @@ class TestBashBlocklistPosition:
assert "rm" in self._find()("timeout 5 find . -exec rm -rf victim {} +")
assert "rm" in self._find()("nice -n 5 find . -exec rm -rf victim {} +")
+ @pytest.mark.parametrize(
+ "command",
+ [
+ "rm -rf victim",
+ "ssh internal-host",
+ "curl http://127.0.0.1/",
+ "echo hi",
+ "cat train.log",
+ ],
+ )
+ def test_coproc_classifies_exactly_as_the_command_behind_it(self, command):
+ # Equal in BOTH directions: merely getting stricter would start prompting for coprocesses that are fine.
+ assert self._find()(f"coproc {command}") == self._find()(command)
+ # A forged lookahead moves nothing, since the walker reads the name rather than skipping it.
+ head, _, rest = command.partition(" ")
+ assert self._find()(f"coproc {head} 'if' {rest}") == self._find()(f"{head} 'if' {rest}")
+ assert is_high_risk_tool_call(
+ "terminal", {"command": f"coproc {command}"}
+ ) == is_high_risk_tool_call("terminal", {"command": command})
+ assert self._find()(f"coproc JOB if {command}; then :; fi") == self._find()(command)
+ # `git clean -fd` is destructive without being blocklisted, so the auto gate must reach past the name.
+ assert is_high_risk_tool_call(
+ "terminal", {"command": "coproc JOB if git clean -fd; then :; fi"}
+ ) == is_high_risk_tool_call("terminal", {"command": "git clean -fd"})
+ # ...including a name spelled like a wrapper, which bash allows and which used to eat the compound.
+ assert is_high_risk_tool_call(
+ "terminal", {"command": "coproc env if git clean -fd; then :; fi"}
+ ) == is_high_risk_tool_call("terminal", {"command": "git clean -fd"})
+ # `time` keeps command position for bash, so it must keep it for both classifiers too.
+ assert self._find()(f"time coproc {command}") == self._find()(f"time {command}")
+ assert is_high_risk_tool_call(
+ "terminal", {"command": f"time coproc {command}"}
+ ) == is_high_risk_tool_call("terminal", {"command": f"time {command}"})
+
def test_quoted_operator_is_data_not_a_command_boundary(self):
# A quoted operator reaches the command as an argument, so the word
# behind it is not at command position: these lines run nothing.