Skip to content

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

fix(sql): export SQL Server queries that set the isolation level first (#21702)

Vincent Huangeb081fd5f2e9
diff --git a/backend/api/v1/access_grant_service_test.go b/backend/api/v1/access_grant_service_test.go
index b79e5aa3b5..4326a96df4 100644
--- a/backend/api/v1/access_grant_service_test.go
+++ b/backend/api/v1/access_grant_service_test.go
@@ -135,6 +135,28 @@ func TestIsReadOnlyStatementForAccessGrantAllowsDocumentEngineReadStatements(t *
}
}
+func TestIsReadOnlyStatementForAccessGrantOnSQLServer(t *testing.T) {
+ t.Parallel()
+ tests := []struct {
+ statement string
+ readOnly bool
+ }{
+ {statement: "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED\nSELECT id FROM t", readOnly: true},
+ {statement: "SET NOCOUNT ON; SELECT id FROM t", readOnly: true},
+ {statement: "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED\nDELETE FROM t"},
+ {statement: "DECLARE @n int = 1; SELECT @n"},
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.statement, func(t *testing.T) {
+ t.Parallel()
+ readOnly, err := isReadOnlyStatementForAccessGrant(context.Background(), storepb.Engine_MSSQL, tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.readOnly, readOnly)
+ })
+ }
+}
+
func TestIsReadOnlyStatementForAccessGrantRejectsDocumentEngineInvalidStatements(t *testing.T) {
t.Parallel()
readOnly, err := isReadOnlyStatementForAccessGrant(context.Background(), storepb.Engine_ELASTICSEARCH, `db.users.find({})`)
diff --git a/backend/api/v1/mcp_sql_clamp_test.go b/backend/api/v1/mcp_sql_clamp_test.go
index e5c6cf21c7..391bbdc4fe 100644
--- a/backend/api/v1/mcp_sql_clamp_test.go
+++ b/backend/api/v1/mcp_sql_clamp_test.go
@@ -167,6 +167,13 @@ func TestMCPClampRefusesWhatItCannotShowIsARead(t *testing.T) {
refused: true,
reason: "rewrites the session it runs on",
},
+ {
+ name: "a SQL Server session option ahead of a read is refused",
+ engine: storepb.Engine_MSSQL,
+ statement: "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT 1",
+ refused: true,
+ reason: "statement 1 of 2 returns no data",
+ },
{
// A single well-formed statement the server executes as a
// server-side file write. The MySQL family has no read-only
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 21b32af050..04ea398ce5 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1239,7 +1239,7 @@ func (s *SQLService) Export(ctx context.Context, req *connect.Request[v1pb.Expor
// New query ACL experience.
if instance.Metadata.GetEngine() != storepb.Engine_MYSQL {
if err := validateQueryRequest(instance, statement); err != nil {
- return nil, err
+ return nil, exportConnectError(err)
}
}
@@ -1279,8 +1279,9 @@ func (s *SQLService) Export(ctx context.Context, req *connect.Request[v1pb.Expor
}
// exportConnectError returns the connect error Export answers with. A refusal
-// from the access check arrives as a queryError, which errors.As cannot see
-// through, so its own connect error (such as PERMISSION_DENIED) is returned.
+// from the read-only or access check arrives as a queryError, which errors.As
+// cannot see through, so its own connect error (such as PERMISSION_DENIED) is
+// returned.
func exportConnectError(err error) *connect.Error {
var connectErr *connect.Error
var qe *queryError
@@ -1384,7 +1385,7 @@ func doExport(
}
if err := exportResultToZip(ctx, zipw, stores, instance, database, result, request, i+1); err != nil {
- return nil, duration, errors.Errorf("failed to export result to zip with error: %v", result.GetError())
+ return nil, duration, errors.Wrap(err, "failed to export result to zip")
}
exportCount++
diff --git a/backend/api/v1/sql_service_export_permission_test.go b/backend/api/v1/sql_service_export_permission_test.go
index 69e5fcae81..206602a07a 100644
--- a/backend/api/v1/sql_service_export_permission_test.go
+++ b/backend/api/v1/sql_service_export_permission_test.go
@@ -14,6 +14,7 @@ import (
"github.com/bytebase/bytebase/backend/common/testcontainer"
"github.com/bytebase/bytebase/backend/component/iam"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store"
)
@@ -193,3 +194,27 @@ func TestExportRefusalIsPermissionDenied(t *testing.T) {
require.Equal(t, connect.CodeNotFound, exportConnectError(connect.NewError(connect.CodeNotFound, errors.New("missing"))).Code())
require.Equal(t, connect.CodeInternal, exportConnectError(errors.New("driver failed")).Code())
}
+
+func TestExportRefusesNonReadOnlyStatementWithInvalidArgument(t *testing.T) {
+ t.Parallel()
+ ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")
+ f := newExportAccessFixture(ctx, t)
+ _, err := f.service.store.CreateInstance(ctx, &store.InstanceMessage{
+ ResourceID: "mssql", Workspace: "default",
+ Metadata: &storepb.Instance{Engine: storepb.Engine_MSSQL, Activation: true, DataSources: []*storepb.DataSource{{Id: "admin", Type: storepb.DataSourceType_ADMIN}}},
+ })
+ require.NoError(t, err)
+ _, err = f.service.store.UpsertDatabase(ctx, &store.DatabaseMessage{
+ InstanceID: "mssql", DatabaseName: "db", ProjectID: "project-a", Metadata: &storepb.DatabaseMetadata{},
+ })
+ require.NoError(t, err)
+
+ ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{Email: "[email protected]"})
+ _, err = f.service.Export(ctx, connect.NewRequest(&v1pb.ExportRequest{
+ Name: "instances/mssql/databases/db",
+ Statement: "DECLARE @n int = 1; SELECT @n",
+ Format: v1pb.ExportFormat_CSV,
+ }))
+ require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))
+ require.ErrorContains(t, err, "Support read-only command statements only")
+}
diff --git a/backend/api/v1/sql_service_test.go b/backend/api/v1/sql_service_test.go
index 0bb75e3e05..e9b3da477b 100644
--- a/backend/api/v1/sql_service_test.go
+++ b/backend/api/v1/sql_service_test.go
@@ -77,6 +77,14 @@ func TestExportRejectsRetiredRolloutNames(t *testing.T) {
}
}
+func TestRequiresAdminDataSourceForSQLServer(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+ require.False(t, requiresAdminDataSource(ctx, storepb.Engine_MSSQL, "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED\nSELECT id FROM t"))
+ require.True(t, requiresAdminDataSource(ctx, storepb.Engine_MSSQL, "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED\nDELETE FROM t"))
+ require.True(t, requiresAdminDataSource(ctx, storepb.Engine_MSSQL, "DECLARE @n int = 1; SELECT @n"))
+}
+
// TestSelectBestAccessGrantPrefersUnmask covers the Unmask-only ranking:
// Export plays no role (Export callers already filtered the pool via the
// `requireExport` CEL filter, and Query never reads `Payload.Export`).
diff --git a/backend/plugin/parser/tsql/query.go b/backend/plugin/parser/tsql/query.go
index c8efd44b99..629d01823b 100644
--- a/backend/plugin/parser/tsql/query.go
+++ b/backend/plugin/parser/tsql/query.go
@@ -12,8 +12,9 @@ func init() {
}
// ValidateSQLForEditor validates that every statement in the SQL is a read-only
-// SELECT (no SELECT INTO). Returns (valid, allAlike, err). For MSSQL the two
-// booleans move together — we reject on the first non-SELECT.
+// SELECT (no SELECT INTO) or sets read-only session options. Returns (valid,
+// allQuery, err); a session option returns no data, so it clears allQuery.
+// DECLARE and SET @variable are refused (BOT-166).
func ValidateSQLForEditor(statement string) (bool, bool, error) {
stmts, err := ParseTSQL(statement)
if err != nil {
@@ -23,15 +24,42 @@ func ValidateSQLForEditor(statement string) (bool, bool, error) {
return false, false, nil
}
+ allQuery := true
for _, s := range stmts {
if s.Empty() {
continue
}
- if !isReadOnlySelect(s.AST) {
+ if isReadOnlySelect(s.AST) {
+ continue
+ }
+ if !isReadOnlySessionOption(s.AST) {
return false, false, nil
}
+ allQuery = false
+ }
+ return true, allQuery, nil
+}
+
+// readOnlySessionOptions are the SET options a read-only request may carry: they
+// write nothing and add no result set, unlike SET STATISTICS XML.
+var readOnlySessionOptions = map[string]bool{
+ "TRANSACTION ISOLATION LEVEL": true,
+ "NOCOUNT": true,
+}
+
+// isReadOnlySessionOption reports whether node is a SET whose options are all
+// read-only session options. One SET can name several, as in SET NOCOUNT, FMTONLY ON.
+func isReadOnlySessionOption(node ast.Node) bool {
+ set, ok := node.(*ast.SetOptionStmt)
+ if !ok {
+ return false
+ }
+ for _, option := range set.Options {
+ if !readOnlySessionOptions[option] {
+ return false
+ }
}
- return true, true, nil
+ return true
}
// isReadOnlySelect returns true for SELECT statements without an INTO target.
diff --git a/backend/plugin/parser/tsql/query_test.go b/backend/plugin/parser/tsql/query_test.go
new file mode 100644
index 0000000000..31c17e0566
--- /dev/null
+++ b/backend/plugin/parser/tsql/query_test.go
@@ -0,0 +1,101 @@
+package tsql
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestValidateSQLForEditor(t *testing.T) {
+ tests := []struct {
+ name string
+ statement string
+ valid bool
+ allQuery bool
+ }{
+ {
+ name: "select",
+ statement: "SELECT id FROM t",
+ valid: true,
+ allQuery: true,
+ },
+ {
+ name: "isolation level before a select",
+ statement: "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED\nSELECT id FROM t",
+ valid: true,
+ },
+ {
+ name: "isolation level with terminators",
+ statement: "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SELECT id FROM t;",
+ valid: true,
+ },
+ {
+ name: "every isolation level",
+ statement: "SET TRANSACTION ISOLATION LEVEL READ COMMITTED; SET TRANSACTION ISOLATION LEVEL REPEATABLE READ; SET TRANSACTION ISOLATION LEVEL SNAPSHOT; SELECT 1",
+ valid: true,
+ },
+ {
+ name: "nocount in lower case",
+ statement: "set nocount on; select id from t",
+ valid: true,
+ },
+ {
+ name: "session options alone",
+ statement: "SET NOCOUNT OFF; SET TRANSACTION ISOLATION LEVEL READ COMMITTED",
+ valid: true,
+ },
+ {
+ name: "fmtonly listed after an allowed option",
+ statement: "SET NOCOUNT, FMTONLY ON; SELECT id FROM t",
+ },
+ {
+ name: "an option outside the list",
+ statement: "SET XACT_ABORT ON; SELECT id FROM t",
+ },
+ {
+ name: "statistics xml adds a result set",
+ statement: "SET STATISTICS XML ON; SELECT id FROM t",
+ },
+ {
+ name: "showplan replaces the result",
+ statement: "SET SHOWPLAN_XML ON",
+ },
+ {
+ name: "rowcount",
+ statement: "SET ROWCOUNT 10; SELECT id FROM t",
+ },
+ {
+ name: "identity insert",
+ statement: "SET IDENTITY_INSERT t ON",
+ },
+ {
+ name: "declare",
+ statement: "DECLARE @n int = 1; SELECT @n",
+ },
+ {
+ name: "set a variable",
+ statement: "SET @n = 1",
+ },
+ {
+ name: "isolation level before a write",
+ statement: "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; DELETE FROM t",
+ },
+ {
+ name: "isolation level before select into a temp table",
+ statement: "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT id INTO #t FROM t",
+ },
+ {
+ name: "select into a global temp table",
+ statement: "SELECT id INTO ##t FROM t",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ valid, allQuery, err := ValidateSQLForEditor(tc.statement)
+ require.NoError(t, err)
+ require.Equal(t, tc.valid, valid)
+ require.Equal(t, tc.allQuery, allQuery)
+ })
+ }
+}