fix: pin validated DNS for SSRF-safe fetches

maintainer edit: keep MCP HTTP SSRF checks strict, pin validated DNS for direct web_fetch and HTTP/SSE MCP requests, preserve explicit and environment proxy compatibility, and cover the proxy/redirect/rebinding cases with tests.
This commit is contained in:
chengyongru
2026-07-07 15:40:53 +08:00
committed by Xubin Ren
parent b68ae4f9bc
commit c5e053f83b
8 changed files with 436 additions and 34 deletions
+22
View File
@@ -11,11 +11,21 @@ import pytest
from nanobot.security.network import (
configure_ssrf_whitelist,
contains_internal_url,
env_proxy_applies_to_url,
httpx_env_proxy_mounts,
pin_resolved_url_dns,
resolve_url_target,
validate_url_target,
)
_PROXY_ENV_VARS = ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy")
@pytest.fixture(autouse=True)
def _clear_proxy_env(monkeypatch: pytest.MonkeyPatch) -> None:
for name in (*_PROXY_ENV_VARS, "NO_PROXY", "no_proxy"):
monkeypatch.delenv(name, raising=False)
def _fake_resolve(host: str, results: list[str]):
"""Return a getaddrinfo mock that maps the given host to fake IP results."""
@@ -184,6 +194,18 @@ def test_allows_normal_https():
assert ok
def test_env_proxy_helpers_respect_no_proxy(monkeypatch):
monkeypatch.setenv("HTTPS_PROXY", "http://proxy.example:8080")
monkeypatch.setenv("NO_PROXY", "localhost,127.0.0.1,::1")
assert env_proxy_applies_to_url("https://example.com/page")
assert not env_proxy_applies_to_url("http://localhost:8765/mcp")
mounts = httpx_env_proxy_mounts()
assert any(transport is None for transport in mounts.values())
assert any(transport is not None for transport in mounts.values())
# ---------------------------------------------------------------------------
# contains_internal_url — shell command scanning
# ---------------------------------------------------------------------------