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

Validate database names for SQLite handler #2924

Merged
merged 21 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
56 changes: 56 additions & 0 deletions integration/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,43 @@ func TestDatabaseName(t *testing.T) {

t.Parallel()

t.Run("SpecialCharacters", func(t *testing.T) {
ctx, collection := setup.Setup(t)
for name, tc := range map[string]struct {
db string // database name, defaults to empty string

err *mongo.CommandError // required, expected error from MongoDB
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
altMessage string // optional, alternative error message for FerretDB, ignored if empty
skip string // optional, skip test with a specified reason
}{
"Dash": {
db: "--",
},
"Underscore": {
db: "__",
},
"Sqlite": {
db: "sqlite_",
},
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}

t.Parallel()

// there is no explicit command to create database, so create collection instead
err := collection.Database().Client().Database(tc.db).CreateCollection(ctx, collection.Name())
require.NoError(t, err)

err = collection.Database().Client().Database(tc.db).Drop(ctx)
require.NoError(t, err)
})
}
})

t.Run("Err", func(t *testing.T) {
ctx, collection := setup.Setup(t)

Expand All @@ -472,6 +509,25 @@ func TestDatabaseName(t *testing.T) {
},
altMessage: fmt.Sprintf("Invalid namespace: %s.%s", dbName64, "TestDatabaseName-Err"),
},
"WithASlash": {
db: "/",
err: &mongo.CommandError{
Name: "InvalidNamespace",
Code: 73,
Message: `Invalid namespace specified '/.TestDatabaseName-Err'`,
},
altMessage: `Invalid namespace: /.TestDatabaseName-Err`,
},

"WithABackslash": {
db: "\\",
err: &mongo.CommandError{
Name: "InvalidNamespace",
Code: 73,
Message: `Invalid namespace specified '\.TestDatabaseName-Err'`,
},
altMessage: `Invalid namespace: \.TestDatabaseName-Err`,
},
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
"WithADollarSign": {
db: "name_with_a-$",
err: &mongo.CommandError{
Expand Down
9 changes: 9 additions & 0 deletions internal/handlers/sqlite/msg_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package sqlite
import (
"context"
"fmt"
"regexp"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handlers/common"
Expand All @@ -27,6 +28,9 @@ import (
"github.com/FerretDB/FerretDB/internal/wire"
)

// validateDatabaseNameRe validates FerretDB database name.
var validateDatabaseNameRe = regexp.MustCompile("^[a-zA-Z_-][a-zA-Z0-9_-]{0,62}$")

AlekSi marked this conversation as resolved.
Show resolved Hide resolved
// MsgCreate implements HandlerInterface.
func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, error) {
document, err := msg.Document()
Expand Down Expand Up @@ -78,6 +82,11 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
return nil, err
}

if !validateDatabaseNameRe.MatchString(dbName) {
msg := fmt.Sprintf("Invalid namespace: %s.%s", dbName, collectionName)
return nil, commonerrors.NewCommandErrorMsg(commonerrors.ErrInvalidNamespace, msg)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
}

db := h.b.Database(dbName)
defer db.Close()

Expand Down