Skip to content

Block markerless execution-class tool-call promotion in Studio - #6967

Closed
danielhanchen wants to merge 58 commits into
mainfrom
danielhanchen/block-markerless-code-tools
Closed

danielhanchen wants to merge 58 commits into
mainfrom
danielhanchen/block-markerless-code-tools

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jul 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Two HIGH-severity prompt-injection to RCE findings in Unsloth Studio.

The markerless tool-call parsers promoted a bare call:NAME{...}, NAME[ARGS]{json} or {"name":...} found anywhere in assistant text into a real tool call, gated only on whether NAME was enabled. When the model quoted attacker-controlled content (web, RAG, pasted text) shaped like one of those, the safetensors, MLX and GGUF loops ran it through execute_tool -> _bash_exec / _python_exec. The model never decided to call a tool; it echoed text shaped like one.

Root cause: real Gemma, Mistral and Llama calls carry an unambiguous wrapper (<|tool_call>, [TOOL_CALLS], <|python_tag|>), but generation streams with skip_special_tokens=True, which strips the wrapper before the parser sees it and forces a bare-text fallback that cannot tell a real call from a quote.

Fix

1. Never promote a markerless execution-class call. EXECUTION_CLASS_TOOL_NAMES (python, terminal, edit_file, plus every mcp__*) is always prose in the markerless paths, regardless of the enabled list. Such a call must carry a wrapper or arrive as a structured tool_calls entry. New _markerless_promotable() in core/tool_healing.py; every markerless parse and strip path routes through it. Parse and strip stay symmetric, so a blocked call stays visible as text instead of silently vanishing.

2. Stop destroying the wrapper. NativeToolTokenDecoder (core/inference/native_tool_tokens.py) is a tokenizer proxy used on tool-enabled turns by the Transformers text and vision streamers and both MLX paths. It keeps the provenance-bearing controls (<tool_call>, [TOOL_CALLS], <|python_tag|>, <think>, DeepSeek and Kimi envelopes) and suppresses every other special token exactly as before. A tokenizer with no usable all_special_ids falls back to skip_special_tokens=True, so (1) remains the security boundary and this is capability restoration only.

