Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter strings that cannot be parsed as Regex no longer cause an SDK crash #4213

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth raising the level to Warning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also be intentional that a filter String isn't meant as a regex and just contains chars not applicable for a regex. I wouldn't consider that something worth WARNING about.

"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)
}
}
Loading