Skip to content

Commit b84421b

Browse files
authored
fix: return false when object contains invalid regex (#25)
* fix: return false when object contains invalid regex * Update ConditionSpec.groovy
1 parent bf2dcb4 commit b84421b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/com/featureprobe/sdk/server/model/Condition.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.*;
99
import java.util.regex.Pattern;
10+
import java.util.regex.PatternSyntaxException;
1011

1112
public final class Condition {
1213

@@ -126,7 +127,12 @@ private boolean matchStringCondition(FPUser user) {
126127
return false;
127128
}
128129

129-
return stringMatcher.match(subjectValue, this.objects);
130+
try {
131+
return stringMatcher.match(subjectValue, this.objects);
132+
} catch (PatternSyntaxException e) {
133+
logger.error("Invalid regex pattern", e);
134+
return false;
135+
}
130136
}
131137

132138
private boolean matchSegmentCondition(FPUser user, Map<String, Segment> segments) {

src/test/groovy/com/featureprobe/sdk/server/ConditionSpec.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ class ConditionSpec extends Specification {
9898
!hitMiss
9999
}
100100

101+
def "[matches invalid regex] string condition match"() {
102+
when:
103+
condition.setObjects(["\\\\\\"])
104+
user.with("userId", "13797347245")
105+
condition.setPredicate(PredicateType.MATCHES_REGEX)
106+
def hitMiss = condition.matchObjects(user, segments)
107+
condition.setPredicate(PredicateType.DOES_NOT_MATCH_REGEX)
108+
def hitMiss2 = condition.matchObjects(user, segments)
109+
110+
then:
111+
!hitMiss
112+
!hitMiss2
113+
}
114+
101115
def "[is not any of] string condition match"() {
102116
when:
103117
condition.setObjects(["12345", "987654", "665544"])

0 commit comments

Comments
 (0)