Skip to content

Commit 8d7a5ae

Browse files
matthew-lengMatthewLENG2002
andauthored
fix: return false when object contains invalid regex (#18)
* fix: return false when object contains invalid regex * style: format python code (pep8) * Update condition_test.py Co-authored-by: MatthewLENG2002 <MatthewLENG2002@users.noreply.github.com>
1 parent 5017255 commit 8d7a5ae

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

featureprobe/model/condition.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import logging
16+
import re
1617
import time
1718
from typing import List, Dict, Union, Optional, TYPE_CHECKING
1819

@@ -81,7 +82,11 @@ def _match_string_condition(self, user: "User", **_) -> bool:
8182
subject_val = user[self._subject]
8283
if empty_str(subject_val):
8384
return False
84-
return self._predicate.matcher(subject_val, self._objects)
85+
try:
86+
return self._predicate.matcher(subject_val, self._objects)
87+
except re.error as e:
88+
self._logger.error('Invalid regular expression', exc_info=e)
89+
return False
8590

8691
def _match_segment_condition(
8792
self, user: "User", segments: Dict[str, "Segment"], **_) -> bool:

tests/condition_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ def test_string_matches_regex():
9999
assert not condition.match_objects(user, segments)
100100

101101

102+
def test_string_invalid_regex():
103+
condition.objects = ['\\\\\\']
104+
user['userId'] = '13797347245'
105+
106+
condition.predicate = fp.StringPredicate.MATCHES_REGEX
107+
assert not condition.match_objects(user, segments)
108+
109+
condition.predicate = fp.StringPredicate.DOES_NOT_MATCH_REGEX
110+
assert not condition.match_objects(user, segments)
111+
112+
102113
def test_string_is_not_any_of():
103114
condition.objects = ['12345', '987654', '665544']
104115
condition.predicate = fp.StringPredicate.IS_NOT_ANY_OF

0 commit comments

Comments
 (0)