Skip to content

Commit

Permalink
Filter strings that cannot be parsed as Regex no longer cause an SDK …
Browse files Browse the repository at this point in the history
…crash (#4213)

* Fix exception when creating FilterString from string that cannot be parsed as regex

* changelog
  • Loading branch information
adinauer authored Feb 26, 2025
1 parent f64d1f2 commit c1a567b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

### Fixes

- Filter strings that cannot be parsed as Regex no longer cause an SDK crash ([#4213](https://github.com/getsentry/sentry-java/pull/4213))
- This was the case e.g. for `ignoredErrors`, `ignoredTransactions` and `ignoredCheckIns`
- We now simply don't use such strings for Regex matching and only use them for String comparison
- `SentryOptions.setTracePropagationTargets` is no longer marked internal ([#4170](https://github.com/getsentry/sentry-java/pull/4170))
- Session Replay: Fix crash when a navigation breadcrumb does not have "to" destination ([#4185](https://github.com/getsentry/sentry-java/pull/4185))
- 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))
Expand Down
20 changes: 18 additions & 2 deletions sentry/src/main/java/io/sentry/FilterString.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@
import java.util.Objects;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class FilterString {
private final @NotNull String filterString;
private final @NotNull Pattern pattern;
private final @Nullable Pattern pattern;

public FilterString(@NotNull String filterString) {
this.filterString = filterString;
this.pattern = Pattern.compile(filterString);
@Nullable Pattern pattern = null;
try {
pattern = Pattern.compile(filterString);
} catch (Throwable t) {
Sentry.getCurrentScopes()
.getOptions()
.getLogger()
.log(
SentryLevel.DEBUG,
"Only using filter string for String comparison as it could not be parsed as regex: %s",
filterString);
}
this.pattern = pattern;
}

public @NotNull String getFilterString() {
return filterString;
}

public boolean matches(String input) {
if (pattern == null) {
return false;
}
return pattern.matcher(input).matches();
}

Expand Down
24 changes: 24 additions & 0 deletions sentry/src/test/java/io/sentry/FilterStringTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.sentry

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class FilterStringTest {

@Test
fun `turns string into pattern`() {
val filterString = FilterString(".*")
assertTrue(filterString.matches("anything"))
assertEquals(".*", filterString.filterString)
}

@Test
fun `skips pattern if not a valid regex`() {
// does not throw if the string is not a valid pattern
val filterString = FilterString("I love my mustache {")
assertFalse(filterString.matches("I love my mustache {"))
assertEquals("I love my mustache {", filterString.filterString)
}
}

0 comments on commit c1a567b

Please sign in to comment.