Skip to content

Commit

Permalink
Merge branch 'main' into percona-ferretdb-blog
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Jun 27, 2023
2 parents 91372b8 + 49d338c commit 5b9cf93
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
8 changes: 8 additions & 0 deletions integration/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ func TestCollectionName(t *testing.T) {
},
altMessage: "Invalid collection name: 'TestCollectionName.\x00'",
},
"DotSurround": {
collection: ".collection..",
err: &mongo.CommandError{
Name: "InvalidNamespace",
Code: 73,
Message: "Collection names cannot start with '.': .collection..",
},
},
"Dot": {
collection: "collection.name",
},
Expand Down
4 changes: 4 additions & 0 deletions internal/handlers/pg/msg_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
msg := fmt.Sprintf("Invalid collection name: '%s.%s'", db, collection)
return commonerrors.NewCommandErrorMsg(commonerrors.ErrInvalidNamespace, msg)

case errors.Is(err, pgdb.ErrCollectionStartsWithDot):
msg := fmt.Sprintf("Collection names cannot start with '.': %s", collection)
return commonerrors.NewCommandErrorMsg(commonerrors.ErrInvalidNamespace, msg)

default:
return lazyerrors.Error(err)
}
Expand Down
8 changes: 7 additions & 1 deletion internal/handlers/pg/pgdb/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import (

// validateCollectionNameRe validates collection names.
// Empty collection name, names with `$` and `\x00` are not allowed.
var validateCollectionNameRe = regexp.MustCompile("^[^$\x00]{1,235}$")
// Collection names that start with `.` are also not allowed.
var validateCollectionNameRe = regexp.MustCompile("^[^.$\x00][^$\x00]{0,234}$")

// Collections returns a sorted list of FerretDB collection names.
//
Expand Down Expand Up @@ -108,9 +109,14 @@ func CollectionExists(ctx context.Context, tx pgx.Tx, db, collection string) (bo
// It returns possibly wrapped error:
// - ErrInvalidDatabaseName - if the given database name doesn't conform to restrictions.
// - ErrInvalidCollectionName - if the given collection name doesn't conform to restrictions.
// - ErrCollectionStartsWithDot - if the given collection name starts with dot.
// - ErrAlreadyExist - if a FerretDB collection with the given name already exists.
// - *transactionConflictError - if a PostgreSQL conflict occurs (the caller could retry the transaction).
func CreateCollection(ctx context.Context, tx pgx.Tx, db, collection string) error {
if strings.HasPrefix(collection, ".") {
return ErrCollectionStartsWithDot
}

if !validateCollectionNameRe.MatchString(collection) ||
strings.HasPrefix(collection, reservedPrefix) ||
!utf8.ValidString(collection) {
Expand Down
3 changes: 3 additions & 0 deletions internal/handlers/pg/pgdb/pgdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ var (
// ErrInvalidCollectionName indicates that a collection didn't pass name checks.
ErrInvalidCollectionName = fmt.Errorf("invalid FerretDB collection name")

// ErrCollectionStartsWithDot indicates that collection name starts with dot, but it shouldn't.
ErrCollectionStartsWithDot = fmt.Errorf("Collection names cannot start with '.'")

// ErrInvalidDatabaseName indicates that a database didn't pass name checks.
ErrInvalidDatabaseName = fmt.Errorf("invalid FerretDB database name")

Expand Down

0 comments on commit 5b9cf93

Please sign in to comment.