Open platform to collect and prioritize feedback
Address review: extra blocked ranges, call-site tests, single gate
Matt Roberts0a1cd53dfd27
diff --git a/app/pkg/netguard/netguard.go b/app/pkg/netguard/netguard.goindex a68d7a859..94de4c6aa 100644--- a/app/pkg/netguard/netguard.go+++ b/app/pkg/netguard/netguard.go@@ -13,13 +13,14 @@package netguardimport (- "context""errors""fmt""net""net/http""syscall""time"++ "github.com/getfider/fider/app/pkg/env")// ErrBlockedAddress is returned when a connection to a private/internal@@ -45,6 +46,7 @@ var blockedIPv4 = mustParseCIDRs("127.0.0.0/8", // loopback"169.254.0.0/16", // link-local (incl. cloud metadata 169.254.169.254)"172.16.0.0/12", // private+ "192.0.0.0/24", // IETF protocol assignments (incl. Oracle Cloud metadata 192.0.0.192)"192.168.0.0/16", // private"198.18.0.0/15", // benchmarking"224.0.0.0/4", // multicast@@ -56,6 +58,7 @@ var blockedIPv6 = mustParseCIDRs("::1/128", // loopback"fc00::/7", // unique local"fe80::/10", // link-local+ "fec0::/10", // site-local (deprecated)"ff00::/8", // multicast"64:ff9b:1::/48", // NAT64 local-use)@@ -64,12 +67,13 @@ var (nat64Prefix = mustParseCIDRs("64:ff9b::/96")[0]sixToFourPrefix = mustParseCIDRs("2002::/16")[0]teredoPrefix = mustParseCIDRs("2001::/32")[0]- ipv4Compatible = mustParseCIDRs("::/96")[0] // deprecated IPv4-compatible IPv6 (::a.b.c.d)+ ipv4Compatible = mustParseCIDRs("::/96")[0] // deprecated IPv4-compatible IPv6 (::a.b.c.d)+ ipv4Translated = mustParseCIDRs("::ffff:0:0:0/96")[0] // IPv4-translated / SIIT (::ffff:0:a.b.c.d))// IsBlockedIP reports whether ip belongs to a private, internal, or otherwise// non-public range that outbound requests to user-configured URLs must not reach.-// IPv6 transition addresses (NAT64, 6to4, Teredo, IPv4-compatible) are+// IPv6 transition addresses (NAT64, 6to4, Teredo, IPv4-compatible, SIIT) are// unwrapped and the embedded IPv4 address is checked as well.// A nil/invalid IP is treated as blocked.func IsBlockedIP(ip net.IP) bool {@@ -121,6 +125,9 @@ func embeddedIPv4(ip net.IP) net.IP {case ipv4Compatible.Contains(ip):// ::a.b.c.d (:: and ::1 are already handled by blockedIPv6)return net.IPv4(ip[12], ip[13], ip[14], ip[15])+ case ipv4Translated.Contains(ip):+ // ::ffff:0:a.b.c.d+ return net.IPv4(ip[12], ip[13], ip[14], ip[15])}return nil}@@ -156,22 +163,9 @@ func NewClient() *http.Client {Control: dialControl,}- var transport *http.Transport- if t, ok := http.DefaultTransport.(*http.Transport); ok {- transport = t.Clone()- } else {- transport = &http.Transport{- ForceAttemptHTTP2: true,- MaxIdleConns: 100,- IdleConnTimeout: 90 * time.Second,- TLSHandshakeTimeout: 10 * time.Second,- ExpectContinueTimeout: 1 * time.Second,- }- }+ transport := http.DefaultTransport.(*http.Transport).Clone()transport.Proxy = nil- transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {- return dialer.DialContext(ctx, network, addr)- }+ transport.DialContext = dialer.DialContextreturn &http.Client{Timeout: 30 * time.Second,@@ -184,3 +178,15 @@ func NewClient() *http.Client {// 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.+// 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 http.DefaultClient+}diff --git a/app/pkg/netguard/netguard_test.go b/app/pkg/netguard/netguard_test.goindex f95b8a182..9ccd4fbec 100644--- a/app/pkg/netguard/netguard_test.go+++ b/app/pkg/netguard/netguard_test.go@@ -9,6 +9,7 @@ import ("testing". "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/netguard")@@ -31,6 +32,9 @@ func TestIsBlockedIP(t *testing.T) {{"172.16.0.1", true},{"172.31.255.255", true},{"192.168.1.1", true},+ {"192.0.0.1", true},+ {"192.0.0.192", true}, // Oracle Cloud metadata+ {"192.0.0.255", true},{"198.18.0.1", true},{"198.19.255.255", true},{"224.0.0.1", true},@@ -49,6 +53,8 @@ func TestIsBlockedIP(t *testing.T) {{"fc00::1", true},{"fd12:3456::1", true},{"fe80::1", true},+ {"fec0::1", true},+ {"feff:ffff::1", true},{"ff02::1", true},{"ff0e::1", true},{"64:ff9b:1::1", true},@@ -74,6 +80,12 @@ func TestIsBlockedIP(t *testing.T) {{"::7f00:1", true},{"::a9fe:a9fe", true},+ // IPv4-translated / SIIT ::ffff:0:0:0/96+ {"::ffff:0:a9fe:a9fe", true}, // 169.254.169.254+ {"::ffff:0:7f00:1", true}, // 127.0.0.1+ {"::ffff:0:c000:c0", true}, // 192.0.0.192+ {"::ffff:0:808:808", false}, // 8.8.8.8+// Public{"8.8.8.8", false},{"1.1.1.1", false},@@ -81,9 +93,11 @@ func TestIsBlockedIP(t *testing.T) {{"100.63.255.255", false},{"100.128.0.0", false},{"198.20.0.1", false},+ {"192.0.1.1", false},+ {"192.0.2.1", false},+ {"191.255.255.255", false},{"2001:4860:4860::8888", false},- {"2606:4700:4700::1111", false},- }+ {"2606:4700:4700::1111", false}}for _, tc := range testCases {ip := net.ParseIP(tc.ip)@@ -126,3 +140,18 @@ func TestClient_RejectsBlockedTargetsAtDialTime(t *testing.T) {}}}++func TestClientFor(t *testing.T) {+ RegisterT(t)++ original := env.Config.AllowPrivateNetworkTargets+ t.Cleanup(func() { env.Config.AllowPrivateNetworkTargets = original })++ env.Config.AllowPrivateNetworkTargets = false+ Expect(netguard.ClientFor(true) == netguard.Client).IsTrue()+ Expect(netguard.ClientFor(false) == http.DefaultClient).IsTrue()++ env.Config.AllowPrivateNetworkTargets = true+ Expect(netguard.ClientFor(true) == http.DefaultClient).IsTrue()+ Expect(netguard.ClientFor(false) == http.DefaultClient).IsTrue()+}diff --git a/app/pkg/validate/general_test.go b/app/pkg/validate/general_test.goindex 4b7b02c06..aadee733e 100644--- a/app/pkg/validate/general_test.go+++ b/app/pkg/validate/general_test.go@@ -107,6 +107,9 @@ func TestWebhookURL_BlockedAddresses(t *testing.T) {"http://[2002:a9fe:a9fe::1]/hook","http://[2002:7f00:1::1]:8080/hook","http://[2001:0:4136:e378:8000:63bf:80ff:fffe]/hook",+ "http://192.0.0.192/opc/v2/instance/",+ "http://[fec0::1]/hook",+ "http://[::ffff:0:a9fe:a9fe]/latest/meta-data/","ftp://example.com/hook","file:///etc/passwd","gopher://example.com",diff --git a/app/services/httpclient/httpclient.go b/app/services/httpclient/httpclient.goindex 26fccd829..1b9f8107c 100644--- a/app/services/httpclient/httpclient.go+++ b/app/services/httpclient/httpclient.go@@ -40,17 +40,6 @@ func (s Service) Init() {bus.AddHandler(requestHandler)}-// clientFor returns the HTTP client to use for the given request. Requests that-// opt in to BlockPrivateNetworkTargets use the netguard client, which rejects-// private/internal addresses at dial time, unless the instance explicitly-// allows private network targets.-func clientFor(c *cmd.HTTPRequest) *http.Client {- if c.BlockPrivateNetworkTargets && !env.Config.AllowPrivateNetworkTargets {- return netguard.Client- }- return http.DefaultClient-}-func requestHandler(ctx context.Context, c *cmd.HTTPRequest) error {req, err := http.NewRequest(c.Method, c.URL, c.Body)if err != nil {@@ -65,7 +54,7 @@ func requestHandler(ctx context.Context, c *cmd.HTTPRequest) error {req.SetBasicAuth(c.BasicAuth.User, c.BasicAuth.Password)}- res, err := clientFor(c).Do(req)+ res, err := netguard.ClientFor(c.BlockPrivateNetworkTargets).Do(req)if err != nil {return err}diff --git a/app/services/httpclient/httpclientmock/httpclientmock.go b/app/services/httpclient/httpclientmock/httpclientmock.goindex 901e6cadf..414d70299 100644--- a/app/services/httpclient/httpclientmock/httpclientmock.go+++ b/app/services/httpclient/httpclientmock/httpclientmock.go@@ -30,12 +30,17 @@ func (s Service) Category() string {var RequestsHistory = make([]*http.Request, 0)+// CommandsHistory records every dispatched cmd.HTTPRequest, so tests can assert+// on fields that are not part of the http.Request (e.g. BlockPrivateNetworkTargets).+var CommandsHistory = make([]*cmd.HTTPRequest, 0)+func (s Service) Enabled() bool {return env.IsTest()}func (s Service) Init() {RequestsHistory = make([]*http.Request, 0)+ CommandsHistory = make([]*cmd.HTTPRequest, 0)bus.AddHandler(requestHandler)}@@ -53,6 +58,7 @@ func requestHandler(ctx context.Context, c *cmd.HTTPRequest) error {}RequestsHistory = append(RequestsHistory, req)+ CommandsHistory = append(CommandsHistory, c)c.ResponseStatusCode = http.StatusOKc.ResponseBody = []byte("")diff --git a/app/services/oauth/guard_internal_test.go b/app/services/oauth/guard_internal_test.gonew file mode 100644index 000000000..535522d35--- /dev/null+++ b/app/services/oauth/guard_internal_test.go@@ -0,0 +1,70 @@+package oauth++import (+ "context"+ "net/http"+ "testing"++ "github.com/getfider/fider/app"+ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/pkg/netguard"+ "golang.org/x/oauth2"+)++func TestTokenExchangeContext(t *testing.T) {+ RegisterT(t)++ original := env.Config.AllowPrivateNetworkTargets+ t.Cleanup(func() { env.Config.AllowPrivateNetworkTargets = original })++ testCases := []struct {+ provider string+ allowPrivate bool+ expected *http.Client+ }{+ // Custom providers use the guarded 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).+ {app.GitHubProvider, false, nil},+ {app.GoogleProvider, false, nil},+ {app.FacebookProvider, false, nil},+ }++ for _, tc := range testCases {+ env.Config.AllowPrivateNetworkTargets = tc.allowPrivate++ ctx := tokenExchangeContext(context.Background(), tc.provider)+ client, _ := ctx.Value(oauth2.HTTPClient).(*http.Client)+ if client != tc.expected {+ t.Errorf("tokenExchangeContext(%q) with AllowPrivateNetworkTargets=%v returned unexpected client", tc.provider, tc.allowPrivate)+ }+ }+}++func TestNewProfileRequest_BlocksPrivateNetworkTargetsForCustomProviders(t *testing.T) {+ RegisterT(t)++ testCases := []struct {+ provider string+ block bool+ }{+ {"_custom", true},+ {"_1234", true},+ {app.GitHubProvider, false},+ {app.GoogleProvider, false},+ {app.FacebookProvider, false},+ }++ for _, tc := range testCases {+ req := newProfileRequest(tc.provider, "https://example.org/me", "TOKEN")+ Expect(req.URL).Equals("https://example.org/me")+ Expect(req.Method).Equals("GET")+ Expect(req.Headers["Authorization"]).Equals("Bearer TOKEN")+ if req.BlockPrivateNetworkTargets != tc.block {+ t.Errorf("newProfileRequest(%q).BlockPrivateNetworkTargets = %v, expected %v", tc.provider, req.BlockPrivateNetworkTargets, tc.block)+ }+ }+}diff --git a/app/services/oauth/guard_test.go b/app/services/oauth/guard_test.gonew file mode 100644index 000000000..a39a3e951--- /dev/null+++ b/app/services/oauth/guard_test.go@@ -0,0 +1,59 @@+package oauth_test++import (+ "context"+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/query"+ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/services/httpclient/httpclientmock"+ "github.com/getfider/fider/app/services/oauth"+)++// End-to-end check that getOAuthRawProfile sends the custom provider's profile+// request with the SSRF guard flag set. AllowPrivateNetworkTargets is enabled+// only so the local httptest token endpoint passes the URL preflight; the flag+// on the request is independent of it (netguard.ClientFor applies the escape+// hatch when the request is executed).+func TestGetOAuthRawProfile_CustomProviderProfileRequestIsGuarded(t *testing.T) {+ RegisterT(t)++ original := env.Config.AllowPrivateNetworkTargets+ env.Config.AllowPrivateNetworkTargets = true+ t.Cleanup(func() { env.Config.AllowPrivateNetworkTargets = original })++ tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"access_token":"ACCESS_TOKEN","token_type":"bearer"}`))+ }))+ t.Cleanup(tokenServer.Close)++ bus.Init(&oauth.Service{}, httpclientmock.Service{})+ bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {+ q.Result = &entity.OAuthConfig{+ Provider: q.Provider,+ ClientID: "CU_CL_ID",+ ClientSecret: "CU_CL_SECRET",+ AuthorizeURL: "https://example.org/oauth/authorize",+ TokenURL: tokenServer.URL + "/token",+ ProfileURL: "https://203.0.113.10/me",+ }+ return nil+ })++ ctx := newGetContext("http://login.test.fider.io:3000")+ rawProfile := &query.GetOAuthRawProfile{Provider: "_custom", Code: "CODE"}+ err := bus.Dispatch(ctx, rawProfile)+ Expect(err).IsNil()++ Expect(httpclientmock.CommandsHistory).HasLen(1)+ req := httpclientmock.CommandsHistory[0]+ Expect(req.URL).Equals("https://203.0.113.10/me")+ Expect(req.Headers["Authorization"]).Equals("Bearer ACCESS_TOKEN")+ Expect(req.BlockPrivateNetworkTargets).IsTrue()+}diff --git a/app/services/oauth/oauth.go b/app/services/oauth/oauth.goindex 94026f109..c20ea2040 100644--- a/app/services/oauth/oauth.go+++ b/app/services/oauth/oauth.go@@ -336,17 +336,7 @@ func getOAuthRawProfile(ctx context.Context, q *query.GetOAuthRawProfile) errorRedirectURL: fmt.Sprintf("%s/oauth/%s/callback", oauthBaseURL, q.Provider),}).Exchange- // Custom providers have admin-configurable Token/Profile URLs, so enforce the- // SSRF guard at dial time as well (the WebhookURL preflight above can be- // bypassed via DNS rebinding). Built-in providers use fixed public URLs and- // keep the default client (which honours HTTP(S)_PROXY).- guard := isCustomProvider(config) && !env.Config.AllowPrivateNetworkTargets- exchangeCtx := ctx- if guard {- exchangeCtx = context.WithValue(ctx, oauth2.HTTPClient, netguard.Client)- }-- oauthToken, err := exchange(exchangeCtx, q.Code)+ oauthToken, err := exchange(tokenExchangeContext(ctx, q.Provider), q.Code)if err != nil {return err}@@ -367,14 +357,7 @@ func getOAuthRawProfile(ctx context.Context, q *query.GetOAuthRawProfile) errorreturn errors.New("Profile URL is not allowed: %s", strings.Join(msgs, "; "))}- req := &cmd.HTTPRequest{- URL: config.ProfileURL,- Method: "GET",- Headers: map[string]string{- "Authorization": "Bearer " + oauthToken.AccessToken,- },- BlockPrivateNetworkTargets: isCustomProvider(config),- }+ req := newProfileRequest(q.Provider, config.ProfileURL, oauthToken.AccessToken)if err := bus.Dispatch(ctx, req); err != nil {return err@@ -445,15 +428,37 @@ func listAllOAuthProviders(ctx context.Context, q *query.ListAllOAuthProviders)return nil}-// isCustomProvider reports whether config is a tenant-defined (custom) OAuth-// provider rather than one of the built-in system providers.-func isCustomProvider(config *entity.OAuthConfig) bool {- for _, p := range systemProviders {- if p == config {- return false- }+// isCustomOAuthProvider reports whether provider is a tenant-defined (custom)+// OAuth provider. Custom provider keys are prefixed with "_".+func isCustomOAuthProvider(provider string) bool {+ return strings.HasPrefix(provider, "_")+}++// Custom providers have admin-configurable Token/Profile URLs, so the SSRF guard+// is enforced at dial time for them (the validate.WebhookURL preflight can be+// bypassed via DNS rebinding). Built-in providers use fixed public URLs and keep+// the default client (which honours HTTP(S)_PROXY).++// tokenExchangeContext returns the context to pass to oauth2's Exchange. For+// custom providers it carries the guarded HTTP client (see netguard.ClientFor).+func tokenExchangeContext(ctx context.Context, provider string) context.Context {+ if !isCustomOAuthProvider(provider) {+ return ctx+ }+ return context.WithValue(ctx, oauth2.HTTPClient, netguard.ClientFor(true))+}++// newProfileRequest builds the request that fetches the user profile after the+// token exchange. Requests for custom providers block private network targets.+func newProfileRequest(provider, profileURL, accessToken string) *cmd.HTTPRequest {+ return &cmd.HTTPRequest{+ URL: profileURL,+ Method: "GET",+ Headers: map[string]string{+ "Authorization": "Bearer " + accessToken,+ },+ BlockPrivateNetworkTargets: isCustomOAuthProvider(provider),}- return true}func getConfig(ctx context.Context, provider string) (*entity.OAuthConfig, error) {diff --git a/app/services/webhook/webhook_test.go b/app/services/webhook/webhook_test.gonew file mode 100644index 000000000..ed79a4300--- /dev/null+++ b/app/services/webhook/webhook_test.go@@ -0,0 +1,49 @@+package webhook_test++import (+ "context"+ "testing"++ "github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/models/entity"+ "github.com/getfider/fider/app/models/enum"+ "github.com/getfider/fider/app/models/query"+ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/bus"+ pkgwebhook "github.com/getfider/fider/app/pkg/webhook"+ "github.com/getfider/fider/app/services/httpclient/httpclientmock"+ "github.com/getfider/fider/app/services/webhook"+)++// Webhook URLs are admin-configurable, so the outgoing request must opt in to+// the dial-time SSRF guard (the WebhookURL preflight alone is bypassable via+// DNS rebinding).+func TestTriggerWebhooks_BlocksPrivateNetworkTargets(t *testing.T) {+ RegisterT(t)+ bus.Init(webhook.Service{}, httpclientmock.Service{})++ bus.AddHandler(func(ctx context.Context, q *query.ListActiveWebhooksByType) error {+ q.Result = []*entity.Webhook{+ {+ ID: 1,+ Name: "Public webhook",+ Type: enum.WebhookNewPost,+ Status: enum.WebhookEnabled,+ Url: "https://203.0.113.10/hook", // IP literal: no DNS lookup in tests+ Content: "hello",+ HttpMethod: "POST",+ },+ }+ return nil+ })++ err := bus.Dispatch(context.Background(), &cmd.TriggerWebhooks{+ Type: enum.WebhookNewPost,+ Props: pkgwebhook.Props{},+ })+ Expect(err).IsNil()++ Expect(httpclientmock.CommandsHistory).HasLen(1)+ Expect(httpclientmock.CommandsHistory[0].URL).Equals("https://203.0.113.10/hook")+ Expect(httpclientmock.CommandsHistory[0].BlockPrivateNetworkTargets).IsTrue()+}