Skip to content

Commit

Permalink
executor: grant privilege to table now check table existence use case…
Browse files Browse the repository at this point in the history
… insensitive (#36778) (#36798)

close #34610
  • Loading branch information
ti-srebot authored Oct 10, 2022
1 parent 0801773 commit 370da7d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions executor/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 23 additions & 3 deletions executor/grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;")
}

0 comments on commit 370da7d

Please sign in to comment.