Skip to content

Commit

Permalink
Merge f384493 into f64d1f2
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer authored Feb 26, 2025
2 parents f64d1f2 + f384493 commit 86bce89
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
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 86bce89

Please sign in to comment.