PHPLIB-1927: Reject "." and NUL bytes in database and collection names - #1967
Conversation
Database and collection names were only checked for being non-empty. A "." in a database name, or a NUL byte in a database or collection name, shifts the namespace split performed by the server, so operations end up targeting a different database or collection than the caller intended. Reject "." and NUL bytes in database names, and NUL bytes in collection names. Dots remain legal in collection names. Add a create_namespace() helper that validates both names and returns the concatenated namespace, and use it in Collection, Database, and all Operation classes that build a namespace by concatenating database and collection names (BulkWrite, Delete, Find, InsertMany, InsertOne, RenameCollection, Update). Move the shared invalid name data provider to the base TestCase, and add prose test 17 for database and collection name validation.
There was a problem hiding this comment.
Pull request overview
This PR hardens namespace handling by validating database/collection names against characters that can change how the server splits namespaces, preventing operations from silently targeting an unintended database or collection.
Changes:
- Added centralized namespace construction via
MongoDB\create_namespace()and updated CRUD operations to use it instead of string concatenation. - Tightened
DatabaseandCollectionconstructors to reject.and NUL bytes in database names, and NUL bytes in collection names (dots still allowed in collection names). - Expanded test coverage with shared invalid-name providers and CRUD prose test 17.
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/TestCase.php | Adds shared data provider for invalid db/collection name combinations. |
| tests/SpecTests/Crud/Prose17_DatabaseAndCollectionNameValidationTest.php | New prose spec test ensuring invalid names are rejected through public APIs (including ClientBulkWrite). |
| tests/Operation/UpdateTest.php | Verifies Update rejects invalid db/collection names. |
| tests/Operation/RenameCollectionTest.php | Verifies RenameCollection rejects invalid “from/to” db/collection names. |
| tests/Operation/InsertOneTest.php | Verifies InsertOne rejects invalid db/collection names. |
| tests/Operation/InsertManyTest.php | Verifies InsertMany rejects invalid db/collection names. |
| tests/Operation/FindTest.php | Verifies Find rejects invalid db/collection names. |
| tests/Operation/DeleteTest.php | Verifies Delete rejects invalid db/collection names. |
| tests/Operation/BulkWriteTest.php | Verifies BulkWrite rejects invalid db/collection names. |
| tests/Database/DatabaseFunctionalTest.php | Extends invalid database-name cases to include . and NUL byte. |
| tests/Collection/CollectionFunctionalTest.php | Splits invalid db vs collection validation and explicitly asserts dots remain allowed in collection names. |
| src/Operation/Update.php | Uses create_namespace() and stores a validated namespace for execution. |
| src/Operation/RenameCollection.php | Uses create_namespace() for both from and to namespaces. |
| src/Operation/InsertOne.php | Uses create_namespace() to ensure validated namespace for writes. |
| src/Operation/InsertMany.php | Uses create_namespace() to ensure validated namespace for writes. |
| src/Operation/Find.php | Uses create_namespace() to ensure validated namespace for queries. |
| src/Operation/Delete.php | Uses create_namespace() to ensure validated namespace for writes. |
| src/Operation/BulkWrite.php | Uses create_namespace() to ensure validated namespace for bulk writes. |
| src/functions.php | Introduces create_namespace() helper with validation and namespace concatenation. |
| src/Database.php | Rejects . and NUL bytes in database names at construction time. |
| src/Collection.php | Rejects . and NUL bytes in database names; rejects NUL bytes in collection names. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
❌ 1 Tests Failed:
View the full list of 7 ❄️ flaky test(s)
To view more test analytics, go to the Test Analytics Dashboard |
Database and collection names were only checked for being non-empty. A "." in a database name, or a NUL byte in a database or collection name, shifts the namespace split performed by the server, so operations end up targeting a different database or collection than the caller intended.
Reject "." and NUL bytes in database names, and NUL bytes in collection names. Dots remain legal in collection names.
Add a create_namespace() helper that validates both names and returns the concatenated namespace, and use it in Collection, Database, and all Operation classes that build a namespace by concatenating database and collection names (BulkWrite, Delete, Find, InsertMany, InsertOne, RenameCollection, Update).
Move the shared invalid name data provider to the base TestCase, and add prose test 17 for database and collection name validation.