Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions sentry/src/main/java/io/sentry/util/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class HttpUtils {
"X-CSRFTOKEN",
"X-XSRF-TOKEN");

// Keep these terms lowercase so sensitive-key checks only normalize the input key.
private static final List<String> SENSITIVE_DATA_KEYS =
Arrays.asList(
"auth",
Expand Down Expand Up @@ -80,7 +81,7 @@ public static boolean containsSensitiveHeader(final @NotNull String header) {
final int separator = param.indexOf('=');
final @NotNull String name = separator < 0 ? param : param.substring(0, separator);
final @NotNull String decodedName = decodeQueryParamName(name);
final boolean sensitive = containsTerm(decodedName, SENSITIVE_DATA_KEYS);
final boolean sensitive = containsSensitiveDataKey(decodedName);
final boolean matchesTerm = containsTerm(decodedName, behavior.getTerms());
final boolean shouldFilter =
sensitive
Expand Down Expand Up @@ -108,7 +109,7 @@ public static boolean containsSensitiveHeader(final @NotNull String header) {
for (final Map.Entry<String, String> header : headers.entrySet()) {
final @NotNull String name = header.getKey();
final boolean sensitive =
containsTerm(name, SENSITIVE_DATA_KEYS)
containsSensitiveDataKey(name)
|| "Cookie".equalsIgnoreCase(name)
|| "Set-Cookie".equalsIgnoreCase(name);
if (sensitive) {
Expand Down Expand Up @@ -136,7 +137,13 @@ public static boolean containsSensitiveHeader(final @NotNull String header) {
}

static boolean containsSensitiveDataKey(final @NotNull String key) {
return containsTerm(key, SENSITIVE_DATA_KEYS);
final @NotNull String normalizedKey = key.toLowerCase(Locale.ROOT);
for (final String term : SENSITIVE_DATA_KEYS) {
if (normalizedKey.contains(term)) {
return true;
}
}
return false;
}

static boolean containsTerm(final @NotNull String key, final @NotNull List<String> terms) {
Comment thread
0xadam-brown marked this conversation as resolved.
Resolved
Expand Down
120 changes: 120 additions & 0 deletions sentry/src/test/java/io/sentry/util/HttpUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry.util

import com.google.common.truth.Truth.assertThat
import io.sentry.KeyValueCollectionBehavior
import java.util.Locale
import kotlin.test.Test

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

@Test
fun `query parameter deny list matches sensitive names and configured terms regardless of case`() {
assertThat(
HttpUtils.filterQueryParams(
"name=value&ACCESS_TOKEN=secret&customerId=123",
KeyValueCollectionBehavior.denyList("CuStOmEr"),
),
)
.isEqualTo("name=value&ACCESS_TOKEN=[Filtered]&customerId=[Filtered]")
}

@Test
fun `query parameter allow list only retains allowed non-sensitive values`() {
assertThat(
Expand All @@ -32,6 +44,17 @@ class HttpUtilsTest {
.isEqualTo("name=value&access_token=[Filtered]&customerId=[Filtered]")
}

@Test
fun `query parameter allow list matches mixed-case terms but still filters sensitive names`() {
assertThat(
HttpUtils.filterQueryParams(
"name=value&AcCeSs_ToKeN=secret&customerId=123",
KeyValueCollectionBehavior.allowList("NaMe", "AcCeSs_ToKeN"),
),
)
.isEqualTo("name=value&AcCeSs_ToKeN=[Filtered]&customerId=[Filtered]")
}

@Test
fun `query parameter filter matches decoded names and preserves encoding`() {
assertThat(
Expand Down Expand Up @@ -91,6 +114,32 @@ class HttpUtilsTest {
)
}

@Test
fun `header deny list matches sensitive names and configured terms regardless of case`() {
val filtered =
HttpUtils.filterHeaders(
mapOf(
"content-type" to "application/json",
"AUTHORIZATION" to "Bearer token",
"x-customer" to "customer value",
"Cookie" to "name=value",
),
KeyValueCollectionBehavior.denyList("CuStOmEr"),
)

assertThat(filtered)
.containsExactly(
"content-type",
"application/json",
"AUTHORIZATION",
"[Filtered]",
"x-customer",
"[Filtered]",
"Cookie",
"[Filtered]",
)
}

@Test
fun `header allow list only retains allowed non-sensitive values`() {
val filtered =
Expand All @@ -113,4 +162,75 @@ class HttpUtilsTest {
"[Filtered]",
)
}

@Test
fun `header allow list matches mixed-case terms but still filters sensitive names`() {
val filtered =
HttpUtils.filterHeaders(
mapOf(
"content-type" to "application/json",
"AUTHORIZATION" to "Bearer token",
"x-customer" to "customer value",
),
KeyValueCollectionBehavior.allowList("CoNtEnT", "AuThOrIzAtIoN"),
)

assertThat(filtered)
.containsExactly(
"content-type",
"application/json",
"AUTHORIZATION",
"[Filtered]",
"x-customer",
"[Filtered]",
)
}

@Test
fun `sensitive data key check rejects an empty key`() {
assertThat(HttpUtils.containsSensitiveDataKey("")).isFalse()
}

@Test
fun `sensitive data key check matches independently of the default locale`() {
val originalLocale = Locale.getDefault()
try {
Locale.setDefault(Locale("tr", "TR"))

assertThat(HttpUtils.containsSensitiveDataKey("SESSION")).isTrue()
assertThat(HttpUtils.containsSensitiveDataKey("IDENTITY")).isTrue()
} finally {
Locale.setDefault(originalLocale)
}
}

@Test
fun `configured term check matches substrings regardless of key and term case`() {
assertThat(HttpUtils.containsTerm("X-CUSTOMER-ID", listOf("CuStOmEr"))).isTrue()
assertThat(HttpUtils.containsTerm("X-CUSTOMER-ID", listOf("account"))).isFalse()
}

@Test
fun `configured term check ignores null and empty terms`() {
assertThat(HttpUtils.containsTerm("customerId", listOf(null, "", "CuStOmEr"))).isTrue()
assertThat(HttpUtils.containsTerm("customerId", listOf(null, ""))).isFalse()
}

@Test
fun `configured term check matches no keys when no terms are configured`() {
assertThat(HttpUtils.containsTerm("customerId", emptyList())).isFalse()
}

@Test
fun `configured term check matches independently of the default locale`() {
val originalLocale = Locale.getDefault()
try {
Locale.setDefault(Locale("tr", "TR"))

assertThat(HttpUtils.containsTerm("DISPLAY", listOf("display"))).isTrue()
assertThat(HttpUtils.containsTerm("display", listOf("DISPLAY"))).isTrue()
} finally {
Locale.setDefault(originalLocale)
}
}
}
Loading