navidrome/utils/httpclient/httpclient.go
Deluan Quintão 237276efcd
fix(artwork): block private and loopback addresses in remote image fetches (#6181)
* fix(artwork): block private and loopback addresses in remote image fetches

fromURL fetched any URL with a plain HTTP client, and two untrusted inputs reach it. A playlist
can set #EXTALBUMARTURL to an http(s) URL, which the artwork worker later fetches when
EnableM3UExternalAlbumArt is on, so any user who can import a playlist controls the target.
Metadata agents, including WASM plugins without the http permission, return image URLs that the
core fetches too. Either path could make the server request loopback, LAN or link-local
addresses and store the response as artwork that is served back.

Add httpclient.NewExternal, which dials through a net.Dialer Control hook that rejects private,
loopback, link-local and unspecified addresses. The check runs at dial time on the resolved IP,
so DNS names, redirects and DNS rebinding are covered. fromURL now uses one shared client built
with it and treats a refused address as a definitive miss, so the item settles absent instead of
retrying and tripping the agent's circuit breaker. httpclient.New is unchanged for the other callers.

The IP classification moves from plugins to the new utils/netguard package, shared by the plugin
host client and the new constructor. The artwork test suite swaps in a client that allows
loopback so existing specs can keep using httptest servers; the fromURL specs use the production
client to assert the refusal.

* fix(httpclient): keep dialing a configured proxy in the guarded client

The guard runs on the resolved address, and with HTTP_PROXY set that address is the proxy, not
the image host. A proxy on a private address would have had every remote artwork fetch refused,
and a refusal settles the item as absent, so covers would silently disappear for those setups.

Dial the configured proxy endpoint directly and keep the guard for every other dial. A proxy
relays the request itself, so it is the operator's egress policy, the same one every other
httpclient.New caller already goes through.

* fix(httpclient): exempt only the hop that actually goes through the proxy

The exemption matched any dial to a configured proxy's address, but net/http never proxies
loopback targets, so a URL aimed at a loopback proxy was dialed directly and skipped the guard.
That let an image URL reach that one address.

Tag each request with the proxy it resolves to and exempt a dial only when it is that hop.
Redirects re-enter the RoundTripper, so every hop is tagged on its own.
2026-09-20 20:46:46 -04:00

84 lines
2.8 KiB
Go

// Package httpclient provides a shared http.Client factory that identifies
// Navidrome via the User-Agent header on all outgoing requests.
package httpclient
import (
"context"
"net"
"net/http"
"net/netip"
"net/url"
"time"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/utils/netguard"
)
type uaTransport struct {
base http.RoundTripper
}
func (t *uaTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if _, ok := req.Header["User-Agent"]; !ok {
req = req.Clone(req.Context())
req.Header.Set("User-Agent", consts.HTTPUserAgent)
}
return t.base.RoundTrip(req)
}
// NewTransport wraps base (or http.DefaultTransport if nil) to set the
// Navidrome User-Agent on requests that don't have one.
func NewTransport(base http.RoundTripper) http.RoundTripper {
if base == nil {
base = http.DefaultTransport
}
return &uaTransport{base: base}
}
func New(timeout time.Duration) *http.Client {
return &http.Client{Timeout: timeout, Transport: NewTransport(nil)}
}
// proxyFunc resolves the proxy for a request; tests replace it.
var proxyFunc = http.ProxyFromEnvironment
type proxyAddrKey struct{}
// NewExternal is New for URLs from untrusted sources: it refuses to dial private, loopback,
// link-local and unspecified addresses, except those covered by allowed.
func NewExternal(timeout time.Duration, allowed ...netip.Prefix) *http.Client {
t := http.DefaultTransport.(*http.Transport).Clone()
t.Proxy = proxyFunc
direct := net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
guarded := direct
guarded.Control = netguard.DialControl(allowed...)
t.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
// Exempt the hop to the proxy, which relays the request and is operator config. A URL
// aimed at the proxy's own address is not proxied, so it stays guarded.
if proxy, ok := ctx.Value(proxyAddrKey{}).(string); ok && proxy == addr {
return direct.DialContext(ctx, network, addr)
}
return guarded.DialContext(ctx, network, addr)
}
return &http.Client{Timeout: timeout, Transport: NewTransport(&proxyTagger{base: t})}
}
// proxyTagger records the proxy each request resolves to, so the dialer can tell a hop to the
// proxy from a dial to the URL's own host.
type proxyTagger struct{ base http.RoundTripper }
func (p *proxyTagger) RoundTrip(req *http.Request) (*http.Response, error) {
if u, err := proxyFunc(req); err == nil && u != nil {
req = req.WithContext(context.WithValue(req.Context(), proxyAddrKey{}, proxyAddr(u)))
}
return p.base.RoundTrip(req)
}
// proxyAddr mirrors how net/http addresses a proxy connection.
func proxyAddr(u *url.URL) string {
port := u.Port()
if port == "" {
port = map[string]string{"http": "80", "https": "443", "socks5": "1080", "socks5h": "1080"}[u.Scheme]
}
return net.JoinHostPort(u.Hostname(), port)
}