Skip to content

Commit

Permalink
planner/core,session: fix privilege check for update (8376) (pingcap#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored and jackysp committed May 13, 2019
1 parent 7ad9b1a commit 2222ffa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) (Plan, error) {
if dbName == "" {
dbName = b.ctx.GetSessionVars().CurrentDB
}
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.UpdatePriv, dbName, t.Name.L, "")
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName, t.Name.L, "")
}

if sel.Where != nil {
Expand Down
26 changes: 25 additions & 1 deletion session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package session_test

import (
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -2350,6 +2351,29 @@ func (s *testSessionSuite) TestSetGroupConcatMaxLen(c *C) {

func (s *testSessionSuite) TestUpdatePrivilege(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (id int);")
tk.MustExec("create table t2 (id int);")
tk.MustExec("insert into t1 values (1);")
tk.MustExec("insert into t2 values (2);")
tk.MustExec("create user xxx;")
tk.MustExec("grant all on test.t1 to xxx;")
tk.MustExec("grant select on test.t2 to xxx;")
tk.MustExec("flush privileges;")

tk1 := testkit.NewTestKitWithInit(c, s.store)
c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "xxx", Hostname: "localhost"},
[]byte(""),
[]byte("")), IsTrue)

_, err := tk1.Exec("update t2 set id = 666 where id = 1;")
c.Assert(err, NotNil)
c.Assert(strings.Contains(err.Error(), "privilege check fail"), IsTrue)

// Cover a bug that t1 and t2 both require update privilege.
// In fact, the privlege check for t1 should be update, and for t2 should be select.
_, err = tk1.Exec("update t1,t2 set t1.id = t2.id;")
c.Assert(err, IsNil)

// Fix issue 8911
tk.MustExec("create database weperk")
Expand All @@ -2359,7 +2383,6 @@ func (s *testSessionSuite) TestUpdatePrivilege(c *C) {
tk.MustExec("grant all privileges on weperk.* to 'weperk'@'%'")
tk.MustExec("flush privileges;")

tk1 := testkit.NewTestKitWithInit(c, s.store)
c.Assert(tk1.Se.Auth(&auth.UserIdentity{Username: "weperk", Hostname: "%"},
[]byte(""), []byte("")), IsTrue)
tk1.MustExec("use weperk")
Expand Down Expand Up @@ -2387,6 +2410,7 @@ WHERE
s.a = t.a
and t.c >= 1 and t.c <= 10000
and s.b !='xx';`)

}

func (s *testSessionSuite) TestTxnGoString(c *C) {
Expand Down

0 comments on commit 2222ffa

Please sign in to comment.