Skip to content

Commit c1a567b

Browse files
authored
Filter strings that cannot be parsed as Regex no longer cause an SDK crash (#4213)
* Fix exception when creating FilterString from string that cannot be parsed as regex * changelog
1 parent f64d1f2 commit c1a567b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
### Fixes
2222

23+
- Filter strings that cannot be parsed as Regex no longer cause an SDK crash ([#4213](https://github.com/getsentry/sentry-java/pull/4213))
24+
- This was the case e.g. for `ignoredErrors`, `ignoredTransactions` and `ignoredCheckIns`
25+
- We now simply don't use such strings for Regex matching and only use them for String comparison
2326
- `SentryOptions.setTracePropagationTargets` is no longer marked internal ([#4170](https://github.com/getsentry/sentry-java/pull/4170))
2427
- Session Replay: Fix crash when a navigation breadcrumb does not have "to" destination ([#4185](https://github.com/getsentry/sentry-java/pull/4185))
2528
- Session Replay: Cap video segment duration to maximum 5 minutes to prevent endless video encoding in background ([#4185](https://github.com/getsentry/sentry-java/pull/4185))

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)