Skip to content

fix(security): refuse internal addresses when fetching file URLs - #43

Merged
duanbing merged 2 commits into
mainfrom
fix/file-url-ssrf-guard
Nov 19, 2026
Merged

duanbing merged 2 commits into
mainfrom
fix/file-url-ssrf-guard

Conversation

@duanbing

@duanbing duanbing commented Nov 19, 2026 •

Copy link
Copy Markdown

Summary

The gateway downloads caller-supplied file URLs (image_url, file blocks) itself. Before this change it ran no address check and copied the response body and socket error into the error it returned. So an API caller could make the gateway request loopback ports, the cloud metadata endpoint or cluster services, and read the result.

File URLs now go through a dedicated client in tensorzero-core/src/public_url.rs, which:

  • allows only http and https;
  • resolves hostnames itself and drops non-public addresses: loopback, RFC 1918, link-local, CGNAT (which includes 100.100.100.200), multicast and reserved ranges, IPv6 ULA and link-local, and the v4-mapped and NAT64 forms of all of these. A hostname that resolves only to internal addresses fails at connect time, which also covers DNS rebinding;
  • refuses literal internal IPs and localhost before sending anything, and re-checks every redirect hop (at most 5);
  • never uses a proxy.

Fetch errors now say only that the file could not be downloaded, or give the HTTP status. The details go to the gateway log at warn.

TENSORZERO_ALLOW_PRIVATE_FILE_URLS=1 turns the address checks off, for local development against private hosts.

Test plan

  • cargo check -p tensorzero-core
  • New unit tests in public_url:
    • address classification;
    • URL checks, including decimal and hex IPv4 forms and odd schemes;
    • the resolver refusing localhost;
    • a local server that 302s to 169.254.169.254 is not followed.
  • The existing file-URL tests pass when run on their own: providers::openai::tests::test_file_url_no_mime_type_fetch_and_encode, test_cannot_forward_file_url and the input_files tests.
  • Fetching a real public URL through the guarded client returns 200.

Caller-supplied file URLs (image_url and friends) were fetched by the gateway
with no address check, and fetch errors echoed the response body and socket
error back to the caller. That let any API caller probe loopback ports, the
cloud metadata endpoint and cluster services, and read the answers.

File URLs now go through a dedicated client (public_url.rs) that:
- allows only http/https;
- resolves hostnames itself and drops non-public addresses (loopback,
  RFC 1918, link-local, CGNAT incl. 100.100.100.200, multicast, reserved,
  IPv6 ULA/link-local, v4-mapped and NAT64 forms), so DNS rebinding and
  internal hostnames fail at connect time;
- re-checks every redirect hop (max 5) and never uses a proxy.

Fetch errors now say only that the file could not be downloaded (or the
HTTP status); details go to the gateway log.

TENSORZERO_ALLOW_PRIVATE_FILE_URLS=1 turns the address checks off for local
development.
URL files are downloaded with `public_url::client()`, so the shared
`TensorzeroHttpClient` argument was ignored. Remove it from the trait,
the impl and the one caller. Also mark the redirect test's local server
spawn as an expected `disallowed_methods` exception.
@duanbing
duanbing merged commit 38d8506 into main Nov 19, 2026
8 checks passed
@duanbing
duanbing deleted the fix/file-url-ssrf-guard branch November 19, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant