Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(ssrf): clarify proxy destination policy without blocking local proxies (#9554)

Nianze Wuf19bb9d7d892
diff --git a/invokeai/app/util/ssrf.py b/invokeai/app/util/ssrf.py
index 4825c9e1cc..3b12acd5b0 100644
--- a/invokeai/app/util/ssrf.py
+++ b/invokeai/app/util/ssrf.py
@@ -5,7 +5,7 @@ point the server at addresses only the server can reach — loopback services, o
hosts on the private network, or the cloud metadata endpoint at 169.254.169.254 — and
use the download job as a proxy for them.
-There are two layers here, and the second is the one that actually holds:
+There are two layers for direct downloads, and the second is the one that actually holds:
`build_guarded_session()` returns a `requests.Session` that validates the peer address
of every socket it opens, before a single byte of the request is written. Checking the
@@ -191,8 +191,10 @@ def validate_download_url(url: str, allow_private_urls: bool = False) -> None:
loopback record is rejected, because we cannot control which one the HTTP client picks.
An unresolvable host is allowed through to the HTTP client, so that offline test
- environments and mocked sessions keep working. That is only safe because the session
- from `build_guarded_session()` re-checks the address it actually connects to.
+ environments and mocked sessions keep working. For direct downloads this is safe
+ because the session from `build_guarded_session()` re-checks the address it actually
+ connects to. With an explicit proxy, the operator must enforce the destination policy
+ at the proxy; this process cannot inspect the proxy's DNS resolution or final peer.
"""
parts = urlsplit(str(url))
@@ -326,7 +328,7 @@ class _SsrfGuardedSession(requests.Session):
def build_guarded_session(proxy: str | None = None) -> requests.Session:
- """A `requests.Session` that will not open a connection to a non-public address."""
+ """Guard direct connections; an explicit proxy owns destination-address policy."""
session = _SsrfGuardedSession()
if proxy:
session.proxies.update({"http": proxy, "https": proxy})
@@ -353,8 +355,9 @@ def warn_if_proxied(session: requests.Session, logger: logging.Logger) -> None:
proxies = proxies_in_effect(session)
if proxies:
logger.warning(
- "An explicit HTTP proxy is configured (%s). Downloads through it cannot be checked "
- "against loopback/private addresses at the socket; restrict outbound access at the proxy.",
+ "An explicit HTTP proxy is configured (%s). InvokeAI cannot verify the download "
+ "destination address through it. The proxy must enforce the destination address policy "
+ "and block loopback, private-network, and cloud-metadata targets, including after DNS resolution.",
", ".join(sorted(proxies)),
)
elif getattr(session, "_ignore_environment_proxies", False) and getproxies():
diff --git a/tests/app/util/test_ssrf.py b/tests/app/util/test_ssrf.py
index 4cd5f82df7..95ac6c9310 100644
--- a/tests/app/util/test_ssrf.py
+++ b/tests/app/util/test_ssrf.py
@@ -228,6 +228,17 @@ def test_guarded_session_is_installed_for_both_schemes():
assert adapter.poolmanager.pool_classes_by_scheme["https"] is ssrf._GuardedHTTPSConnectionPool
+def test_guarded_session_allows_private_proxy_for_public_url(loopback_server: int):
+ """An operator's local proxy must not be mistaken for the download destination."""
+ session = build_guarded_session(proxy=f"http://127.0.0.1:{loopback_server}")
+ try:
+ response = session.get("http://example.com/public-model", timeout=5)
+ assert response.status_code == 200
+ assert response.content == b"internal-only"
+ finally:
+ session.close()
+
+
@pytest.mark.parametrize(
"url",
[
@@ -300,9 +311,20 @@ def test_guarded_session_supports_explicit_proxy(monkeypatch: Any):
assert captured["proxies"] == session.proxies
+def test_explicit_proxy_warning_assigns_destination_policy_to_proxy(caplog: Any):
+ session = build_guarded_session(proxy="http://proxy.internal:3128")
+ logger = logging.getLogger("test-ssrf-explicit-proxy")
+ with caplog.at_level(logging.WARNING, logger="test-ssrf-explicit-proxy"):
+ ssrf.warn_if_proxied(session, logger)
+ assert "destination address policy" in caplog.text
+ assert "proxy" in caplog.text
+
+
def test_no_warning_without_a_proxy(monkeypatch: Any, caplog: Any):
for var in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"):
monkeypatch.delenv(var, raising=False)
+ # Windows may report a system proxy from the registry even with no proxy env vars.
+ monkeypatch.setattr(ssrf, "getproxies", lambda: {})
session = build_guarded_session()
assert ssrf.proxies_in_effect(session) == {}