Skip to content

Commit 10be7df

Browse files
fix: handle unparseable mime-type (#4939)
* fix: handle unparseable mime-type * fix: apply same catch on sentry-spring-7 * chore: add CHANGELOG * change Spring Boot 3 / Spring 6 implementation --------- Co-authored-by: Alexander Dinauer <[email protected]> Co-authored-by: Alexander Dinauer <[email protected]>
1 parent d36d909 commit 10be7df

File tree

7 files changed

+40
-6
lines changed

7 files changed

+40
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ SentryAndroid.init(
5959
### Fixes
6060

6161
- Fix missing thread stacks for ANRv1 events ([#4918](https://github.com/getsentry/sentry-java/pull/4918))
62+
- Fix handling of unparseable mime-type on request filter ([#4939](https://github.com/getsentry/sentry-java/pull/4939))
6263

6364
### Internal
6465

sentry-spring-7/src/main/java/io/sentry/spring7/SentrySpringFilter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jetbrains.annotations.NotNull;
2727
import org.jetbrains.annotations.Nullable;
2828
import org.springframework.http.MediaType;
29+
import org.springframework.util.InvalidMimeTypeException;
2930
import org.springframework.util.MimeType;
3031
import org.springframework.web.filter.OncePerRequestFilter;
3132
import org.springframework.web.util.ContentCachingRequestWrapper;
@@ -131,8 +132,12 @@ && shouldCacheMimeType(contentType)
131132
}
132133

133134
private static boolean shouldCacheMimeType(String contentType) {
134-
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
135-
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
135+
try {
136+
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
137+
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
138+
} catch (InvalidMimeTypeException e) {
139+
return false;
140+
}
136141
}
137142

138143
static final class RequestBodyExtractingEventProcessor implements EventProcessor {

sentry-spring-7/src/test/kotlin/io/sentry/spring7/SentrySpringFilterTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ class SentrySpringFilterTest {
266266
body = "x".repeat(10001),
267267
expectedToBeCached = false,
268268
),
269+
TestParams(
270+
maxRequestBodySize = MEDIUM,
271+
body = "xxx",
272+
expectedToBeCached = false,
273+
contentType = "invalid",
274+
),
269275
TestParams(
270276
maxRequestBodySize = ALWAYS,
271277
body = "x".repeat(10001),

sentry-spring-jakarta/src/main/java/io/sentry/spring/jakarta/SentrySpringFilter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jetbrains.annotations.NotNull;
2727
import org.jetbrains.annotations.Nullable;
2828
import org.springframework.http.MediaType;
29+
import org.springframework.util.InvalidMimeTypeException;
2930
import org.springframework.util.MimeType;
3031
import org.springframework.web.filter.OncePerRequestFilter;
3132
import org.springframework.web.util.ContentCachingRequestWrapper;
@@ -131,8 +132,12 @@ && shouldCacheMimeType(contentType)
131132
}
132133

133134
private static boolean shouldCacheMimeType(String contentType) {
134-
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
135-
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
135+
try {
136+
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
137+
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
138+
} catch (InvalidMimeTypeException e) {
139+
return false;
140+
}
136141
}
137142

138143
static final class RequestBodyExtractingEventProcessor implements EventProcessor {

sentry-spring-jakarta/src/test/kotlin/io/sentry/spring/jakarta/SentrySpringFilterTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ class SentrySpringFilterTest {
266266
body = "x".repeat(10001),
267267
expectedToBeCached = false,
268268
),
269+
TestParams(
270+
maxRequestBodySize = MEDIUM,
271+
body = "xxx",
272+
expectedToBeCached = false,
273+
contentType = "invalid",
274+
),
269275
TestParams(
270276
maxRequestBodySize = ALWAYS,
271277
body = "x".repeat(10001),

sentry-spring/src/main/java/io/sentry/spring/SentrySpringFilter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.jetbrains.annotations.NotNull;
2727
import org.jetbrains.annotations.Nullable;
2828
import org.springframework.http.MediaType;
29+
import org.springframework.util.InvalidMimeTypeException;
2930
import org.springframework.util.MimeType;
3031
import org.springframework.web.filter.OncePerRequestFilter;
3132
import org.springframework.web.util.ContentCachingRequestWrapper;
@@ -131,8 +132,12 @@ && shouldCacheMimeType(contentType)
131132
}
132133

133134
private static boolean shouldCacheMimeType(String contentType) {
134-
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
135-
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
135+
try {
136+
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
137+
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
138+
} catch (InvalidMimeTypeException e) {
139+
return false;
140+
}
136141
}
137142

138143
static final class RequestBodyExtractingEventProcessor implements EventProcessor {

sentry-spring/src/test/kotlin/io/sentry/spring/SentrySpringFilterTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ class SentrySpringFilterTest {
266266
body = "x".repeat(10001),
267267
expectedToBeCached = false,
268268
),
269+
TestParams(
270+
maxRequestBodySize = MEDIUM,
271+
body = "xxx",
272+
expectedToBeCached = false,
273+
contentType = "invalid",
274+
),
269275
TestParams(
270276
maxRequestBodySize = ALWAYS,
271277
body = "x".repeat(10001),

0 commit comments

Comments
 (0)