Skip to content

Commit 4b30647

Browse files
committed
bug: fix NullPointerException if allowedHeaders contains null
review comment - https://github.com/getsentry/sentry-java/pull/4919/files#r2550496731 seems possible, e.g. if a client passes null in the array to SentryReplayOptions#set[Request|Response]Headers
1 parent e6d7289 commit 4b30647

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

sentry/src/main/java/io/sentry/util/network/NetworkDetailCaptureUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ private static boolean shouldCaptureUrl(
130130
// Convert to lowercase for case-insensitive matching
131131
Set<String> normalizedAllowed = new HashSet<>();
132132
for (String header : allowedHeaders) {
133-
normalizedAllowed.add(header.toLowerCase());
133+
if (header != null) {
134+
normalizedAllowed.add(header.toLowerCase());
135+
}
134136
}
135137

136138
for (Map.Entry<String, String> entry : allHeaders.entrySet()) {

sentry/src/test/java/io/sentry/util/network/NetworkDetailCaptureUtilsTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,27 @@ class NetworkDetailCaptureUtilsTest {
7878
// Unwanted header should not be present
7979
assertTrue(!result.containsKey("X-Unwanted-Header"))
8080
}
81+
82+
@Test
83+
fun `getCaptureHeaders should handle null elements in allowedHeaders`() {
84+
val allHeaders =
85+
mapOf(
86+
"Content-Type" to "application/json",
87+
"Authorization" to "Bearer token123",
88+
"X-Custom-Header" to "custom-value",
89+
)
90+
91+
// allowedHeaders contains null elements which should be ignored
92+
val allowedHeaders = arrayOf(null, "content-type", null, "authorization", null)
93+
94+
val result = NetworkDetailCaptureUtils.getCaptureHeaders(allHeaders, allowedHeaders)
95+
96+
// Only non-null allowed headers should be matched
97+
assertEquals(2, result.size)
98+
assertEquals("application/json", result["Content-Type"])
99+
assertEquals("Bearer token123", result["Authorization"])
100+
101+
// X-Custom-Header should not be present as it's not in the allowed list
102+
assertTrue(!result.containsKey("X-Custom-Header"))
103+
}
81104
}

0 commit comments

Comments
 (0)