mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 12:05:34 +08:00
fix(proxy): refuse a private x-headroom-base-url on the async chat and responses path (#4879)
## What changes Main currently fails four tests in `test_grok_shared_proxy_call_sites.py` and `test_upstream_guard.py`. Two changes to `handlers/openai.py` merged independently: - One made `_resolve_openai_upstream_base` reject a non-public `x-headroom-base-url` with 400. - The other moved the chat and responses handlers onto a new `_resolve_openai_upstream_base_async`. It was written before the rejection existed, so it still logged "ignoring" and fell back to the configured upstream. Since the handlers call the async resolver, a private override on `/v1/chat/completions` and `/v1/responses` was served from the default upstream instead of being refused. Both resolvers now refuse through one shared `_reject_unsafe_openai_override`, so they can't diverge again. The guard check still runs off the event loop. The change is source-only in one file, with no test changes: the existing tests on main already cover it. ## Verification - `tests/test_grok_shared_proxy_call_sites.py`, `tests/test_upstream_guard.py`, `tests/test_proxy/test_openai_upstream_guard_off_loop.py`, and the two OpenAI cache-key/stability suites: 109 passed. The four that fail on main now pass. - `tests/test_proxy -k "openai or upstream or base_url"`: 90 passed. - `ruff check` and `ruff format --check` are clean. mypy reports no errors in `openai.py`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4879" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d6df127d57
commit
5023c84e52
@@ -23,7 +23,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from dataclasses import replace
|
||||
from datetime import datetime
|
||||
from types import MappingProxyType
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, NoReturn
|
||||
from urllib.parse import quote, unquote, urlparse
|
||||
|
||||
from headroom.log_safety import describe_exception, safe_id
|
||||
@@ -531,18 +531,25 @@ def _resolve_openai_upstream_base(request_headers: dict[str, str]) -> str | None
|
||||
return None
|
||||
raw_base_url, normalized = candidate
|
||||
if not is_safe_upstream_url(normalized):
|
||||
# Client-supplied upstream resolves to a private/loopback/link-local or
|
||||
# cloud-metadata address. Refuse, as the other routes do: falling back to
|
||||
# the configured upstream would send the prompt and the Authorization
|
||||
# meant for the named gateway to a host the client did not choose. Set
|
||||
# HEADROOM_ALLOWED_BASE_URLS to permit specific internal endpoints.
|
||||
from fastapi import HTTPException
|
||||
|
||||
logger.warning("rejecting unsafe x-headroom-base-url: %r", raw_base_url)
|
||||
raise HTTPException(status_code=400, detail="Rejected unsafe upstream base URL")
|
||||
_reject_unsafe_openai_override(raw_base_url)
|
||||
return normalized
|
||||
|
||||
|
||||
def _reject_unsafe_openai_override(raw_base_url: str) -> NoReturn:
|
||||
"""Refuse an ``x-headroom-base-url`` that resolves to a non-public address.
|
||||
|
||||
Client-supplied upstream resolves to a private/loopback/link-local or
|
||||
cloud-metadata address. Refuse, as the other routes do: falling back to
|
||||
the configured upstream would send the prompt and the Authorization
|
||||
meant for the named gateway to a host the client did not choose. Set
|
||||
HEADROOM_ALLOWED_BASE_URLS to permit specific internal endpoints.
|
||||
"""
|
||||
from fastapi import HTTPException
|
||||
|
||||
logger.warning("rejecting unsafe x-headroom-base-url: %r", raw_base_url)
|
||||
raise HTTPException(status_code=400, detail="Rejected unsafe upstream base URL")
|
||||
|
||||
|
||||
async def _resolve_openai_upstream_base_async(request_headers: dict[str, str]) -> str | None:
|
||||
""":func:`_resolve_openai_upstream_base` for the async request handlers.
|
||||
|
||||
@@ -555,8 +562,7 @@ async def _resolve_openai_upstream_base_async(request_headers: dict[str, str]) -
|
||||
return None
|
||||
raw_base_url, normalized = candidate
|
||||
if not await is_safe_upstream_url_async(normalized):
|
||||
logger.warning("ignoring unsafe x-headroom-base-url override: %r", raw_base_url)
|
||||
return None
|
||||
_reject_unsafe_openai_override(raw_base_url)
|
||||
return normalized
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user