Repository navigation
(feat) Generic Guardrail API - allows guardrail providers to add INSTANT support for LiteLLM w/out PR to repo - #17175
6 commits merged into
Conversation
Allows guardrail providers to work with litellm for guardrails without needing to make a PR to LiteLLM
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
This is the final PR Bugbot will review for you during this billing cycle
Your free Bugbot reviews will reset on December 25
Details
You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
| guardrail_name=guardrail.get("guardrail_name", ""), | ||
| event_hook=litellm_params.mode, | ||
| default_on=litellm_params.default_on, | ||
| ) |
There was a problem hiding this comment.
Bug: Headers from config not passed to GenericGuardrailAPI
The initialize_guardrail function creates a GenericGuardrailAPI instance without passing the headers parameter from the configuration. The GenericGuardrailAPI.__init__ expects headers as a parameter (used for authentication), but only api_base, api_key, and additional_provider_specific_params are passed. Since headers isn't forwarded, any authentication headers (like Authorization: Bearer ...) configured in the YAML will be ignored, and API calls to the external guardrail will fail authentication. The api_key being passed is also unused since GenericGuardrailAPI doesn't have an api_key parameter—it expects authentication via headers.
There was a problem hiding this comment.
Perfect! I've fixed all three issues identified in the PR review:
Fixed Issues
1. Headers not passed to GenericGuardrailAPI (High Severity)
- File:
litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/__init__.py - Fix: Added
headers=getattr(litellm_params, "headers", None)to pass authentication headers from the config to theGenericGuardrailAPIinstance - Removed: The unused
api_keyparameter that wasn't accepted byGenericGuardrailAPI
2. Request body always empty (Medium Severity)
- File:
litellm/proxy/guardrails/guardrail_hooks/generic_guardrail_api/generic_guardrail_api.py - Fix: Changed
request_body={}torequest_body=request_datato pass the full original request for context to the external guardrail API
3. Regular expression injection vulnerability (High Severity - Security)
- File:
cookbook/mock_guardrail_server/mock_bedrock_guardrail_server.py - Fix: Added try-except block to handle invalid regex patterns safely by:
- Compiling the pattern first (which validates it)
- Catching
re.errorexceptions for invalid patterns - Logging warnings and continuing instead of crashing
- This prevents malicious regex patterns from being executed
All changes passed linter checks with no errors.
Learn more about Cursor Agents
Co-authored-by: krrishdholakia <krrishdholakia@gmail.com>
|
Krrish Dholakia seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
|
Cursor Agent can help with this pull request. Just |
| for pii_type, pattern in GUARDRAIL_CONFIG.pii_patterns.items(): | ||
| try: | ||
| # Compile the regex pattern with a timeout to prevent ReDoS attacks | ||
| compiled_pattern = re.compile(pattern) |
Check failure
Code scanning / CodeQL
Regular expression injection High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 11 months ago
To resolve the issue, you should sanitize any user-supplied regex patterns before compiling them. In Python, this can be accomplished using re.escape, which escapes all regex metacharacters, ensuring the input can only be interpreted literally and not as a regular expression. The best approach here is to escape all patterns supplied via GUARDRAIL_CONFIG.pii_patterns before compiling them in the Content Safety/PII detection workflow.
Edit the block where pattern is used (in line 254) to apply re.escape. If you need to support advanced configuration where some patterns are meant to be real regexes (not only literal matches), you should validate or add an explicit configuration and only escape untrusted sources, but in this context, full escaping is safest.
The only change required is to edit the region (lines 251-254) inside the anonymization logic, replacing the compile step to use an escaped version of the pattern.
No new imports are required.
| @@ -250,8 +250,9 @@ | ||
|
|
||
| for pii_type, pattern in GUARDRAIL_CONFIG.pii_patterns.items(): | ||
| try: | ||
| # Compile the regex pattern with a timeout to prevent ReDoS attacks | ||
| compiled_pattern = re.compile(pattern) | ||
| # Sanitize the regex pattern to prevent injection attacks and ReDoS | ||
| safe_pattern = re.escape(pattern) | ||
| compiled_pattern = re.compile(safe_pattern) | ||
| matches = compiled_pattern.finditer(text) | ||
| for match in matches: | ||
| matched_text = match.group() |
Update Pillar Security documentation to use the generic_guardrail_api instead of the dedicated pillar guardrail type. This aligns with the new Generic Guardrail API pattern introduced in BerriAI#17175. Changes: - Rewrite pillar_security.md to use generic_guardrail_api configuration - Add Pillar as an example in generic_guardrail_api.md - Add Pillar example to quick_start.md guardrails list This migration leverages improvements made to the Generic Guardrail API: - BerriAI#17175: Generic Guardrail API introduction - BerriAI#18647: Generic guardrail API updates - BerriAI#18932: SerializationIterator fix and tools support - BerriAI#19023: Clean error messages for blocked requests
Update Pillar Security integration to use the generic_guardrail_api instead of the dedicated pillar guardrail type. This aligns with the Generic Guardrail API specification introduced in previous PRs. Changes: - Rewrite pillar_security.md with new generic_guardrail_api config - Add Pillar Security example to generic_guardrail_api.md - Add Pillar Security to quick_start.md guardrails examples Related PRs: BerriAI#17175, BerriAI#18647, BerriAI#18932, BerriAI#19023
Update Pillar Security integration to use the generic_guardrail_api instead of the dedicated pillar guardrail type. This aligns with the Generic Guardrail API specification introduced in previous PRs. Changes: - Rewrite pillar_security.md with new generic_guardrail_api config - Add Pillar Security example to generic_guardrail_api.md - Add Pillar Security to quick_start.md guardrails examples Related PRs: #17175, #18647, #18932, #19023
…ANT support for LiteLLM w/out PR to repo (BerriAI#17175) * feat(generic_guardrail_api.py): new generic api for guardrails Allows guardrail providers to work with litellm for guardrails without needing to make a PR to LiteLLM * docs(generic_guardrail_api.md): document new generic guardrail api * Fix: Improve PII detection and guardrail API integration Co-authored-by: krrishdholakia <krrishdholakia@gmail.com> * feat: correctly extract raw request from guardrail api * docs(generic_guardrail_api.md): document this is a beta feature --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Update Pillar Security integration to use the generic_guardrail_api instead of the dedicated pillar guardrail type. This aligns with the Generic Guardrail API specification introduced in previous PRs. Changes: - Rewrite pillar_security.md with new generic_guardrail_api config - Add Pillar Security example to generic_guardrail_api.md - Add Pillar Security to quick_start.md guardrails examples Related PRs: BerriAI#17175, BerriAI#18647, BerriAI#18932, BerriAI#19023
Title
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
🐛 Bug Fix
🧹 Refactoring
📖 Documentation
🚄 Infrastructure
✅ Test
Changes
Note
Introduces a Generic Guardrail API integration (configurable, pre/during/post hooks) plus a mock Bedrock/Generic server and documentation, enabling instant third‑party guardrail support without PRs.
generic_guardrail_apiintegration withGenericGuardrailAPIhook to call externalPOST /beta/litellm_basic_guardrail_apiendpoints.guardrail_initializer_registryandguardrail_class_registry; support modes:pre_call,post_call,during_call.SupportedGuardrailIntegrations.GENERIC_GUARDRAIL_API, optionaladditional_provider_specific_params, and config models for UI.example_config.yamlwith dynamic params support.mock_bedrock_guardrail_server.pyFastAPI server mimicking Bedrock ApplyGuardrail and the Generic Guardrail API endpoint.[BETA] Generic Guardrail APIguide describing contract, config, and usage.Written by Cursor Bugbot for commit 27b0468. This will update automatically on new commits. Configure here.