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: screen a quoted program path that cmd /c or start launches (#13386)

Daniel Han1ecae8646d75
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index 66186d45dc7..5b735586e2b 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -15,6 +15,7 @@ import hashlib
from html.parser import HTMLParser
import json
import http.client
+import ntpath
import os
from functools import partial
import signal
@@ -1385,6 +1386,38 @@ _START_SWITCHES = frozenset(
)
+# A program spelled as a Windows path, spaces allowed (C:\Program Files\..., %SystemDrive%\...). The characters
+# Windows forbids in a file name are not, so a whole command line (`dir C:\x\curl`) does not read as one path.
+_WIN_PROGRAM_PATH_RE = re.compile(r'(?:[A-Za-z]:|\.{1,2}|%[A-Za-z_][\w()]*%)?[\\/][^:*?"<>|\r\n]*')
+_WIN_RELATIVE_PROGRAM_PATH_RE = re.compile(r'[^:*?"<>|\r\n]*[\\/][^:*?"<>|\r\n]*')
+
+
+def _blocked_program_path(word: str, program_slot: bool = False) -> str:
+ r"""The blocked name ``word`` runs when cmd or START takes it as one program path. Re-lexing it
+ as a command line splits `C:\Program Files\PowerShell\7\pwsh.exe` at the space. ntpath, not
+ os.path, so the Linux test runner reads backslashes the way Windows does."""
+ word = word.strip('"')
+ # ``program_slot``: the word can only be a program (START's), so `tools dir\pwsh.exe` is a path too.
+ # An extensionless path resolves through PATHEXT either way.
+ if not (
+ _WIN_PROGRAM_PATH_RE.fullmatch(word)
+ or (program_slot and _WIN_RELATIVE_PROGRAM_PATH_RE.fullmatch(word))
+ ):
+ return ""
+ base = ntpath.basename(word).lower()
+ stem, ext = ntpath.splitext(base)
+ if (
+ not program_slot
+ and any(char.isspace() for char in word)
+ and any(_WIN_PROGRAM_PATH_RE.match(piece) for piece in word.split()[1:])
+ ):
+ # A rooted command line (`\Windows\System32\where.exe \src\rm`), not one path holding spaces.
+ return ""
+ if ext in {".exe", ".com", ".bat", ".cmd"}:
+ base = stem
+ return base if base in _BLOCKED_COMMANDS else ""
+
+
def _is_start_title(token: str) -> bool:
"""True when START would read ``token`` as its window title, not the program. The cmd lexer
keeps the quote marks, so a title still arrives quoted; the posix lexer leaves two spellings
@@ -2047,6 +2080,16 @@ def _find_blocked_commands(command: str, posix: "bool | None" = None) -> set[str
if len(payload) > 1 and payload[0] == '"' and payload[-1] == '"':
payload = payload[1:-1]
blocked |= _find_blocked_commands(payload, posix = posix)
+ # cmd runs a leading quoted path as the program (`cmd /c "C:\x y\pwsh.exe" -c ...`); `""prog" args"`
+ # loses its outer pair first. The posix lexer already handed that path over as one token.
+ if lexed_posix:
+ program = tokens[i + 1]
+ else:
+ rest = " ".join(tokens[i + 1 :])
+ rest = '"' + rest[2:].lstrip() if rest.startswith('""') else rest
+ quoted = re.match(r'"([^"]*)"', rest)
+ program = quoted.group(1) if quoted else ""
+ blocked |= {_blocked_program_path(program)} - {""}
break # stop at first non-flag token
# `cmd /c start "" prog` puts prog in a command position the scan above sees only as an argument, so screen what
@@ -2064,6 +2107,8 @@ def _find_blocked_commands(command: str, posix: "bool | None" = None) -> set[str
# approximation under-approximated.
if j < len(tokens):
blocked |= _find_blocked_commands(tokens[j], posix = posix)
+ if not _is_start_title(tokens[j]):
+ blocked |= {_blocked_program_path(tokens[j], program_slot = True)} - {""}
if j + 1 < len(tokens) and _is_start_title(tokens[j]):
k = j + 1
# `start "my window" /min prog` puts switches after the title too.
@@ -2071,6 +2116,7 @@ def _find_blocked_commands(command: str, posix: "bool | None" = None) -> set[str
k += 2 if _win_switch(tokens[k].lower()) in _START_SWITCHES_WITH_VALUE else 1
if k < len(tokens):
blocked |= _find_blocked_commands(tokens[k], posix = posix)
+ blocked |= {_blocked_program_path(tokens[k], program_slot = True)} - {""}
# sed's `e COMMAND` hands COMMAND to the shell, a real command position the scan above sees only as a text
# argument, so screen it like `bash -c`. The pattern-space forms yield an empty payload; the auto gate prompts on
diff --git a/studio/backend/tests/test_windows_bash_shell.py b/studio/backend/tests/test_windows_bash_shell.py
index ebe2b28e46b..4a7e6516202 100644
--- a/studio/backend/tests/test_windows_bash_shell.py
+++ b/studio/backend/tests/test_windows_bash_shell.py
@@ -477,3 +477,64 @@ def test_a_blocked_program_behind_a_drive_path_is_still_screened(
@pytest.mark.parametrize("command", ["echo hi; . script.sh", "x=$(. script.sh)", ". ./env.sh"])
def test_dot_as_a_command_is_still_screened(monkeypatch, command, _windows_backstop):
assert "." in _screen_on_windows(monkeypatch, _WINDOWS_SHELLS[0], command)
+
+
[email protected]("bash", _WINDOWS_SHELLS)
+ ("command", "expected"),
+ [
+ # A quoted program path with a space, handed on by cmd or START: re-lexing it as a command line
+ # split it at the space and screened `C:\Program` instead.
+ (r'cmd /c "C:\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
+ (r'cmd //c "C:\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
+ (r'start "" "C:\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
+ (r'cmd //c start "" "C:\x y\powershell.exe"', "powershell"),
+ ('cmd /c "C:/x y/rm.exe" -rf z', "rm"),
+ # START's program slot holds only a program, so a relative path counts there too.
+ (r'start "" "tools dir\pwsh.exe" -c ls', "pwsh"),
+ (r'cmd /c "%SystemDrive%\Program Files\PowerShell\7\pwsh.exe" -c ls', "pwsh"),
+ # START resolves an extensionless program through PATHEXT.
+ (r'start "" "C:\Program Files\PowerShell\7\pwsh" -c ls', "pwsh"),
+ (r'cmd /c "C:\Program Files\PowerShell\7\pwsh" -c ls', "pwsh"),
+ ],
+)
+def test_a_quoted_program_path_handed_to_cmd_or_start_is_screened(
+ monkeypatch, bash, command, expected, _windows_blocklist
+):
+ assert expected in _screen_on_windows(monkeypatch, bash, command)
+
+
+def test_cmd_strips_the_outer_quotes_of_a_doubled_quote_payload(monkeypatch, _windows_blocklist):
+ # cmd turns `""prog" args"` into `"prog" args`. Git Bash would strip the backslashes first, so only
+ # the cmd Terminal runs this as pwsh.
+ assert "pwsh" in _screen_on_windows(monkeypatch, None, r'cmd /c ""C:\x\pwsh.exe" -c ls"')
+
+
[email protected]("bash", _WINDOWS_SHELLS)
+ "command",
+ [
+ r'cmd /c "C:\Program Files\nodejs\node.exe" -v',
+ r'start "" "C:\Program Files\Microsoft VS Code\Code.exe" .',
+ # A blocked name that is only an argument, a folder or a document.
+ r'cmd /c "dir C:\src\curl"',
+ r'cmd /c "git -C C:\src\ssh status"',
+ r'start "" "tools dir\notepad.exe" readme.txt',
+ # A rooted command line whose last argument names a blocked program.
+ r'cmd /c "\Windows\System32\where.exe \src\rm"',
+ r'cmd /c type "C:\Users\me\My Documents\rm.txt"',
+ ],
+)
+def test_a_quoted_program_path_that_is_not_blocked_stays_runnable(
+ monkeypatch, bash, command, _windows_blocklist
+):
+ assert not _screen_on_windows(monkeypatch, bash, command)
+
+
+def test_a_quoted_start_title_is_not_screened_as_the_program(monkeypatch, _windows_blocklist):
+ # cmd keeps the quotes, so START reads this as the title and runs notepad. Git Bash hands START the
+ # path unquoted, which it then runs, so that reading stays blocked.
+ assert not _screen_on_windows(monkeypatch, None, r'start "C:\x\rm.exe" notepad')
+ assert "rm" in _screen_on_windows(
+ monkeypatch, _WINDOWS_SHELLS[0], r'start "C:\x\rm.exe" notepad'
+ )