diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d1082fa9d2..adbc32dae87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ SentryAndroid.init( ### Fixes - Fix missing thread stacks for ANRv1 events ([#4918](https://github.com/getsentry/sentry-java/pull/4918)) +- Fix handling of unparseable mime-type on request filter ([#4939](https://github.com/getsentry/sentry-java/pull/4939)) ### Internal diff --git a/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java b/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java index c709cebbca7..38bc4379088 100644 --- a/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java +++ b/sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java @@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.http.MediaType; +import org.springframework.util.InvalidMimeTypeException; import org.springframework.util.MimeType; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.ContentCachingRequestWrapper; @@ -131,8 +132,12 @@ && shouldCacheMimeType(contentType) } private static boolean shouldCacheMimeType(String contentType) { - return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON) - || MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED); + try { + return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON) + || MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED); + } catch (InvalidMimeTypeException e) { + return false; + } } static final class RequestBodyExtractingEventProcessor implements EventProcessor { diff --git a/sentry-spring-7/src/test/kotlin/io/sentry/spring7/SentrySpringFilterTest.kt b/sentry-spring-7/src/test/kotlin/io/sentry/spring7/SentrySpringFilterTest.kt index 639b1642a51..5a83c9d72a4 100644 --- a/sentry-spring-7/src/test/kotlin/io/sentry/spring7/SentrySpringFilterTest.kt +++ b/sentry-spring-7/src/test/kotlin/io/sentry/spring7/SentrySpringFilterTest.kt @@ -266,6 +266,12 @@ class SentrySpringFilterTest { body = "x".repeat(10001), expectedToBeCached = false, ), + TestParams( + maxRequestBodySize = MEDIUM, + body = "xxx", + expectedToBeCached = false, + contentType = "invalid", + ), TestParams( maxRequestBodySize = ALWAYS, body = "x".repeat(10001), diff --git a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java index 34dd5f0c469..c51a2053b8d 100644 --- a/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java +++ b/sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java @@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.http.MediaType; +import org.springframework.util.InvalidMimeTypeException; import org.springframework.util.MimeType; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.ContentCachingRequestWrapper; @@ -131,8 +132,12 @@ && shouldCacheMimeType(contentType) } private static boolean shouldCacheMimeType(String contentType) { - return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON) - || MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED); + try { + return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON) + || MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED); + } catch (InvalidMimeTypeException e) { + return false; + } } static final class RequestBodyExtractingEventProcessor implements EventProcessor { diff --git a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt index 6c8db2fc097..349839b5d15 100644 --- a/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt +++ b/sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt @@ -266,6 +266,12 @@ class SentrySpringFilterTest { body = "x".repeat(10001), expectedToBeCached = false, ), + TestParams( + maxRequestBodySize = MEDIUM, + body = "xxx", + expectedToBeCached = false, + contentType = "invalid", + ), TestParams( maxRequestBodySize = ALWAYS, body = "x".repeat(10001), diff --git a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java index 4d8767334ae..69438c82617 100644 --- a/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java +++ b/sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java @@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.springframework.http.MediaType; +import org.springframework.util.InvalidMimeTypeException; import org.springframework.util.MimeType; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.ContentCachingRequestWrapper; @@ -131,8 +132,12 @@ && shouldCacheMimeType(contentType) } private static boolean shouldCacheMimeType(String contentType) { - return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON) - || MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED); + try { + return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON) + || MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED); + } catch (InvalidMimeTypeException e) { + return false; + } } static final class RequestBodyExtractingEventProcessor implements EventProcessor { diff --git a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt index 06bea9fc9de..eb145bcd8a1 100644 --- a/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt +++ b/sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt @@ -266,6 +266,12 @@ class SentrySpringFilterTest { body = "x".repeat(10001), expectedToBeCached = false, ), + TestParams( + maxRequestBodySize = MEDIUM, + body = "xxx", + expectedToBeCached = false, + contentType = "invalid", + ), TestParams( maxRequestBodySize = ALWAYS, body = "x".repeat(10001),