Skip to content

Commit 34b69d0

Browse files
authored
Merge pull request #5802 from getsentry/feat/data-collection-incoming-request-body
feat(spring): [Data Collection 6] Apply incoming request body policy
2 parents 77067c9 + 6ea6d11 commit 34b69d0

6 files changed

Lines changed: 147 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void configureScope(
110110

111111
private @NotNull HttpServletRequest resolveHttpServletRequest(
112112
final @NotNull IScopes scopes, final @NotNull HttpServletRequest request) {
113-
if (scopes.getOptions().isSendDefaultPii()
113+
if (scopes.getOptions().getDataCollectionResolver().isIncomingRequestBody()
114114
&& qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) {
115115
return new ContentCachingRequestWrapper(request, 0);
116116
}
@@ -155,7 +155,7 @@ public RequestBodyExtractingEventProcessor(
155155
@Override
156156
public @NotNull SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) {
157157
if (event.getRequest() != null
158-
&& options.isSendDefaultPii()
158+
&& options.getDataCollectionResolver().isIncomingRequestBody()
159159
&& qualifiesForCaching(request, options.getMaxRequestBodySize())) {
160160
event.getRequest().setData(requestPayloadExtractor.extract(request, options));
161161
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry.spring7
22

33
import io.sentry.Breadcrumb
4+
import io.sentry.HttpBodyType
45
import io.sentry.IScope
56
import io.sentry.IScopes
67
import io.sentry.ISentryLifecycleToken
@@ -318,6 +319,52 @@ class SentrySpringFilterTest {
318319
}
319320
}
320321

322+
@Test
323+
fun `data collection can enable request body when sendDefaultPii is false`() {
324+
val options =
325+
SentryOptions().apply {
326+
isSendDefaultPii = false
327+
maxRequestBodySize = SMALL
328+
dataCollection.httpBodies = setOf(HttpBodyType.INCOMING_REQUEST)
329+
}
330+
val listener =
331+
fixture.getSut(
332+
request =
333+
MockMvcRequestBuilders.post(URI.create("http://example.com"))
334+
.content("xxx")
335+
.contentType("application/json")
336+
.buildRequest(MockServletContext()),
337+
options = options,
338+
)
339+
340+
listener.doFilter(fixture.request, fixture.response, fixture.chain)
341+
342+
verify(fixture.chain).doFilter(check { assertTrue(it is ContentCachingRequestWrapper) }, any())
343+
}
344+
345+
@Test
346+
fun `data collection can disable request body when sendDefaultPii is true`() {
347+
val options =
348+
SentryOptions().apply {
349+
isSendDefaultPii = true
350+
maxRequestBodySize = SMALL
351+
dataCollection.httpBodies = emptySet()
352+
}
353+
val listener =
354+
fixture.getSut(
355+
request =
356+
MockMvcRequestBuilders.post(URI.create("http://example.com"))
357+
.content("xxx")
358+
.contentType("application/json")
359+
.buildRequest(MockServletContext()),
360+
options = options,
361+
)
362+
363+
listener.doFilter(fixture.request, fixture.response, fixture.chain)
364+
365+
verify(fixture.chain).doFilter(check { assertFalse(it is ContentCachingRequestWrapper) }, any())
366+
}
367+
321368
private fun servletContextWithCustomCookieName(name: String): ServletContext =
322369
MockServletContext().also { it.sessionCookieConfig.name = name }
323370
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void configureScope(
110110

111111
private @NotNull HttpServletRequest resolveHttpServletRequest(
112112
final @NotNull IScopes scopes, final @NotNull HttpServletRequest request) {
113-
if (scopes.getOptions().isSendDefaultPii()
113+
if (scopes.getOptions().getDataCollectionResolver().isIncomingRequestBody()
114114
&& qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) {
115115
return new ContentCachingRequestWrapper(request);
116116
}
@@ -155,7 +155,7 @@ public RequestBodyExtractingEventProcessor(
155155
@Override
156156
public @NotNull SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) {
157157
if (event.getRequest() != null
158-
&& options.isSendDefaultPii()
158+
&& options.getDataCollectionResolver().isIncomingRequestBody()
159159
&& qualifiesForCaching(request, options.getMaxRequestBodySize())) {
160160
event.getRequest().setData(requestPayloadExtractor.extract(request, options));
161161
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry.spring.jakarta
22

33
import io.sentry.Breadcrumb
4+
import io.sentry.HttpBodyType
45
import io.sentry.IScope
56
import io.sentry.IScopes
67
import io.sentry.ISentryLifecycleToken
@@ -318,6 +319,52 @@ class SentrySpringFilterTest {
318319
}
319320
}
320321

322+
@Test
323+
fun `data collection can enable request body when sendDefaultPii is false`() {
324+
val options =
325+
SentryOptions().apply {
326+
isSendDefaultPii = false
327+
maxRequestBodySize = SMALL
328+
dataCollection.httpBodies = setOf(HttpBodyType.INCOMING_REQUEST)
329+
}
330+
val listener =
331+
fixture.getSut(
332+
request =
333+
MockMvcRequestBuilders.post(URI.create("http://example.com"))
334+
.content("xxx")
335+
.contentType("application/json")
336+
.buildRequest(MockServletContext()),
337+
options = options,
338+
)
339+
340+
listener.doFilter(fixture.request, fixture.response, fixture.chain)
341+
342+
verify(fixture.chain).doFilter(check { assertTrue(it is ContentCachingRequestWrapper) }, any())
343+
}
344+
345+
@Test
346+
fun `data collection can disable request body when sendDefaultPii is true`() {
347+
val options =
348+
SentryOptions().apply {
349+
isSendDefaultPii = true
350+
maxRequestBodySize = SMALL
351+
dataCollection.httpBodies = emptySet()
352+
}
353+
val listener =
354+
fixture.getSut(
355+
request =
356+
MockMvcRequestBuilders.post(URI.create("http://example.com"))
357+
.content("xxx")
358+
.contentType("application/json")
359+
.buildRequest(MockServletContext()),
360+
options = options,
361+
)
362+
363+
listener.doFilter(fixture.request, fixture.response, fixture.chain)
364+
365+
verify(fixture.chain).doFilter(check { assertFalse(it is ContentCachingRequestWrapper) }, any())
366+
}
367+
321368
private fun servletContextWithCustomCookieName(name: String): ServletContext =
322369
MockServletContext().also { it.sessionCookieConfig.name = name }
323370
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private void configureScope(
110110

111111
private @NotNull HttpServletRequest resolveHttpServletRequest(
112112
final @NotNull IScopes scopes, final @NotNull HttpServletRequest request) {
113-
if (scopes.getOptions().isSendDefaultPii()
113+
if (scopes.getOptions().getDataCollectionResolver().isIncomingRequestBody()
114114
&& qualifiesForCaching(request, scopes.getOptions().getMaxRequestBodySize())) {
115115
return new ContentCachingRequestWrapper(request);
116116
}
@@ -155,7 +155,7 @@ public RequestBodyExtractingEventProcessor(
155155
@Override
156156
public @NotNull SentryEvent process(@NotNull SentryEvent event, @NotNull Hint hint) {
157157
if (event.getRequest() != null
158-
&& options.isSendDefaultPii()
158+
&& options.getDataCollectionResolver().isIncomingRequestBody()
159159
&& qualifiesForCaching(request, options.getMaxRequestBodySize())) {
160160
event.getRequest().setData(requestPayloadExtractor.extract(request, options));
161161
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry.spring
22

33
import io.sentry.Breadcrumb
4+
import io.sentry.HttpBodyType
45
import io.sentry.IScope
56
import io.sentry.IScopes
67
import io.sentry.ISentryLifecycleToken
@@ -318,6 +319,52 @@ class SentrySpringFilterTest {
318319
}
319320
}
320321

322+
@Test
323+
fun `data collection can enable request body when sendDefaultPii is false`() {
324+
val options =
325+
SentryOptions().apply {
326+
isSendDefaultPii = false
327+
maxRequestBodySize = SMALL
328+
dataCollection.httpBodies = setOf(HttpBodyType.INCOMING_REQUEST)
329+
}
330+
val listener =
331+
fixture.getSut(
332+
request =
333+
MockMvcRequestBuilders.post(URI.create("http://example.com"))
334+
.content("xxx")
335+
.contentType("application/json")
336+
.buildRequest(MockServletContext()),
337+
options = options,
338+
)
339+
340+
listener.doFilter(fixture.request, fixture.response, fixture.chain)
341+
342+
verify(fixture.chain).doFilter(check { assertTrue(it is ContentCachingRequestWrapper) }, any())
343+
}
344+
345+
@Test
346+
fun `data collection can disable request body when sendDefaultPii is true`() {
347+
val options =
348+
SentryOptions().apply {
349+
isSendDefaultPii = true
350+
maxRequestBodySize = SMALL
351+
dataCollection.httpBodies = emptySet()
352+
}
353+
val listener =
354+
fixture.getSut(
355+
request =
356+
MockMvcRequestBuilders.post(URI.create("http://example.com"))
357+
.content("xxx")
358+
.contentType("application/json")
359+
.buildRequest(MockServletContext()),
360+
options = options,
361+
)
362+
363+
listener.doFilter(fixture.request, fixture.response, fixture.chain)
364+
365+
verify(fixture.chain).doFilter(check { assertFalse(it is ContentCachingRequestWrapper) }, any())
366+
}
367+
321368
private fun servletContextWithCustomCookieName(name: String): ServletContext =
322369
MockServletContext().also { it.sessionCookieConfig.name = name }
323370
}

0 commit comments

Comments
 (0)