Open platform to collect and prioritize feedback
Address second review: fail-closed OAuth guard, blocklist tweaks
Matt Roberts11ad005d6c09
diff --git a/app/pkg/netguard/netguard.go b/app/pkg/netguard/netguard.goindex 272232448..c933a98e5 100644--- a/app/pkg/netguard/netguard.go+++ b/app/pkg/netguard/netguard.go@@ -40,22 +40,36 @@ func mustParseCIDRs(cidrs ...string) []*net.IPNet {}var blockedIPv4 = mustParseCIDRs(- "0.0.0.0/8", // "this" network- "10.0.0.0/8", // private- "100.64.0.0/10", // carrier-grade NAT- "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- "240.0.0.0/4", // reserved, incl. broadcast 255.255.255.255+ "0.0.0.0/8", // "this" network+ "10.0.0.0/8", // private+ "100.64.0.0/10", // carrier-grade NAT+ "127.0.0.0/8", // loopback+ "169.254.0.0/16", // link-local (incl. cloud metadata 169.254.169.254)+ "168.63.129.16/32", // Azure WireServer / host agent+ "172.16.0.0/12", // private+ "192.0.0.0/24", // IETF protocol assignments (incl. Oracle Cloud metadata 192.0.0.192); see globallyReachableIPv4+ "192.168.0.0/16", // private+ "198.18.0.0/15", // benchmarking+ "224.0.0.0/4", // multicast+ "240.0.0.0/4", // reserved, incl. broadcast 255.255.255.255+)++// globallyReachableIPv4 are exceptions inside blockedIPv4. Per the IANA IPv4+// special-purpose address registry, 192.0.0.0/24 is not globally reachable,+// except 192.0.0.9 (PCP anycast, RFC 7723) and 192.0.0.10 (TURN anycast,+// RFC 8155). The non-reachable entries (192.0.0.0/29 service continuity,+// 192.0.0.8 dummy, 192.0.0.170-171 NAT64/DNS64 discovery) and the unassigned+// remainder, including Oracle Cloud metadata 192.0.0.192, stay blocked.+var globallyReachableIPv4 = mustParseCIDRs(+ "192.0.0.9/32",+ "192.0.0.10/32",)var blockedIPv6 = mustParseCIDRs("::/128", // unspecified"::1/128", // loopback+ "100::/64", // discard-only+ "2001:db8::/32", // documentation"fc00::/7", // unique local"fe80::/10", // link-local"fec0::/10", // site-local (deprecated)@@ -83,6 +97,11 @@ func IsBlockedIP(ip net.IP) bool {// Covers plain IPv4 and IPv4-mapped IPv6 (::ffff:a.b.c.d).if v4 := ip.To4(); v4 != nil {+ for _, n := range globallyReachableIPv4 {+ if n.Contains(v4) {+ return false+ }+ }for _, n := range blockedIPv4 {if n.Contains(v4) {return truediff --git a/app/pkg/netguard/netguard_test.go b/app/pkg/netguard/netguard_test.goindex 9ccd4fbec..e65bdb14a 100644--- a/app/pkg/netguard/netguard_test.go+++ b/app/pkg/netguard/netguard_test.go@@ -32,9 +32,17 @@ func TestIsBlockedIP(t *testing.T) {{"172.16.0.1", true},{"172.31.255.255", true},{"192.168.1.1", true},+ {"168.63.129.16", true}, // Azure WireServer+ {"192.0.0.0", true},{"192.0.0.1", true},+ {"192.0.0.7", true},+ {"192.0.0.8", true}, // IPv4 dummy address+ {"192.0.0.11", true}, // IETF protocol assignment, not globally reachable+ {"192.0.0.170", true}, // NAT64/DNS64 discovery+ {"192.0.0.171", true}, // NAT64/DNS64 discovery{"192.0.0.192", true}, // Oracle Cloud metadata{"192.0.0.255", true},+ {"::ffff:192.0.0.192", true},{"198.18.0.1", true},{"198.19.255.255", true},{"224.0.0.1", true},@@ -58,6 +66,10 @@ func TestIsBlockedIP(t *testing.T) {{"ff02::1", true},{"ff0e::1", true},{"64:ff9b:1::1", true},+ {"100::1", true}, // discard-only+ {"100::ffff:ffff:ffff:ffff", true}, // discard-only+ {"2001:db8::1", true}, // documentation+ {"2001:db8:ffff::1", true}, // documentation// NAT64 64:ff9b::/96{"64:ff9b::a9fe:a9fe", true},@@ -93,6 +105,13 @@ func TestIsBlockedIP(t *testing.T) {{"100.63.255.255", false},{"100.128.0.0", false},{"198.20.0.1", false},+ {"192.0.0.9", false}, // PCP anycast (globally reachable)+ {"192.0.0.10", false}, // TURN anycast (globally reachable)+ {"::ffff:192.0.0.9", false},+ {"168.63.129.15", false},+ {"168.63.129.17", false},+ {"100:0:0:1::1", false}, // just outside 100::/64+ {"2001:db9::1", false}, // just outside 2001:db8::/32{"192.0.1.1", false},{"192.0.2.1", false},{"191.255.255.255", false},diff --git a/app/services/httpclient/httpclientmock/httpclientmock.go b/app/services/httpclient/httpclientmock/httpclientmock.goindex 414d70299..71d4e7c34 100644--- a/app/services/httpclient/httpclientmock/httpclientmock.go+++ b/app/services/httpclient/httpclientmock/httpclientmock.go@@ -38,9 +38,16 @@ func (s Service) Enabled() bool {return env.IsTest()}-func (s Service) Init() {+// Reset clears the recorded request history. It runs on every Init (i.e. each+// bus.Init that includes this service); tests that assert on the history also+// call it directly so nothing can leak in from earlier tests.+func Reset() {RequestsHistory = make([]*http.Request, 0)CommandsHistory = make([]*cmd.HTTPRequest, 0)+}++func (s Service) Init() {+ Reset()bus.AddHandler(requestHandler)}diff --git a/app/services/oauth/export_test.go b/app/services/oauth/export_test.gonew file mode 100644index 000000000..ade84f63c--- /dev/null+++ b/app/services/oauth/export_test.go@@ -0,0 +1,17 @@+package oauth++import "testing"++// SetSystemProviderStatus overrides the instance-level status of a built-in+// provider for the duration of the test (test-only hook).+func SetSystemProviderStatus(t *testing.T, provider string, status int) {+ for _, p := range systemProviders {+ if p.Provider == provider {+ original := p.Status+ p.Status = status+ t.Cleanup(func() { p.Status = original })+ return+ }+ }+ t.Fatalf("unknown system provider %s", provider)+}diff --git a/app/services/oauth/guard_internal_test.go b/app/services/oauth/guard_internal_test.goindex 535522d35..7bbcd6a06 100644--- a/app/services/oauth/guard_internal_test.go+++ b/app/services/oauth/guard_internal_test.go@@ -6,12 +6,51 @@ import ("testing""github.com/getfider/fider/app"+ "github.com/getfider/fider/app/models/entity". "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 systemProvider(provider string) *entity.OAuthConfig {+ for _, p := range systemProviders {+ if p.Provider == provider {+ return p+ }+ }+ panic("unknown system provider " + provider)+}++func TestRequiresSSRFGuard(t *testing.T) {+ RegisterT(t)++ testCases := []struct {+ name string+ provider string+ config *entity.OAuthConfig+ guard bool+ }{+ {"custom provider", "_custom", &entity.OAuthConfig{Provider: "_custom"}, true},+ {"built-in GitHub", app.GitHubProvider, systemProvider(app.GitHubProvider), false},+ {"built-in Google", app.GoogleProvider, systemProvider(app.GoogleProvider), false},+ {"built-in Facebook", app.FacebookProvider, systemProvider(app.FacebookProvider), false},+ // getConfig falls through to the tenant's custom config when a built-in+ // provider is not enabled at instance level: fail closed.+ {"tenant config named like a built-in", app.GitHubProvider, &entity.OAuthConfig{Provider: app.GitHubProvider}, true},+ {"unknown provider without prefix", "custom", &entity.OAuthConfig{Provider: "custom"}, true},+ {"nil config", app.GitHubProvider, nil, true},+ // A "_" key is guarded even if it somehow resolved to a system config.+ {"prefixed key", "_github", systemProvider(app.GitHubProvider), true},+ }++ for _, tc := range testCases {+ if got := requiresSSRFGuard(tc.provider, tc.config); got != tc.guard {+ t.Errorf("%s: requiresSSRFGuard(%q) = %v, expected %v", tc.name, tc.provider, got, tc.guard)+ }+ }+}+func TestTokenExchangeContext(t *testing.T) {RegisterT(t)@@ -19,52 +58,38 @@ func TestTokenExchangeContext(t *testing.T) {t.Cleanup(func() { env.Config.AllowPrivateNetworkTargets = original })testCases := []struct {- provider string+ guard boolallowPrivate boolexpected *http.Client}{- // Custom providers use the guarded client...- {"_custom", false, netguard.Client},+ // Guarded exchanges use the guarded client...+ {true, 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},+ {true, true, http.DefaultClient},+ // Unguarded (built-in) exchanges leave the context untouched, so+ // oauth2 uses its default client.+ {false, false, nil},}for _, tc := range testCases {env.Config.AllowPrivateNetworkTargets = tc.allowPrivate- ctx := tokenExchangeContext(context.Background(), tc.provider)+ ctx := tokenExchangeContext(context.Background(), tc.guard)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)+ t.Errorf("tokenExchangeContext(guard=%v) with AllowPrivateNetworkTargets=%v returned unexpected client", tc.guard, tc.allowPrivate)}}}-func TestNewProfileRequest_BlocksPrivateNetworkTargetsForCustomProviders(t *testing.T) {+func TestNewProfileRequest(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")+ for _, guard := range []bool{true, false} {+ req := newProfileRequest(guard, "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)- }+ Expect(req.BlockPrivateNetworkTargets).Equals(guard)}}diff --git a/app/services/oauth/guard_test.go b/app/services/oauth/guard_test.goindex a39a3e951..356287c66 100644--- a/app/services/oauth/guard_test.go+++ b/app/services/oauth/guard_test.go@@ -6,7 +6,9 @@ import ("net/http/httptest""testing"+ "github.com/getfider/fider/app""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"@@ -15,45 +17,55 @@ import ("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) {+// End-to-end check that getOAuthRawProfile sends the profile request with the+// SSRF guard flag set whenever the resolved config is tenant-controlled.+// 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 runs).+func TestGetOAuthRawProfile_TenantConfigProfileRequestIsGuarded(t *testing.T) {RegisterT(t)original := env.Config.AllowPrivateNetworkTargetsenv.Config.AllowPrivateNetworkTargets = truet.Cleanup(func() { env.Config.AllowPrivateNetworkTargets = original })+ // Built-in GitHub disabled at instance level: getConfig falls through to+ // GetCustomOAuthConfigByProvider, i.e. a tenant row with provider='github'.+ oauth.SetSystemProviderStatus(t, app.GitHubProvider, enum.OAuthConfigDisabled)+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",+ for _, provider := range []string{"_custom", app.GitHubProvider} {+ httpclientmock.Reset()+ bus.Init(&oauth.Service{}, httpclientmock.Service{})+ bus.AddHandler(func(ctx context.Context, q *query.GetCustomOAuthConfigByProvider) error {+ q.Result = &entity.OAuthConfig{+ Provider: q.Provider,+ Status: enum.OAuthConfigEnabled,+ 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: provider, 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")+ if !req.BlockPrivateNetworkTargets {+ t.Errorf("profile request for %q is not guarded", provider)}- 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 c20ea2040..0325896f3 100644--- a/app/services/oauth/oauth.go+++ b/app/services/oauth/oauth.go@@ -336,7 +336,8 @@ func getOAuthRawProfile(ctx context.Context, q *query.GetOAuthRawProfile) errorRedirectURL: fmt.Sprintf("%s/oauth/%s/callback", oauthBaseURL, q.Provider),}).Exchange- oauthToken, err := exchange(tokenExchangeContext(ctx, q.Provider), q.Code)+ guard := requiresSSRFGuard(q.Provider, config)+ oauthToken, err := exchange(tokenExchangeContext(ctx, guard), q.Code)if err != nil {return err}@@ -357,7 +358,7 @@ func getOAuthRawProfile(ctx context.Context, q *query.GetOAuthRawProfile) errorreturn errors.New("Profile URL is not allowed: %s", strings.Join(msgs, "; "))}- req := newProfileRequest(q.Provider, config.ProfileURL, oauthToken.AccessToken)+ req := newProfileRequest(guard, config.ProfileURL, oauthToken.AccessToken)if err := bus.Dispatch(ctx, req); err != nil {return err@@ -401,7 +402,7 @@ func listAllOAuthProviders(ctx context.Context, q *query.ListAllOAuthProviders)oauthBaseURL := web.OAuthBaseURL(ctx)for _, p := range oauthProviders.Result {- isCustomProvider := string(p.Provider[0]) == "_"+ isCustomProvider := isCustomOAuthProvider(p.Provider)isEnabled := p.Status == enum.OAuthConfigEnabled// For built-in (non-custom) providers, check tenant-level override@@ -434,30 +435,49 @@ 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).+// requiresSSRFGuard reports whether outbound requests for this provider must+// use the dial-time SSRF guard. Only the built-in system provider configs (with+// their fixed, public Token/Profile URLs) are exempt. Anything else is guarded,+// including a tenant-stored config whose provider key has no "_" prefix (e.g.+// "github" when the built-in GitHub provider is not enabled and getConfig falls+// through to GetCustomOAuthConfigByProvider), so the check fails closed.+func requiresSSRFGuard(provider string, config *entity.OAuthConfig) bool {+ if isCustomOAuthProvider(provider) {+ return true+ }+ for _, p := range systemProviders {+ if p == config {+ return false+ }+ }+ return true+}-// 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) {+// tokenExchangeContext returns the context to pass to oauth2's Exchange.+//+// 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): when guard is true the context carries+// the guarded HTTP client (see netguard.ClientFor). Built-in providers use+// fixed public URLs and keep oauth2's default client, which honours+// HTTP(S)_PROXY.+func tokenExchangeContext(ctx context.Context, guard bool) context.Context {+ if !guard {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 {+// token exchange. When guard is true it blocks private network targets.+func newProfileRequest(guard bool, profileURL, accessToken string) *cmd.HTTPRequest {return &cmd.HTTPRequest{URL: profileURL,Method: "GET",Headers: map[string]string{"Authorization": "Bearer " + accessToken,},- BlockPrivateNetworkTargets: isCustomOAuthProvider(provider),+ BlockPrivateNetworkTargets: guard,}}diff --git a/app/services/webhook/webhook_test.go b/app/services/webhook/webhook_test.goindex ed79a4300..ca17d09af 100644--- a/app/services/webhook/webhook_test.go+++ b/app/services/webhook/webhook_test.go@@ -20,6 +20,7 @@ import (// DNS rebinding).func TestTriggerWebhooks_BlocksPrivateNetworkTargets(t *testing.T) {RegisterT(t)+ httpclientmock.Reset()bus.Init(webhook.Service{}, httpclientmock.Service{})bus.AddHandler(func(ctx context.Context, q *query.ListActiveWebhooksByType) error {