@@ -7,6 +7,7 @@ package sql_test
7
7
8
8
import (
9
9
"context"
10
+ gosql "database/sql"
10
11
"fmt"
11
12
"strings"
12
13
"sync"
@@ -321,6 +322,14 @@ func TestCheckUnsafeInternalsAccess(t *testing.T) {
321
322
})
322
323
defer s .Stopper ().Stop (ctx )
323
324
325
+ // helper func for setting a safe connection.
326
+ safeConn := func (t * testing.T ) * gosql.DB {
327
+ conn := s .SQLConn (t )
328
+ _ , err := conn .Exec ("SET allow_unsafe_internals = false" )
329
+ require .NoError (t , err )
330
+ return conn
331
+ }
332
+
324
333
t .Run ("accessing the system database" , func (t * testing.T ) {
325
334
q := "SELECT * FROM system.namespace"
326
335
@@ -378,47 +387,66 @@ func TestCheckUnsafeInternalsAccess(t *testing.T) {
378
387
379
388
t .Run ("accessing the crdb_internal schema" , func (t * testing.T ) {
380
389
t .Run ("supported table allowed" , func (t * testing.T ) {
381
- conn := s .SQLConn (t )
382
- _ , err := conn .Exec ("SET allow_unsafe_internals = false" )
383
- require .NoError (t , err )
390
+ conn := safeConn (t )
384
391
385
392
// Supported crdb_internal tables should be allowed even when allow_unsafe_internals = false
386
- _ , err = conn .Query ("SELECT * FROM crdb_internal.zones" )
393
+ _ , err : = conn .Query ("SELECT * FROM crdb_internal.zones" )
387
394
require .NoError (t , err , "supported crdb_internal table (zones) should be accessible when allow_unsafe_internals = false" )
388
395
})
389
396
390
397
t .Run ("unsupported table denied" , func (t * testing.T ) {
391
- conn := s .SQLConn (t )
392
- _ , err := conn .Exec ("SET allow_unsafe_internals = false" )
393
- require .NoError (t , err )
398
+ conn := safeConn (t )
394
399
395
400
// Unsupported crdb_internal tables should be denied when allow_unsafe_internals = false
396
- _ , err = conn .Query ("SELECT * FROM crdb_internal.gossip_alerts" )
401
+ _ , err : = conn .Query ("SELECT * FROM crdb_internal.gossip_alerts" )
397
402
checkUnsafeErr (t , err )
398
403
})
399
404
})
400
405
401
- // The functionality for this lies in the pkg/sql/optbuilder/scalar.go
402
- // file, but it is tested here as that package does not setup a test
403
- // server.
406
+ // The functionality for this lies in the optbuilder package file,
407
+ // but it is tested here as that package does not setup a test server.
404
408
t .Run ("accessing crdb_internal builtins" , func (t * testing.T ) {
405
409
t .Run ("non crdb_internal builtin allowed" , func (t * testing.T ) {
406
- conn := s .SQLConn (t )
407
- _ , err := conn .Exec ("SET allow_unsafe_internals = false" )
408
- require .NoError (t , err )
410
+ conn := safeConn (t )
409
411
410
412
// Non crdb_internal tables should be allowed.
411
- _ , err = conn .Query ("SELECT * FROM generate_series(1,5)" )
413
+ _ , err : = conn .Query ("SELECT * FROM generate_series(1,5)" )
412
414
require .NoError (t , err )
413
415
})
414
416
415
417
t .Run ("crdb_internal builtin not allowed" , func (t * testing.T ) {
416
- conn := s .SQLConn (t )
417
- _ , err := conn .Exec ("SET allow_unsafe_internals = false" )
418
- require .NoError (t , err )
418
+ conn := safeConn (t )
419
419
420
420
// Unsupported crdb_internal builtins should be denied.
421
- _ , err = conn .Query ("SELECT * FROM crdb_internal.tenant_span_stats()" )
421
+ _ , err := conn .Query ("SELECT * FROM crdb_internal.tenant_span_stats()" )
422
+ checkUnsafeErr (t , err )
423
+ })
424
+ })
425
+
426
+ // The functionality for this check also lives in the optbuilder package
427
+ // but is tested here.
428
+ t .Run ("skips delegation" , func (t * testing.T ) {
429
+ t .Run ("delegation is allowed" , func (t * testing.T ) {
430
+ conn := safeConn (t )
431
+
432
+ // tests delegation to builtins
433
+ _ , err := conn .Exec ("show grants" )
434
+ require .NoError (t , err )
435
+
436
+ // tests delegation to crdb_internal tables
437
+ _ , err = conn .Exec ("show databases" )
438
+ require .NoError (t , err )
439
+ })
440
+
441
+ t .Run ("underlying tables which delegates rely on are not" , func (t * testing.T ) {
442
+ conn := safeConn (t )
443
+
444
+ // tests delegation to builtins
445
+ _ , err := conn .Exec ("SELECT * FROM crdb_internal.privilege_name('DELETE')" )
446
+ checkUnsafeErr (t , err )
447
+
448
+ // tests delegation to crdb_internal tables
449
+ _ , err = conn .Exec ("SELECT * FROM crdb_internal.databases" )
422
450
checkUnsafeErr (t , err )
423
451
})
424
452
})
0 commit comments