Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to backtick schema names properly #6550

Merged
merged 4 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion go/mysql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"sync"
"time"

"vitess.io/vitess/go/sqlescape"

"vitess.io/vitess/go/bucketpool"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/sync2"
Expand Down Expand Up @@ -774,7 +776,7 @@ func (c *Conn) handleNextCommand(handler Handler) error {
case ComInitDB:
db := c.parseComInitDB(data)
c.recycleReadPacket()
if err := c.execQuery(fmt.Sprintf("use `%s`", db), handler, false); err != nil {
if err := c.execQuery("use "+sqlescape.EscapeID(db), handler, false); err != nil {
return err
}
case ComQuery:
Expand Down
5 changes: 3 additions & 2 deletions go/mysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package mysql

import (
"crypto/tls"
"fmt"
"io"
"net"
"strings"
"sync/atomic"
"time"

"vitess.io/vitess/go/sqlescape"

proxyproto "github.com/pires/go-proxyproto"
"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/sqltypes"
Expand Down Expand Up @@ -443,7 +444,7 @@ func (l *Listener) handle(conn net.Conn, connectionID uint32, acceptTime time.Ti

// Set initial db name.
if c.schemaName != "" {
err = l.handler.ComQuery(c, fmt.Sprintf("use `%s`", c.schemaName), func(result *sqltypes.Result) error {
err = l.handler.ComQuery(c, "use "+sqlescape.EscapeID(c.schemaName), func(result *sqltypes.Result) error {
return nil
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vttablet/tabletmanager/rpc_lock_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (tm *TabletManager) lockTablesUsingLockTables(conn *dbconnpool.DBConnection
tableNames = append(tableNames, fmt.Sprintf("%s READ", sqlescape.EscapeID(name)))
}
lockStatement := fmt.Sprintf("LOCK TABLES %v", strings.Join(tableNames, ", "))
_, err := conn.ExecuteFetch(fmt.Sprintf("USE %s", tm.DBConfigs.DBName), 0, false)
_, err := conn.ExecuteFetch("USE "+sqlescape.EscapeID(tm.DBConfigs.DBName), 0, false)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions go/vt/vttablet/tabletmanager/rpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tabletmanager

import (
"golang.org/x/net/context"
"vitess.io/vitess/go/sqlescape"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/log"

Expand All @@ -44,7 +45,7 @@ func (tm *TabletManager) ExecuteFetchAsDba(ctx context.Context, query []byte, db
if dbName != "" {
// This execute might fail if db does not exist.
// Error is ignored because given query might create this database.
conn.ExecuteFetch("USE "+dbName, 1, false)
conn.ExecuteFetch("USE "+sqlescape.EscapeID(dbName), 1, false)
}

// run the query
Expand Down Expand Up @@ -81,7 +82,7 @@ func (tm *TabletManager) ExecuteFetchAsAllPrivs(ctx context.Context, query []byt
if dbName != "" {
// This execute might fail if db does not exist.
// Error is ignored because given query might create this database.
conn.ExecuteFetch("USE "+dbName, 1, false)
conn.ExecuteFetch("USE "+sqlescape.EscapeID(dbName), 1, false)
}

// run the query
Expand Down
52 changes: 52 additions & 0 deletions go/vt/vttablet/tabletmanager/rpc_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2020 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tabletmanager

import (
"context"
"testing"

"vitess.io/vitess/go/sqltypes"

"vitess.io/vitess/go/mysql/fakesqldb"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/mysqlctl/fakemysqldaemon"
"vitess.io/vitess/go/vt/vttablet/tabletservermock"

"github.com/stretchr/testify/require"
)

func TestTabletManager_ExecuteFetchAsDba(t *testing.T) {
ctx := context.Background()
cp := mysql.ConnParams{}
db := fakesqldb.New(t)
db.AddQueryPattern(".*", &sqltypes.Result{})
daemon := fakemysqldaemon.NewFakeMysqlDaemon(db)

dbName := " escap`e me "
tm := &TabletManager{
MysqlDaemon: daemon,
DBConfigs: dbconfigs.NewTestDBConfigs(cp, cp, dbName),
QueryServiceControl: tabletservermock.NewController(),
}

_, err := tm.ExecuteFetchAsDba(ctx, []byte("select 42"), dbName, 10, false, false)
require.NoError(t, err)
require.Equal(t, "use ` escap``e me `;select 42", db.QueryLog())
}