diff --git a/CHANGELOG.md b/CHANGELOG.md index c69a1e0c43..ec4f011575 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - When no `dataCollection` setting is specified, user information, database query data, file paths, GraphQL documents and variables, and HTTP request and response bodies are enabled. Cookies, URL query parameters, and HTTP headers use their default deny lists. - Logback raw message templates and parameters are controlled only by `includeUnencodedMessage` when an encoder is configured. - OpenTelemetry-derived HTTP headers are controlled by OpenTelemetry capture configuration, not Data Collection, because the SDK can't distinguish manually supplied attributes from values added by automatic instrumentation. Known security cookies remain filtered; use `beforeSendTransaction` for additional scrubbing. +- Apply the Data Collection URL query-parameter policy to URL fragments. Sensitive fragment parameters are now filtered by default, and fragments are omitted when URL query-parameter collection is disabled ([#6814](https://github.com/getsentry/sentry-java/pull/6814)) - The Spring Boot 3 integrations are tested against Spring Boot 3.4 and newer. Spring Boot 3.2 and 3.3 are no longer supported ([#6656](https://github.com/getsentry/sentry-java/pull/6656)) - Remove the deprecated `io.sentry.android.core.LoadClass`. Use `io.sentry.util.LoadClass` instead ([#6594](https://github.com/getsentry/sentry-java/pull/6594)) - `SentryOptions.getIgnoredErrors()`, `getIgnoredTransactions()`, `getIgnoredCheckIns()` and `getIgnoredSpanOrigins()` now return `List` instead of `List`, matching their setters ([#6489](https://github.com/getsentry/sentry-java/pull/6489)) diff --git a/sentry/src/main/java/io/sentry/util/HttpUtils.java b/sentry/src/main/java/io/sentry/util/HttpUtils.java index d3a0198064..cfe217317e 100644 --- a/sentry/src/main/java/io/sentry/util/HttpUtils.java +++ b/sentry/src/main/java/io/sentry/util/HttpUtils.java @@ -50,16 +50,41 @@ public final class HttpUtils { if (query == null || behavior.getMode() == KeyValueCollectionBehavior.Mode.OFF) { return null; } + return filterKeyValuePairs(query, behavior, false); + } - final @NotNull StringBuilder filteredQuery = new StringBuilder(); - final @NotNull String[] params = query.split("&", -1); + static @Nullable String filterFragment( + final @Nullable String fragment, final @NotNull KeyValueCollectionBehavior behavior) { + if (fragment == null || behavior.getMode() == KeyValueCollectionBehavior.Mode.OFF) { + return null; + } + + final int querySeparator = fragment.indexOf('?'); + if (querySeparator < 0) { + return filterKeyValuePairs(fragment, behavior, true); + } + return fragment.substring(0, querySeparator + 1) + + filterKeyValuePairs(fragment.substring(querySeparator + 1), behavior, true); + } + + private static @NotNull String filterKeyValuePairs( + final @NotNull String value, + final @NotNull KeyValueCollectionBehavior behavior, + final boolean preserveSegmentsWithoutSeparator) { + final @NotNull StringBuilder filteredValue = new StringBuilder(); + final @NotNull String[] params = value.split("&", -1); for (int i = 0; i < params.length; i++) { if (i > 0) { - filteredQuery.append('&'); + filteredValue.append('&'); } final @NotNull String param = params[i]; final int separator = param.indexOf('='); + if (separator < 0 && preserveSegmentsWithoutSeparator) { + filteredValue.append(param); + continue; + } + final @NotNull String name = separator < 0 ? param : param.substring(0, separator); final @NotNull String decodedName = decodeQueryParamName(name); final boolean sensitive = containsSensitiveDataKey(decodedName); @@ -69,14 +94,14 @@ public final class HttpUtils { || (behavior.getMode() == KeyValueCollectionBehavior.Mode.DENY_LIST && matchesTerm) || (behavior.getMode() == KeyValueCollectionBehavior.Mode.ALLOW_LIST && !matchesTerm); - filteredQuery.append(name); + filteredValue.append(name); if (shouldFilter) { - filteredQuery.append('=').append(SENSITIVE_DATA_SUBSTITUTE); + filteredValue.append('=').append(SENSITIVE_DATA_SUBSTITUTE); } else if (separator >= 0) { - filteredQuery.append(param.substring(separator)); + filteredValue.append(param.substring(separator)); } } - return filteredQuery.toString(); + return filteredValue.toString(); } public static @NotNull Map filterHeaders( diff --git a/sentry/src/main/java/io/sentry/util/UrlUtils.java b/sentry/src/main/java/io/sentry/util/UrlUtils.java index 5c0ec5735c..b5fd5ce32a 100644 --- a/sentry/src/main/java/io/sentry/util/UrlUtils.java +++ b/sentry/src/main/java/io/sentry/util/UrlUtils.java @@ -2,6 +2,7 @@ import io.sentry.DataCollectionResolver; import io.sentry.ISpan; +import io.sentry.KeyValueCollectionBehavior; import io.sentry.SpanDataConvention; import io.sentry.protocol.Request; import java.net.URI; @@ -35,9 +36,17 @@ public final class UrlUtils { final @NotNull String authority = uri.getRawAuthority() == null ? "" : uri.getRawAuthority(); final @NotNull String path = uri.getRawPath() == null ? "" : uri.getRawPath(); final @Nullable String rawQuery = uri.getRawQuery(); - final @Nullable String query = - resolver == null ? rawQuery : filterQueryParams(rawQuery, resolver); - final @Nullable String fragment = uri.getRawFragment(); + final @Nullable String rawFragment = uri.getRawFragment(); + final @Nullable String query; + final @Nullable String fragment; + if (resolver == null) { + query = rawQuery; + fragment = rawFragment; + } else { + final @NotNull KeyValueCollectionBehavior behavior = resolver.getUrlQueryParams(); + query = HttpUtils.filterQueryParams(rawQuery, behavior); + fragment = HttpUtils.filterFragment(rawFragment, behavior); + } final @NotNull String filteredUrl = schemeAndSeparator + filterUserInfo(authority) + path; diff --git a/sentry/src/test/java/io/sentry/util/UrlUtilsTest.kt b/sentry/src/test/java/io/sentry/util/UrlUtilsTest.kt index d75dc3f7cc..65acdb9e97 100644 --- a/sentry/src/test/java/io/sentry/util/UrlUtilsTest.kt +++ b/sentry/src/test/java/io/sentry/util/UrlUtilsTest.kt @@ -52,14 +52,65 @@ class UrlUtilsTest { } @Test - fun `resolver aware helpers remove query values in off mode`() { + fun `resolver aware helpers filter fragment values by default`() { + val details = + UrlUtils.parse( + "https://example.com/callback#access_token=abc123&state=xyz", + SentryOptions().dataCollectionResolver, + ) + val request = Request() + val span = mock() + + details.applyToRequest(request) + details.applyToSpan(span) + + assertThat(request.fragment).isEqualTo("access_token=[Filtered]&state=xyz") + verify(span).setData(SpanDataConvention.HTTP_FRAGMENT_KEY, "access_token=[Filtered]&state=xyz") + } + + @Test + fun `resolver aware helpers filter fragment query after hash route`() { + val details = + UrlUtils.parse( + "https://example.com/#/callback?access_token=abc123&state=xyz", + SentryOptions().dataCollectionResolver, + ) + + assertThat(details.fragment).isEqualTo("/callback?access_token=[Filtered]&state=xyz") + } + + @Test + fun `resolver aware helpers preserve fragment text that is not a key value pair`() { + val details = + UrlUtils.parse("https://example.com/#details", SentryOptions().dataCollectionResolver) + + assertThat(details.fragment).isEqualTo("details") + } + + @Test + fun `resolver aware helpers preserve fragment encoding`() { + val details = + UrlUtils.parse( + "https://example.com/#%61ccess_token=abc%20123&state=a%20b", + SentryOptions().dataCollectionResolver, + ) + + assertThat(details.fragment).isEqualTo("%61ccess_token=[Filtered]&state=a%20b") + } + + @Test + fun `resolver aware helpers remove query and fragment values in off mode`() { val options = SentryOptions().also { it.dataCollection.urlQueryParams = KeyValueCollectionBehavior.off() } - val details = UrlUtils.parse("https://example.com?name=value", options.dataCollectionResolver) + val details = + UrlUtils.parse( + "https://example.com?name=value#access_token=abc123", + options.dataCollectionResolver, + ) val request = Request() val breadcrumb = Breadcrumb.http( - "https://example.com?name=value", + "https://example.com?name=value#access_token=abc123", "GET", null, options.dataCollectionResolver, @@ -68,7 +119,9 @@ class UrlUtilsTest { details.applyToRequest(request) assertThat(request.queryString).isNull() + assertThat(request.fragment).isNull() assertThat(breadcrumb.getData("http.query")).isNull() + assertThat(breadcrumb.getData("http.fragment")).isNull() } @Test