Let a test give the upstream fetcher its own dial function

httpfetcher.Config gets an optional DialContext. When it is set, New
connects with it in place of the dialer that refuses internal addresses;
the URL check and the redirect check still run. Nothing in the config
file or the environment sets it, and pixa builds its fetcher without it,
so production connects exactly as before. It lets a test outside this
package send a public-looking address to a local test server. New tests
check that a fetcher built without it refuses to connect to a local
server, and that one built with it connects through it while a loopback
URL and a redirect to a link-local address are still refused.

Model: opus-5-5
This commit is contained in:
2026-10-04 19:27:06 +00:00
parent 8568c17d1b
commit c0f2783990
2 changed files with 83 additions and 6 deletions
+17 -6
View File
@@ -137,6 +137,11 @@ type Config struct {
// BlockedNetworks are operator-supplied CIDR ranges refused by the
// dialer, in addition to the always-enforced built-in ranges.
BlockedNetworks []netip.Prefix
// DialContext, when set, makes the fetcher's connections in place of
// the dialer that refuses internal addresses; the URL and redirect
// checks still run. Only tests set it, to reach a local server; the
// config file and the environment cannot.
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
}
// DefaultConfig returns a Config with sensible defaults.
@@ -190,13 +195,19 @@ func New(config *Config) *HTTPFetcher {
config = DefaultConfig()
}
// Create transport with SSRF-safe dialer. The dialer re-resolves and
// re-checks at connect time (closing the DNS-rebinding window) against
// both the built-in ranges and the operator-supplied blocklist.
transport := &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
// Unless config.DialContext replaces it, the transport connects with
// the SSRF-safe dialer, which re-resolves and re-checks at connect time
// (closing the DNS-rebinding window) against both the built-in ranges
// and the operator-supplied blocklist.
dialContext := config.DialContext
if dialContext == nil {
dialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialSSRFSafe(ctx, network, addr, config.BlockedNetworks)
},
}
}
transport := &http.Transport{
DialContext: dialContext,
TLSHandshakeTimeout: DefaultTLSTimeout,
MaxIdleConns: DefaultMaxIdleConns,
IdleConnTimeout: DefaultIdleConnTimeout,