Skip to content

Commit 9c26cdd

Browse files
authored
perf(core): Avoid repeated sensitive denylist normalization (#6704)
Match built-in sensitive terms in their existing lowercase form and normalize only the input key. Reuse this path for query parameters and headers while preserving case-insensitive matching for caller-configured terms. Cover mixed-case matching, sensitive allowlist precedence, empty terms, and locale independence.
1 parent e71aef1 commit 9c26cdd

2 files changed

Lines changed: 130 additions & 3 deletions

File tree

‎sentry/src/main/java/io/sentry/util/HttpUtils.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public final class HttpUtils {
3333
"X-CSRFTOKEN",
3434
"X-XSRF-TOKEN");
3535

36+
// Keep these terms lowercase so sensitive-key checks only normalize the input key.
3637
private static final List<String> SENSITIVE_DATA_KEYS =
3738
Arrays.asList(
3839
"auth",
@@ -80,7 +81,7 @@ public static boolean containsSensitiveHeader(final @NotNull String header) {
8081
final int separator = param.indexOf('=');
8182
final @NotNull String name = separator < 0 ? param : param.substring(0, separator);
8283
final @NotNull String decodedName = decodeQueryParamName(name);
83-
final boolean sensitive = containsTerm(decodedName, SENSITIVE_DATA_KEYS);
84+
final boolean sensitive = containsSensitiveDataKey(decodedName);
8485
final boolean matchesTerm = containsTerm(decodedName, behavior.getTerms());
8586
final boolean shouldFilter =
8687
sensitive
@@ -108,7 +109,7 @@ public static boolean containsSensitiveHeader(final @NotNull String header) {
108109
for (final Map.Entry<String, String> header : headers.entrySet()) {
109110
final @NotNull String name = header.getKey();
110111
final boolean sensitive =
111-
containsTerm(name, SENSITIVE_DATA_KEYS)
112+
containsSensitiveDataKey(name)
112113
|| "Cookie".equalsIgnoreCase(name)
113114
|| "Set-Cookie".equalsIgnoreCase(name);
114115
if (sensitive) {
@@ -136,7 +137,13 @@ public static boolean containsSensitiveHeader(final @NotNull String header) {
136137
}
137138

138139
static boolean containsSensitiveDataKey(final @NotNull String key) {
139-
return containsTerm(key, SENSITIVE_DATA_KEYS);
140+
final @NotNull String normalizedKey = key.toLowerCase(Locale.ROOT);
141+
for (final String term : SENSITIVE_DATA_KEYS) {
142+
if (normalizedKey.contains(term)) {
143+
return true;
144+
}
145+
}
146+
return false;
140147
}
141148

142149
static boolean containsTerm(final @NotNull String key, final @NotNull List<String> terms) {

‎sentry/src/test/java/io/sentry/util/HttpUtilsTest.kt‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.sentry.util
22

33
import com.google.common.truth.Truth.assertThat
44
import io.sentry.KeyValueCollectionBehavior
5+
import java.util.Locale
56
import kotlin.test.Test
67

78
class HttpUtilsTest {
@@ -21,6 +22,17 @@ class HttpUtilsTest {
2122
.isEqualTo("name=value&access_token=[Filtered]&customerId=[Filtered]")
2223
}
2324

25+
@Test
26+
fun `query parameter deny list matches sensitive names and configured terms regardless of case`() {
27+
assertThat(
28+
HttpUtils.filterQueryParams(
29+
"name=value&ACCESS_TOKEN=secret&customerId=123",
30+
KeyValueCollectionBehavior.denyList("CuStOmEr"),
31+
),
32+
)
33+
.isEqualTo("name=value&ACCESS_TOKEN=[Filtered]&customerId=[Filtered]")
34+
}
35+
2436
@Test
2537
fun `query parameter allow list only retains allowed non-sensitive values`() {
2638
assertThat(
@@ -32,6 +44,17 @@ class HttpUtilsTest {
3244
.isEqualTo("name=value&access_token=[Filtered]&customerId=[Filtered]")
3345
}
3446

47+
@Test
48+
fun `query parameter allow list matches mixed-case terms but still filters sensitive names`() {
49+
assertThat(
50+
HttpUtils.filterQueryParams(
51+
"name=value&AcCeSs_ToKeN=secret&customerId=123",
52+
KeyValueCollectionBehavior.allowList("NaMe", "AcCeSs_ToKeN"),
53+
),
54+
)
55+
.isEqualTo("name=value&AcCeSs_ToKeN=[Filtered]&customerId=[Filtered]")
56+
}
57+
3558
@Test
3659
fun `query parameter filter matches decoded names and preserves encoding`() {
3760
assertThat(
@@ -91,6 +114,32 @@ class HttpUtilsTest {
91114
)
92115
}
93116

117+
@Test
118+
fun `header deny list matches sensitive names and configured terms regardless of case`() {
119+
val filtered =
120+
HttpUtils.filterHeaders(
121+
mapOf(
122+
"content-type" to "application/json",
123+
"AUTHORIZATION" to "Bearer token",
124+
"x-customer" to "customer value",
125+
"Cookie" to "name=value",
126+
),
127+
KeyValueCollectionBehavior.denyList("CuStOmEr"),
128+
)
129+
130+
assertThat(filtered)
131+
.containsExactly(
132+
"content-type",
133+
"application/json",
134+
"AUTHORIZATION",
135+
"[Filtered]",
136+
"x-customer",
137+
"[Filtered]",
138+
"Cookie",
139+
"[Filtered]",
140+
)
141+
}
142+
94143
@Test
95144
fun `header allow list only retains allowed non-sensitive values`() {
96145
val filtered =
@@ -113,4 +162,75 @@ class HttpUtilsTest {
113162
"[Filtered]",
114163
)
115164
}
165+
166+
@Test
167+
fun `header allow list matches mixed-case terms but still filters sensitive names`() {
168+
val filtered =
169+
HttpUtils.filterHeaders(
170+
mapOf(
171+
"content-type" to "application/json",
172+
"AUTHORIZATION" to "Bearer token",
173+
"x-customer" to "customer value",
174+
),
175+
KeyValueCollectionBehavior.allowList("CoNtEnT", "AuThOrIzAtIoN"),
176+
)
177+
178+
assertThat(filtered)
179+
.containsExactly(
180+
"content-type",
181+
"application/json",
182+
"AUTHORIZATION",
183+
"[Filtered]",
184+
"x-customer",
185+
"[Filtered]",
186+
)
187+
}
188+
189+
@Test
190+
fun `sensitive data key check rejects an empty key`() {
191+
assertThat(HttpUtils.containsSensitiveDataKey("")).isFalse()
192+
}
193+
194+
@Test
195+
fun `sensitive data key check matches independently of the default locale`() {
196+
val originalLocale = Locale.getDefault()
197+
try {
198+
Locale.setDefault(Locale("tr", "TR"))
199+
200+
assertThat(HttpUtils.containsSensitiveDataKey("SESSION")).isTrue()
201+
assertThat(HttpUtils.containsSensitiveDataKey("IDENTITY")).isTrue()
202+
} finally {
203+
Locale.setDefault(originalLocale)
204+
}
205+
}
206+
207+
@Test
208+
fun `configured term check matches substrings regardless of key and term case`() {
209+
assertThat(HttpUtils.containsTerm("X-CUSTOMER-ID", listOf("CuStOmEr"))).isTrue()
210+
assertThat(HttpUtils.containsTerm("X-CUSTOMER-ID", listOf("account"))).isFalse()
211+
}
212+
213+
@Test
214+
fun `configured term check ignores null and empty terms`() {
215+
assertThat(HttpUtils.containsTerm("customerId", listOf(null, "", "CuStOmEr"))).isTrue()
216+
assertThat(HttpUtils.containsTerm("customerId", listOf(null, ""))).isFalse()
217+
}
218+
219+
@Test
220+
fun `configured term check matches no keys when no terms are configured`() {
221+
assertThat(HttpUtils.containsTerm("customerId", emptyList())).isFalse()
222+
}
223+
224+
@Test
225+
fun `configured term check matches independently of the default locale`() {
226+
val originalLocale = Locale.getDefault()
227+
try {
228+
Locale.setDefault(Locale("tr", "TR"))
229+
230+
assertThat(HttpUtils.containsTerm("DISPLAY", listOf("display"))).isTrue()
231+
assertThat(HttpUtils.containsTerm("display", listOf("DISPLAY"))).isTrue()
232+
} finally {
233+
Locale.setDefault(originalLocale)
234+
}
235+
}
116236
}

0 commit comments

Comments
 (0)