Skip to content

Commit f384493

Browse files
committed
Fix exception when creating FilterString from string that cannot be parsed as regex
1 parent f64d1f2 commit f384493

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

sentry/src/main/java/io/sentry/FilterString.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,37 @@
33
import java.util.Objects;
44
import java.util.regex.Pattern;
55
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
67

78
public final class FilterString {
89
private final @NotNull String filterString;
9-
private final @NotNull Pattern pattern;
10+
private final @Nullable Pattern pattern;
1011

1112
public FilterString(@NotNull String filterString) {
1213
this.filterString = filterString;
13-
this.pattern = Pattern.compile(filterString);
14+
@Nullable Pattern pattern = null;
15+
try {
16+
pattern = Pattern.compile(filterString);
17+
} catch (Throwable t) {
18+
Sentry.getCurrentScopes()
19+
.getOptions()
20+
.getLogger()
21+
.log(
22+
SentryLevel.DEBUG,
23+
"Only using filter string for String comparison as it could not be parsed as regex: %s",
24+
filterString);
25+
}
26+
this.pattern = pattern;
1427
}
1528

1629
public @NotNull String getFilterString() {
1730
return filterString;
1831
}
1932

2033
public boolean matches(String input) {
34+
if (pattern == null) {
35+
return false;
36+
}
2137
return pattern.matcher(input).matches();
2238
}
2339

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.sentry
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertFalse
6+
import kotlin.test.assertTrue
7+
8+
class FilterStringTest {
9+
10+
@Test
11+
fun `turns string into pattern`() {
12+
val filterString = FilterString(".*")
13+
assertTrue(filterString.matches("anything"))
14+
assertEquals(".*", filterString.filterString)
15+
}
16+
17+
@Test
18+
fun `skips pattern if not a valid regex`() {
19+
// does not throw if the string is not a valid pattern
20+
val filterString = FilterString("I love my mustache {")
21+
assertFalse(filterString.matches("I love my mustache {"))
22+
assertEquals("I love my mustache {", filterString.filterString)
23+
}
24+
}

0 commit comments

Comments
 (0)