Open platform to collect and prioritize feedback
Revert "Add SSRF_GUARD_USE_PROXY option for the guarded HTTP client"
Matt Robertsaaa66d24a95c
diff --git a/.example.env b/.example.envindex b9cff1b7a..5214e3965 100644--- a/.example.env+++ b/.example.env@@ -6,11 +6,6 @@ JWT_SECRET=hsjl]W;&ZcHxT&FK;s%bgIQF:#ch=~#Al4:5]N;7V<qPZ3e9lT4'%;go;LIkc%k# Relaxes the SSRF guard so webhooks and OAuth token/profile URLs may target# private/internal network addresses (LAN, loopback, link-local). Self-hosted only.# ALLOW_PRIVATE_NETWORK_TARGETS=true-# By default, webhook and custom OAuth requests ignore HTTP_PROXY/HTTPS_PROXY so-# the SSRF guard can check every connection. Set this to send them through the-# proxy instead; proxied requests then rely only on the URL preflight check-# (requests matching NO_PROXY are still fully checked).-# SSRF_GUARD_USE_PROXY=trueLOG_LEVEL=DEBUGLOG_CONSOLE=truediff --git a/app/pkg/env/env.go b/app/pkg/env/env.goindex 55b61aaa3..39b113ed4 100644--- a/app/pkg/env/env.go+++ b/app/pkg/env/env.go@@ -57,7 +57,6 @@ type config struct {PostCreationWithTagsEnabled bool `env:"POST_CREATION_WITH_TAGS_ENABLED,default=false"`AllowAllowedSchemes bool `env:"ALLOW_ALLOWED_SCHEMES,default=true"`AllowPrivateNetworkTargets bool `env:"ALLOW_PRIVATE_NETWORK_TARGETS,default=false"`- SSRFGuardUseProxy bool `env:"SSRF_GUARD_USE_PROXY,default=false"`Stripe struct {SecretKey string `env:"STRIPE_SECRET_KEY"`WebhookSecret string `env:"STRIPE_WEBHOOK_SECRET"`diff --git a/app/pkg/netguard/netguard.go b/app/pkg/netguard/netguard.goindex 5553b87be..272232448 100644--- a/app/pkg/netguard/netguard.go+++ b/app/pkg/netguard/netguard.go@@ -5,23 +5,18 @@// There are two layers:// - validate.WebhookURL performs a friendly preflight check (it resolves the// hostname and rejects private targets with a readable message).-// - The client returned by Client/ClientFor enforces the same IsBlockedIP-// check at dial time, on the exact IP address being connected to. This is-// the real enforcement: it cannot be bypassed by DNS rebinding (a hostname-// that resolves to a public IP during validation and to a private IP when-// the request is made). The exception is requests sent through an HTTP(S)-// proxy when SSRF_GUARD_USE_PROXY=true; see NewClient.+// - The client returned by NewClient enforces the same IsBlockedIP check at+// dial time, on the exact IP address being connected to. This is the real+// enforcement: it cannot be bypassed by DNS rebinding (a hostname that+// resolves to a public IP during validation and to a private IP when the+// request is made).package netguardimport (- "context""errors""fmt""net""net/http"- "net/url"- "strings"- "sync""syscall""time"@@ -151,134 +146,49 @@ func dialControl(network, address string, _ syscall.RawConn) error {return nil}-// ProxyFunc selects the proxy for a request, like http.Transport.Proxy.-// A nil ProxyFunc means requests are always sent directly.-type ProxyFunc func(*http.Request) (*url.URL, error)--// proxyAddrKey is the context key under which guardedTransport stores the-// "host:port" of the proxy chosen for a request.-type proxyAddrKey struct{}--// guardedTransport wraps the real transport to tell the dialer which address,-// if any, is the proxy for this particular request.-//-// With a proxy, net/http dials the proxy rather than the target, so a-// dial-time check can only ever see the proxy's address. The proxy itself is-// operator-configured and may well live on a private network, so that one-// connection must be allowed. Everything else, in particular requests for-// which the proxy func returns nil (NO_PROXY match, loopback targets, or no-// proxy configured), must still be checked. To do that without guessing, the-// wrapper evaluates the proxy func itself and stores the resulting proxy-// address in the request context; the dialer skips the IP check only when the-// address it is asked to dial equals that value exactly. If the two ever-// disagree (e.g. the proxy environment changed between calls), the dial is-// checked as normal, i.e. the guard fails closed.-type guardedTransport struct {- proxy ProxyFunc- base *http.Transport-}--func (t *guardedTransport) RoundTrip(req *http.Request) (*http.Response, error) {- if t.proxy != nil {- proxyURL, err := t.proxy(req)- if err != nil {- return nil, err- }- if proxyURL != nil {- ctx := context.WithValue(req.Context(), proxyAddrKey{}, proxyDialAddr(proxyURL))- req = req.WithContext(ctx)- }- }- return t.base.RoundTrip(req)-}--// proxyDialAddr returns the address net/http dials for proxyURL (mirrors-// net/http's canonicalAddr: default port by scheme).-func proxyDialAddr(proxyURL *url.URL) string {- port := proxyURL.Port()- if port == "" {- switch strings.ToLower(proxyURL.Scheme) {- case "https":- port = "443"- case "socks5", "socks5h":- port = "1080"- default:- port = "80"- }- }- return net.JoinHostPort(proxyURL.Hostname(), port)-}-// NewClient returns an http.Client that refuses to connect to any address for// which IsBlockedIP returns true. It mirrors Fider's default client settings:// 30s timeout and redirects are not followed.//-// If proxy is nil, requests are always sent directly and every connection is-// checked. If proxy is set, requests it routes through a proxy are NOT covered-// by the dial-time check (the proxy resolves and connects to the target), so-// for those only the validate.WebhookURL preflight applies; this is weaker-// against DNS rebinding. Requests the proxy func sends directly are still-// fully checked. See guardedTransport.-func NewClient(proxy ProxyFunc) *http.Client {- guarded := &net.Dialer{+// Proxies are intentionally disabled (Proxy: nil, i.e. HTTP(S)_PROXY is+// ignored). With a proxy, the dial would go to the proxy rather than to the+// target, so the dial-time check would inspect the proxy's address and either+// block every request (proxy on a private network) or protect nothing (the+// proxy resolves and connects to the target itself). Guarded requests are+// therefore always sent directly. Installs that can only reach the internet+// through a proxy can set ALLOW_PRIVATE_NETWORK_TARGETS=true, which makes+// ClientFor fall back to http.DefaultClient (and so honour HTTP(S)_PROXY).+func NewClient() *http.Client {+ dialer := &net.Dialer{Timeout: 30 * time.Second,KeepAlive: 30 * time.Second,Control: dialControl,}- toProxy := &net.Dialer{- Timeout: 30 * time.Second,- KeepAlive: 30 * time.Second,- }- base := http.DefaultTransport.(*http.Transport).Clone()- base.Proxy = proxy- base.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {- if proxyAddr, _ := ctx.Value(proxyAddrKey{}).(string); proxyAddr != "" && addr == proxyAddr {- return toProxy.DialContext(ctx, network, addr)- }- return guarded.DialContext(ctx, network, addr)- }+ transport := http.DefaultTransport.(*http.Transport).Clone()+ transport.Proxy = nil+ transport.DialContext = dialer.DialContextreturn &http.Client{Timeout: 30 * time.Second,- Transport: &guardedTransport{proxy: proxy, base: base},+ Transport: transport,CheckRedirect: func(req *http.Request, via []*http.Request) error {return http.ErrUseLastResponse},}}-var (- directOnce sync.Once- directClient *http.Client- proxiedOnce sync.Once- proxiedClient *http.Client-)--// Client returns the shared guarded client (safe for concurrent use).-// It is chosen when called, not at package init, so it reflects the loaded-// env config:-// - SSRF_GUARD_USE_PROXY=false (default): HTTP(S)_PROXY is ignored and every-// connection is checked at dial time.-// - SSRF_GUARD_USE_PROXY=true: http.ProxyFromEnvironment is used; proxied-// requests rely on the preflight only, direct ones are checked at dial time.-func Client() *http.Client {- if env.Config.SSRFGuardUseProxy {- proxiedOnce.Do(func() { proxiedClient = NewClient(http.ProxyFromEnvironment) })- return proxiedClient- }- directOnce.Do(func() { directClient = NewClient(nil) })- return directClient-}+// Client is a shared guarded client, safe for concurrent use.+var Client = NewClient()// ClientFor returns the HTTP client to use for an outbound request. When-// blockPrivateNetworkTargets is true it returns the guarded Client(), unless-// the instance opted out via ALLOW_PRIVATE_NETWORK_TARGETS=true, in which case-// (and when blockPrivateNetworkTargets is false) it returns http.DefaultClient.+// blockPrivateNetworkTargets is true it returns the guarded Client, unless the+// instance opted out via ALLOW_PRIVATE_NETWORK_TARGETS=true, in which case (and+// when blockPrivateNetworkTargets is false) it returns http.DefaultClient.// This is the single place where the escape hatch is applied.func ClientFor(blockPrivateNetworkTargets bool) *http.Client {if blockPrivateNetworkTargets && !env.Config.AllowPrivateNetworkTargets {- return Client()+ return Client}return http.DefaultClient}diff --git a/app/pkg/netguard/netguard_test.go b/app/pkg/netguard/netguard_test.goindex 6eb1e21b1..9ccd4fbec 100644--- a/app/pkg/netguard/netguard_test.go+++ b/app/pkg/netguard/netguard_test.go@@ -3,17 +3,14 @@ package netguard_testimport ("context""errors"- "io""net""net/http""net/http/httptest"- "net/url""testing". "github.com/getfider/fider/app/pkg/assert""github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/netguard"- "golang.org/x/net/http/httpproxy")func TestIsBlockedIP(t *testing.T) {@@ -134,7 +131,7 @@ func TestClient_RejectsBlockedTargetsAtDialTime(t *testing.T) {"http://localhost:" + port + "/", // hostname resolving to loopback} {req, _ := http.NewRequestWithContext(context.Background(), "GET", rawurl, nil)- res, err := netguard.Client().Do(req)+ res, err := netguard.Client.Do(req)if res != nil {_ = res.Body.Close()}@@ -151,109 +148,10 @@ func TestClientFor(t *testing.T) {t.Cleanup(func() { env.Config.AllowPrivateNetworkTargets = original })env.Config.AllowPrivateNetworkTargets = false- Expect(netguard.ClientFor(true) == netguard.Client()).IsTrue()+ Expect(netguard.ClientFor(true) == netguard.Client).IsTrue()Expect(netguard.ClientFor(false) == http.DefaultClient).IsTrue()env.Config.AllowPrivateNetworkTargets = trueExpect(netguard.ClientFor(true) == http.DefaultClient).IsTrue()Expect(netguard.ClientFor(false) == http.DefaultClient).IsTrue()}--// newTestProxy starts a local forward-proxy stand-in on 127.0.0.1 that answers-// every request itself and records the absolute request URLs it received.-func newTestProxy(t *testing.T) (*httptest.Server, *[]string) {- seen := make([]string, 0)- proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- seen = append(seen, r.URL.String())- _, _ = w.Write([]byte("via proxy"))- }))- t.Cleanup(proxy.Close)- return proxy, &seen-}--// proxyFuncFor builds a deterministic proxy func (http.ProxyFromEnvironment-// caches the environment on first use, so it can't be driven from tests).-func proxyFuncFor(cfg *httpproxy.Config) netguard.ProxyFunc {- fn := cfg.ProxyFunc()- return func(req *http.Request) (*url.URL, error) {- return fn(req.URL)- }-}--func get(client *http.Client, rawurl string) (*http.Response, []byte, error) {- req, _ := http.NewRequestWithContext(context.Background(), "GET", rawurl, nil)- res, err := client.Do(req)- if err != nil {- return nil, nil, err- }- defer func() { _ = res.Body.Close() }()- body, err := io.ReadAll(res.Body)- return res, body, err-}--func TestNewClient_WithProxy(t *testing.T) {- RegisterT(t)-- proxy, seen := newTestProxy(t)- target := newLocalServer(t)- client := netguard.NewClient(proxyFuncFor(&httpproxy.Config{- HTTPProxy: proxy.URL,- NoProxy: "10.0.0.0/8",- }))-- // Proxied request: the dial goes to the proxy on 127.0.0.1, which must not- // be blocked. The target itself is only covered by the preflight check.- res, body, err := get(client, "http://203.0.113.10/hook")- Expect(err).IsNil()- Expect(res.StatusCode).Equals(http.StatusOK)- Expect(string(body)).Equals("via proxy")- Expect(*seen).Equals([]string{"http://203.0.113.10/hook"})-- // NO_PROXY match: sent directly, so the dial-time guard still applies.- _, _, err = get(client, "http://10.1.2.3/hook")- Expect(errors.Is(err, netguard.ErrBlockedAddress)).IsTrue()-- // Loopback targets are never proxied by httpproxy / ProxyFromEnvironment:- // also direct, also blocked, even though the proxy address is loopback too.- _, _, err = get(client, target.URL)- Expect(errors.Is(err, netguard.ErrBlockedAddress)).IsTrue()-- Expect(*seen).HasLen(1)-}--func TestNewClient_WithoutProxyIgnoresProxyEnv(t *testing.T) {- RegisterT(t)-- proxy, seen := newTestProxy(t)- t.Setenv("HTTP_PROXY", proxy.URL)- t.Setenv("http_proxy", proxy.URL)-- // 10.1.2.3 is rejected by the dial guard before any packet is sent. Had a- // proxy been used, the local proxy would have answered with 200.- _, _, err := get(netguard.NewClient(nil), "http://10.1.2.3/hook")- Expect(errors.Is(err, netguard.ErrBlockedAddress)).IsTrue()- Expect(*seen).HasLen(0)-}--func TestClient_ReflectsSSRFGuardUseProxy(t *testing.T) {- RegisterT(t)-- original := env.Config.SSRFGuardUseProxy- t.Cleanup(func() { env.Config.SSRFGuardUseProxy = original })-- env.Config.SSRFGuardUseProxy = false- direct := netguard.Client()- Expect(netguard.Client() == direct).IsTrue()-- env.Config.SSRFGuardUseProxy = true- proxied := netguard.Client()- Expect(proxied == direct).IsFalse()- Expect(netguard.Client() == proxied).IsTrue()- Expect(netguard.ClientFor(true) == proxied).IsTrue()-- // Whether or not a proxy is configured in this environment, loopback- // targets are never proxied, so they are still blocked at dial time.- target := newLocalServer(t)- _, _, err := get(proxied, target.URL)- Expect(errors.Is(err, netguard.ErrBlockedAddress)).IsTrue()-}diff --git a/app/services/oauth/guard_internal_test.go b/app/services/oauth/guard_internal_test.goindex 7a858a362..535522d35 100644--- a/app/services/oauth/guard_internal_test.go+++ b/app/services/oauth/guard_internal_test.go@@ -24,7 +24,7 @@ func TestTokenExchangeContext(t *testing.T) {expected *http.Client}{// Custom providers use the guarded client...- {"_custom", false, netguard.Client()},+ {"_custom", false, netguard.Client},// ...unless the instance allows private network targets.{"_custom", true, http.DefaultClient},// Built-in providers leave the context untouched (oauth2 then uses its default).