Skip to content

OpenHands Fork Audit - HIGH: get_litellm_model_info makes unvalidated httpx.get call at LLM init #4263

Closed

Description

@zentradev-rabih

FLEET AUDIT FINDING: OpenHands Zentra fork — egress completeness audit

Location: vendor/openhands-sdk/openhands/sdk/llm/utils/model_info.py:31, called from llm.py:2229

Severity: HIGH (P2) — Potential egress hole if policy not enforced before LLM init

The Issue:
model_info.py:31 makes an httpx.get call:

response = httpx.get(f"{base_url}/v1/model/info", headers=headers)

This is called from LLM._init_model_info_and_caps() (llm.py:619), which runs in post_init (during LLM object construction).

The call is gated on:

if model.startswith("litellm_proxy/") and base_url:

But the execution sequence matters:

  1. In the agent-server, enforce_agent_llm_policy() is called at conversation start
  2. This clamps the LLM model/base_url BEFORE an LLM object is constructed
  3. So by the time _init_model_info_and_caps runs, the policy should have already filtered off-box models

However:

  • The SDK guard is the ONLY chokepoint if an LLM is constructed outside the agent-server (e.g., in unit tests, local SDK usage)
  • If a model starts with "litellm_proxy/" but base_url points off-box, the call proceeds
  • The _zentra_airgap.py guard in _prepare_transport_kwargs (line 2051) only fires on COMPLETION calls, not on post_init

Risk:
If the policy enforcement order changes or an LLM is constructed outside the agent-server path, an off-box base_url could trigger the httpx.get before the SDK call-boundary guard fires.

Code evidence:

# llm.py:619 (__post_init__)
self._init_model_info_and_caps()

# model_info.py:78-82
if model.startswith("litellm_proxy/") and base_url:
    response = httpx.get(f"{base_url}/v1/model/info", headers=headers)  # Unvalidated!

Fix guidance:
Add a call to assert_onbox_target() in _init_model_info_and_caps() BEFORE the httpx.get, OR gate the call on airgap_enforced(). Ensures the base_url is validated to the appliance endpoint before ANY HTTP call.

