Skip to content

cmd/imageproxy: add -denyPrivateNetworks flag - #507

Open
vfoucault wants to merge 1 commit into
willnorris:mainfrom
gorgias-oss:deny-private-networks-upstream
Open

vfoucault wants to merge 1 commit into
willnorris:mainfrom
gorgias-oss:deny-private-networks-upstream

Conversation

@vfoucault

Copy link
Copy Markdown

Adds a -denyPrivateNetworks flag (off by default; IMAGEPROXY_DENYPRIVATENETWORKS=true also works) so imageproxy only fetches remote images from publicly routable IP addresses.

Why

#72 asked for a way to stop imageproxy from requesting itself and other services on the same private network, and was resolved with denyHosts. CIDR entries in denyHosts only match IP addresses written in the URL, though, so these are still fetched:

  • an internal hostname, or a public name pointing to a private IP (including wildcard DNS services like nip.io and sslip.io)
  • DNS rebinding, where a name resolves to a public IP when checked and a private one when fetched
  • a redirect from an allowed host to a hostname that resolves to a private IP

With the image proxy running in a cloud environment, that includes the instance metadata endpoint (169.254.169.254). Even when contentTypes keeps the response from being returned, the request is still made.

How it works

The check runs in the dialer's Control function, on the address actually connected to after DNS resolution, for every connection including each redirect hop.

It fails closed: only addresses known to be public are allowed.

  • IPv4: everything except the IANA special-purpose ranges that are not globally reachable (private, loopback, link-local, carrier-grade NAT, documentation, benchmarking), multicast and reserved space.
  • IPv6: only global unicast (2000::/3), minus its non-global blocks (Teredo, documentation). NAT64 (64:ff9b::/96) and 6to4 addresses are checked against their embedded IPv4 address. IPv4-mapped addresses are checked as IPv4. Scoped addresses (fe80::1%eth0) are refused.

Side effects when enabled

These are documented in the README and the flag's help text:

  • HTTP_PROXY / HTTPS_PROXY are ignored, since the proxy would be the address checked instead of the origin.
  • Missing intermediate certificates are no longer fetched. The AIA transport dials TLS itself and fetches issuer certificates with http.Get, both of which would bypass the check, so origins serving an incomplete certificate chain will fail.
  • Cached responses are served without a connection, so a cache populated before enabling the flag can still serve images from internal addresses.

Also changed

When fetching the remote image fails, clients now get a generic error fetching remote image instead of the full error, which can name internal addresses and ports (and whether a port is open). The details are still logged. This applies with or without the flag. I'm happy to limit it to the flag, or split it into a separate PR, if you prefer.

Scope

The flag is wired into cmd/imageproxy only. The Caddy module doesn't get it. Moving the transport into the library package so the Caddy module can use it would be a small follow-up if you'd like that.

Testing

  • go test -race ./..., staticcheck and golangci-lint pass, and GOOS=windows go vet is clean.
  • cmd/imageproxy/egress_test.go: a table of allowed and blocked IPv4 and IPv6 forms asserting on the specific error, and a transport test that reaches the same loopback origin with and without the check.
  • Manually with the built binary: loopback, localhost, *.nip.io / *.sslip.io names resolving to private addresses, zone-scoped IPv6 and 169.254.169.254 are refused. Clients get the generic error and the log names the blocked address.

🤖 Generated with Claude Code

Add a flag to only fetch remote images from publicly routable IP
addresses.

DenyHosts CIDR entries only match IPs written in the URL, so a hostname
resolving to a private address (an internal name, a wildcard DNS
service like nip.io, DNS rebinding) or a redirect to one is still
fetched. Instead, check the address in the dialer's Control function,
after DNS resolution, on every connection including redirects.

The check fails closed: IPv4 addresses in IANA special-purpose ranges
that are not globally reachable, multicast and reserved space are
refused; IPv6 must be global unicast (2000::/3) outside its non-global
blocks. NAT64 and 6to4 addresses are checked against their embedded
IPv4 address, and scoped IPv6 addresses are refused.

When enabled, HTTP_PROXY is ignored (the proxy would be the address
checked) and the AIA transport is replaced, since its TLS dialer and
issuer certificate fetches would bypass the check.

Also stop including the fetch error in the response to clients: it can
name internal addresses and ports. It is still logged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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