Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: only grant default admin role during bootstrap or zero-admin recovery, not to any account named arcane (#3504)
Kyle Mendellf07ceff983e9
diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.goindex 38e875e3b..4260c609f 100644--- a/backend/internal/bootstrap/bootstrap.go+++ b/backend/internal/bootstrap/bootstrap.go@@ -259,7 +259,16 @@ func initializeStartupState(p initializeStartupStateParams) {startup.InitializeNonAgentFeatures(appCtx, runtimeCfg,p.Role.EnsureBuiltInRoles,- p.User.CreateDefaultAdmin,+ func(ctx context.Context) error {+ // Backfill legacy users.roles first so CreateDefaultAdmin's+ // zero-global-admin recovery gate sees upgraded assignments.+ // No-op on modern schemas; runRoleStartupTasks repeats it+ // idempotently for agent-mode and error-retry coverage.+ if err := p.Role.BackfillLegacyRoleAssignments(ctx); err != nil {+ slog.WarnContext(ctx, "Failed to backfill legacy role assignments before admin bootstrap", "error", err)+ }+ return p.User.CreateDefaultAdmin(ctx)+ },func(ctx context.Context) error {return p.ApiKey.ReconcileDefaultAdminAPIKey(ctx, runtimeCfg.AdminStaticAPIKey)},diff --git a/backend/internal/services/api_key_service.go b/backend/internal/services/api_key_service.goindex 7c29c027d..a7a90dcbf 100644--- a/backend/internal/services/api_key_service.go+++ b/backend/internal/services/api_key_service.go@@ -347,6 +347,19 @@ func (s *ApiKeyService) getDefaultAdminUser(ctx context.Context) (*models.User,return nil, errors.WrapIf(err, "failed to load default admin user")}+ // The username is mutable and not proof of provenance — never mint the+ // managed full-permission key onto an account that isn't a global admin.+ if s.roleService != nil {+ perms, err := s.roleService.ResolvePermissions(ctx, adminUser)+ if err != nil {+ return nil, errors.WrapIf(err, "failed to resolve default admin permissions")+ }+ if !perms.IsGlobalAdmin() {+ slog.WarnContext(ctx, "User is not a global admin, skipping default admin API key reconciliation", "username", defaultAdminUsername)+ return nil, nil+ }+ }+return adminUser, nil}diff --git a/backend/internal/services/auth_service.go b/backend/internal/services/auth_service.goindex debfff60b..b5e09b7bb 100644--- a/backend/internal/services/auth_service.go+++ b/backend/internal/services/auth_service.go@@ -260,16 +260,6 @@ func (s *AuthService) AuthenticateLocalPrimary(ctx context.Context, username, pareturn nil, ErrInvalidCredentials}- if s.userService.NeedsPasswordUpgrade(user.PasswordHash) {- s.runInBackground(ctx, "upgrade_password_hash", func(ctx context.Context) error {- if err := s.userService.UpgradePasswordHash(ctx, user.ID, password); err != nil {- return errors.WrapIff(err, "failed to upgrade password hash for user %s", user.ID)- }- slog.InfoContext(ctx, "Successfully upgraded password hash from bcrypt to Argon2", "user", user.Username)- return nil- })- }-user.LastLogin = new(time.Now())userCopy := new(*user)s.runInBackground(ctx, "update_last_login", func(ctx context.Context) error {diff --git a/backend/internal/services/auth_service_test.go b/backend/internal/services/auth_service_test.goindex 68ab32fd2..579c44040 100644--- a/backend/internal/services/auth_service_test.go+++ b/backend/internal/services/auth_service_test.go@@ -569,7 +569,7 @@ func TestChangePassword_RevokesAllSessions(t *testing.T) {s.userService = userSvcs.sessionService = NewSessionService(db)- passwordHash, err := userSvc.hashPassword("old-password")+ passwordHash, err := userSvc.HashPassword("old-password")require.NoError(t, err)user := &models.User{BaseModel: models.BaseModel{ID: "u-password"},@@ -599,7 +599,7 @@ func TestChangePassword_KeepsCurrentSessionAlive(t *testing.T) {s.userService = userSvcs.sessionService = NewSessionService(db)- passwordHash, err := userSvc.hashPassword("old-password")+ passwordHash, err := userSvc.HashPassword("old-password")require.NoError(t, err)user := &models.User{BaseModel: models.BaseModel{ID: "u-keep"},diff --git a/backend/internal/services/user_service.go b/backend/internal/services/user_service.goindex 767e63de4..5f4b9b95a 100644--- a/backend/internal/services/user_service.go+++ b/backend/internal/services/user_service.go@@ -12,7 +12,6 @@ import ("emperror.dev/errors""golang.org/x/crypto/argon2"- "golang.org/x/crypto/bcrypt""gorm.io/gorm""gorm.io/gorm/clause"@@ -72,7 +71,7 @@ func (s *UserService) WithRoleService(roleService *RoleService) *UserService {return s}-func (s *UserService) hashPassword(password string) (string, error) {+func (s *UserService) HashPassword(password string) (string, error) {salt := make([]byte, s.argon2Params.saltLength)_, err := rand.Read(salt)if err != nil {@@ -90,20 +89,6 @@ func (s *UserService) hashPassword(password string) (string, error) {}func (s *UserService) ValidatePassword(encodedHash, password string) error {- // Check if it's a bcrypt hash (starts with $2a$, $2b$, or $2y$)- if strings.HasPrefix(encodedHash, "$2a$") || strings.HasPrefix(encodedHash, "$2b$") || strings.HasPrefix(encodedHash, "$2y$") {- return s.validateBcryptPassword(encodedHash, password)- }-- // Otherwise, assume it's Argon2- return s.validateArgon2Password(encodedHash, password)-}--func (s *UserService) validateBcryptPassword(hash, password string) error {- return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))-}--func (s *UserService) validateArgon2Password(encodedHash, password string) error {parts := strings.Split(encodedHash, "$")if len(parts) != 6 {return errors.New("invalid hash format")@@ -284,7 +269,7 @@ func (s *UserService) SetPassword(ctx context.Context, user *models.User, passworeturn nil, errors.New("user is nil")}- hashedPassword, err := s.hashPassword(password)+ hashedPassword, err := s.HashPassword(password)if err != nil {return nil, errors.WrapIf(err, "failed to hash password")}@@ -306,7 +291,7 @@ func (s *UserService) SetPasswordAndRevokeSessionsExcept(ctx context.Context, usreturn nil, ErrUserNotFound}- hashedPassword, err := s.hashPassword(password)+ hashedPassword, err := s.HashPassword(password)if err != nil {return nil, errors.WrapIf(err, "failed to hash password")}@@ -388,24 +373,42 @@ func (s *UserService) AttachOidcSubjectTransactional(ctx context.Context, userID}func (s *UserService) CreateDefaultAdmin(ctx context.Context) error {- // Hash password outside transaction to minimize lock time- hashedPassword, err := s.hashPassword("arcane-admin")- if err != nil {- return errors.WrapIf(err, "failed to hash default admin password")+ // If users already exist and at least one effective global admin remains,+ // there is nothing to bootstrap or recover — bail before touching the+ // `arcane` username at all. The username is mutable, so treating it as a+ // provenance marker would let anyone with users:create/users:update rename+ // an account to `arcane` and get promoted on the next restart.+ if s.roleService != nil {+ var count int64+ if err := s.db.WithContext(ctx).Model(&models.User{}).Count(&count).Error; err != nil {+ return errors.WrapIf(err, "failed to count users")+ }+ if count > 0 {+ admins, err := s.roleService.CountGlobalAdminsExcludingUser(ctx, "")+ if err != nil {+ return errors.WrapIf(err, "failed to count global admins")+ }+ if admins > 0 {+ return nil+ }+ }}// Step 1: ensure the default admin user row exists. If the users table is- // empty we create it; otherwise we find the existing `arcane` user (if any).- // Either way the role assignment is reconciled below — idempotently — so- // upgrades from older builds that didn't grant the role get patched up.+ // empty we create it; otherwise (zero global admins — a broken/ancient+ // upgrade) we find the existing `arcane` user to recover, if any.var adminUserID string- err = dbutil.WithTx(ctx, s.db.DB, func(tx *gorm.DB) error {+ err := dbutil.WithTx(ctx, s.db.DB, func(tx *gorm.DB) error {var count int64if err := tx.Model(&models.User{}).Count(&count).Error; err != nil {return errors.WrapIf(err, "failed to count users")}if count == 0 {+ hashedPassword, err := s.HashPassword("arcane-admin")+ if err != nil {+ return errors.WrapIf(err, "failed to hash default admin password")+ }email := "admin@localhost"displayName := "Arcane Admin"userModel := &models.User{@@ -426,8 +429,9 @@ func (s *UserService) CreateDefaultAdmin(ctx context.Context) error {return nil}- // Users already exist — see if `arcane` is one of them. If not,- // someone removed the default admin on purpose and we leave it alone.+ // Users exist but no global admin does (recovery only — the early+ // return above skips this path on healthy installs). See if `arcane`+ // is present; if not, leave the install alone.var existing models.Usererr := tx.Where("username = ?", "arcane").First(&existing).Errorif err != nil {@@ -446,12 +450,15 @@ func (s *UserService) CreateDefaultAdmin(ctx context.Context) error {if adminUserID == "" || s.roleService == nil {return nil}+ return s.grantDefaultAdminRoleInternal(ctx, adminUserID)+}- // Step 2: ensure the default admin holds the global Admin role assignment.- // Idempotent — if the assignment already exists, SetUserAssignments is a- // no-op write (it dedupes via the unique index). This recovers users that- // were created by older builds before the role-grant on bootstrap was- // wired up.+// grantDefaultAdminRoleInternal grants the global Admin role to the bootstrap+// user. Reached only on fresh bootstrap or zero-admin recovery.+// SetUserAssignments replaces manual-source rows, so carry forward only the+// existing manual assignments — copying OIDC-sourced rows here would duplicate+// them as manual and pin them forever.+func (s *UserService) grantDefaultAdminRoleInternal(ctx context.Context, adminUserID string) error {assignments, err := s.roleService.ListUserAssignments(ctx, adminUserID)if err != nil {return errors.WrapIf(err, "failed to list default admin assignments")@@ -461,16 +468,16 @@ func (s *UserService) CreateDefaultAdmin(ctx context.Context) error {return nil // already has it}}- desired := append([]models.UserRoleAssignment{}, assignments...)- desired = append(desired, models.UserRoleAssignment{+ manual := make([]models.UserRoleAssignment, 0, len(assignments)+1)+ for _, a := range assignments {+ if a.Source == models.RoleAssignmentSourceManual {+ manual = append(manual, models.UserRoleAssignment{RoleID: a.RoleID, EnvironmentID: a.EnvironmentID})+ }+ }+ manual = append(manual, models.UserRoleAssignment{RoleID: authz.BuiltInRoleAdmin,EnvironmentID: nil,})- // Strip the source field so SetUserAssignments classifies everything as manual.- manual := make([]models.UserRoleAssignment, 0, len(desired))- for _, a := range desired {- manual = append(manual, models.UserRoleAssignment{RoleID: a.RoleID, EnvironmentID: a.EnvironmentID})- }if err := s.roleService.SetUserAssignments(ctx, adminUserID, manual); err != nil {return errors.WrapIf(err, "failed to grant default admin global role")}@@ -513,30 +520,6 @@ func (s *UserService) DeleteUser(ctx context.Context, id string, actorPerms *aut})}-func (s *UserService) HashPassword(password string) (string, error) {- return s.hashPassword(password)-}--func (s *UserService) NeedsPasswordUpgrade(hash string) bool {- return strings.HasPrefix(hash, "$2a$") || strings.HasPrefix(hash, "$2b$") || strings.HasPrefix(hash, "$2y$")-}--func (s *UserService) UpgradePasswordHash(ctx context.Context, userID, password string) error {- newHash, err := s.hashPassword(password)- if err != nil {- return errors.WrapIf(err, "failed to create new hash")- }-- return dbutil.WithTx(ctx, s.db.DB, func(tx *gorm.DB) error {- if err := tx.Model(&models.User{}).- Where("id = ?", userID).- Update("password_hash", newHash).Error; err != nil {- return errors.WrapIf(err, "failed to update password hash")- }- return nil- })-}-func (s *UserService) ListUsersPaginated(ctx context.Context, params pagination.QueryParams) ([]user.User, pagination.Response, error) {var users []models.Userquery := s.db.WithContext(ctx).diff --git a/backend/internal/services/user_service_test.go b/backend/internal/services/user_service_test.goindex 3cdf7817d..9706f484b 100644--- a/backend/internal/services/user_service_test.go+++ b/backend/internal/services/user_service_test.go@@ -352,3 +352,33 @@ func TestDeleteUserAllowsGlobalAdminActorDeletingAdmin(t *testing.T) {actorCtx := context.WithValue(ctx, models.CurrentUserContextKey{}, other)require.NoError(t, userSvc.DeleteUser(actorCtx, admin.ID, otherPerms))}++func TestCreateDefaultAdminDoesNotPromoteRenamedArcaneUser(t *testing.T) {+ userSvc, roleSvc := setupUserAndRoleServices(t)+ ctx := context.Background()++ admin := createTestUser(t, userSvc, "admin-1", "boss")+ grantGlobalAdmin(t, roleSvc, admin.ID)+ imposter := createTestUser(t, userSvc, "user-2", "arcane")++ require.NoError(t, userSvc.CreateDefaultAdmin(ctx))++ assignments, err := roleSvc.ListUserAssignments(ctx, imposter.ID)+ require.NoError(t, err)+ require.Empty(t, assignments)+}++func TestCreateDefaultAdminRecoversArcaneUserWhenNoGlobalAdminExists(t *testing.T) {+ userSvc, roleSvc := setupUserAndRoleServices(t)+ ctx := context.Background()++ orphan := createTestUser(t, userSvc, "user-1", "arcane")++ require.NoError(t, userSvc.CreateDefaultAdmin(ctx))++ assignments, err := roleSvc.ListUserAssignments(ctx, orphan.ID)+ require.NoError(t, err)+ require.Len(t, assignments, 1)+ require.Equal(t, authz.BuiltInRoleAdmin, assignments[0].RoleID)+ require.Nil(t, assignments[0].EnvironmentID)+}