litellm/tests/unit/proxy/policy_engine
izzybn fdf3bab02f
fix(guardrails)!: mask PII in Presidio tool calls and refuse streams it cannot read (#53396)
* fix(guardrails): judge and deliver the /v1/messages stream frames a client reads in tool_permission

* fix(guardrails): mask or refuse every /v1/messages stream frame a client reads under presidio output masking

Presidio output masking forwarded the upstream's raw frames whenever its own
read of the stream found nothing to mask, so text a client reads in a shape
assembly skipped (content in message_start, a reused block index, an
unterminated error frame) reached the client raw, and an upstream error
frame was never scanned at all

The stream now goes out as sent only when every client reads it as assembly
does, no upstream error cut it, and masking changed nothing in the whole
response, tool input included. A stream a client could read another way is
refused. Error frames have every string in their data masked, or the whole
payload when it is not JSON

Model Armor fails closed on the same unreadable shapes through its existing
could-not-assemble path

* fix(guardrails): drop the empty text block a guardrail rebuild adds before a tool call

* fix(guardrails): treat stream data that httpx reads past a form feed as a stream clients read differently

* fix(guardrails): refuse mixed-field deltas, keep upstream start fields, and forward clean error-cut streams under presidio

A block delta that carries a field assembly reads before the field its type names, like text on an input_json_delta, now counts as a stream clients read differently, so presidio refuses it and model armor fails closed

Presidio forwards a stream cut by an upstream error as sent when masking changed nothing, and masks every data line of an upstream error frame instead of the first one

A rebuilt stream keeps the upstream message_start fields and each tool call's content_block_start fields, such as service_tier, container and caller, while the id, model and content still come from the gateway's response

* fix(guardrails): forward a masked stream as sent only when its scan read every block, and scan decoded tool arguments

Presidio forwarded a /v1/messages stream as sent whenever masking left the assembled response unchanged. Assembly drops a tool call cut off by an upstream error and a tool start input that streamed input replaces, so the part a client reads went out without a scan. Such a stream is now rebuilt from what was scanned, and Model Armor fails closed on it

Tool arguments are now scanned as decoded JSON, so PII written as unicode escapes gets masked or blocked. Arguments that masking leaves alone go out byte for byte. This applies to /chat/completions tool calls too

* fix(guardrails): mask non-streamed tool calls, every named error frame and duplicate tool keys

Presidio apply_to_output now masks or blocks tool call arguments of a
non-streamed reply on /v1/messages, /chat/completions and /v1/responses, sends
a masked number as a JSON string, masks any frame named error, scans a key a
later duplicate replaces, and keeps the upstream message_delta fields when it
rebuilds a stream

* fix(guardrails): mask numbers in upstream error frames

* fix(guardrails): edit presidio-masked anthropic streams in place and keep tool inputs valid JSON

Output masking on streamed /v1/messages now changes only the frames whose text or tool input changed, so thinking signatures, block order, citations of unmasked text and tool calls cut by the token limit go out as the provider sent them

Tool arguments are scanned with each JSON string decoded where it sits, so PII after a newline is found and the arguments stay valid JSON. Keys that mask to the same text stay separate entries

Model Armor scans a tool call the token limit cut instead of failing closed on it

* fix(guardrails): mask tool calls only for listed entities and read overlapping blocks

Mask tool calls when the guardrail lists its entities and asks to scan the output, so a guardrail on the default entities no longer turns the file paths of an agent into placeholders

Read a stream by block index, so a provider that overlaps its blocks is no longer refused, and scan a JSON key on its own when a detection covers more than the key

When a policy pipeline cannot write a tool call back, deliver the masked text with the tool call as sent

* fix(guardrails): keep main's behavior for callbacks presidio and fail-open Model Armor

The legacy callbacks: ["presidio"] setup builds the guardrail without mask_tool_calls, so the constructor default is now off and tool calls stay as main sends them. Model Armor with fail_on_error: false scans the text it can read from a stream it cannot assemble, as main does, instead of forwarding it unscanned

* fix(guardrails): refuse a stream that hides copies of a frame from other readers

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-23 03:09:43 -08:00
..
__init__.py
test_attachment_registry.py
test_condition_evaluator.py
test_init_policies.py feat(proxy)!: add suppress_startup_output to turn off startup prints (#47708) 2026-10-29 03:47:04 -07:00
test_pipeline_executor.py fix(guardrails)!: mask PII in Presidio tool calls and refuse streams it cannot read (#53396) 2026-11-23 03:09:43 -08:00
test_policy_engine_endpoints.py
test_policy_matcher.py
test_policy_registry.py
test_policy_resolver.py
test_policy_validator.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00
test_policy_versioning.py
test_policy_versioning_e2e.py
test_response_retrieval.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00