Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix: enforce a definitive no-proxy policy for the pinned SSRF fetcher

KaifAhmad1ea3416ed32d8
diff --git a/semantica/explorer/routes/ontology.py b/semantica/explorer/routes/ontology.py
index 005d8a0f..b50eb206 100644
--- a/semantica/explorer/routes/ontology.py
+++ b/semantica/explorer/routes/ontology.py
@@ -1046,7 +1046,6 @@ def _make_pinned_session(pinned_ips: List[str], url: str):
`host` directly and restoring the real hostname via an explicit Host
header (+ SNI params for HTTPS) is the correct mechanism here.
"""
- import logging as _pin_logging
import requests as _req
import urllib3.util.connection as _u3_connection
from urllib3.exceptions import NewConnectionError
@@ -1080,26 +1079,21 @@ def _make_pinned_session(pinned_ips: List[str], url: str):
class _PinnedIPHTTPAdapter(_req.adapters.HTTPAdapter):
def get_connection_with_tls_context(self, request, verify, proxies=None, cert=None):
- # If an HTTP(S) proxy applies (env-configured or per-request),
- # pinning can't meaningfully apply: the actual TCP connection
- # target is the proxy, and for a forward proxy the *proxy*
- # performs its own DNS resolution of the target host on our
- # behalf — a resolution this process has no visibility into or
- # control over, so there is no client-side fix for that
- # specific race. Fall back to the normal (unpinned) path rather
- # than silently bypassing the configured proxy.
- # _validate_fetch_url's destination classification still fully
- # applies either way; only this secondary DNS-pinning hardening
- # is inherently out of scope when a proxy is in the path.
+ # A proxy would perform its own DNS resolution of the target
+ # host on this process's behalf — a resolution outside this
+ # process's visibility or control, so there is no client-side
+ # pin that closes that race. Proxies are disabled outright for
+ # this SSRF-sensitive fetcher (session.trust_env=False below),
+ # so this should be unreachable via environment proxies; fail
+ # closed rather than silently skip pinning if a proxy is
+ # somehow still configured (e.g. passed explicitly in the
+ # future). _validate_fetch_url's destination classification is
+ # a separate, always-enforced check — this only guards the
+ # secondary DNS-pinning hardening.
if _req.utils.select_proxy(request.url, proxies):
- _pin_logging.getLogger(__name__).info(
- "DNS pinning skipped for %s: a proxy is configured for this "
- "request, and proxy-side DNS resolution is outside this "
- "process's control.",
- request.url,
- )
- return super().get_connection_with_tls_context(
- request, verify, proxies=proxies, cert=cert
+ raise HTTPException(
+ status_code=502,
+ detail="Proxied requests are not supported for ontology URL fetching.",
)
host_params, pool_kwargs = self.build_connection_pool_key_attributes(request, verify, cert)
if host_params.get("scheme") == "https":
@@ -1115,6 +1109,13 @@ def _make_pinned_session(pinned_ips: List[str], url: str):
return pool
session = _req.Session()
+ # Never honor HTTP_PROXY/HTTPS_PROXY/NO_PROXY env vars for this
+ # SSRF-sensitive fetcher: a configured proxy would perform its own DNS
+ # resolution of the target host outside this process's control,
+ # silently reopening the DNS check-then-use race pinning exists to
+ # close. See _PinnedIPHTTPAdapter.get_connection_with_tls_context for
+ # the fail-closed backstop if a proxy is somehow still configured.
+ session.trust_env = False
session.headers["Host"] = host_header
adapter = _PinnedIPHTTPAdapter()
session.mount("http://", adapter)
diff --git a/tests/explorer/test_ontology_dns_pinning.py b/tests/explorer/test_ontology_dns_pinning.py
index 90780ba4..a68ff192 100644
--- a/tests/explorer/test_ontology_dns_pinning.py
+++ b/tests/explorer/test_ontology_dns_pinning.py
@@ -171,6 +171,57 @@ def test_pinned_session_raises_when_every_pinned_ip_is_unreachable():
session.close()
+def test_pinned_session_disables_environment_proxy_trust():
+ """A pinned session must never honor HTTP_PROXY/HTTPS_PROXY env vars —
+ a proxy would perform its own DNS resolution of the target host outside
+ this process's control, reopening the exact TOCTOU window pinning
+ exists to close."""
+ session = ontology_mod._make_pinned_session(["127.0.0.1"], "http://example.org/")
+ try:
+ assert session.trust_env is False
+ finally:
+ session.close()
+
+
+def test_pinned_session_ignores_env_proxy_and_connects_directly(monkeypatch):
+ """End-to-end: even with HTTP_PROXY pointed at an address that would
+ fail if contacted, a pinned session must reach the real local server
+ directly — proving the env var is genuinely not consulted, not just
+ that the trust_env flag is set."""
+ monkeypatch.setenv("HTTP_PROXY", "http://127.0.0.5:1/") # would fail if ever used
+ server, thread, _captured = _start_local_server()
+ port = server.server_address[1]
+ url = f"http://pinned-test.invalid:{port}/resource"
+ try:
+ session = ontology_mod._make_pinned_session(["127.0.0.1"], url)
+ try:
+ resp = session.get(url, timeout=5)
+ assert resp.status_code == 200
+ assert resp.content == b"pinned response"
+ finally:
+ session.close()
+ finally:
+ server.shutdown()
+ thread.join(timeout=2)
+
+
+def test_pinned_session_fails_closed_if_a_proxy_is_explicitly_forced():
+ """Backstop: if a proxy is somehow still configured on the session
+ despite trust_env=False (e.g. set explicitly, as a future code path
+ might), the adapter must fail closed with a clear error rather than
+ silently connecting through the proxy unpinned."""
+ from fastapi import HTTPException
+
+ session = ontology_mod._make_pinned_session(["127.0.0.1"], "http://example.org/")
+ session.proxies = {"http": "http://127.0.0.5:1"}
+ try:
+ with pytest.raises(HTTPException) as exc_info:
+ session.get("http://example.org/", timeout=5)
+ assert exc_info.value.status_code == 502
+ finally:
+ session.close()
+
+
def test_validate_fetch_url_returns_the_resolved_ip():
"""_validate_fetch_url must return every IP it validated, so callers can
pin the connection to them (with fallback across all of them)."""