Skip to content

Commit cecefcc

Browse files
Added try-catch to validate the regex passed to matches in SpanFilterMatcher (#166)
* Added try-catch to validate the regex passed * Addressed review comments * Unwanted change * Addressed review comments
1 parent 1e69d80 commit cecefcc

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

span-processing-utils/src/main/java/org/hypertrace/config/span/processing/utils/SpanFilterMatcher.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,18 @@ private boolean matches(String lhs, String rhs, RelationalOperator relationalOpe
111111
case RELATIONAL_OPERATOR_ENDS_WITH:
112112
return lhs.endsWith(rhs);
113113
case RELATIONAL_OPERATOR_REGEX_MATCH:
114-
return Pattern.compile(rhs).matcher(lhs).find();
114+
try {
115+
return Pattern.compile(rhs).matcher(lhs).find();
116+
} catch (Exception e) {
117+
log.error("Invalid regex: {} passed to match: {}", rhs, e);
118+
if (log.isDebugEnabled()) {
119+
log.debug(
120+
"Invalid regex passed to match. Hence returning false. lhs: {} and rhs: {}",
121+
lhs,
122+
rhs);
123+
}
124+
return false;
125+
}
115126
default:
116127
log.error("Unsupported relational operator for string value rhs:{}", relationalOperator);
117128
return false;

span-processing-utils/src/test/java/org/hypertrace/config/span/processing/utils/SpanFilterMatcherTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ void testMatcher() {
9393
"name",
9494
buildSpanFilterValue("[a-zA-z]+[0-9]+"),
9595
RelationalOperator.RELATIONAL_OPERATOR_REGEX_MATCH));
96+
assertFalse(
97+
this.spanFilterMatcher.matches(
98+
"name",
99+
buildSpanFilterValue("[(name"),
100+
RelationalOperator.RELATIONAL_OPERATOR_REGEX_MATCH));
96101

97102
assertTrue(
98103
this.spanFilterMatcher.matches(

0 commit comments

Comments
 (0)