Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent creating and dropping mysql and information_schema databases #2775

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions enginetest/queries/information_schema_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,27 @@ from information_schema.routines where routine_schema = 'mydb' and routine_type
},
},
},
{
Name: "test information_schema database",
Assertions: []ScriptTestAssertion{
{
Query: "show databases like 'information_schema';",
Expected: []sql.Row{{"information_schema"}},
},
{
Query: "create database information_schema;",
ExpectedErr: sql.ErrDatabaseExists,
},
{
Query: "drop database information_schema;",
ExpectedErrStr: "unable to drop database: information_schema",
},
{
Query: "show databases like 'information_schema';",
Expected: []sql.Row{{"information_schema"}},
},
},
},
}

var SkippedInfoSchemaScripts = []ScriptTest{
Expand Down
21 changes: 21 additions & 0 deletions enginetest/queries/mysql_db_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,25 @@ var MySqlDbTests = []ScriptTest{
},
},
},
{
Name: "test mysql database",
Assertions: []ScriptTestAssertion{
{
Query: "show databases like 'mysql';",
Expected: []sql.Row{{"mysql"}},
},
{
Query: "create database mysql;",
ExpectedErr: sql.ErrDatabaseExists,
},
{
Query: "drop database mysql;",
ExpectedErrStr: "unable to drop database: mysql",
},
{
Query: "show databases like 'mysql';",
Expected: []sql.Row{{"mysql"}},
},
},
},
}
8 changes: 5 additions & 3 deletions sql/analyzer/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ func (c *Catalog) RemoveDatabase(ctx *sql.Context, dbName string) error {
defer c.mu.Unlock()

mut, ok := c.DbProvider.(sql.MutableDatabaseProvider)
if ok {
return mut.DropDatabase(ctx, dbName)
} else {
if !ok {
return sql.ErrImmutableDatabaseProvider.New()
}
if strings.EqualFold(dbName, "information_schema") || (c.MySQLDb.Enabled() && strings.EqualFold(dbName, "mysql")) {
return fmt.Errorf("unable to drop database: %s", dbName)
}
return mut.DropDatabase(ctx, dbName)
}

func (c *Catalog) HasDatabase(ctx *sql.Context, db string) bool {
Expand Down
4 changes: 4 additions & 0 deletions sql/mysql_db/privileged_database_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (pdp PrivilegedDatabaseProvider) Database(ctx *sql.Context, name string) (s

// HasDatabase implements the interface sql.DatabaseProvider.
func (pdp PrivilegedDatabaseProvider) HasDatabase(ctx *sql.Context, name string) bool {
if strings.EqualFold(name, "mysql") {
return true
}

db, err := pdp.provider.Database(ctx, name)
if sql.ErrDatabaseNotFound.Is(err) {
// continue to check below, which will deny access or return not found as appropriate
Expand Down
Loading