[Feat] Allow specifying PII Entities Config when using Presidio Guardrails - #10810
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
|
||
| verbose_proxy_logger.debug( | ||
| "Making request to: %s with payload: %s", | ||
| analyze_url, |
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To address the issue, sensitive data should be sanitized or excluded from logs. Specifically:
- Avoid logging the full
analyze_urlif it contains sensitive information. Instead, log only non-sensitive parts (e.g., the endpoint path). - If the
analyze_payloadcontains sensitive data, avoid logging it entirely or sanitize it before logging.
The fix involves modifying the verbose_proxy_logger.debug statement to exclude or sanitize sensitive information. For example:
- Extract and log only the endpoint path from
analyze_url. - Replace sensitive fields in
analyze_payloadwith placeholders before logging.
| @@ -207,6 +207,8 @@ | ||
|
|
||
| sanitized_url = analyze_url.split("?")[0] # Remove query parameters if present | ||
| sanitized_payload = {key: "[REDACTED]" if key.lower() in ["api_key", "token"] else value for key, value in analyze_payload.dict().items()} | ||
| verbose_proxy_logger.debug( | ||
| "Making request to: %s with payload: %s", | ||
| analyze_url, | ||
| analyze_payload, | ||
| "Making request to: %s with sanitized payload: %s", | ||
| sanitized_url, | ||
| sanitized_payload, | ||
| ) |
| async with aiohttp.ClientSession() as session: | ||
| # Make the request to /anonymize | ||
| anonymize_url = f"{self.presidio_anonymizer_api_base}anonymize" | ||
| verbose_proxy_logger.debug("Making request to: %s", anonymize_url) |
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI over 1 year ago
To fix the issue, we should avoid logging sensitive information like the anonymize_url directly. Instead, we can log a sanitized or generic message that does not expose sensitive details. For example, we can log a message indicating that a request is being made to the anonymizer endpoint without including the full URL. This ensures that debugging information is still available without compromising security.
Changes required:
- Replace the
verbose_proxy_logger.debugstatement on line 234 with a sanitized log message that does not include theanonymize_url. - Ensure that no sensitive data is logged elsewhere in the function.
| @@ -233,3 +233,3 @@ | ||
| anonymize_url = f"{self.presidio_anonymizer_api_base}anonymize" | ||
| verbose_proxy_logger.debug("Making request to: %s", anonymize_url) | ||
| verbose_proxy_logger.debug("Making request to the Presidio anonymizer endpoint.") | ||
| anonymize_payload = { |
…rails (BerriAI#10810) * refactor: use analyze_text, anonymize_text * feat: allow defining pii_entities_config for presidio * feat: use entities config for presidio analyze request * feat: add test_presidio_pii.py * testing: add guardrails testing job * feat: allow blocking specific entities pii * test: use 1 file for presidio guard tests * fix: presidio pii tests * test: presidio blocked entity * clean up docs * docs presidio pii parsing * fix: raise_exception_if_blocked_entities_detected * fix: linting errors
[Feat] Allow specifying PII Entities Config when using Presidio Guardrails
Entity Type Configuration
You can configure specific entity types for PII detection and decide how to handle each entity type (mask or block).
Configure Entity Types in config.yaml
Define your guardrails with specific entity type configuration:
Supported Entity Types
LiteLLM Supports all Presidio entity types. See the complete list of presidio entity types here.
Supported Actions
For each entity type, you can specify one of the following actions:
MASK: Replace the entity with a placeholder (e.g.,<PERSON>)BLOCK: Block the request entirely if this entity type is detectedTest request with Entity Type Configuration
When using the masking configuration, entities will be replaced with placeholders:
Example response with masked entities:
{ "id": "chatcmpl-123abc", "choices": [ { "message": { "content": "I can see you provided a <CREDIT_CARD> and an <EMAIL_ADDRESS>. For security reasons, I recommend not sharing this sensitive information.", "role": "assistant" }, "index": 0, "finish_reason": "stop" } ], // ... other response fields }When using the blocking configuration, requests containing the configured entity types will be blocked completely with an exception:
When running this request, the proxy will raise a
BlockedPiiEntityErrorexception.{ "error": { "message": "Blocked PII entity detected: CREDIT_CARD by Guardrail: presidio-block-guard." } }The exception includes the entity type that was blocked (
CREDIT_CARDin this case) and the guardrail name that caused the blocking.Relevant issues
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitType
🆕 New Feature
✅ Test
Changes