Skip to content

Commit 8a86c58

Browse files
authored
prevent creating and dropping mysql and information_schema databases (#2775)
1 parent ef1a969 commit 8a86c58

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

enginetest/queries/information_schema_queries.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,27 @@ from information_schema.routines where routine_schema = 'mydb' and routine_type
18071807
},
18081808
},
18091809
},
1810+
{
1811+
Name: "test information_schema database",
1812+
Assertions: []ScriptTestAssertion{
1813+
{
1814+
Query: "show databases like 'information_schema';",
1815+
Expected: []sql.Row{{"information_schema"}},
1816+
},
1817+
{
1818+
Query: "create database information_schema;",
1819+
ExpectedErr: sql.ErrDatabaseExists,
1820+
},
1821+
{
1822+
Query: "drop database information_schema;",
1823+
ExpectedErrStr: "unable to drop database: information_schema",
1824+
},
1825+
{
1826+
Query: "show databases like 'information_schema';",
1827+
Expected: []sql.Row{{"information_schema"}},
1828+
},
1829+
},
1830+
},
18101831
}
18111832

18121833
var SkippedInfoSchemaScripts = []ScriptTest{

enginetest/queries/mysql_db_queries.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,25 @@ var MySqlDbTests = []ScriptTest{
3838
},
3939
},
4040
},
41+
{
42+
Name: "test mysql database",
43+
Assertions: []ScriptTestAssertion{
44+
{
45+
Query: "show databases like 'mysql';",
46+
Expected: []sql.Row{{"mysql"}},
47+
},
48+
{
49+
Query: "create database mysql;",
50+
ExpectedErr: sql.ErrDatabaseExists,
51+
},
52+
{
53+
Query: "drop database mysql;",
54+
ExpectedErrStr: "unable to drop database: mysql",
55+
},
56+
{
57+
Query: "show databases like 'mysql';",
58+
Expected: []sql.Row{{"mysql"}},
59+
},
60+
},
61+
},
4162
}

sql/analyzer/catalog.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ func (c *Catalog) RemoveDatabase(ctx *sql.Context, dbName string) error {
153153
defer c.mu.Unlock()
154154

155155
mut, ok := c.DbProvider.(sql.MutableDatabaseProvider)
156-
if ok {
157-
return mut.DropDatabase(ctx, dbName)
158-
} else {
156+
if !ok {
159157
return sql.ErrImmutableDatabaseProvider.New()
160158
}
159+
if strings.EqualFold(dbName, "information_schema") || (c.MySQLDb.Enabled() && strings.EqualFold(dbName, "mysql")) {
160+
return fmt.Errorf("unable to drop database: %s", dbName)
161+
}
162+
return mut.DropDatabase(ctx, dbName)
161163
}
162164

163165
func (c *Catalog) HasDatabase(ctx *sql.Context, db string) bool {

sql/mysql_db/privileged_database_provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func (pdp PrivilegedDatabaseProvider) Database(ctx *sql.Context, name string) (s
8080

8181
// HasDatabase implements the interface sql.DatabaseProvider.
8282
func (pdp PrivilegedDatabaseProvider) HasDatabase(ctx *sql.Context, name string) bool {
83+
if strings.EqualFold(name, "mysql") {
84+
return true
85+
}
86+
8387
db, err := pdp.provider.Database(ctx, name)
8488
if sql.ErrDatabaseNotFound.Is(err) {
8589
// continue to check below, which will deny access or return not found as appropriate

0 commit comments

Comments
 (0)