conn: fix database info leaking problem (#3699)

This commit is contained in:
Cholerae Hu
2017-07-11 04:39:35 -05:00
committed by Han Fei
parent f212ef1705
commit c8feff878f
4 changed files with 28 additions and 6 deletions

View File

@ -317,6 +317,12 @@ func (cc *clientConn) readHandshakeResponse() error {
return errors.Trace(errAccessDenied.GenByArgs(cc.user, host, "YES"))
}
}
if cc.dbname != "" {
_, err = cc.ctx.Execute("use " + cc.dbname)
if err != nil {
return errors.Trace(err)
}
}
cc.ctx.SetSessionManager(cc.server)
return nil
}

View File

@ -129,12 +129,6 @@ func (qd *TiDBDriver) OpenCtx(connID uint64, capability uint32, collation uint8,
}
session.SetClientCapability(capability)
session.SetConnectionID(connID)
if dbname != "" {
_, err = session.Execute("use " + dbname)
if err != nil {
return nil, errors.Trace(err)
}
}
tc := &TiDBContext{
session: session,
currentDB: dbname,

View File

@ -487,6 +487,24 @@ func runTestAuth(c *C) {
})
}
func runTestIssue3682(c *C) {
runTests(c, dsn, func(dbt *DBTest) {
dbt.mustExec(`CREATE USER 'abc'@'%' IDENTIFIED BY '123';`)
dbt.mustExec(`FLUSH PRIVILEGES;`)
})
newDsn := "abc:123@tcp(127.0.0.1:4001)/test?strict=true"
runTests(c, newDsn, func(dbt *DBTest) {
dbt.mustExec(`USE mysql;`)
})
wrongDsn := "abc:456@tcp(127.0.0.1:4001)/a_database_not_exist?strict=true"
db, err := sql.Open("mysql", wrongDsn)
c.Assert(err, IsNil)
defer db.Close()
err = db.Ping()
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Error 1045: Access denied for user 'abc'@'127.0.0.1' (using password: YES)")
}
func runTestIssues(c *C) {
// For issue #263
unExistsSchemaDsn := "root@tcp(localhost:4001)/unexists_schema?strict=true"

View File

@ -160,3 +160,7 @@ func (ts *TidbTestSuite) TestIssue3680(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "Error 1045: Access denied for user 'non_existing_user'@'127.0.0.1' (using password: YES)")
}
func (ts *TidbTestSuite) TestIssue3682(c *C) {
runTestIssue3682(c)
}