Fix MongoDB presence verification for values containing regex delimiters #3469
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