Streaming: the safetensors and GGUF loops now treat a promotable bare Gemma call as a boundary and hold a trailing partial call:NAME{.., so a blocked-then-promotable chain cannot leak before the parse decides.

Impact

  • Unchanged: wrapped and structured calls (<|tool_call>, [TOOL_CALLS], <function=>, GGUF structured tool_calls) still promote python and terminal; benign bare tools like web_search still work.
  • Changed: a bare, wrapper-less python / terminal / edit_file / mcp__* call renders as text instead of executing. That is the fix. Wrapped and structured MCP calls are unaffected.

Verification

  • Loop-level A/B against the merge base: on main six markerless PoCs (quoted page, rehearsal, bare JSON, python, edit_file, mcp__*) all reach execute_tool; on this branch all six return no calls, while the wrapped, structured and benign-bare forms still execute.
  • Decoder fidelity on 22 cached tokenizers: decoded ordinary text is byte-identical to skip_special_tokens=True on every one. On unsloth/Llama-3.2-1B-Instruct, <|python_tag|> was being stripped and is now preserved, which is the difference between a lost wrapped call and a promoted one.
  • Real generation on an RTX 6000 Ada (Qwen2.5-0.5B-Instruct, gemma-3-270m-it): streams byte-identical with and without the decoder for ordinary answers; a genuine <tool_call>-wrapped terminal call still parses and strips cleanly.
  • Local run on the merged head: 1190 passed, 16 skipped across the guard, parser, streaming and per-backend loop suites.

Status

Merged up to date with main (the branch was 201 commits behind and is now conflict-free). CI is re-running on the merged head; any red job will be triaged against main before this is ready.

danielhanchen and others added 2 commits July 8, 2026 09:05
The markerless tool-call parsers promoted a bare call:NAME{...} and
NAME[ARGS]{json} found anywhere in assistant text into a real tool call,
gated only by whether NAME was an enabled tool. When the model quotes
attacker-controlled content (web, RAG, pasted text) shaped like one of
those, the safetensors, MLX and GGUF loops would execute it via
execute_tool, so prose could be turned into host code execution through
the python and terminal tools.

Real Gemma and Mistral calls carry an unambiguous wrapper
(<|tool_call>...<tool_call|>, [TOOL_CALLS]), but generation streams with
skip_special_tokens=True, which strips that wrapper before the parser
sees it and forces the bare-text fallback that cannot tell a real call
from a quote.

Never promote or strip a markerless execution-class call. Add a shared
EXECUTION_CLASS_TOOL_NAMES = {python, terminal} and a _markerless_promotable
helper in tool_healing, and route every markerless parse and strip path
through it: the bare Gemma call, the bare rehearsal name[ARGS]{...}, the
bare Llama-3.2 {"name":...} JSON, and the two leading-dispatch guards.
Trusted wrapped and structured forms (<|tool_call>, [TOOL_CALLS],
<function=>, and GGUF structured tool_calls) still promote python and
terminal normally, and benign bare tools are unchanged.

Existing tests that used python/terminal as placeholder names to exercise
parser mechanics are re-pointed to benign tools; add
test_markerless_exec_tool_guard.py plus loop-level cases proving a bare
execution call in prose never reaches execute_tool while the wrapped
forms still execute.
@danielhanchen

Copy link
Copy Markdown
Member Author

Confirmed this hits the markerless paths in studio/backend/core/inference/tool_call_parser.py where a bare call:python{...} is still promoted on the enabled-tool gate alone. The branch has conflicts now, so could you rebase and I will get this reviewed.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-08T12:15:16.813588Z ca022ed New commits
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 53512cf491

ℹ️ 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".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a122980db2

ℹ️ 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".

Comment thread studio/backend/core/tool_healing.py Resolved
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 31, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 2, 2026
@oobabooga

Copy link
Copy Markdown
Member

CI note: the two red jobs on this head are both pre-existing on main, not from this branch.

  • Repo tests (CPU): test_model_picker_contracts.py::test_a_pinned_cached_row_loads_from_the_id_the_backend_pinned asserts picker.count("loadId: c.load_id") == 4 and gets 1.
  • (Python 3.13): test_non_gguf_reload_settings.py::TestNonGgufStatusReportsWhatTheLoadAskedFor::test_the_non_gguf_status_branch_publishes_them.

Both reproduce with the same counts on a worktree checked out at the merge base (6e21a68), so neither is caused by the diff here. The two failures this branch did introduce, the read_text() encoding lint and the four refactor-guard baseline assertions for the new helpers, are fixed in 012eefe and 10988c7.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae7c972eff

ℹ️ 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".

Comment on lines +2754 to +2756
name = _top_level_bare_json_name(probe)
blocked = _markerless_blocked_execution(name, enabled_tool_names)
if not blocked and not _markerless_promotable(name, enabled_tool_names):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the decoded JSON name for the strip guard

When a model emits duplicate top-level name keys, _top_level_bare_json_name returns the first value while _parse_llama3_bare_json uses json.loads, which keeps the last one. For {"name":"web_search","name":"terminal","arguments":{}}, the parser correctly blocks terminal, but this cleanup classifies the object as promotable web_search and deletes the entire assistant response; reversing the names executes web_search while preserving its raw serialization. Derive the guard name from the already decoded object (or reject duplicate keys) so parsing and cleanup make the same decision.

Useful? React with 👍 / 👎.

…arkerless-code-tools

# Conflicts:
#	studio/backend/core/inference/inference.py
#	studio/backend/core/inference/mlx_inference.py
#	studio/backend/core/inference/tool_call_parser.py
#	studio/backend/core/tool_healing.py
#	studio/backend/tests/test_mlx_inference_backend.py

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca022edc14

ℹ️ 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".

Comment on lines +2854 to +2856
``promotable_gemma_call_pos`` needs the ``{``, so a mid-prose call leaks ``call:web``
first. STREAMING holds this tail as it holds a split ``NAME[ARGS]`` rehearsal; prose
releases it, and so does a closed call, whose boundary the signal scan already owns.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Rewind detection to the held Gemma opener

In the safetensors streaming path, a mid-prose bare Gemma call whose enabled tool name exceeds _TOOL_SIGNAL_OVERLAP (27 characters) leaks when the name and { arrive in separate snapshots. _tool_signal_scanned_upto advances while this helper withholds the partial tail, so the next _earliest_tool_signal scan starts after call: and misses the completed opener; once the body closes, this helper returns zero on the assumption that the signal scan owns it, causing the raw serialization to be emitted even though the end-of-turn parser subsequently executes it. Rewind detection to the start of the held tail, or make its overlap cover the full supported Gemma prefix.

Useful? React with 👍 / 👎.

@unslothai unslothai deleted a comment from gemini-code-assist Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

Conflicts are resolved and the branch is current with main (it was 201 commits behind).

Five files conflicted. inference.py, mlx_inference.py and test_mlx_inference_backend.py took both sides: main's add_special_tokens parameter, VLM prompt-cache session and new tests sit alongside the decoder wiring and the provenance tests. tool_call_parser.py and tool_healing.py kept this branch's side, since main had only rewrapped those comments while still carrying the pre-fix enabled_tool_names gating that this PR replaces with _markerless_promotable.

Local run on the merged head: 1190 passed, 16 skipped across the guard, parser, streaming and per-backend loop suites.

Also slimmed the comments across the nine touched source files in two passes, roughly 200 lines lighter, AST-verified comment-only both times so the code is byte-identical. Cleared the superseded review acks from the thread to make the open items easier to find.

@danielhanchen

Copy link
Copy Markdown
Member Author

Superseded by #10507, which carries the identical tree from this branch's head ca022edc1.

Closing this one to keep the review on a single thread. The earlier rounds and their resolutions stay here for reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants