Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(sql): require bb.sql.explain even under an access grant (#21619)
Danny Xue19a19c5e2f9
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 84415b9c1b..a0bcc99855 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -1518,7 +1518,8 @@ func (s *SQLService) accessCheck(// every other source database. This preserves JIT-grant behavior for the// grant's target (the user is already authorized there) but blocks cross-// database exfiltration via cross-DB references like `SELECT * FROM db_b.t`-// from a grant targeting `db_a` — see PR #20487 review.+// from a grant targeting `db_a` — see PR #20487 review. The exemption is for+// data access only; see checkDatabaseAccess for why explain stays with IAM.//// Pass `accessGrant == nil` to skip wrapping; callers in that case should// reference `s.accessCheck` directly.@@ -1567,7 +1568,12 @@ func (s *SQLService) accessCheckWithGrantedTargets(checkDatabaseAccess := func(perm permission.Permission) error {databaseFullName := formatDatabaseResourceName(instance, database)- if _, granted := grantedTargets[databaseFullName]; granted {+ // A grant approves reading one statement's data on its target databases, so+ // the holder needs no IAM role there for that statement. Reading the+ // statement's plan is a separate capability: the approved statement can+ // arrive with the explain flag, or be a typed EXPLAIN, and either way+ // bb.sql.explain still comes from IAM.+ if _, granted := grantedTargets[databaseFullName]; granted && perm != permission.SQLExplain {return nil}attributes := map[string]any{diff --git a/backend/api/v1/sql_service_access_grant_test.go b/backend/api/v1/sql_service_access_grant_test.goindex d79ff770ab..f1d008fd53 100644--- a/backend/api/v1/sql_service_access_grant_test.go+++ b/backend/api/v1/sql_service_access_grant_test.go@@ -6,10 +6,13 @@ import ("testing""time"+ "connectrpc.com/connect""github.com/stretchr/testify/require""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/permission""github.com/bytebase/bytebase/backend/common/testcontainer"+ "github.com/bytebase/bytebase/backend/component/iam"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store")@@ -80,3 +83,53 @@ func TestPreCheckAccessQueryHash(t *testing.T) {})}}++// TestAccessGrantDoesNotExemptExplain pins the boundary between data access and+// plan access: a grant on the database still lets its holder select there without+// a role, but explaining there needs bb.sql.explain from IAM.+func TestAccessGrantDoesNotExemptExplain(t *testing.T) {+ t.Parallel()+ ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")+ db, stores, _ := testcontainer.NewMetadataDB(t)+ _, err := db.ExecContext(ctx, `+ INSERT INTO workspace (resource_id) VALUES ('default');+ INSERT INTO principal (name, email, password_hash) VALUES ('selector', '[email protected]', 'unused'), ('explainer', '[email protected]', 'unused');+ INSERT INTO project (resource_id, workspace, name) VALUES ('grant-project', 'default', 'Grant Project');`)+ require.NoError(t, err)+ for _, role := range []struct {+ id string+ perm permission.Permission+ user string+ }{+ {id: "selector", perm: permission.SQLSelect, user: "[email protected]"},+ {id: "explainer", perm: permission.SQLExplain, user: "[email protected]"},+ } {+ _, err = stores.CreateRole(ctx, &store.RoleMessage{+ ResourceID: role.id, Workspace: "default", Name: role.id,+ Permissions: map[permission.Permission]bool{role.perm: true},+ })+ require.NoError(t, err)+ _, err = stores.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{+ Workspace: "default", Member: common.FormatUserEmail(role.user), Roles: []string{"roles/" + role.id},+ })+ require.NoError(t, err)+ }+ iamManager, err := iam.NewManager(stores, nil, false)+ require.NoError(t, err)+ service := &SQLService{store: stores, iamManager: iamManager}+ instance := &store.InstanceMessage{ResourceID: "test", Metadata: &storepb.Instance{Engine: storepb.Engine_POSTGRES}}+ database := &store.DatabaseMessage{ProjectID: "grant-project", InstanceID: "test", DatabaseName: "db"}+ granted := map[string]struct{}{"instances/test/databases/db": {}}+ check := func(email string, isExplain bool) error {+ user := &store.UserMessage{Email: email}+ return service.accessCheckWithGrantedTargets(ctx, instance, database, user, nil, isExplain, nil, granted, "", false)+ }++ require.NoError(t, check("[email protected]", false), "the grant covers select")+ require.NoError(t, check("[email protected]", false), "the grant covers select without any role")+ require.NoError(t, check("[email protected]", true), "explain is checked against IAM")+ var denied *queryError+ require.ErrorAs(t, check("[email protected]", true), &denied, "the grant does not cover explain")+ require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(denied.err))+ require.Equal(t, permission.SQLExplain, denied.permission)+}