Fix MongoDB presence verification for values containing regex delimiters#3469
Open
zigzagdev wants to merge 2 commits intomongodb:5.xfrom
Open
Fix MongoDB presence verification for values containing regex delimiters#3469zigzagdev wants to merge 2 commits intomongodb:5.xfrom
zigzagdev wants to merge 2 commits intomongodb:5.xfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in the MongoDB presence verifier where validation queries failed when input values contained regex delimiter characters (e.g., /). The fix ensures proper escaping of user input and correct construction of MongoDB regex objects for case-insensitive exact matching.
Changes:
- Updated
getCount()to properly escape input with delimiter and constructMongoDB\BSON\Regexwith correct options - Updated
getMultiCount()to apply the same escaping approach for multiple values - Added regression tests verifying validation of values containing
/, case-insensitive matching, and partial match rejection
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Validation/DatabasePresenceVerifier.php | Fixed regex escaping and construction in both getCount() and getMultiCount() methods |
| tests/ValidationTest.php | Added test cases for slash-containing values, case-insensitive matching, and partial match validation |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes an issue in the MongoDB presence verifier where validation queries could break (or behave unexpectedly) when the input contained regex delimiter characters such as /.
Motivation
The verifier builds a MongoDB regex from user input to perform a case-insensitive exact match. However, the previous implementation used preg_quote without specifying a delimiter and also mixed regex options in a way that could lead to incorrect patterns. This becomes visible when validating values like Foo/Bar.
###What changed
Why passing the Regex object directly
The MongoDB query builder expects a
MongoDB\BSON\Regexinstance to be provided as the value for a regex comparison.Using
where($column, 'regex', $regex)relies on an operator string that may not be interpreted consistently across the MongoDB driver / query builder implementation, whereas passing the Regex object directly (where($column, $regex)) is the explicit and supported way to perform regex matching.Test plan
./vendor/bin/phpunit
Specifically verified the new cases added in tests/ValidationTest.php (values containing /, case-insensitive exact match, and partial mismatch).
Checklist