SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix(session): validate ref and callback state against global allowed_origins (#12172)
Vikrant Gupta253ca7dd7eb4
diff --git a/conf/example.yaml b/conf/example.yamlindex 06e5f28e9b..574bc44cad 100644--- a/conf/example.yaml+++ b/conf/example.yaml@@ -9,6 +9,11 @@ global:# the path component (e.g. /signoz in https://example.com/signoz) is used# as the base path for all HTTP routes (both API and web frontend).external_url: <unset>+ # origins (scheme://host[:port], no path) allowed as login redirect targets. include+ # the origin the signoz ui is served on. when not configured, redirect targets are+ # not validated.+ # allowed_origins:+ # - https://signoz.example.com# the url where the SigNoz backend receives telemetry data (traces, metrics, logs) from instrumented applications.ingestion_url: <unset># the url of the SigNoz MCP server. when unset, the MCP settings page is hidden in the frontend.diff --git a/pkg/global/config.go b/pkg/global/config.goindex 89580cd3ff..909abe96b6 100644--- a/pkg/global/config.go+++ b/pkg/global/config.go@@ -12,13 +12,15 @@ import (var (ErrCodeInvalidGlobalConfig = errors.MustNewCode("invalid_global_config")+ ErrCodeOriginNotAllowed = errors.MustNewCode("origin_not_allowed"))type Config struct {- ExternalURL *url.URL `mapstructure:"external_url"`- IngestionURL *url.URL `mapstructure:"ingestion_url"`- MCPURL *url.URL `mapstructure:"mcp_url"`- AIAssistantURL *url.URL `mapstructure:"ai_assistant_url"`+ ExternalURL *url.URL `mapstructure:"external_url"`+ AllowedOrigins []*url.URL `mapstructure:"allowed_origins"`+ IngestionURL *url.URL `mapstructure:"ingestion_url"`+ MCPURL *url.URL `mapstructure:"mcp_url"`+ AIAssistantURL *url.URL `mapstructure:"ai_assistant_url"`}func NewConfigFactory() factory.ConfigFactory {@@ -49,9 +51,33 @@ func (c Config) Validate() error {}}+ for _, origin := range c.AllowedOrigins {+ if origin == nil || origin.Scheme == "" || origin.Host == "" {+ return errors.NewInvalidInputf(ErrCodeInvalidGlobalConfig, "global::allowed_origins entries must be of the form scheme://host[:port], got %q", origin)+ }++ if origin.Path != "" && origin.Path != "/" {+ return errors.NewInvalidInputf(ErrCodeInvalidGlobalConfig, "global::allowed_origins entries must not contain a path, got %q", origin)+ }+ }+return nil}+func (c Config) IsOriginAllowed(u *url.URL) bool {+ if len(c.AllowedOrigins) == 0 {+ return true+ }++ for _, origin := range c.AllowedOrigins {+ if strings.EqualFold(origin.Scheme, u.Scheme) && strings.EqualFold(origin.Host, u.Host) {+ return true+ }+ }++ return false+}+func (c Config) ExternalPath() string {if c.ExternalURL == nil || c.ExternalURL.Path == "" || c.ExternalURL.Path == "/" {return ""diff --git a/pkg/global/config_test.go b/pkg/global/config_test.goindex 83bbef04cf..9f2ccb7c37 100644--- a/pkg/global/config_test.go+++ b/pkg/global/config_test.go@@ -123,6 +123,26 @@ func TestValidate(t *testing.T) {config: Config{ExternalURL: &url.URL{Path: "signoz"}},fail: true,},+ {+ name: "ValidAllowedOrigin",+ config: Config{AllowedOrigins: []*url.URL{{Scheme: "https", Host: "signoz.example.com"}}},+ fail: false,+ },+ {+ name: "AllowedOriginWithoutScheme",+ config: Config{AllowedOrigins: []*url.URL{{Host: "signoz.example.com"}}},+ fail: true,+ },+ {+ name: "AllowedOriginWithoutHost",+ config: Config{AllowedOrigins: []*url.URL{{Scheme: "https"}}},+ fail: true,+ },+ {+ name: "AllowedOriginWithPath",+ config: Config{AllowedOrigins: []*url.URL{{Scheme: "https", Host: "signoz.example.com", Path: "/login"}}},+ fail: true,+ },}for _, tc := range testCases {@@ -137,3 +157,96 @@ func TestValidate(t *testing.T) {})}}++func TestIsOriginAllowedWhenUnconfigured(t *testing.T) {+ testCases := []struct {+ name string+ config Config+ }{+ {+ name: "Empty",+ config: Config{},+ },+ {+ name: "ExternalURLDoesNotActivateValidation",+ config: Config{ExternalURL: &url.URL{Scheme: "https", Host: "signoz.example.com"}},+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ u, err := url.Parse("https://anything.example.com/login")+ assert.NoError(t, err)+ assert.True(t, tc.config.IsOriginAllowed(u))+ })+ }+}++func TestIsOriginAllowed(t *testing.T) {+ config := Config{+ AllowedOrigins: []*url.URL{+ {Scheme: "https", Host: "signoz.example.com"},+ {Scheme: "http", Host: "localhost:3301"},+ },+ }++ testCases := []struct {+ name string+ input string+ expected bool+ }{+ {+ name: "ConfiguredOrigin",+ input: "https://signoz.example.com/login",+ expected: true,+ },+ {+ name: "ConfiguredOriginWithQuery",+ input: "http://localhost:3301/login?next=/dashboards",+ expected: true,+ },+ {+ name: "CaseInsensitiveHost",+ input: "https://SigNoz.Example.Com/login",+ expected: true,+ },+ {+ name: "UnknownHost",+ input: "https://attacker.example.com/login",+ expected: false,+ },+ {+ name: "SchemeMismatch",+ input: "http://signoz.example.com/login",+ expected: false,+ },+ {+ name: "PortMismatch",+ input: "https://signoz.example.com:8443/login",+ expected: false,+ },+ {+ name: "SuffixConfusion",+ input: "https://evilsignoz.example.com/login",+ expected: false,+ },+ {+ name: "UserInfoConfusion",+ input: "https://[email protected]/login",+ expected: false,+ },+ {+ name: "SchemeRelative",+ input: "//attacker.example.com/login",+ expected: false,+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ u, err := url.Parse(tc.input)+ assert.NoError(t, err)+ assert.Equal(t, tc.expected, config.IsOriginAllowed(u))+ })+ }+}diff --git a/pkg/modules/session/implsession/module.go b/pkg/modules/session/implsession/module.goindex 386e753c04..9ee8f04f89 100644--- a/pkg/modules/session/implsession/module.go+++ b/pkg/modules/session/implsession/module.go@@ -12,6 +12,7 @@ import ("github.com/SigNoz/signoz/pkg/authz""github.com/SigNoz/signoz/pkg/errors""github.com/SigNoz/signoz/pkg/factory"+ "github.com/SigNoz/signoz/pkg/global""github.com/SigNoz/signoz/pkg/modules/authdomain""github.com/SigNoz/signoz/pkg/modules/organization""github.com/SigNoz/signoz/pkg/modules/session"@@ -23,26 +24,28 @@ import ()type module struct {- settings factory.ScopedProviderSettings- authNs map[authtypes.AuthNProvider]authn.AuthN- userSetter user.Setter- userGetter user.Getter- authDomain authdomain.Module- tokenizer tokenizer.Tokenizer- orgGetter organization.Getter- authz authz.AuthZ+ settings factory.ScopedProviderSettings+ authNs map[authtypes.AuthNProvider]authn.AuthN+ userSetter user.Setter+ userGetter user.Getter+ authDomain authdomain.Module+ tokenizer tokenizer.Tokenizer+ orgGetter organization.Getter+ authz authz.AuthZ+ globalConfig global.Config}-func NewModule(providerSettings factory.ProviderSettings, authNs map[authtypes.AuthNProvider]authn.AuthN, userSetter user.Setter, userGetter user.Getter, authDomain authdomain.Module, tokenizer tokenizer.Tokenizer, orgGetter organization.Getter, authz authz.AuthZ) session.Module {+func NewModule(providerSettings factory.ProviderSettings, authNs map[authtypes.AuthNProvider]authn.AuthN, userSetter user.Setter, userGetter user.Getter, authDomain authdomain.Module, tokenizer tokenizer.Tokenizer, orgGetter organization.Getter, authz authz.AuthZ, globalConfig global.Config) session.Module {return &module{- settings: factory.NewScopedProviderSettings(providerSettings, "github.com/SigNoz/signoz/pkg/modules/session/implsession"),- authNs: authNs,- userSetter: userSetter,- userGetter: userGetter,- authDomain: authDomain,- tokenizer: tokenizer,- orgGetter: orgGetter,- authz: authz,+ settings: factory.NewScopedProviderSettings(providerSettings, "github.com/SigNoz/signoz/pkg/modules/session/implsession"),+ authNs: authNs,+ userSetter: userSetter,+ userGetter: userGetter,+ authDomain: authDomain,+ tokenizer: tokenizer,+ orgGetter: orgGetter,+ authz: authz,+ globalConfig: globalConfig,}}@@ -140,6 +143,10 @@ func (module *module) CreateCallbackAuthNSession(ctx context.Context, authNProvireturn "", err}+ if callbackIdentity.State.URL.Host != "" && !module.globalConfig.IsOriginAllowed(callbackIdentity.State.URL) {+ return "", errors.Newf(errors.TypeForbidden, global.ErrCodeOriginNotAllowed, "state redirect %q is not an allowed origin", callbackIdentity.State.URL.String())+ }+authDomain, err := module.authDomain.GetByOrgIDAndID(ctx, callbackIdentity.OrgID, callbackIdentity.State.DomainID)if err != nil {return "", err@@ -217,6 +224,10 @@ func (module *module) getOrgSessionContext(ctx context.Context, org *types.Organreturn nil, err}+ if !module.globalConfig.IsOriginAllowed(siteURL) {+ return nil, errors.Newf(errors.TypeInvalidInput, global.ErrCodeOriginNotAllowed, "ref %q is not an allowed origin", siteURL.String())+ }+loginURL, err := provider.LoginURL(ctx, siteURL, authDomain)if err != nil {return nil, errdiff --git a/pkg/signoz/module.go b/pkg/signoz/module.goindex f937930868..8efd861936 100644--- a/pkg/signoz/module.go+++ b/pkg/signoz/module.go@@ -149,7 +149,7 @@ func NewModules(TraceFunnel: impltracefunnel.NewModule(impltracefunnel.NewStore(sqlstore)),RawDataExport: implrawdataexport.NewModule(querier),AuthDomain: authDomainModule,- Session: implsession.NewModule(providerSettings, authNs, userSetter, userGetter, authDomainModule, tokenizer, orgGetter, authz),+ Session: implsession.NewModule(providerSettings, authNs, userSetter, userGetter, authDomainModule, tokenizer, orgGetter, authz, config.Global),SpanPercentile: implspanpercentile.NewModule(querier, providerSettings),Services: implservices.NewModule(querier, telemetryStore),MetricsExplorer: implmetricsexplorer.NewModule(telemetryStore, telemetryMetadataStore, cache, ruleStore, dashboard, fl, providerSettings, config.MetricsExplorer),