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: ask before Python runs a shell command passed by reference (#17456)

Nilay6fef7b1b9e64
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index b80108c090f..2af22d325da 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -17999,6 +17999,7 @@ def _check_signal_escape_patterns(code: str):
self._cyclic = False
self.collecting = False
self.loop_depth = 0
+ self._followed: set = set()
def _bind(self, name, value):
if self.collecting:
@@ -18055,6 +18056,8 @@ def _check_signal_escape_patterns(code: str):
def _memo(self, kind, node, compute, pending):
key = (kind, node.id if isinstance(node, ast.Name) else id(node))
+ if key in self._final:
+ return self._final[key]
if key in self._cache:
return self._cache[key]
if key in self._active:
@@ -18070,12 +18073,13 @@ def _check_signal_escape_patterns(code: str):
def _converged(self, query, node):
# A self-referential binding (d = d["system"]) reads the previous pass's answer, so repeat
# until no answer grows (they are finite sets, so this ends); keep only converged answers.
+ # Each pass writes a fresh layer over _final: copying _final per query made scans quadratic.
previous, self._guess = None, {}
while True:
- self._cache, self._cyclic = dict(self._final), False
+ self._cache, self._cyclic = {}, False
result = query(node)
if not self._cyclic or self._cache == previous:
- self._final = self._cache
+ self._final.update(self._cache)
return result
previous = self._guess = self._cache
@@ -18200,20 +18204,64 @@ def _check_signal_escape_patterns(code: str):
# Only when no other binding matches: a loop target skips argument zero, which may be the command.
return loop_method
+ def _shell_method_named_by(self, call):
+ if call.args and self._is_builtin(call.func, "methodcaller"):
+ name = _folded_str_literal(call.args[0])
+ for fq in _SHELL_EXEC_FUNCS:
+ if fq.split(".", 1)[1] == name:
+ return fq
+ return None
+
+ def _flag_shell_reference(self, query, node):
+ shell_func = self._converged(query, node)
+ if shell_func:
+ shell_escapes.append(
+ {
+ "type": "shell_escape_dynamic",
+ "line": node.lineno,
+ "description": f"{shell_func} referenced without a direct call (potential shell escape)",
+ }
+ )
+
+ def _skip_type_hint(self, hint):
+ # A call inside an annotation still runs.
+ if hint is not None and not isinstance(hint, ast.Call):
+ self._followed.add(id(hint))
+ for child in ast.iter_child_nodes(hint):
+ self._skip_type_hint(child)
+
+ def visit(self, node):
+ self._skip_type_hint(getattr(node, "annotation", None))
+ self._skip_type_hint(getattr(node, "returns", None))
+ # Dispatched here: super().visit() costs a stack frame per nesting level.
+ getattr(self, "visit_" + type(node).__name__, self.generic_visit)(node)
+ # After the children, so a nested a[0][0]... resolves from their memoised lookups.
+ if (
+ not self.collecting
+ and id(node) not in self._followed
+ and isinstance(node, (ast.Name, ast.Attribute, ast.Subscript, ast.Call))
+ and not isinstance(getattr(node, "ctx", None), (ast.Store, ast.Del))
+ ):
+ self._flag_shell_reference(self._shell_func_of, node)
+
def visit_Assign(self, node):
for tgt in node.targets:
if isinstance(tgt, ast.Name):
self._bind(tgt.id, node.value)
+ if all(isinstance(tgt, ast.Name) for tgt in node.targets):
+ self._followed.add(id(node.value))
self.generic_visit(node)
def visit_AnnAssign(self, node):
if node.value is not None and isinstance(node.target, ast.Name):
self._bind(node.target.id, node.value)
+ self._followed.add(id(node.value))
self.generic_visit(node)
def visit_NamedExpr(self, node):
if isinstance(node.target, ast.Name):
self._bind(node.target.id, node.value)
+ self._followed.add(id(node.value))
self.generic_visit(node)
def visit_While(self, node):
@@ -18279,6 +18327,8 @@ def _check_signal_escape_patterns(code: str):
)
# Shell escape detection: resolve the FQ function name for os.*/subprocess.*
+ self._followed.add(id(func))
+ self._flag_shell_reference(self._shell_method_named_by, node)
shell_func = self._converged(self._shell_func_of, func)
if shell_func and shell_func in _SHELL_EXEC_FUNCS:
diff --git a/studio/backend/tests/test_permission_mode.py b/studio/backend/tests/test_permission_mode.py
index f6d033a1b96..40488f4a252 100644
--- a/studio/backend/tests/test_permission_mode.py
+++ b/studio/backend/tests/test_permission_mode.py
@@ -1511,6 +1511,73 @@ def test_shell_command_through_asyncio_or_pty_is_high_risk(code, refused):
assert (_check_code_safety(code) is not None) is refused
+ "code, refused",
+ [
+ ("import os\nlist(map(os.system, ['rm -rf x']))", True),
+ (
+ "import subprocess\nfrom concurrent.futures import ThreadPoolExecutor\n"
+ "ThreadPoolExecutor().submit(subprocess.run, 'rm -rf x', shell=True)",
+ True,
+ ),
+ ("import functools, os\nfunctools.partial(os.system, 'rm -rf x')()", True),
+ (
+ "import os, threading\nthreading.Thread(target=os.system, args=('rm -rf x',)).start()",
+ True,
+ ),
+ ("import asyncio, os\nasyncio.run(asyncio.to_thread(os.system, 'rm -rf x'))", True),
+ ("import os\ndef go(f=os.system):\n f('rm -rf x')\ngo()", True),
+ ("import os\nfor f in [os.system]:\n f('rm -rf x')", True),
+ ("import operator, os\noperator.methodcaller('system', 'rm -rf x')(os)", True),
+ (
+ "from operator import methodcaller as mc\nimport subprocess\nmc('run', ['rm', 'x'])(subprocess)",
+ True,
+ ),
+ ("import os\nrun = os.system\nlist(map(run, ['rm -rf x']))", True),
+ ("from subprocess import run\nlist(map(run, [['rm', '-rf', 'x']]))", True),
+ ("import os\nlist(map(getattr(os, 'system'), ['rm -rf x']))", True),
+ ("import os\nrun, n = os.system, 1\nrun('rm -rf x')", True),
+ ("import os\nruns = {'sh': os.system}\nruns['sh']('rm -rf x')", True),
+ ("import os\ndef pick():\n return os.system\npick()('rm -rf x')", True),
+ ("import os\nclass Box: ...\nrun = Box.run = os.system\nBox.run('rm -rf x')", True),
+ ("import os\nx: list(map(os.system, ['rm -rf x'])) = 1", True),
+ ("import os\nlist(map(os.path.basename, ['a/b']))", False),
+ ("import os\nrun = os.system\nrun('ls')", False),
+ (
+ "import subprocess\ndef serve() -> subprocess.Popen:\n"
+ " return subprocess.Popen(['ls'])\np: 'subprocess.Popen | None' = serve()",
+ False,
+ ),
+ ("from subprocess import Popen\ndef wait(p: Popen): ...\nq: Popen = Popen(['ls'])", False),
+ ("import subprocess\nsubprocess.run(['ls'], stdout=subprocess.PIPE)", False),
+ ("import operator\nprint(operator.methodcaller('upper')('x'))", False),
+ ("def system(c): ...\nlist(map(system, ['rm -rf x']))", False),
+ ],
+)
+def test_shell_function_passed_by_reference_is_high_risk(code, refused):
+ assert is_high_risk_tool_call("python", {"code": code}) is refused
+ assert (_check_code_safety(code) is not None) is refused
+
+
[email protected]("code", ["value = " + "+".join(["1"] * 400), "value = data" + "[0]" * 400])
+def test_long_expression_chain_is_classified(code):
+ import threading
+
+ errors = []
+
+ def classify():
+ try:
+ is_high_risk_tool_call("python", {"code": code})
+ except RecursionError as exc:
+ errors.append(exc)
+
+ # A fresh thread starts at stack depth zero, so the margin does not depend on pytest's own frames.
+ thread = threading.Thread(target = classify)
+ thread.start()
+ thread.join()
+ assert not errors
+
+
def test_high_risk_dispatcher_non_terminal():
assert is_high_risk_tool_call("web_search", {"query": "hi"}) is False
assert is_high_risk_tool_call("search_knowledge_base", {}) is False