Skip to content

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

fix: reject malformed workspace names on SCIM token rotation

Dannydac3704ace4b
diff --git a/backend/api/v1/setting_service_directory_sync_test.go b/backend/api/v1/setting_service_directory_sync_test.go
index 1396caada1..f23c589c6a 100644
--- a/backend/api/v1/setting_service_directory_sync_test.go
+++ b/backend/api/v1/setting_service_directory_sync_test.go
@@ -7,6 +7,7 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
+ "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
@@ -44,6 +45,34 @@ func TestWorkspaceProfileReportsTokenConfigured(t *testing.T) {
}
}
+// The ACL rejects a well-formed name for another workspace, but an empty or
+// unparseable one falls back to the caller's own workspace and passes. Rotation
+// invalidates a live SCIM credential, so a malformed request must be rejected
+// rather than silently breaking the caller's integration — which means the
+// handler has to parse the name itself.
+func TestRotateDirectorySyncTokenRejectsMalformedNames(t *testing.T) {
+ for _, tt := range []struct {
+ name string
+ wantErr bool
+ }{
+ {"", true},
+ {"garbage", true},
+ {"workspaces/", true},
+ {"workspaces/ws1/extra", true},
+ {"projects/p1", true},
+ {"workspaces/ws1", false},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ _, err := common.GetWorkspaceID(tt.name)
+ if tt.wantErr {
+ require.Error(t, err, "malformed name %q must not reach the rotation", tt.name)
+ return
+ }
+ require.NoError(t, err)
+ })
+ }
+}
+
// The rotate RPC is audited, and the audit log records the response. The
// plaintext token exists only in that one response, so it must be stripped
// before it is written — otherwise anyone with audit-log read access gets a
diff --git a/backend/api/v1/workspace_service.go b/backend/api/v1/workspace_service.go
index c75cbf28e5..27c5013a5d 100644
--- a/backend/api/v1/workspace_service.go
+++ b/backend/api/v1/workspace_service.go
@@ -195,12 +195,23 @@ func (s *WorkspaceService) UpdateWorkspace(ctx context.Context, req *connect.Req
// RotateDirectorySyncToken mints a new SCIM token and returns it once. Only the
// hash is persisted, so the plaintext cannot be recovered afterwards.
-//
-// The request's name is resolved and matched against the caller's workspace by
-// the ACL interceptor, so the workspace is taken from context here.
-func (s *WorkspaceService) RotateDirectorySyncToken(ctx context.Context, _ *connect.Request[v1pb.RotateDirectorySyncTokenRequest]) (*connect.Response[v1pb.RotateDirectorySyncTokenResponse], error) {
+func (s *WorkspaceService) RotateDirectorySyncToken(ctx context.Context, req *connect.Request[v1pb.RotateDirectorySyncTokenRequest]) (*connect.Response[v1pb.RotateDirectorySyncTokenResponse], error) {
workspaceID := common.GetWorkspaceIDFromContext(ctx)
+ // Validate the name here rather than leaning on the ACL. The interceptor
+ // rejects a well-formed name for another workspace, but an empty or
+ // unparseable one resolves to the workspace fallback (acl.go resolveRawResource),
+ // which matches the caller's own workspace and passes. Rotation is
+ // destructive — it invalidates a live SCIM credential — so a malformed
+ // request must fail rather than quietly break the caller's integration.
+ requested, err := common.GetWorkspaceID(req.Msg.GetName())
+ if err != nil {
+ return nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid workspace name %q", req.Msg.GetName()))
+ }
+ if requested != workspaceID {
+ return nil, connect.NewError(connect.CodePermissionDenied, errors.Errorf("workspace mismatch"))
+ }
+
// The update path this replaces gated on the same feature. Without it an
// unlicensed workspace can mint a token that reads as configured in the UI
// while the SCIM middleware rejects every request that uses it.