Guardrail API V2 - user api key metadata, session id, specify input type (request/response), image support - #17338
Guardrail API V2 - user api key metadata, session id, specify input type (request/response), image support #1733810 commits merged into
Conversation
… a list[str] instead of one at a time
allows request vs. response processing to occur
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
|
||
| verbose_proxy_logger.debug( | ||
| "Generic Guardrail API: Extracted user metadata: %s", | ||
| {k: v for k, v in result_metadata.items() if v is not None}, |
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
To address the issue, only non-sensitive fields should be included in logging. This can be achieved by constructing a filtered dictionary excluding sensitive keys from the log output. Sensitive fields commonly include "user_api_key_hash", "user_api_key_token", "user_api_key" and any other fields related to authentication secrets, including all values in the "GenericGuardrailAPIMetadata" that could be used for authentication or authorization.
Key changes:
- In the log statement (line 140), instead of logging all key-values, construct a dictionary excluding sensitive keys such as "user_api_key_hash", "user_api_key_token" and similar.
- Ideally, define a list of sensitive keys, and filter them out from the logged dictionary.
- Only modify the specific lines responsible for logging (lines 138–141).
- No outside dependencies are needed; standard Python is sufficient.
- You may need to add a helper in the function to perform the filtering.
| @@ -135,9 +135,16 @@ | ||
| "user_api_key_token" | ||
| ) | ||
|
|
||
| # Define sensitive fields to mask or filter out from logging | ||
| SENSITIVE_KEYS = { | ||
| "user_api_key_hash", | ||
| "user_api_key_token", | ||
| "user_api_key", | ||
| } | ||
| safe_metadata = {k: v for k, v in result_metadata.items() if v is not None and k not in SENSITIVE_KEYS} | ||
| verbose_proxy_logger.debug( | ||
| "Generic Guardrail API: Extracted user metadata: %s", | ||
| {k: v for k, v in result_metadata.items() if v is not None}, | ||
| "Generic Guardrail API: Extracted user metadata (non-sensitive fields only): %s", | ||
| safe_metadata, | ||
| ) | ||
|
|
||
| return result_metadata |
| f"""send_user_api_key_alias: {self.send_user_api_key_alias}, | ||
| send_user_api_key_user_id:{self.send_user_api_key_user_id}, | ||
| send_user_api_key_team_id:{self.send_user_api_key_team_id}''' | ||
| send_user_api_key_team_id:{self.send_user_api_key_team_id}""" |
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
The best way to fix this is to avoid logging the specific values of the flags associated with user API key handling (send_user_api_key_alias, send_user_api_key_user_id, and send_user_api_key_team_id). Instead, you can log that these flags are set (e.g., "Configured", "Not configured") without disclosing exact values, or simply avoid logging them altogether. The code to change is within the constructor (__init__) of ZscalerAIGuard, specifically the call to verbose_proxy_logger.debug on line 52-56.
Recommended fix: Replace the f-string logging statement with a generic message such as "User API key flags configured" or remove it altogether.
No additional imports or definitions are required; just change or remove the problematic debug log in the constructor.
| @@ -50,9 +50,7 @@ | ||
| ).lower() in ("true", "1") | ||
|
|
||
| verbose_proxy_logger.debug( | ||
| f"""send_user_api_key_alias: {self.send_user_api_key_alias}, | ||
| send_user_api_key_user_id:{self.send_user_api_key_user_id}, | ||
| send_user_api_key_team_id:{self.send_user_api_key_team_id}""" | ||
| "User API key flags are set; not displaying their values for security." | ||
| ) | ||
|
|
||
| super().__init__(default_on=True) |
| verbose_proxy_logger.debug( | ||
| f"extra_headers: {extra_headers}" | ||
| ) | ||
| verbose_proxy_logger.debug(f"extra_headers: {extra_headers}") |
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 9 months ago
The best way to fix the problem is to ensure that when logging extra_headers, any header values that are sensitive (such as Authorization or any headers that may contain user API keys) are masked or excluded before logging. We will create a sanitized/shallow-copied version of extra_headers for logging purposes.
Steps:
- In
_prepare_headers, before loggingextra_headers, create a copy of the dict where the values for sensitive keys (like"Authorization") are replaced with a safe placeholder, e.g.,"****". - Only log this sanitized dict instead of the full one.
- This involves adding a small helper/snippet directly before the log line in
_prepare_headers. - No external imports are required, only built-in features.
| @@ -194,7 +194,12 @@ | ||
| user_api_key_user_id = kwargs.get("user-api-key-user-id", "N/A") | ||
| extra_headers.update({"user-api-key-user-id": user_api_key_user_id}) | ||
|
|
||
| verbose_proxy_logger.debug(f"extra_headers: {extra_headers}") | ||
| # Log a sanitized version of extra_headers with sensitive headers masked | ||
| sanitized_headers = extra_headers.copy() | ||
| for key in sanitized_headers: | ||
| if key.lower() == "authorization": | ||
| sanitized_headers[key] = "***" | ||
| verbose_proxy_logger.debug(f"extra_headers: {sanitized_headers}") | ||
| return extra_headers | ||
|
|
||
| async def _send_request(self, url, headers, data): |
…ype (request/response), image support (BerriAI#17338) * refactor(generic_guardrail_api.py): refactor to update to new guardrail api logic * refactor: refactor llm api integrations to support passing in text as a list[str] instead of one at a time * refactor: fix linting errors * refactor: pass request type to guardrail api allows request vs. response processing to occur * feat: pass user api key dict information to the guardrail api * fix: pass user api key dict information to the guardrail api * feat: pass litellm call id + trace id, if present * docs: update docs
Title
Guardrail API V2 - user api key metadata, session id, specify input type (request/response), image support
Example Response (NONE):
{ "action": "NONE" }Relevant issues
Make Guardrail API more useful for integration providers
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
Changes