Activity

  1. lxcxjxhx commented on Jul 23, 2026

    @lxcxjxhx

    问题分析

    这是一个典型的安全审计发现:在 LLM 初始化时,get_litellm_model_info() 函数会发起一个未经充分验证的 HTTP GET 请求到 {base_url}/v1/model/info。虽然当前有 litellm_proxy/ 前缀检查,但缺少对 base_url 本身的验证,可能导致在 airgap 环境中泄露请求到外部服务器。

    根因定位

    问题出在 vendor/openhands-sdk/openhands/sdk/llm/utils/model_info.py:31:

    response = httpx.get(f"{base_url}/v1/model/info", headers=headers)

    调用链:

    1. LLM.__post_init__() → _init_model_info_and_caps()
    2. 如果 model.startswith("litellm_proxy/") 且 base_url 存在,直接发起 HTTP 请求
    3. 此时还没有经过 agent-server 的策略验证(enforce_agent_llm_policy())

    虽然 agent-server 会在对话启动时验证 LLM 配置,但 SDK 本身缺少防护,在以下场景存在风险:

    • 单元测试中直接构造 LLM 对象
    • 本地使用 SDK(不经过 agent-server)
    • 策略验证顺序变更

    修复方案

    方案 1:在 HTTP 请求前验证 base_url(推荐)

    在 model_info.py 中添加 base_url 验证:

    # model_info.py
    def get_litellm_model_info(base_url: str, headers: dict, model: str) -> dict:
        """获取 LiteLLM 模型信息"""
        if not model.startswith("litellm_proxy/") or not base_url:
            return {}
        
        # 验证 base_url 是否符合安全策略
        from openhands.sdk.llm.utils.security import validate_base_url
        if not validate_base_url(base_url):
            logger.warning(f"Blocked unvalidated base_url: {base_url}")
            return {}
        
        try:
            response = httpx.get(
                f"{base_url}/v1/model/info",
                headers=headers,
                timeout=5.0  # 添加超时防止挂起
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            logger.error(f"Failed to get model info: {e}")
            return {}

    安全验证函数:

    # security.py
    from urllib.parse import urlparse
    import ipaddress
    
    def validate_base_url(base_url: str, allowed_hosts: list[str] = None) -> bool:
        """验证 base_url 是否安全"""
        try:
            parsed = urlparse(base_url)
            
            # 1. 只允许 http/https
            if parsed.scheme not in ['http', 'https']:
                return False
            
            # 2. 检查主机名
            hostname = parsed.hostname
            if not hostname:
                return False
            
            # 3. 如果提供了白名单,检查是否在白名单中
            if allowed_hosts:
                return hostname in allowed_hosts
            
            # 4. 阻止私有 IP 地址(防止 SSRF)
            try:
                ip = ipaddress.ip_address(hostname)
                if ip.is_private or ip.is_loopback or ip.is_link_local:
                    return False
            except ValueError:
                # 不是 IP 地址,是域名
                # 可以添加域名黑名单
                blocked_domains = ['localhost', '127.0.0.1', '0.0.0.0', 'metadata.google.internal']
                if hostname in blocked_domains:
                    return False
            
            return True
        except Exception:
            return False

    方案 2:集成 airgap 检查

    如果系统启用了 airgap 模式,在 HTTP 请求前检查:

    def _init_model_info_and_caps(self):
        """初始化模型信息和能力"""
        if self.model.startswith("litellm_proxy/") and self.base_url:
            # 检查 airgap 策略
            from openhands.sdk.llm.utils.airgap import is_airgap_enforced
            
            if is_airgap_enforced():
                # 在 airgap 模式下,只允许访问 appliance endpoint
                from openhands.sdk.llm.utils.airgap import get_appliance_endpoint
                appliance_url = get_appliance_endpoint()
                
                if not self.base_url.startswith(appliance_url):
                    logger.warning(
                        f"Blocked model info request to {self.base_url} "
                        f"(airgap requires {appliance_url})"
                    )
                    return
            
            # 继续原有的 HTTP 请求逻辑
            self._fetch_model_info()

    方案 3:延迟加载模型信息

    将模型信息获取延迟到首次使用时,确保所有策略验证已完成:

    class LLM:
        def __post_init__(self):
            # 不在构造时获取模型信息
            self._model_info = None
            self._model_info_fetched = False
        
        @property
        def model_info(self):
            """延迟加载模型信息"""
            if not self._model_info_fetched:
                self._fetch_model_info_safe()
                self._model_info_fetched = True
            return self._model_info
        
        def _fetch_model_info_safe(self):
            """安全地获取模型信息"""
            # 此时所有策略验证应该已完成
            if not self._validate_for_model_info():
                return
            
            self._fetch_model_info()

    测试用例

    def test_model_info_blocks_unvalidated_urls():
        """测试阻止未验证的 URL"""
        from openhands.sdk.llm import LLM
        
        # 1. 测试阻止私有 IP
        llm = LLM(
            model="litellm_proxy/gpt-4",
            base_url="http://192.168.1.100:8080"
        )
        assert llm._model_info == {}  # 应该被阻止
        
        # 2. 测试阻止 localhost
        llm = LLM(
            model="litellm_proxy/gpt-4",
            base_url="http://localhost:8080"
        )
        assert llm._model_info == {}
        
        # 3. 测试允许合法 URL
        llm = LLM(
            model="litellm_proxy/gpt-4",
            base_url="https://api.example.com"
        )
        # 应该正常获取(或至少尝试)
        assert llm._model_info is not None
    
    def test_model_info_with_airgap():
        """测试 airgap 模式下的行为"""
        from openhands.sdk.llm import LLM
        from openhands.sdk.llm.utils.airgap import enable_airgap, set_appliance_endpoint
        
        enable_airgap()
        set_appliance_endpoint("https://appliance.company.com")
        
        # 1. 测试阻止非 appliance URL
        llm = LLM(
            model="litellm_proxy/gpt-4",
            base_url="https://external.api.com"
        )
        assert llm._model_info == {}
        
        # 2. 测试允许 appliance URL
        llm = LLM(
            model="litellm_proxy/gpt-4",
            base_url="https://appliance.company.com"
        )
        # 应该正常获取
        assert llm._model_info is not None

    安全建议

    1. 添加超时:所有 HTTP 请求都应该有超时设置,防止挂起
    2. 日志记录:记录所有被阻止的请求,便于审计
    3. 白名单机制:在 airgap 环境中,使用白名单而不是黑名单
    4. 定期审计:定期检查所有外部 HTTP 调用点

    环境限制说明:由于本地 Windows 环境缺少完整的 OpenHands 运行环境和 airgap 配置,无法进行完整的动态测试。以上修复方案基于代码审查和安全最佳实践,建议维护者在完整环境中验证。

    需要我提交 PR 实现这个修复吗?

  2. transferred this issue fromOpenHands/OpenHandson Jul 27, 2026
  3. all-hands-bot commented on Jul 30, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you
    please take a look and add the appropriate priority label when you have a chance?

    This comment was created by an AI agent (OpenHands) on behalf of the user.

  4. all-hands-bot commented on Jul 31, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you please take a look and add the appropriate priority label when you have a chance?

    This comment was created by an AI agent (OpenHands) on behalf of the user.

  5. all-hands-bot commented on Aug 2, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you
    please take a look and add the appropriate priority label when you have a chance?

    This comment was posted by an AI agent (OpenHands) on behalf of the user.

  6. all-hands-bot commented on Aug 3, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you
    please take a look and add the appropriate priority label when you have a chance?

    This comment was created by an AI agent (OpenHands) on behalf of the user.

  7. all-hands-bot commented on Sep 4, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has had no activity for 32 days (labels: bug, needs-triage). @enyst @neubig @xingyaoww, could you take a look when you have a chance and help figure out next steps?

    Generated by OpenHands automation on behalf of the user.

  8. VascoSch92 commented on Oct 12, 2026

    @VascoSch92
    Member

    Closing as fork-specific: the air-gap guard it refers to (_zentra_airgap.py) only exists downstream. Upstream, querying the user-configured proxy is intended, and the timeout concern is tracked in #3912.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions