Skip to content

Commit 71f7cab

Browse files
optbuilder, sql: fix stable sql api edge cases
The logictests have flushed out a handful of situations in which the stable sql api gates block unexpectedly. Examples of this include unsafe access in safe builtin execution, nested delegates, and other similar use cases. What was required to fix them was to update the way we unguard the unsafe internals during query planning. Fixes: cockroachdb#154675 Epic: CRDB-55267 Release note: None
1 parent 321213f commit 71f7cab

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

pkg/sql/opt/optbuilder/builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,11 @@ func (b *Builder) maybeTrackUserDefinedTypeDepsForViews(texpr tree.TypedExpr) {
593593
// DisableUnsafeInternalCheck is used to disable the check that the
594594
// prevents external users from accessing unsafe internals.
595595
func (b *Builder) DisableUnsafeInternalCheck() func() {
596+
// Already in the middle of a disabled section.
597+
if b.skipUnsafeInternalsCheck {
598+
return func() {}
599+
}
600+
596601
b.skipUnsafeInternalsCheck = true
597602
var cleanup func()
598603
if b.catalog != nil {

pkg/sql/opt/optbuilder/routine.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ func (b *Builder) buildUDF(
5151
))
5252
}
5353

54+
// builtins should have access to unsafe internals
55+
if o.Type == tree.BuiltinRoutine {
56+
defer b.DisableUnsafeInternalCheck()()
57+
}
58+
5459
// Check for execution privileges for user-defined overloads. Built-in
5560
// overloads do not need to be checked.
5661
if o.Type == tree.UDFRoutine {

pkg/sql/opt_catalog.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -742,13 +742,9 @@ func (oc *optCatalog) codec() keys.SQLCodec {
742742
return oc.planner.ExecCfg().Codec
743743
}
744744

745-
// DisableUnsafeInternalCheck sets the planners skipUnsafeInternalsCheck
746-
// to true, and returns a function which reverses it to false.
745+
// DisableUnsafeInternalCheck forwards the call to the planner.
747746
func (oc *optCatalog) DisableUnsafeInternalCheck() func() {
748-
oc.planner.skipUnsafeInternalsCheck = true
749-
return func() {
750-
oc.planner.skipUnsafeInternalsCheck = false
751-
}
747+
return oc.planner.DisableUnsafeInternalsCheck()
752748
}
753749

754750
// optView is a wrapper around catalog.TableDescriptor that implements

pkg/sql/planner.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,19 @@ func (p *planner) InternalSQLTxn() descs.Txn {
667667
return &p.internalSQLTxn
668668
}
669669

670+
// DisableUnsafeInternalCheck sets the skipUnsafeInternalsCheck property
671+
// to true, and returns a function which reverses it to false.
672+
func (p *planner) DisableUnsafeInternalsCheck() func() {
673+
if p.skipUnsafeInternalsCheck {
674+
return func() {}
675+
}
676+
677+
p.skipUnsafeInternalsCheck = true
678+
return func() {
679+
p.skipUnsafeInternalsCheck = false
680+
}
681+
}
682+
670683
func (p *planner) regionsProvider() *regions.Provider {
671684
if txn := p.InternalSQLTxn(); txn != nil {
672685
_ = txn.Regions() // force initialization

pkg/sql/unsafesql/unsafesql_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ func TestAccessCheckServer(t *testing.T) {
231231
Passes: false,
232232
LogsDenied: true,
233233
},
234+
{
235+
// test nested delegates
236+
Query: "SHOW JOBS WHEN COMPLETE SELECT job_id FROM [SHOW JOBS]",
237+
Passes: true,
238+
},
239+
{
240+
// unexpected unsafe builtin access
241+
Query: "SELECT col_description(0, 0)",
242+
Passes: true,
243+
},
234244
} {
235245
t.Run(fmt.Sprintf("query=%s,internal=%t,allowUnsafe=%t", test.Query, test.Internal, test.AllowUnsafeInternals), func(t *testing.T) {
236246
accessedSpy.Reset()

0 commit comments

Comments
 (0)