Neutralize chat-template special-token literals in untrusted tool output
fix(middleware): neutralize chat-template special-token literals in untrusted tool output (#715)
Neutralize chat-template special-token literals in untrusted tool output79ee2a-fix-neutralize-chat-template-special-token-literals-in-untrusted-cve-2026-61732fixneutralize chat template special token literals
Committed by PurpleCHOIms
commit message
fix(middleware): neutralize chat-template special-token literals in untrusted tool output (#715)
GHSA-g5f9-3xfg-p9mf (CVSS 9.8). Under BYOK with a self-hosted /
OpenAI-compatible backend (vLLM, SGLang, TGI, Ollama, …) whose tokenizer
preserves special-token IDs, a ChatML literal such as `<|im_start|>`
planted in attacker-controlled tool output (web crawl, recon scan, bash
stdout) is parsed into a structural role-delimiter token, forging an
operator/system turn the model treats as authoritative — bypassing the
quarantine envelope and enabling arbitrary command execution in the
sandbox. Hosted vendors strip these server-side, but that is vendor-side
luck, not an architectural control.
The two quarantine middlewares already DETECT these literals and stamp
risk, but emitted the bytes verbatim. Add application-layer neutralization:
- new shared helper `neutralize_special_tokens()` in `_injection_detector`
inserts a zero-width space inside each recognized special-token literal so
the exact-match special-token vocab entry can no longer fire, while the
text stays readable for the model and the operator audit trail. Covers
ChatML/Qwen/DeepSeek, Llama-3.x, Gemma, and Mistral families plus the
U+FF5C fullwidth-vertical-bar bypass (and half/full-width mixing).
- call it at the two chokepoints every untrusted tool output funnels through:
`UntrustedOutputMiddleware._format_envelope` and
`PromptInjectionShieldMiddleware._wrap_untrusted`.
Tests: string-level defang across all token families + both middleware
integration paths, plus a gated (`-m slow`, transformers importorskip)
tokenizer-level regression. Verified live against the real Qwen2.5
tokenizer: the advisory's EXPLOIT payload drops from 5 forged role-boundary
token IDs to 0 after neutralization.Editor assessment
This commit patches CVE-2026-61732. Outstanding body: the ChatML special-token forgery, the zero-width-space neutralization, the covered token families including the fullwidth-bar bypass, the two middleware chokepoints and the live Qwen2.5 verification are all explained. The middleware scope and fix type are accurate. The 96-character subject is the only flaw - long enough to hurt scanning - so trimming it is the single change needed for a 5. The zero-width-space choice is clever but unusual; one sentence on why readability and audit trails survive would preempt reviewer doubt.