Skip to content

Commit 4e23b92

Browse files
authored
Attach request body for application/x-www-form-urlencoded requests in Spring (#3731)
* attach request body for application/x-www-form-urlencoded * extend tests * changelog
1 parent cfc5405 commit 4e23b92

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
- You may now set `forceInit=true` (`force-init` for `.properties` files) to ensure a call to Sentry.init / SentryAndroid.init takes effect
2222
- Add force init option to Android Manifest ([#3675](https://github.com/getsentry/sentry-java/pull/3675))
2323
- Use `<meta-data android:name="io.sentry.force-init" android:value="true" />` to ensure Sentry Android auto init is not easily overwritten
24+
- Attach request body for `application/x-www-form-urlencoded` requests in Spring ([#3731](https://github.com/getsentry/sentry-java/pull/3731))
25+
- Previously request body was only attached for `application/json` requests
2426

2527
### Fixes
2628

sentry-spring-boot-jakarta/api/sentry-spring-boot-jakarta.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public class io/sentry/spring/boot/jakarta/SentryLogbackAppenderAutoConfiguratio
1818
public fun sentryLogbackInitializer (Lio/sentry/spring/boot/jakarta/SentryProperties;)Lio/sentry/spring/boot/jakarta/SentryLogbackInitializer;
1919
}
2020

21+
public class io/sentry/spring/boot/jakarta/SentryLogbackInitializer : org/springframework/context/event/GenericApplicationListener {
22+
public fun <init> (Lio/sentry/spring/boot/jakarta/SentryProperties;)V
23+
public fun onApplicationEvent (Lorg/springframework/context/ApplicationEvent;)V
24+
public fun supportsEventType (Lorg/springframework/core/ResolvableType;)Z
25+
}
26+
2127
public class io/sentry/spring/boot/jakarta/SentryProperties : io/sentry/SentryOptions {
2228
public fun <init> ()V
2329
public fun getExceptionResolverOrder ()I

sentry-spring-boot/api/sentry-spring-boot.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public class io/sentry/spring/boot/SentryLogbackAppenderAutoConfiguration {
1818
public fun sentryLogbackInitializer (Lio/sentry/spring/boot/SentryProperties;)Lio/sentry/spring/boot/SentryLogbackInitializer;
1919
}
2020

21+
public class io/sentry/spring/boot/SentryLogbackInitializer : org/springframework/context/event/GenericApplicationListener {
22+
public fun <init> (Lio/sentry/spring/boot/SentryProperties;)V
23+
public fun onApplicationEvent (Lorg/springframework/context/ApplicationEvent;)V
24+
public fun supportsEventType (Lorg/springframework/core/ResolvableType;)Z
25+
}
26+
2127
public class io/sentry/spring/boot/SentryProperties : io/sentry/SentryOptions {
2228
public fun <init> ()V
2329
public fun getExceptionResolverOrder ()I

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,17 @@ private boolean qualifiesForCaching(
134134
return maxRequestBodySize != RequestSize.NONE
135135
&& contentLength != -1
136136
&& contentType != null
137-
&& MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
137+
&& shouldCacheMimeType(contentType)
138138
&& ((maxRequestBodySize == SMALL && contentLength < 1000)
139139
|| (maxRequestBodySize == MEDIUM && contentLength < 10000)
140140
|| maxRequestBodySize == ALWAYS);
141141
}
142142

143+
private static boolean shouldCacheMimeType(String contentType) {
144+
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
145+
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
146+
}
147+
143148
static final class RequestBodyExtractingEventProcessor implements EventProcessor {
144149
private final @NotNull RequestPayloadExtractor requestPayloadExtractor =
145150
new RequestPayloadExtractor();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ class SentrySpringFilterTest {
248248
TestParams(maxRequestBodySize = SMALL, body = "x".repeat(1001), expectedToBeCached = false),
249249
TestParams(maxRequestBodySize = MEDIUM, body = "x".repeat(1001), expectedToBeCached = true),
250250
TestParams(maxRequestBodySize = MEDIUM, body = "x".repeat(10001), expectedToBeCached = false),
251-
TestParams(maxRequestBodySize = ALWAYS, body = "x".repeat(10001), expectedToBeCached = true)
251+
TestParams(maxRequestBodySize = ALWAYS, body = "x".repeat(10001), expectedToBeCached = true),
252+
TestParams(maxRequestBodySize = SMALL, body = "xxx", contentType = "application/x-www-form-urlencoded", expectedToBeCached = true)
252253
)
253254

254255
params.forEach { param ->

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,17 @@ private boolean qualifiesForCaching(
134134
return maxRequestBodySize != RequestSize.NONE
135135
&& contentLength != -1
136136
&& contentType != null
137-
&& MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
137+
&& shouldCacheMimeType(contentType)
138138
&& ((maxRequestBodySize == SMALL && contentLength < 1000)
139139
|| (maxRequestBodySize == MEDIUM && contentLength < 10000)
140140
|| maxRequestBodySize == ALWAYS);
141141
}
142142

143+
private static boolean shouldCacheMimeType(String contentType) {
144+
return MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_JSON)
145+
|| MimeType.valueOf(contentType).isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED);
146+
}
147+
143148
static final class RequestBodyExtractingEventProcessor implements EventProcessor {
144149
private final @NotNull RequestPayloadExtractor requestPayloadExtractor =
145150
new RequestPayloadExtractor();

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ class SentrySpringFilterTest {
248248
TestParams(maxRequestBodySize = SMALL, body = "x".repeat(1001), expectedToBeCached = false),
249249
TestParams(maxRequestBodySize = MEDIUM, body = "x".repeat(1001), expectedToBeCached = true),
250250
TestParams(maxRequestBodySize = MEDIUM, body = "x".repeat(10001), expectedToBeCached = false),
251-
TestParams(maxRequestBodySize = ALWAYS, body = "x".repeat(10001), expectedToBeCached = true)
251+
TestParams(maxRequestBodySize = ALWAYS, body = "x".repeat(10001), expectedToBeCached = true),
252+
TestParams(maxRequestBodySize = SMALL, body = "xxx", contentType = "application/x-www-form-urlencoded", expectedToBeCached = true)
252253
)
253254

254255
params.forEach { param ->

0 commit comments

Comments
 (0)