Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: seat guard for undelete user (#20497)

ecmadaodc54e50eb4ee
diff --git a/backend/api/v1/user_service.go b/backend/api/v1/user_service.go
index f67d4c5b63..cb50a20909 100644
--- a/backend/api/v1/user_service.go
+++ b/backend/api/v1/user_service.go
@@ -632,7 +632,7 @@ func (s *UserService) UndeleteUser(ctx context.Context, request *connect.Request
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("user %q is already active", email))
}
- if err := s.preAddUserGuard(ctx, workspaceID); err != nil {
+ if err := s.preUndeleteUserGuard(ctx, workspaceID, user); err != nil {
return nil, err
}
@@ -938,7 +938,7 @@ func userCountGuard(ctx context.Context, s *store.Store, licenseService *enterpr
return nil
}
-// preAddUserGuard checks seat limits before creating or undeleting a principal.
+// preAddUserGuard checks seat limits before creating a principal.
// Only enforces when the IAM policy contains allUsers, because without allUsers
// a new principal does not occupy a seat until explicitly added to IAM.
func (s *UserService) preAddUserGuard(ctx context.Context, workspaceID string) error {
@@ -952,6 +952,22 @@ func (s *UserService) preAddUserGuard(ctx context.Context, workspaceID string) e
return userCountGuard(ctx, s.store, s.licenseService, workspaceID, p.Policy, s.profile.SaaS)
}
+// preUndeleteUserGuard checks seat limits before reactivating a principal.
+// A soft-deleted principal does not occupy a seat, so undeleting one that is
+// already referenced by workspace IAM (via allUsers, a direct binding, or a
+// group) re-occupies a seat and must respect the limit. When the principal is
+// not referenced, undeleting adds no seat and is always allowed.
+func (s *UserService) preUndeleteUserGuard(ctx context.Context, workspaceID string, user *store.UserMessage) error {
+ p, err := s.store.GetWorkspaceIamPolicy(ctx, workspaceID)
+ if err != nil {
+ return connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to get workspace IAM policy"))
+ }
+ if len(utils.GetUserIAMPolicyBindings(ctx, s.store, workspaceID, user, p.Policy)) == 0 {
+ return nil
+ }
+ return userCountGuard(ctx, s.store, s.licenseService, workspaceID, p.Policy, s.profile.SaaS)
+}
+
func policyContainsAllUsers(policy *storepb.IamPolicy) bool {
for _, binding := range policy.Bindings {
for _, member := range binding.Members {
diff --git a/backend/tests/user_test.go b/backend/tests/user_test.go
index 78085f5a7c..b3a1d78bb1 100644
--- a/backend/tests/user_test.go
+++ b/backend/tests/user_test.go
@@ -268,6 +268,88 @@ func TestSeatLimitAllowsServiceAccountWhenOverLimit(t *testing.T) {
a.ErrorContains(err, "exceeding the limit")
}
+// TestSeatLimitGuardsUndeleteOfBoundUser verifies that undeleting a user who is
+// still referenced by workspace IAM re-occupies a seat and is blocked when the
+// workspace is at the limit — closing the delete-bound-user, refill, undelete
+// loophole. Undelete is still allowed when a seat is free.
+func TestSeatLimitGuardsUndeleteOfBoundUser(t *testing.T) {
+ a := require.New(t)
+ ctx := context.Background()
+ ctl := &controller{}
+ ctx, err := ctl.StartServerWithExternalPg(ctx)
+ a.NoError(err)
+ defer ctl.Close(ctx)
+
+ const freeSeatLimit = 20
+
+ a.NoError(ctl.setLicense(ctx))
+
+ actuator, err := ctl.actuatorServiceClient.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))
+ a.NoError(err)
+ workspace := actuator.Msg.Workspace
+ baseline := int(actuator.Msg.UserCountInIam)
+
+ // Fill the workspace to exactly the FREE limit with explicit IAM members (no allUsers).
+ policyResp, err := ctl.workspaceServiceClient.GetIamPolicy(ctx, connect.NewRequest(&v1pb.GetIamPolicyRequest{Resource: workspace}))
+ a.NoError(err)
+ policy := policyResp.Msg
+ memberBinding := &v1pb.Binding{Role: "roles/workspaceMember"}
+ policy.Bindings = append(policy.Bindings, memberBinding)
+
+ var victim *v1pb.User
+ for i := baseline; i < freeSeatLimit; i++ {
+ resp, err := ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{
+ User: &v1pb.User{
+ Title: fmt.Sprintf("member-%d", i),
+ Email: fmt.Sprintf("member-%[email protected]", i),
+ Password: "1024bytebase",
+ },
+ }))
+ a.NoError(err)
+ memberBinding.Members = append(memberBinding.Members, fmt.Sprintf("user:%s", resp.Msg.Email))
+ if victim == nil {
+ victim = resp.Msg
+ }
+ }
+ _, err = ctl.workspaceServiceClient.SetIamPolicy(ctx, connect.NewRequest(&v1pb.SetIamPolicyRequest{
+ Etag: policy.Etag,
+ Policy: policy,
+ Resource: workspace,
+ }))
+ a.NoError(err)
+
+ // A filler user created up front, bound later to consume the seat freed by deletion.
+ fillerResp, err := ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{
+ User: &v1pb.User{Title: "filler", Email: "[email protected]", Password: "1024bytebase"},
+ }))
+ a.NoError(err)
+
+ // Drop the license: FREE limit (20) now applies; the workspace sits exactly at limit.
+ a.NoError(ctl.removeLicense(ctx))
+ atLimit, err := ctl.actuatorServiceClient.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))
+ a.NoError(err)
+ a.EqualValues(freeSeatLimit, atLimit.Msg.UserCountInIam)
+
+ // Deleting the bound victim frees a seat (its lingering binding no longer counts).
+ _, err = ctl.userServiceClient.DeleteUser(ctx, connect.NewRequest(&v1pb.DeleteUserRequest{Name: victim.Name}))
+ a.NoError(err)
+
+ // Undelete is allowed while a seat is free (still bound, re-occupies the freed seat).
+ _, err = ctl.userServiceClient.UndeleteUser(ctx, connect.NewRequest(&v1pb.UndeleteUserRequest{Name: victim.Name}))
+ a.NoError(err)
+
+ // Now exploit: free the seat again and refill it with the filler user.
+ _, err = ctl.userServiceClient.DeleteUser(ctx, connect.NewRequest(&v1pb.DeleteUserRequest{Name: victim.Name}))
+ a.NoError(err)
+ _, err = ctl.addMemberToWorkspaceIAM(ctx, workspace, fmt.Sprintf("user:%s", fillerResp.Msg.Email), "roles/workspaceMember")
+ a.NoError(err)
+
+ // Undeleting the still-bound victim would exceed the limit and must be rejected.
+ _, err = ctl.userServiceClient.UndeleteUser(ctx, connect.NewRequest(&v1pb.UndeleteUserRequest{Name: victim.Name}))
+ a.Error(err)
+ a.ErrorContains(err, "reaching the limit")
+}
+
func TestUpdateUserEmail(t *testing.T) {
a := require.New(t)
ctx := context.Background()