diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fbd38a3f7..4e6c20beb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/sentry/src/main/java/io/sentry/FilterString.java b/sentry/src/main/java/io/sentry/FilterString.java index 8dd5f47949..3fd146aa4e 100644 --- a/sentry/src/main/java/io/sentry/FilterString.java +++ b/sentry/src/main/java/io/sentry/FilterString.java @@ -3,14 +3,27 @@ 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() { @@ -18,6 +31,9 @@ public FilterString(@NotNull String filterString) { } public boolean matches(String input) { + if (pattern == null) { + return false; + } return pattern.matcher(input).matches(); } diff --git a/sentry/src/test/java/io/sentry/FilterStringTest.kt b/sentry/src/test/java/io/sentry/FilterStringTest.kt new file mode 100644 index 0000000000..a253a91eb8 --- /dev/null +++ b/sentry/src/test/java/io/sentry/FilterStringTest.kt @@ -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) + } +}