From 370da7d0a529a98f0a8c46db08ad1b06f5950306 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Mon, 10 Oct 2022 14:17:49 +0800 Subject: [PATCH] executor: grant privilege to table now check table existence use case insensitive (#36778) (#36798) close pingcap/tidb#34610 --- executor/grant.go | 5 +++-- executor/grant_test.go | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/executor/grant.go b/executor/grant.go index 387ced7e69f40..2aabd3c4aca77 100644 --- a/executor/grant.go +++ b/executor/grant.go @@ -105,8 +105,9 @@ func (e *GrantExec) Next(ctx context.Context, req *chunk.Chunk) error { return err } } - // Note the table name compare is case sensitive here. - if tbl != nil && tbl.Meta().Name.String() != e.Level.TableName { + // Note the table name compare is not case sensitive here. + // In TiDB, system variable lower_case_table_names = 2 which means name comparisons are not case-sensitive. + if tbl != nil && tbl.Meta().Name.L != strings.ToLower(e.Level.TableName) { return infoschema.ErrTableNotExists.GenWithStackByArgs(dbName, e.Level.TableName) } if len(e.Level.DBName) > 0 { diff --git a/executor/grant_test.go b/executor/grant_test.go index 05d64b0d7b698..0519c778fe1bd 100644 --- a/executor/grant_test.go +++ b/executor/grant_test.go @@ -449,9 +449,9 @@ func TestGrantOnNonExistTable(t *testing.T) { require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotExists)) tk.MustExec("create table if not exists xx (id int)") - // Case sensitive - _, err = tk.Exec("grant Select,Insert on XX to 'genius'") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotExists)) + // Case insensitive, differ from MySQL default behaviour. + // In TiDB, system variable lower_case_table_names = 2, which means compare table name using lower case. + tk.MustExec("grant Select,Insert on XX to 'genius'") _, err = tk.Exec("grant Select,Insert on xx to 'genius'") require.NoError(t, err) @@ -606,3 +606,23 @@ func TestNonExistTableIllegalGrant(t *testing.T) { // Column level, not existing table, illegal privilege tk.MustGetErrCode("grant create temporary tables (NotExistsCol) on NotExistsD29302.NotExistsT29302 to u29302;", mysql.ErrWrongUsage) } + +func TestIssue34610(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + + tk := testkit.NewTestKit(t, store) + tk.MustExec("DROP DATABASE IF EXISTS d1;") + tk.MustExec("CREATE DATABASE d1;") + tk.MustExec("USE d1;") + tk.MustExec("CREATE USER user_1@localhost;") + defer func() { + tk.MustExec("DROP DATABASE d1;") + tk.MustExec("DROP USER user_1@localhost;") + }() + + tk.MustExec("CREATE TABLE T1(f1 INT);") + tk.MustGetErrCode("CREATE TABLE t1(f1 INT);", mysql.ErrTableExists) + tk.MustExec("GRANT SELECT ON T1 to user_1@localhost;") + tk.MustExec("GRANT SELECT ON t1 to user_1@localhost;") +}