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.

Screen the cmd spellings the blocklist lexer quotes (#8010)

Daniel Han74d41b94b1fc
diff --git a/studio/backend/core/inference/tools.py b/studio/backend/core/inference/tools.py
index d7955e995e5..d50847c8dbe 100644
--- a/studio/backend/core/inference/tools.py
+++ b/studio/backend/core/inference/tools.py
@@ -1342,6 +1342,51 @@ def _win_switch(token: str) -> str:
# position. These switches precede it; the value-taking ones eat a token.
_START_SWITCHES_WITH_VALUE = {"/d", "/node", "/affinity", "/machine"}
+# A slash, one letter, optional `:value` (/s, /v:on, /t:0a). Matched in full so
+# a program spelled as a path (/bin/bash) is never skipped as a switch.
+_CMD_SWITCH_RE = re.compile(r"/[a-zA-Z](?::[\w.]+)?")
+
+# START's documented switches. Matched by name rather than by a leading slash,
+# because MSYS rewrites a POSIX path argument and hands cmd back something like
+# /c/Windows/.../powershell.exe, which is a program and not a switch.
+_START_SWITCHES = frozenset(
+ {
+ "/min",
+ "/max",
+ "/separate",
+ "/shared",
+ "/low",
+ "/normal",
+ "/high",
+ "/realtime",
+ "/abovenormal",
+ "/belownormal",
+ "/wait",
+ "/b",
+ "/i",
+ "/d",
+ "/node",
+ "/affinity",
+ "/machine",
+ }
+)
+
+
+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 has stripped them, leaving two spellings a bare program name
+ cannot have: the empty ``start ""`` idiom, and a title containing
+ whitespace. A single-word posix title (``start "job" prog``) is
+ indistinguishable from a program name and is deliberately not guessed.
+ """
+ return (
+ token == ""
+ or any(char.isspace() for char in token)
+ or (len(token) >= 2 and token[0] == '"' and token[-1] == '"')
+ )
+
def _find_blocked_commands(command: str) -> set[str]:
"""Detect blocked commands at shell command position only.
@@ -1632,18 +1677,26 @@ def _find_blocked_commands(command: str) -> set[str]:
if not (is_unix_c or is_win_c) or i < 1 or i + 1 >= len(tokens):
continue
# Look back past flags for the shell binary. Windows flags and absolute
- # paths both start with /, so only skip short /X flags (not /bin/bash).
+ # paths both start with /, so only skip things shaped like a whole
+ # switch (/s, /v:on) and never a program spelled as a path (/bin/bash).
for j in range(i - 1, -1, -1):
prev = tokens[j]
if prev.startswith("-"):
continue # skip Unix flags like --login, -l
- if is_win_c and prev.startswith("/") and len(prev) <= 3:
- continue # skip Windows flags like /s, /q (not /bin/bash)
+ # Git Bash doubles the slash on these switches too, so normalise
+ # them like the trigger: else `cmd //v:on //c powershell` stops here.
+ if is_win_c and _CMD_SWITCH_RE.fullmatch(_win_switch(prev)):
+ continue # skip Windows switches like /s, /q, /v:on
prev_base = os.path.basename(prev).lower()
if is_unix_c and prev_base in _SHELLS:
blocked |= _find_blocked_commands(tokens[i + 1])
elif is_win_c and prev_base in _SHELLS_WIN:
- blocked |= _find_blocked_commands(tokens[i + 1])
+ # The cmd lexer keeps the marks, so `cmd /c "powershell ls"`
+ # would recurse on a first word of `"powershell` and match nothing.
+ payload = tokens[i + 1]
+ if len(payload) > 1 and payload[0] == '"' and payload[-1] == '"':
+ payload = payload[1:-1]
+ blocked |= _find_blocked_commands(payload)
break # stop at first non-flag token
# `cmd /c start "" prog` puts prog in a command position the scan above
@@ -1652,17 +1705,24 @@ def _find_blocked_commands(command: str) -> set[str]:
if os.path.basename(token).lower() not in ("start", "start.exe"):
continue
j = i + 1
- while j < len(tokens):
- switch = _win_switch(tokens[j].lower())
- if not switch.startswith("/"):
- break
+ while j < len(tokens) and _win_switch(tokens[j].lower()) in _START_SWITCHES:
# /d C:\dir and friends carry their value in the next token.
- j += 2 if switch in _START_SWITCHES_WITH_VALUE else 1
- # `start ""` is the idiom for "no title"; anything else is the program.
- if j < len(tokens) and tokens[j] == "":
- j += 1
+ j += 2 if _win_switch(tokens[j].lower()) in _START_SWITCHES_WITH_VALUE else 1
+ # cmd reads a quoted first argument as the window title, putting the
+ # program one token further on. Reading that second token only when the
+ # first is recognisably a title keeps `echo start notepad powershell`
+ # runnable; deciding whether `start` itself is executed is deliberately
+ # not attempted, since every local approximation of it under-approximated
+ # and let a real launch through.
if j < len(tokens):
blocked |= _find_blocked_commands(tokens[j])
+ 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.
+ while k < len(tokens) and _win_switch(tokens[k].lower()) in _START_SWITCHES:
+ 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])
# 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
diff --git a/studio/backend/tests/test_windows_bash_shell.py b/studio/backend/tests/test_windows_bash_shell.py
index 921bce7b820..8f7cf094267 100644
--- a/studio/backend/tests/test_windows_bash_shell.py
+++ b/studio/backend/tests/test_windows_bash_shell.py
@@ -317,3 +317,113 @@ def test_detached_windows_stay_launchable(command, _windows_blocklist):
# The blocklist is faked here too, or the Linux runner asserts this against
# a set with no powershell in it and cannot see a blanket block at all.
assert not tools._find_blocked_commands(command)
+
+
+# Whether Git Bash resolves picks the lexer, and the cmd lexer keeps the quote
+# marks the posix one strips. The tests above pin only the shell the Linux runner
+# happens to pick, so these run each command through both.
+_WINDOWS_SHELLS = [r"C:\Program Files\Git\bin\bash.exe", None]
+
+
+def _screen_on_windows(monkeypatch, bash, command):
+ monkeypatch.setattr(sys, "platform", "win32")
+ monkeypatch.setattr(tools, "_windows_bash", lambda: bash)
+ return tools._find_blocked_commands(command)
+
+
[email protected]("bash", _WINDOWS_SHELLS)
+ "command",
+ [
+ # A quoted payload: the cmd lexer hands the recursion `"powershell`.
+ 'cmd /c "powershell -Command ls"',
+ 'start "My Title" powershell -Command ls',
+ # Switches may follow the title as well as precede it.
+ 'cmd //c start "my window" /min powershell',
+ # A value-style switch, which the old width heuristic did not recognise.
+ "cmd /v:on /c powershell -Command ls",
+ # Git Bash doubles the slash on the switches ahead of /c too.
+ "cmd //v:on //c powershell -Command ls",
+ # MSYS rewrites a POSIX path before cmd sees it, so the program arrives
+ # with a leading slash. Skipping every /-word as a switch stepped over it.
+ 'cmd //c start "" /c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command ls',
+ 'start "" powershell -Command ls',
+ 'cmd //c env start "" powershell',
+ ],
+)
+def test_windows_shellouts_are_screened_on_either_shell(
+ monkeypatch, bash, command, _windows_blocklist
+):
+ assert _screen_on_windows(monkeypatch, bash, command)
+
+
[email protected]("bash", _WINDOWS_SHELLS)
+ "command",
+ [
+ 'start "" notepad readme.txt',
+ 'start "Build" npm run build',
+ 'cmd /c "echo hello"',
+ r'echo "C:\Windows\System32\cmd.exe"',
+ # A document opened through its file association, what `start ""` is for.
+ r'cmd //c start "" "C:\Users\me\My Documents\report.docx"',
+ "npm start",
+ "./start.sh",
+ # `start` and `cmd` as data, not as programs. A shell name the shell
+ # never runs is text, however it is spelled after it.
+ "echo start notepad powershell",
+ "grep start README powershell",
+ ],
+)
+def test_ordinary_windows_commands_stay_runnable(monkeypatch, bash, command, _windows_blocklist):
+ assert not _screen_on_windows(monkeypatch, bash, command)
+
+
+ "command",
+ [
+ # The documented `start "title" prog` form. Only the cmd lexer can see
+ # it: the posix lexer has stripped the marks, leaving a one-word title
+ # indistinguishable from a program name, and guessing there would refuse
+ # ordinary text like `echo start notepad powershell`.
+ 'cmd //c start "job" powershell -Command ls',
+ 'cmd /c start "t" pwsh -c ls',
+ ],
+)
+def test_a_single_word_start_title_is_screened_under_the_cmd_lexer(
+ monkeypatch, command, _windows_blocklist
+):
+ assert _screen_on_windows(monkeypatch, None, command)
+
+
[email protected]("bash", _WINDOWS_SHELLS)
+ "command",
+ [
+ # An assignment prefixes a command the shell still runs, and a shell
+ # handed to another shell is still run. Deciding "is this token executed"
+ # from the token before it missed all of these, which is a bypass and
+ # strictly worse than the echo-data over-blocks it was meant to fix.
+ 'FOO=1 bash -c "rm -rf x"',
+ 'env FOO=1 bash -c "rm -rf x"',
+ 'FOO=1 start "" powershell',
+ 'cmd //c start "" cmd /c powershell',
+ # A wrapper option's value sits between the wrapper and `start`, so the
+ # token before `start` says nothing about whether it is executed.
+ 'cmd //c env -u FOO start "" powershell',
+ 'cmd //c nice -n 5 start "" powershell',
+ ],
+)
+def test_a_prefixed_shell_is_still_screened(monkeypatch, bash, command, _windows_blocklist):
+ # An assignment is not cmd syntax, so under the cmd lexer `FOO=1` is itself
+ # the program name and the rest really is its arguments.
+ if bash is None and not command.startswith("cmd "):
+ pytest.skip("assignment prefixes are not cmd syntax")
+ assert _screen_on_windows(monkeypatch, bash, command)
+
+
+def test_a_program_path_is_not_read_as_a_cmd_switch(monkeypatch, _windows_blocklist):
+ # Matched loosely, the pattern would find the `/b` of /bin/bash, skip the
+ # token as a flag, and never reach the shell name behind it.
+ assert not tools._CMD_SWITCH_RE.fullmatch("/bin/bash")
+ assert _screen_on_windows(monkeypatch, _WINDOWS_SHELLS[0], '/bin/bash -c "rm -rf x"') == {"rm"}