← Back to DAGDA | Support | Source: https://github.com/BerriAI/litellm/pull/50214
Skip to content

fix(proxy): redact guardrail-blocked request body from spend logs - #50214

Open
shadowmodder wants to merge 11 commits into
BerriAI:mainfrom
shadowmodder:litellm_guardrail_blocked_spend_logs
Open

shadowmodder wants to merge 11 commits into
BerriAI:mainfrom
shadowmodder:litellm_guardrail_blocked_spend_logs

Conversation

@shadowmodder

@shadowmodder shadowmodder commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor
A guardrail that blocks a request in pre_call leaves the blocked secret in the spend logs. The proxy snapshots the request body before guardrails run and only refreshes that snapshot after they pass, so a block means the refresh never runs and the failure logger persists the raw body. This PR replaces the stored body with a redacted placeholder when a guardrail blocks, while the request still fails exactly as before.
Details

TLDR

Problem this solves:

  • A pre_call guardrail BLOCK stores the blocked secret in plain text in spend logs
  • The body snapshot is taken before guardrails run and refreshed only after they pass
  • A block raises first, so the refresh never runs and the failure logger persists the raw body

How it solves it:

  • The shared pre-call path redacts the stored body when a guardrail intervention raises
  • The stored body becomes a placeholder naming the model and the block, with no prompt content
  • The block itself is unchanged, the same error still reaches the caller

User Flow

Before: a proxy operator with spend-log prompt storage enabled cannot stop blocked secrets reaching the logs

  1. They send POST https://litellm-domain/v1/chat/completions with a message containing a secret and a guardrail that blocks secrets
  2. The proxy returns 400, the guardrail worked, and the request never reaches the model
  3. They open the spend logs and find the blocked secret stored in plain text in the failed request row

After: the same blocked request leaves only a redacted placeholder in the logs

  1. They send the same POST https://litellm-domain/v1/chat/completions with a message containing a secret
  2. The proxy returns the same 400 with the same guardrail error
  3. They open the spend logs and find the failed request row holds {"model": "small", "[redacted]": "blocked by guardrail"} instead of the prompt

Relevant issues

Fixes #50127

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally: uv run pytest tests/unit/proxy/test_common_request_processing.py tests/unit/proxy/test_litellm_pre_call_utils.py (1254 passed). Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

E2E exception at b91bc972

  • Scope: one shared pre-call funnel plus a redaction helper and regression tests, no new endpoint or provider behavior
  • Rationale: the diff is mechanical, a try/except around the existing guardrail hook plus a helper that follows the existing snapshot refresh pattern, and the focused checks below prove the fix end to end in process, including the exact defect from the issue
  • Commands and results: python /tmp/repro_50127.py drives common_processing_pre_call_logic with a mocked pre_call_hook raising GuardrailRaisedException and HTTPException 400, each carrying a fake secret. Before the fix the stored body held the raw prompt with the secret. After the fix both cases store {"model": "small", "[redacted]": "blocked by guardrail"} and the secret appears nowhere in the stored body

Red-then-green mutation proof:

  • With the fix stashed, the two parametrized wiring tests fail: the stored body still holds the raw prompt with the fake token. With the fix restored, all 5 new tests pass
  • pytest tests/unit/proxy/test_common_request_processing.py tests/unit/proxy/test_litellm_pre_call_utils.py -k "redacts_blocked_request_body or keeps_body_on_non_guardrail or redact_blocked_request_body_snapshot": 5 passed

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Endpoints that call pre_call_hook directly instead of through the shared pre-call funnel are out of scope for this PR
  • The redaction helper is available for a follow-up if those paths are shown to persist the raw body

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Follow-up fix (veria-ai review)

  • After a primary-model rate-limit error, a fallback guardrail block redacted the fallback copy's snapshot, but _pre_call_with_fallbacks then restored the original unredacted snapshot (self.data = rate_limited_data), so the blocked prompt could still reach the failure logs. Fixed by redacting the restored snapshot too when the fallback raises a guardrail intervention.
  • New regression test test_fallback_guardrail_block_redacts_restored_request_body (fails without the fix, passes with it).

Update: DynamoAI guardrails

DynamoAI blocks raised a plain ValueError, which the redaction check does not recognize as a guardrail intervention. The hook now raises GuardrailRaisedException with blocked_content set and the same error message, so DynamoAI-blocked bodies are redacted from spend logs too. Its tests assert the intervention contract.

@veria-ai

veria-ai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Fixed/addressed: 3 · PR risk: 0/10

Reviews

…cked_spend_logs

# Conflicts:
#	litellm/proxy/common_request_processing.py
#	tests/unit/proxy/test_common_request_processing.py
…r merging main

Merging main left _pre_call_with_fallbacks one branch over the strict complexity limit, so the guardrail check moves into a small helper used by both call sites. redact_blocked_request_body_snapshot only reads its argument, so it now takes a Mapping, which clears the LIT001 finding on its signature.
DynamoAI blocks raised a plain ValueError, which is_guardrail_intervention does not recognize, so guardrail-blocked request bodies from DynamoAI skipped the spend-log redaction this PR adds. The three violations_detected sites now raise GuardrailRaisedException with blocked_content set and the same error message text.

Signed-off-by: shadowmodder <sudhirvissa@gmail.com>
Same change as the previous commit, stored as plain source (the previous write stored the content base64-encoded).

Signed-off-by: shadowmodder <sudhirvissa@gmail.com>
Signed-off-by: shadowmodder <sudhirvissa@gmail.com>
@shadowmodder

Copy link
Copy Markdown
Contributor Author

Fixed both. DynamoAI blocks now raise GuardrailRaisedException with blocked_content set, and dynamoai.py is restored as plain source after an encoding slip in the previous write.

…locked_spend_logs

# Conflicts:
#	litellm/proxy/common_request_processing.py
# Conflicts:
#	litellm/proxy/common_request_processing.py
#	tests/unit/proxy/test_common_request_processing.py

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Requests blocked by a pre_call guardrail are stored in spend logs with the text that got them blocked

1 participant