litellm - 💡(How to fix) Fix Security: SSRF via api_base parameter + RCE via guardrail missing sandbox [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
BerriAI/litellm#24952Fetched 2026-04-08 02:23:34
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Author
Participants
Timeline (top)
commented ×2subscribed ×2closed ×1mentioned ×1

Fix Action

Fix

  1. Add URL validation with private IP blocking for api_base
  2. Apply the same sandbox (__builtins__={} + FORBIDDEN_PATTERNS) to production guardrails as the test endpoint

Code Example

# route_llm_request.py:185-189
if 'api_base' in data:
    return getattr(litellm, f'{route_type}')(**data)  # api_base controls target URL

---

curl -X POST http://TARGET/chat/completions \
  -H 'Authorization: Bearer sk-xxx' \
  -H 'Content-Type: application/json' \
  -d '{"model":"gpt-4","api_base":"http://169.254.169.254/latest/meta-data/","messages":[{"role":"user","content":"hi"}]}'

---

# Production (custom_code_guardrail.py:130) - NO sandbox:
exec(compile(self.custom_code, '<guardrail>', 'exec'), exec_globals)

# Test endpoint (guardrail_endpoints.py:1409) - HAS sandbox:
exec_globals['__builtins__'] = {}  # <-- Missing in production!
RAW_BUFFERClick to expand / collapse

Finding 1: SSRF via api_base (CVSS 8.5)

Any authenticated user can set api_base in the request body to redirect LLM API calls to arbitrary internal URLs. No URL validation or private IP blocking exists anywhere in the codebase.

# route_llm_request.py:185-189
if 'api_base' in data:
    return getattr(litellm, f'{route_type}')(**data)  # api_base controls target URL

PoC:

curl -X POST http://TARGET/chat/completions \
  -H 'Authorization: Bearer sk-xxx' \
  -H 'Content-Type: application/json' \
  -d '{"model":"gpt-4","api_base":"http://169.254.169.254/latest/meta-data/","messages":[{"role":"user","content":"hi"}]}'

Finding 2: RCE via Custom Code Guardrail (CVSS 8.0)

The production CustomCodeGuardrail._compile_custom_code() does NOT clear __builtins__ or apply FORBIDDEN_PATTERNS, unlike the test endpoint which does both.

# Production (custom_code_guardrail.py:130) - NO sandbox:
exec(compile(self.custom_code, '<guardrail>', 'exec'), exec_globals)

# Test endpoint (guardrail_endpoints.py:1409) - HAS sandbox:
exec_globals['__builtins__'] = {}  # <-- Missing in production!

Any user who can create a guardrail can execute arbitrary Python.

Fix

  1. Add URL validation with private IP blocking for api_base
  2. Apply the same sandbox (__builtins__={} + FORBIDDEN_PATTERNS) to production guardrails as the test endpoint

extent analysis

TL;DR

To address the identified security vulnerabilities, implement URL validation and private IP blocking for the api_base parameter, and apply a sandbox to the production CustomCodeGuardrail by clearing __builtins__ and applying FORBIDDEN_PATTERNS.

Guidance

  • Implement URL validation to ensure api_base only accepts expected and safe URLs, blocking any private IP addresses to prevent SSRF attacks.
  • Apply the same sandboxing used in the test endpoint to the production CustomCodeGuardrail, specifically by setting __builtins__ to an empty dictionary and enforcing FORBIDDEN_PATTERNS to prevent RCE.
  • Review the codebase for any similar instances where user-inputted data is used to construct URLs or execute code without proper validation and sandboxing.
  • Consider implementing additional security measures such as input validation, rate limiting, and monitoring for suspicious activity to further mitigate potential attacks.

Example

# Example of setting __builtins__ to an empty dictionary for sandboxing
exec_globals = {}
exec_globals['__builtins__'] = {}
exec(compile(self.custom_code, '<guardrail>', 'exec'), exec_globals)

Notes

The provided fixes directly address the mentioned vulnerabilities but a comprehensive security audit is recommended to identify any other potential issues within the codebase.

Recommendation

Apply workaround by implementing the suggested fixes for URL validation and sandboxing to immediately mitigate the identified vulnerabilities.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

litellm - 💡(How to fix) Fix Security: SSRF via api_base parameter + RCE via guardrail missing sandbox [2 comments, 2 participants]