Skip to content

Commit 7138a29

Browse files
committed
fix(serverless): refuse proxied fetches that defeat IP pinning
Pinning only holds when this process opens the socket. Through a proxy the request carries the hostname and the proxy resolves it, so the validated IP is not what gets dialed and the DNS-rebind protection silently stops applying while still appearing to be in force. Refuse such a URL with SSRFError instead. NO_PROXY exclusions are honored, since those hosts are fetched directly, and RUNPOD_ALLOW_PRIVATE_DOWNLOAD_URLS allows the proxied fetch for deployments that accept the trade-off. Chosen over trust_env=False, which would silently ignore a needed proxy and also drop REQUESTS_CA_BUNDLE.
1 parent 6286a28 commit 7138a29

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎runpod/serverless/utils/rp_ssrf.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
non-public addresses (CWE-918). This module blocks any destination that is not a
99
globally-routable IP, pins connections to a pre-validated address to defeat DNS
1010
rebinding, re-validates redirect hops, and caps download size.
11+
12+
Pinning requires this process to open the socket, so a URL that the environment
13+
routes through a proxy is refused rather than fetched unprotected; see
14+
_refuse_if_proxied.
1115
"""
1216

1317
import ipaddress
@@ -19,6 +23,7 @@
1923

2024
from requests import ConnectionError as RequestsConnectionError
2125
from requests.adapters import HTTPAdapter
26+
from requests.utils import get_environ_proxies, select_proxy
2227

2328
from runpod.http_client import SyncClientSession
2429

@@ -139,6 +144,31 @@ def max_download_bytes() -> int:
139144
return value if value > 0 else _DEFAULT_MAX_BYTES
140145

141146

147+
def _refuse_if_proxied(url: str) -> None:
148+
"""
149+
Reject `url` when the environment routes it through a proxy.
150+
151+
Pinning only holds if this process opens the socket: through a proxy the
152+
request carries the hostname and the proxy resolves it, so the validated IP
153+
is not what gets dialed and the rebind protection silently stops applying.
154+
Failing here turns that into a loud, actionable error instead of a false
155+
sense of protection.
156+
157+
NO_PROXY exclusions are honored (such a host is fetched directly, so pinning
158+
still holds), and the escape hatch env var allows the proxied fetch for
159+
deployments that accept the trade-off.
160+
"""
161+
if not _ssrf_protection_enabled():
162+
return
163+
164+
proxy = select_proxy(url, get_environ_proxies(url))
165+
if proxy:
166+
raise SSRFError(
167+
f"refusing to fetch {url} through proxy {proxy}: IP pinning cannot be "
168+
f"enforced via a proxy; set {_ALLOW_PRIVATE_ENV}=true to allow it"
169+
)
170+
171+
142172
def _host_header_for(url: str) -> str:
143173
"""
144174
Build the Host header authority for `url`.
@@ -342,6 +372,7 @@ def safe_get(
342372
raise SSRFError(f"blocked URL scheme {parsed.scheme!r}: {current}")
343373
if not parsed.hostname:
344374
raise SSRFError(f"URL has no host: {current}")
375+
_refuse_if_proxied(current)
345376

346377
port = parsed.port or (443 if parsed.scheme == "https" else 80)
347378
ips = resolve_and_validate(parsed.hostname, port)

‎tests/test_serverless/test_utils/test_ssrf.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,56 @@ def log_message(self, *args): # silence test server logging
325325
pass
326326

327327

328+
class TestProxyIsRefused(unittest.TestCase):
329+
"""
330+
A proxy re-resolves the hostname itself, so pinning (and with it the
331+
rebind protection) silently stops applying. Fail loudly instead.
332+
"""
333+
334+
@patch("runpod.serverless.utils.rp_ssrf._build_pinned_session")
335+
@patch("runpod.serverless.utils.rp_ssrf.resolve_and_validate", return_value=["93.184.216.34"])
336+
@patch.dict(os.environ, {"HTTPS_PROXY": "http://proxy.internal:3128"}, clear=True)
337+
def test_raises_when_a_proxy_is_configured(self, _mock_resolve, mock_session):
338+
with self.assertRaises(SSRFError) as ctx:
339+
safe_get("https://example.com/image.jpg")
340+
341+
self.assertIn("proxy", str(ctx.exception).lower())
342+
mock_session.assert_not_called() # refused before any connection
343+
344+
@patch("runpod.serverless.utils.rp_ssrf._build_pinned_session")
345+
@patch("runpod.serverless.utils.rp_ssrf.resolve_and_validate", return_value=["93.184.216.34"])
346+
@patch.dict(
347+
os.environ,
348+
{"HTTPS_PROXY": "http://proxy.internal:3128", "NO_PROXY": "example.com"},
349+
clear=True,
350+
)
351+
def test_allows_host_excluded_by_no_proxy(self, _mock_resolve, mock_session):
352+
response = _FakeResponse(200, {"Content-Length": "5"})
353+
session = MagicMock()
354+
session.get.return_value = response
355+
mock_session.return_value = session
356+
357+
self.assertIs(safe_get("https://example.com/image.jpg"), response)
358+
359+
@patch("runpod.serverless.utils.rp_ssrf._build_pinned_session")
360+
@patch("runpod.serverless.utils.rp_ssrf.resolve_and_validate", return_value=["93.184.216.34"])
361+
@patch.dict(
362+
os.environ,
363+
{
364+
"HTTPS_PROXY": "http://proxy.internal:3128",
365+
"RUNPOD_ALLOW_PRIVATE_DOWNLOAD_URLS": "true",
366+
},
367+
clear=True,
368+
)
369+
def test_escape_hatch_permits_proxied_fetch(self, _mock_resolve, mock_session):
370+
response = _FakeResponse(200, {"Content-Length": "5"})
371+
session = MagicMock()
372+
session.get.return_value = response
373+
mock_session.return_value = session
374+
375+
self.assertIs(safe_get("https://example.com/image.jpg"), response)
376+
377+
328378
class TestHostHeaderFor(unittest.TestCase):
329379
"""The Host header must reproduce the URL authority, brackets included."""
330380

0 commit comments

Comments
 (0)