Open platform to collect and prioritize feedback
Deactivate oauth role check by removing all allowed roles or the roles path no longer logs out users. Changing roles path with existing allowed roles will trigger a security stamp reroll.
Albert Nimtzef2d6f8ee8a5
diff --git a/app/services/sqlstore/postgres/oauth.go b/app/services/sqlstore/postgres/oauth.goindex ce02bc453..3b8ed11d0 100644--- a/app/services/sqlstore/postgres/oauth.go+++ b/app/services/sqlstore/postgres/oauth.go@@ -91,15 +91,20 @@ func saveCustomOAuthConfig(ctx context.Context, c *cmd.SaveCustomOAuthConfig) erc.Scope, c.JSONUserIDPath, c.JSONUserNamePath,c.JSONUserEmailPath, c.JSONUserRolesPath, c.AllowedRoles, c.Logo.BlobKey)} else {- // Detect if allowed_roles is being changed. If so, we must rotate- // security stamps for all other users so that currently-logged-in users- // are forced to re-authenticate and have their OAuth roles re-evaluated.- // The user making this change is excluded so their own session is not- // invalidated.+ // Detect if role-related fields are being changed. If the new configuration+ // is active (both allowed_roles and json_user_roles_path are non-empty) and+ // either field changed, we must rotate security stamps for all other users so+ // that currently-logged-in users are forced to re-authenticate and have their+ // OAuth roles re-evaluated. The user making this change is excluded so their+ // own session is not invalidated.var prevAllowedRoles string_ = trx.Scalar(&prevAllowedRoles,"SELECT COALESCE(allowed_roles, '') FROM oauth_providers WHERE tenant_id = $1 AND id = $2",tenant.ID, c.ID)+ var prevJSONUserRolesPath string+ _ = trx.Scalar(&prevJSONUserRolesPath,+ "SELECT COALESCE(json_user_roles_path, '') FROM oauth_providers WHERE tenant_id = $1 AND id = $2",+ tenant.ID, c.ID)query := `UPDATE oauth_providers@@ -115,24 +120,28 @@ func saveCustomOAuthConfig(ctx context.Context, c *cmd.SaveCustomOAuthConfig) erc.Scope, c.JSONUserIDPath, c.JSONUserNamePath,c.JSONUserEmailPath, c.JSONUserRolesPath, c.AllowedRoles, c.Logo.BlobKey, c.IsTrusted)- if err == nil && prevAllowedRoles != c.AllowedRoles {- // Rotate stamps inside the same transaction so the change is atomic.- // Exclude the current user so they are not forced to re-authenticate- // after making the change — their session should remain valid.- var stampErr error- if user != nil {- _, stampErr = trx.Execute(- "UPDATE users SET security_stamp = md5(random()::text || id::text) WHERE tenant_id = $1 AND id != $2",- tenant.ID, user.ID,- )- } else {- _, stampErr = trx.Execute(- "UPDATE users SET security_stamp = md5(random()::text || id::text) WHERE tenant_id = $1",- tenant.ID,- )- }- if stampErr != nil {- return errors.Wrap(stampErr, "failed to rotate security stamps after allowed_roles change")+ if err == nil && (prevAllowedRoles != c.AllowedRoles || prevJSONUserRolesPath != c.JSONUserRolesPath) {+ // Only rotate security stamps if the new configuration still enforces+ // role-based access control. Both allowed_roles and json_user_roles_path+ // must be non-empty for restrictions to be active. If either is empty,+ // access control is effectively deactivated and all users can log in,+ // so forcing re-authentication is unnecessary.+ if c.AllowedRoles != "" && c.JSONUserRolesPath != "" {+ var stampErr error+ if user != nil {+ _, stampErr = trx.Execute(+ "UPDATE users SET security_stamp = md5(random()::text || id::text) WHERE tenant_id = $1 AND id != $2",+ tenant.ID, user.ID,+ )+ } else {+ _, stampErr = trx.Execute(+ "UPDATE users SET security_stamp = md5(random()::text || id::text) WHERE tenant_id = $1",+ tenant.ID,+ )+ }+ if stampErr != nil {+ return errors.Wrap(stampErr, "failed to rotate security stamps after allowed_roles change")+ }}}}diff --git a/app/services/sqlstore/postgres/oauth_test.go b/app/services/sqlstore/postgres/oauth_test.gonew file mode 100644index 000000000..1ec678763--- /dev/null+++ b/app/services/sqlstore/postgres/oauth_test.go@@ -0,0 +1,253 @@+package postgres_test++import (+ "testing"++ "github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/models/dto"+ "github.com/getfider/fider/app/pkg/bus"+ . "github.com/getfider/fider/app/pkg/assert"+)++// stampRow is used to read security_stamp directly from the DB.+type stampRow struct {+ ID int `db:"id"`+ SecurityStamp string `db:"security_stamp"`+}++// getSecurityStamps returns a map of userID → security_stamp for all users in the tenant.+func getSecurityStamps(tenantID int) map[int]string {+ var rows []*stampRow+ _ = trx.Select(&rows, "SELECT id, security_stamp FROM users WHERE tenant_id = $1", tenantID)+ result := make(map[int]string)+ for _, r := range rows {+ result[r.ID] = r.SecurityStamp+ }+ return result+}++// newOAuthConfig builds a minimal SaveCustomOAuthConfig suitable for INSERT (ID = 0).+// Callers set JSONUserRolesPath and AllowedRoles to control whether role access is active.+func newOAuthConfig(rolesPath, allowedRoles string) *cmd.SaveCustomOAuthConfig {+ return &cmd.SaveCustomOAuthConfig{+ Logo: &dto.ImageUpload{},+ Provider: "_TEST_ROLES",+ DisplayName: "Test Roles Provider",+ ClientID: "client-id",+ ClientSecret: "client-secret",+ AuthorizeURL: "http://provider/authorize",+ TokenURL: "http://provider/token",+ ProfileURL: "http://provider/profile",+ JSONUserIDPath: "id",+ JSONUserNamePath: "name",+ JSONUserEmailPath: "email",+ JSONUserRolesPath: rolesPath,+ AllowedRoles: allowedRoles,+ }+}++// TestOAuthConfig_SecurityStamp_ActiveToActive checks that when allowed_roles is+// changed between two non-empty values (and json_user_roles_path is set), security+// stamps are rotated for all users except the one making the change.+func TestOAuthConfig_SecurityStamp_ActiveToActive(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ // Insert initial active config (AllowedRoles="admin", JSONUserRolesPath="roles")+ insertCmd := newOAuthConfig("roles", "admin")+ err := bus.Dispatch(jonSnowCtx, insertCmd)+ Expect(err).IsNil()++ stampsBefore := getSecurityStamps(demoTenant.ID)++ // Update: change AllowedRoles to a different non-empty value — still active.+ updateCmd := newOAuthConfig("roles", "superadmin")+ updateCmd.ID = insertCmd.ID+ err = bus.Dispatch(jonSnowCtx, updateCmd)+ Expect(err).IsNil()++ stampsAfter := getSecurityStamps(demoTenant.ID)++ // Other users' stamps must have been rotated.+ Expect(stampsAfter[aryaStark.ID]).NotEquals(stampsBefore[aryaStark.ID])+ Expect(stampsAfter[sansaStark.ID]).NotEquals(stampsBefore[sansaStark.ID])++ // The acting user's stamp must NOT have changed.+ Expect(stampsAfter[jonSnow.ID]).Equals(stampsBefore[jonSnow.ID])+}++// TestOAuthConfig_SecurityStamp_InactiveToActive checks that when a provider+// transitions from inactive (empty AllowedRoles) to active, security stamps are+// rotated for all users except the one making the change.+func TestOAuthConfig_SecurityStamp_InactiveToActive(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ // Insert initial inactive config: JSONUserRolesPath set but AllowedRoles empty.+ insertCmd := newOAuthConfig("roles", "")+ err := bus.Dispatch(jonSnowCtx, insertCmd)+ Expect(err).IsNil()++ stampsBefore := getSecurityStamps(demoTenant.ID)++ // Update: add AllowedRoles — transitions to active.+ updateCmd := newOAuthConfig("roles", "admin")+ updateCmd.ID = insertCmd.ID+ err = bus.Dispatch(jonSnowCtx, updateCmd)+ Expect(err).IsNil()++ stampsAfter := getSecurityStamps(demoTenant.ID)++ // Other users' stamps must have been rotated.+ Expect(stampsAfter[aryaStark.ID]).NotEquals(stampsBefore[aryaStark.ID])+ Expect(stampsAfter[sansaStark.ID]).NotEquals(stampsBefore[sansaStark.ID])++ // The acting user's stamp must NOT have changed.+ Expect(stampsAfter[jonSnow.ID]).Equals(stampsBefore[jonSnow.ID])+}++// TestOAuthConfig_SecurityStamp_ActiveToInactive_ClearAllowedRoles checks that+// clearing AllowedRoles (deactivating role access) does NOT rotate security stamps,+// because all users are allowed to log in when restrictions are lifted.+func TestOAuthConfig_SecurityStamp_ActiveToInactive_ClearAllowedRoles(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ // Insert initial active config.+ insertCmd := newOAuthConfig("roles", "admin")+ err := bus.Dispatch(jonSnowCtx, insertCmd)+ Expect(err).IsNil()++ stampsBefore := getSecurityStamps(demoTenant.ID)++ // Update: clear AllowedRoles — transitions to inactive (JSONUserRolesPath still set).+ updateCmd := newOAuthConfig("roles", "")+ updateCmd.ID = insertCmd.ID+ err = bus.Dispatch(jonSnowCtx, updateCmd)+ Expect(err).IsNil()++ stampsAfter := getSecurityStamps(demoTenant.ID)++ // No stamps should have changed.+ Expect(stampsAfter[jonSnow.ID]).Equals(stampsBefore[jonSnow.ID])+ Expect(stampsAfter[aryaStark.ID]).Equals(stampsBefore[aryaStark.ID])+ Expect(stampsAfter[sansaStark.ID]).Equals(stampsBefore[sansaStark.ID])+}++// TestOAuthConfig_SecurityStamp_ActiveToInactive_ClearRolesPath checks that+// when json_user_roles_path is cleared (making role evaluation impossible) while+// AllowedRoles also changes, security stamps are NOT rotated.+func TestOAuthConfig_SecurityStamp_ActiveToInactive_ClearRolesPath(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ // Insert initial active config.+ insertCmd := newOAuthConfig("roles", "admin")+ err := bus.Dispatch(jonSnowCtx, insertCmd)+ Expect(err).IsNil()++ stampsBefore := getSecurityStamps(demoTenant.ID)++ // Update: clear JSONUserRolesPath and change AllowedRoles — inactive because no path.+ updateCmd := newOAuthConfig("", "superadmin")+ updateCmd.ID = insertCmd.ID+ err = bus.Dispatch(jonSnowCtx, updateCmd)+ Expect(err).IsNil()++ stampsAfter := getSecurityStamps(demoTenant.ID)++ // No stamps should have changed.+ Expect(stampsAfter[jonSnow.ID]).Equals(stampsBefore[jonSnow.ID])+ Expect(stampsAfter[aryaStark.ID]).Equals(stampsBefore[aryaStark.ID])+ Expect(stampsAfter[sansaStark.ID]).Equals(stampsBefore[sansaStark.ID])+}++// TestOAuthConfig_SecurityStamp_ActiveToInactive_ClearBoth checks that clearing+// both AllowedRoles and json_user_roles_path does NOT rotate security stamps.+func TestOAuthConfig_SecurityStamp_ActiveToInactive_ClearBoth(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ // Insert initial active config.+ insertCmd := newOAuthConfig("roles", "admin")+ err := bus.Dispatch(jonSnowCtx, insertCmd)+ Expect(err).IsNil()++ stampsBefore := getSecurityStamps(demoTenant.ID)++ // Update: clear both fields — fully inactive.+ updateCmd := newOAuthConfig("", "")+ updateCmd.ID = insertCmd.ID+ err = bus.Dispatch(jonSnowCtx, updateCmd)+ Expect(err).IsNil()++ stampsAfter := getSecurityStamps(demoTenant.ID)++ // No stamps should have changed.+ Expect(stampsAfter[jonSnow.ID]).Equals(stampsBefore[jonSnow.ID])+ Expect(stampsAfter[aryaStark.ID]).Equals(stampsBefore[aryaStark.ID])+ Expect(stampsAfter[sansaStark.ID]).Equals(stampsBefore[sansaStark.ID])+}++// TestOAuthConfig_SecurityStamp_InactiveToActive_ViaRolesPath checks that when+// AllowedRoles is already set but json_user_roles_path is empty (so access control+// is inactive), adding json_user_roles_path (without changing AllowedRoles) DOES+// rotate security stamps, because the provider just became active.+func TestOAuthConfig_SecurityStamp_InactiveToActive_ViaRolesPath(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ // Insert initial inactive config: AllowedRoles set but JSONUserRolesPath empty.+ insertCmd := newOAuthConfig("", "admin")+ err := bus.Dispatch(jonSnowCtx, insertCmd)+ Expect(err).IsNil()++ stampsBefore := getSecurityStamps(demoTenant.ID)++ // Update: add JSONUserRolesPath only — AllowedRoles stays "admin", transitions to active.+ updateCmd := newOAuthConfig("roles", "admin")+ updateCmd.ID = insertCmd.ID+ err = bus.Dispatch(jonSnowCtx, updateCmd)+ Expect(err).IsNil()++ stampsAfter := getSecurityStamps(demoTenant.ID)++ // Other users' stamps must have been rotated.+ Expect(stampsAfter[aryaStark.ID]).NotEquals(stampsBefore[aryaStark.ID])+ Expect(stampsAfter[sansaStark.ID]).NotEquals(stampsBefore[sansaStark.ID])++ // The acting user's stamp must NOT have changed.+ Expect(stampsAfter[jonSnow.ID]).Equals(stampsBefore[jonSnow.ID])+}++// TestOAuthConfig_SecurityStamp_InactiveToInactive checks that updating a provider+// that has role access disabled (no AllowedRoles, no JSONUserRolesPath) without+// enabling it does NOT rotate security stamps.+func TestOAuthConfig_SecurityStamp_InactiveToInactive(t *testing.T) {+ SetupDatabaseTest(t)+ defer TeardownDatabaseTest()++ // Insert initial inactive config — no role fields set at all.+ insertCmd := newOAuthConfig("", "")+ err := bus.Dispatch(jonSnowCtx, insertCmd)+ Expect(err).IsNil()++ stampsBefore := getSecurityStamps(demoTenant.ID)++ // Update: change an unrelated field — role access remains inactive.+ updateCmd := newOAuthConfig("", "")+ updateCmd.ID = insertCmd.ID+ updateCmd.DisplayName = "Updated Display Name"+ err = bus.Dispatch(jonSnowCtx, updateCmd)+ Expect(err).IsNil()++ stampsAfter := getSecurityStamps(demoTenant.ID)++ // No stamps should have changed.+ Expect(stampsAfter[jonSnow.ID]).Equals(stampsBefore[jonSnow.ID])+ Expect(stampsAfter[aryaStark.ID]).Equals(stampsBefore[aryaStark.ID])+ Expect(stampsAfter[sansaStark.ID]).Equals(stampsBefore[sansaStark.ID])+}+++