Skip to content

Commit 3344297

Browse files
clisqlclient: ensure allow_unsafe_internals is on during metadata checks
Previously cli commands (ex/ `cockroach sql`) would error if the allow_unsafe_internals session variable was set to false/off. This is because constructing a clisqlclient.sqlConn involves performing metadata checks that access internal/system tables. This commit ensures allow_unsafe_internals is true/on while doing these checks. Fixes: #152754 Relase note: None
1 parent d71deec commit 3344297

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pkg/cli/clisqlclient/conn.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,23 @@ func (c *sqlConn) checkServerMetadata(ctx context.Context) error {
436436
defer func(prev bool) { c.alwaysInferResultTypes = prev }(c.alwaysInferResultTypes)
437437
c.alwaysInferResultTypes = false
438438

439+
// Metadata checks require allow_unsafe_internals to be on.
440+
allowUnsafeInternals, err := c.getSessionVariable(ctx, "allow_unsafe_internals")
441+
if err != nil {
442+
fmt.Fprintf(c.errw, "warning: unable to retrieve allow_unsafe_internals setting: %v\n", err)
443+
} else if allowUnsafeInternals == "off" {
444+
// Temporarily turn on allow_unsafe_internals.
445+
if err := c.Exec(ctx, "SET allow_unsafe_internals = on"); err != nil {
446+
fmt.Fprintf(c.errw, "warning: unable to set allow_unsafe_internals to true: %v\n", err)
447+
} else {
448+
defer func() {
449+
if resetErr := c.Exec(ctx, "SET allow_unsafe_internals = off"); resetErr != nil {
450+
fmt.Fprintf(c.errw, "warning: unable to reset allow_unsafe_internals to false: %v\n", resetErr)
451+
}
452+
}()
453+
}
454+
}
455+
439456
_, newServerVersion, newClusterID, err := c.GetServerMetadata(ctx)
440457
if c.conn.IsClosed() {
441458
return MarkWithConnectionClosed(err)
@@ -546,6 +563,15 @@ func (c *sqlConn) GetServerValue(
546563
return dbVals[0], true
547564
}
548565

566+
// getSessionVariable retrieves the value of a session variable.
567+
func (c *sqlConn) getSessionVariable(ctx context.Context, varName string) (string, error) {
568+
val, ok := c.GetServerValue(ctx, varName, fmt.Sprintf("SHOW %s", varName))
569+
if !ok {
570+
return "", errors.Newf("unable to retrieve session variable %s", varName)
571+
}
572+
return toString(val), nil
573+
}
574+
549575
func (c *sqlConn) GetLastQueryStatistics(ctx context.Context) (results QueryStats, resErr error) {
550576
if !c.connCtx.EnableServerExecutionTimings || c.lastQueryStatsMode == modeDisabled {
551577
return results, nil

0 commit comments

Comments
 (0)