litellm/tests/test_litellm/proxy/guardrails/guardrail_hooks
yucheng-berri 049c6836d2
fix(model_armor): sanitize error details by default (#33908)
* fix(model_armor): sanitize error details by default

Generated with AI

Co-Authored-By: Claude Code

* fix(model_armor): sanitize handler-raised HTTP errors and redact scanned content in guardrail logging

The async HTTP handler raises MaskedHTTPStatusError on any non-2xx via
raise_for_status, so the non-200 branch in make_model_armor_request never ran
against a live API and the raw upstream body reached callers and logs. Catch
the raised error and build the sanitized detail from the response status

Replace the empty-dict guardrail logging payload with field-level redaction of
the keys that echo scanned content (text, sanitizedText, findings) so guardrail
traces keep filter states and block reasons while scanned content stays out

Restore the upstream status code in the sanitized error detail, read guardrail
metadata from the same key the hooks write, and keep guardrail_status within
its typed literal values

* fix(model_armor): bound redactor recursion depth and allowlist it in the recursion detector

_redact_scanned_content walks provider JSON bounded by _REDACT_MAX_DEPTH=20 and
fails closed by returning the redaction sentinel at the cap

* fix(model_armor): honor fail_on_error for upstream API failures

API failures now raise a dedicated ModelArmorAPIError so hooks can tell them
apart from content-block HTTPExceptions; fail_on_error=False lets the request
proceed on a Model Armor outage again while fail-closed configs get the same
sanitized 400 as before

Also addresses review notes: sanitize_error_detail constructor annotation
matches the nullable config field, redaction is owned by the metadata write
sites so _process_response no longer re-applies it, and the request and
response debug log branches move into helpers

* test(model_armor): cover fail_on_error routing on during-call, post-call, streaming, and file-scan paths

* chore: remove accidentally committed pytest cache files

* fix(model_armor): keep sanitize_error_detail coerced across in-memory config reloads

update_in_memory_litellm_params assigns raw LitellmParams fields, so a hot
reloaded config carrying an explicit null would silently disable sanitization;
re-apply the only-explicit-False-opts-out coercion after the update

* fix(model_armor): redact matched malicious URIs and reuse the shared recursion depth constant

maliciousUriMatchedItems echoes the caller-supplied URL including path and
query, so it joins the scanned-content key set; the redactor depth cap now
comes from DEFAULT_MAX_RECURSE_DEPTH in litellm constants instead of a local
literal

* fix(model_armor): keep API failures out of the intervention trace status

Fail-closed upstream failures re-raise ModelArmorAPIError instead of
converting to HTTPException(400), so the shared guardrail logging keeps
recording them as guardrail_failed_to_respond while content blocks stay
guardrail_intervened. Callers see the same 500 shape as before this PR,
with the sanitized message

* chore(model_armor): drop explanatory comment per repository comment policy

---------

Co-authored-by: eugene-yao-zocdoc <eugene.yao@zocdoc.com>
2026-07-21 10:28:24 -07:00
..
azure fix(guardrails): cover multi-choice output variants 2026-05-01 19:38:04 -07:00
content_filter feat(guardrails): add pre_mcp_call support to Content Filter (#32936) 2026-07-11 16:17:49 -07:00
guardrails_ai style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
openai feat(guardrails): surface OpenAI moderation violation_categories on guardrail traces (#30659) 2026-06-17 09:44:19 -07:00
unified_guardrails feat(guardrails): streaming text transformation in generic_guardrail_api (#33110) 2026-07-14 17:38:11 -07:00
_cisco_ai_defense_test_utils.py feat(guardrails): add Cisco AI Defense integration (#28249) (#30338) 2026-06-12 23:21:23 -07:00
code_execution_compliance_dataset.json feat(add-new-block_code_execution-guardrail): prevent agent from executing code (#22154) 2026-02-25 22:02:14 -08:00
test_aim.py fix(guardrails): walk Responses-API text taxonomy in shared content helpers (#32542) 2026-07-08 23:24:11 -07:00
test_bedrock_guardrails.py fix(proxy): capture logging_obj before post_call_failure_hook pops it in ModifyResponseException streaming path (#32665) 2026-07-09 13:48:47 -07:00
test_bedrock_invoke_guardrail_checks.py feat(bedrock guardrails): add resource-less InvokeGuardrailChecks (detect-only) mode (#33299) 2026-07-14 19:31:17 -07:00
test_block_code_execution.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_block_code_execution_compliance.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_cato_networks.py Litellm OSS Staging (#29161) 2026-06-01 21:22:35 -07:00
test_cisco_ai_defense_chat.py feat(guardrails): add Cisco AI Defense integration (#28249) (#30338) 2026-06-12 23:21:23 -07:00
test_cisco_ai_defense_mcp.py feat(guardrails): add Cisco AI Defense integration (#28249) (#30338) 2026-06-12 23:21:23 -07:00
test_compresr.py feat(guardrails): add Compresr guardrail for query-aware context compression (#33295) 2026-07-15 13:53:41 -07:00
test_crowdstrike_aidr.py fix(guardrails): send only new messages since last assistant turn to CrowdStrike AIDR (#31974) 2026-07-06 09:47:00 -07:00
test_deepkeep.py feat: add deepkeep as custom guardrail (#33844) 2026-07-20 19:27:40 -07:00
test_dynamoai.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_enkryptai.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_generic_guardrail_api.py feat(guardrails): streaming text transformation in generic_guardrail_api (#33110) 2026-07-14 17:38:11 -07:00
test_grayswan.py fix(guardrails): forward grayswan scan id header (#32544) 2026-07-08 15:05:27 -07:00
test_headroom.py fix(guardrails): address review comments on headroom fail_open 2026-07-03 19:40:33 +00:00
test_hiddenlayer.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_lakera_ai_v2.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_lasso.py feat(lasso): send source.type=litellm for Used By attribution (#33090) 2026-07-13 10:39:41 -07:00
test_mcp_end_user_permission.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_mcp_security.py Add MCP Security guardrail to block unregistered MCP servers (#21429) 2026-02-17 21:19:13 -08:00
test_microsoft_purview.py feat(guardrails): add Microsoft Purview DLP guardrail (#24966) 2026-05-22 15:59:04 -07:00
test_model_armor.py fix(model_armor): sanitize error details by default (#33908) 2026-07-21 10:28:24 -07:00
test_noma.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_noma_v2.py fix noma v2 deepcopy crashing in build scan payload - new PR (#26605) 2026-04-27 08:51:26 -07:00
test_onyx.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_ovalix.py feat: litellm oss 110626 (#30202) 2026-06-11 22:30:26 -07:00
test_pangea.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_panw_prisma_airs.py Litellm OSS Staging 010626 (#29422) 2026-06-01 21:42:51 -07:00
test_presidio.py Revert "fix(presidio): stream SSE output incrementally instead of buffering t…" (#31764) 2026-06-30 21:37:11 +00:00
test_presidio_union_fix.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_promptguard.py Add PromptGuard guardrail integration (#24268) 2026-04-09 08:12:24 -07:00
test_qualifire.py Hotfix - docs qualifire (#18724) 2026-01-07 17:23:12 +05:30
test_repelloai.py chore: litellm oss staging (#30745) 2026-06-18 13:55:35 -07:00
test_response_rejection_guardrail_code.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_singulr.py chore(ci): merge oss branch (#33784) 2026-07-17 23:22:13 +00:00
test_straiker.py feat: add Straiker guardrail integration (#33781) 2026-07-18 03:31:29 +00:00
test_structured_messages_writeback.py feat(guardrails): add headroom guardrail for message compression (#31407) 2026-06-26 19:36:44 -07:00
test_tool_permission.py Litellm oss staging 040626 (#29671) 2026-06-04 11:07:20 -07:00
test_tool_policy_guardrail.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_vigil_guard.py Litellm OSS Staging 010626 (#29422) 2026-06-01 21:42:51 -07:00
test_xecguard.py fix(xecguard): sanitize scan result before recording it for logging (#32935) 2026-07-12 02:46:02 +00:00