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 collection names #844

Merged
merged 32 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
seeforschauer committed Jul 5, 2022
commit 9a8da5ec831914189d8dd895081426411e8c361f
5 changes: 3 additions & 2 deletions integration/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestCollectionName(t *testing.T) {
"and_in_ferretdb_databases_for_ferretdb_it_fails_because_it_is_more_than_119_characters__for_mongo_it_fails_because_it_is_more_than_" +
"255_charachters_long_that_excludes_non_latin_letters_spaces_dots_dollars_dashes Max: 255",
},
alt: "Fully qualified namespace is too long.",
alt: "Collection must not contain non-latin letters, spaces, dots, dollars, dashes and be shorter than 119",
},
"WithADollarSign": {
collection: "collection_name_with_a-$",
Expand All @@ -159,6 +159,7 @@ func TestCollectionName(t *testing.T) {
Code: 73,
Message: `Invalid collection name: collection_name_with_a-$`,
},
alt: "Collection must not contain non-latin letters, spaces, dots, dollars, dashes and be shorter than 119",
},
"Empty": {
collection: "",
Expand All @@ -167,7 +168,7 @@ func TestCollectionName(t *testing.T) {
Code: 73,
Message: "Invalid namespace specified 'testcollectionname-err.'",
},
alt: "Namespace is empty",
alt: "Collection must not contain non-latin letters, spaces, dots, dollars, dashes and be shorter than 119",
},
}

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 @@ -77,6 +77,10 @@ func (h *Handler) MsgCreate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
msg := fmt.Sprintf("Collection already exists. NS: %s.%s", db, collection)
return nil, common.NewErrorMsg(common.ErrNamespaceExists, msg)
}
if err == pgdb.ErrInvalidTableName {
msg := "Collection must not contain non-latin letters, spaces, dots, dollars, dashes and be shorter than 119"
return nil, common.NewErrorMsg(common.ErrInvalidNamespace, msg)
}
return nil, lazyerrors.Error(err)
}

Expand Down
18 changes: 7 additions & 11 deletions internal/handlers/pg/pgdb/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"golang.org/x/exp/slices"

"github.com/FerretDB/FerretDB/internal/fjson"
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
Expand All @@ -54,6 +53,9 @@ var (

// ErrAlreadyExist indicates that a schema or table already exists.
ErrAlreadyExist = fmt.Errorf("schema or table already exist")

// ErrInvalidTableName indicates that a schema or table didn't passed name checks.
ErrInvalidTableName = fmt.Errorf("invalid table name")
)

// Pool represents PostgreSQL concurrency-safe connection pool.
Expand Down Expand Up @@ -350,26 +352,20 @@ func (pgPool *Pool) DropDatabase(ctx context.Context, db string) error {
// The collection name length is no longer than 119 letters.
func (pgPool *Pool) validateCollectionName(collection string) error {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
if len(collection) == 0 {
return common.NewErrorMsg(common.ErrInvalidNamespace, "Namespace is empty")
return ErrInvalidTableName
}

if len(collection) > 119 {
return common.NewErrorMsg(common.ErrInvalidNamespace, "Fully qualified namespace is too long.")
return ErrInvalidTableName
}

regex := regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]{0,119}$")
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
if !regex.MatchString(collection) {
return common.NewErrorMsg(
common.ErrInvalidNamespace,
fmt.Sprintf("Invalid collection name: %s", collection),
)
return ErrInvalidTableName
}

if strings.HasPrefix(collection, collectionPrefix) {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
return common.NewErrorMsg(
common.ErrInvalidNamespace,
"Namespace must not contain start with reserved word _ferretdb_.",
)
return ErrInvalidTableName
}

return nil
Expand Down