[SERVER-30986] Server should error on invalid regex match expressions Created: 07/Sep/17  Updated: 30/Oct/23  Resolved: 19/Sep/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.6.12, 3.0.15, 3.2.16, 3.4.7
Fix Version/s: 3.6.0-rc0

Type: Bug Priority: Major - P3
Reporter: David Storch Assignee: Blake Oler
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
causes SERVER-34933 pcre verb support Closed
Related
related to SERVER-32356 Use of options:x and comments with $r... Closed
related to SERVER-39697 Regex MatchExpression should error at... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Query 2017-10-02
Participants:

 Description   

The server accepts invalid regular expressions in the match language. It appears that such invalid regular expressions will match nothing:

> db.c.drop();
true
> db.c.insert({a: 1, b: "["});
WriteResult({ "nInserted" : 1 })
> db.c.find({a: 1, b: {$regex: "["}});
// Empty result set.

In order to fix this, the MatchExpression library should call pcrecpp::RE::error() and check whether the resulting error string is non-empty:

https://github.com/mongodb/mongo/blob/9f8084f2c87cdbe5616cd5d6d8adb2a8272a504a/src/third_party/pcre-8.41/pcrecpp.h#L526-L528

This bug appears to affect all versions of the server since at least 2.6.



 Comments   
Comment by Githook User [ 29/Jun/18 ]

Author:

{'username': 'ksuarz', 'name': 'Kyle Suarez', 'email': 'kyle.suarez@mongodb.com'}

Message: Revert "SERVER-30986 Prevent invalid regex in RegexMatchExpressions"

There exist PCRE regexes whose error strings are nonempty but can still
be used for matching. This restores the functionality of the (*UCP)
option for SERVER-34933.

This reverts commit 1531b7b7280dd37d5f7ffd49171a65305ad442ba.
Conflicts:
src/mongo/db/matcher/expression_leaf.cpp
src/mongo/db/matcher/expression_leaf.h
src/mongo/db/matcher/expression_leaf_test.cpp

(cherry picked from commit e9a6f62b3053733354cabb6d9488d0d4dcc6c424)
Branch: v4.0
https://github.com/mongodb/mongo/commit/3cd8ab42a9401bebccbc2294d86149e71801f167

Comment by Githook User [ 21/Jun/18 ]

Author:

{'username': 'ksuarz', 'name': 'Kyle Suarez', 'email': 'kyle.suarez@mongodb.com'}

Message: Revert "SERVER-30986 Prevent invalid regex in RegexMatchExpressions"

This reverts commit 1531b7b7280dd37d5f7ffd49171a65305ad442ba.
Branch: v3.6
https://github.com/mongodb/mongo/commit/568086fa19039d2a879f2da602456e341bfed102

Comment by Githook User [ 20/Jun/18 ]

Author:

{'username': 'ksuarz', 'name': 'Kyle Suarez', 'email': 'kyle.suarez@mongodb.com'}

Message: Revert "SERVER-30986 Prevent invalid regex in RegexMatchExpressions"

There exist PCRE regexes whose error strings are nonempty but can still
be used for matching. This restores the functionality of the (*UCP)
option for SERVER-34933.

This reverts commit 1531b7b7280dd37d5f7ffd49171a65305ad442ba.
Conflicts:
src/mongo/db/matcher/expression_leaf.cpp
src/mongo/db/matcher/expression_leaf.h
src/mongo/db/matcher/expression_leaf_test.cpp
Branch: master
https://github.com/mongodb/mongo/commit/e9a6f62b3053733354cabb6d9488d0d4dcc6c424

Comment by Githook User [ 19/Sep/17 ]

Author:

{'username': 'BlakeIsBlake', 'name': 'Blake Oler', 'email': 'blake.oler@10gen.com'}

Message: SERVER-30986 Prevent invalid regex in RegexMatchExpressions
Branch: master
https://github.com/mongodb/mongo/commit/1531b7b7280dd37d5f7ffd49171a65305ad442ba

Generated at Thu Feb 08 04:25:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.