Repository navigation
fix(guardrails): keep client tools untouched when a guardrail does not rewrite them - #40077
duhd-vnpay wants to merge 1 commit into
Conversation
…t rewrite them
The Anthropic /v1/messages guardrail translation re-serialized tools back into
Anthropic shape whenever a guardrail returned a tools list, even when the
guardrail had returned the very list it was handed. _map_tool_helper always
stamps type:"custom", so a client tool sent as {name, description, input_schema}
reached the upstream as {name, description, input_schema, type:"custom"}. The
real Anthropic API accepts both forms, but strict Anthropic-compatible upstreams
reject the explicit discriminator, so a guarded request that changed nothing
came back 400 with `tools[0]: unknown variant 'custom'`.
Guardrails now have to hand back a different list object to trigger the rewrite,
which is the contract the same function already uses for structured_messages a
few lines below. Genuine tool rewrites and tool injections keep working, and the
scan_only_tool_results merge path is unaffected because the guardrail never
receives the request tools there, so anything it returns is a new object.
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR avoids reserializing Anthropic tools when a guardrail returns the original list and adds regression coverage for unchanged and rewritten tools
Confidence Score: 4/5The PR should not merge until tool changes are detected without breaking existing custom guardrails or retriggering serialization for equivalent copies The identity check fixes one pass-through shape but silently drops in-place tool edits and still rewrites equivalent copied lists Files Needing Attention: litellm/llms/anthropic/chat/guardrail_translation/handler.py
|
| Filename | Overview |
|---|---|
| litellm/llms/anthropic/chat/guardrail_translation/handler.py | Adds identity-based tool rewrite detection, fixing the reported pass-through path but introducing compatibility failures for other valid custom guardrail return patterns |
| tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py | Adds focused coverage for identity-preserving pass-through and replacement-list rewrites, but does not cover copied unchanged lists or in-place edits |
Reviews (1): Last reviewed commit: "fix(guardrails): keep client tools untou..." | Re-trigger Greptile
| # structured_messages below). Re-serializing an untouched list would stamp type:"custom" onto | ||
| # every tool, which strict Anthropic-compat upstreams (e.g. DeepSeek) reject with | ||
| # `tools[0]: unknown variant 'custom'`. | ||
| if guardrailed_tools is not None and guardrailed_tools is not tools_to_check: |
There was a problem hiding this comment.
Identity Misclassifies Tool Changes
Copied lists are reserialized and may be rejected upstream; in-place edits are dropped. This violates the requirement to gate incompatible behavior behind a user flag
Rule Used: What: avoid backwards-incompatible changes without... (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
| # structured_messages below). Re-serializing an untouched list would stamp type:"custom" onto | ||
| # every tool, which strict Anthropic-compat upstreams (e.g. DeepSeek) reject with | ||
| # `tools[0]: unknown variant 'custom'`. | ||
| if guardrailed_tools is not None and guardrailed_tools is not tools_to_check: |
There was a problem hiding this comment.
Low: In-place tool sanitization is discarded
GenericGuardrailAPIInputs exposes a mutable tool list, and custom guardrails can modify its tool dictionaries in place before returning inputs. In that case guardrailed_tools is tools_to_check, so this branch skips write-back; for example, an attacker-controlled secret in an Anthropic tool description remains in data["tools"] and is sent upstream even if the guardrail replaced that description in place. Preserve a pre-guardrail snapshot and write back when either list identity or contents changed, rather than making copy-on-write an undocumented requirement.
PR overviewThis PR updates Anthropic guardrail translation so client-provided tool definitions remain untouched when a guardrail does not rewrite them. One security issue remains: in-place tool edits made by a custom guardrail may not be written back to the outgoing request. This could cause sensitive content in attacker-influenced tool descriptions to be sent upstream despite attempted sanitization, though exploitation depends on a guardrail that mutates existing tool objects in place. Open issues (1)
Fixed/addressed: 0 · PR risk: 4/10 |
|
Closing this since main already keeps unchanged tools exactly as sent after #53290. Thanks for putting it together, and comment here if something is still missing |
TLDR
Problem this solves:
/v1/messagesrequests get their tools rewrittentype:"custom", strict Anthropic-compatible upstreams 400How it solves it:
structured_messagesUser Flow
Before: a developer using an Anthropic-compatible model behind a guardrailed key cannot use tools at all, and the error text points at a tool type they never sent
pre_callmode{"name": "Bash", "description": "Run a shell command", "input_schema": {...}}Failed to deserialize the JSON body into the target type: tools[0]: unknown variant 'custom', expected 'web_search_20250305' or 'web_search_20260209', naming acustomvariant that was never in their requesttoolsat all and it returns 200, so the tools array is the only differenceAfter: the same request works with the guardrail left on
pre_callmodetool_useblock when the model decides to callBashRelevant issues
Related to #24913, which is the mirror-image leak in the other direction (
translate_anthropic_tools_to_openaicopying the tool-leveltypeintofunction.parameters). That one is fixed, this is the write-back side of the same round-trip and is not covered by itLinear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Shared setup, both runs use the same config and the same payload
config.yaml, pointed at an Anthropic-compatible upstream that rejects the explicit tool discriminator, with a content-scanning guardrail on for every request:Proxy started with
python litellm/proxy/proxy_cli.py --config config.yaml --detailed_debugPayload, a client tool in the shape Anthropic clients actually send, with no
typekey:Before (168a005)
typethe request did not haveAfter (27470d7)
typeStaying in draft until the same run lands a funded 200 with a
tool_useblock. The guardrail is on for both runs, so the difference is the write-back and nothing elseType
🐛 Bug Fix
Caveats (if any)
Low
structured_messagesin the same function