|
| 1 | +package io.sentry; |
| 2 | + |
| 3 | +import java.util.Set; |
| 4 | +import org.jetbrains.annotations.ApiStatus; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import org.jetbrains.annotations.Nullable; |
| 7 | + |
| 8 | +/** Resolves effective Data Collection policies for SDK integrations. */ |
| 9 | +@ApiStatus.Internal |
| 10 | +public final class DataCollectionResolver { |
| 11 | + |
| 12 | + private static final @NotNull KeyValueCollectionBehavior OFF = KeyValueCollectionBehavior.off(); |
| 13 | + private static final @NotNull KeyValueCollectionBehavior EMPTY_DENY_LIST = |
| 14 | + KeyValueCollectionBehavior.denyList(); |
| 15 | + |
| 16 | + private final @NotNull SentryOptions options; |
| 17 | + |
| 18 | + DataCollectionResolver(final @NotNull SentryOptions options) { |
| 19 | + this.options = options; |
| 20 | + } |
| 21 | + |
| 22 | + public boolean isDataCollectionConfigured() { |
| 23 | + return options.getDataCollection().isExplicitlyConfigured(); |
| 24 | + } |
| 25 | + |
| 26 | + public boolean isUserInfo() { |
| 27 | + return explicitOrSendDefaultPii(options.getDataCollection().getUserInfo(), true); |
| 28 | + } |
| 29 | + |
| 30 | + public boolean isDatabaseQueryData() { |
| 31 | + return explicitOrSendDefaultPii(options.getDataCollection().getDatabaseQueryData(), true); |
| 32 | + } |
| 33 | + |
| 34 | + public boolean isGraphqlDocument() { |
| 35 | + return explicitOrSendDefaultPii(options.getDataCollection().getGraphql().getDocument(), true); |
| 36 | + } |
| 37 | + |
| 38 | + public boolean isGraphqlVariables() { |
| 39 | + return explicitOrSendDefaultPii(options.getDataCollection().getGraphql().getVariables(), true); |
| 40 | + } |
| 41 | + |
| 42 | + public @NotNull KeyValueCollectionBehavior getCookies() { |
| 43 | + final @NotNull DataCollection dataCollection = options.getDataCollection(); |
| 44 | + final @Nullable KeyValueCollectionBehavior cookies = dataCollection.getCookies(); |
| 45 | + |
| 46 | + if (cookies != null) { |
| 47 | + return cookies; |
| 48 | + } |
| 49 | + if (isDataCollectionConfigured()) { |
| 50 | + return EMPTY_DENY_LIST; |
| 51 | + } |
| 52 | + return options.isSendDefaultPii() ? EMPTY_DENY_LIST : OFF; |
| 53 | + } |
| 54 | + |
| 55 | + public @NotNull KeyValueCollectionBehavior getUrlQueryParams() { |
| 56 | + return explicitOrEmptyDenyList(options.getDataCollection().getUrlQueryParams()); |
| 57 | + } |
| 58 | + |
| 59 | + public @NotNull KeyValueCollectionBehavior getHttpRequestHeaders() { |
| 60 | + return explicitOrEmptyDenyList(options.getDataCollection().getHttpHeaders().getRequest()); |
| 61 | + } |
| 62 | + |
| 63 | + public @NotNull KeyValueCollectionBehavior getHttpResponseHeaders() { |
| 64 | + return explicitOrEmptyDenyList(options.getDataCollection().getHttpHeaders().getResponse()); |
| 65 | + } |
| 66 | + |
| 67 | + public boolean isIncomingRequestBody() { |
| 68 | + return isHttpBodyEnabled(HttpBodyType.INCOMING_REQUEST, options.isSendDefaultPii()); |
| 69 | + } |
| 70 | + |
| 71 | + public boolean isOutgoingRequestBody() { |
| 72 | + return isHttpBodyEnabled(HttpBodyType.OUTGOING_REQUEST, true); |
| 73 | + } |
| 74 | + |
| 75 | + public boolean isIncomingResponseBody() { |
| 76 | + return isHttpBodyEnabled(HttpBodyType.INCOMING_RESPONSE, true); |
| 77 | + } |
| 78 | + |
| 79 | + public boolean isOutgoingResponseBody() { |
| 80 | + return isHttpBodyEnabled(HttpBodyType.OUTGOING_RESPONSE, options.isSendDefaultPii()); |
| 81 | + } |
| 82 | + |
| 83 | + private boolean explicitOrSendDefaultPii( |
| 84 | + final @Nullable Boolean explicit, final boolean defaultValue) { |
| 85 | + if (explicit != null) { |
| 86 | + return explicit; |
| 87 | + } |
| 88 | + return isDataCollectionConfigured() ? defaultValue : options.isSendDefaultPii(); |
| 89 | + } |
| 90 | + |
| 91 | + private @NotNull KeyValueCollectionBehavior explicitOrEmptyDenyList( |
| 92 | + final @Nullable KeyValueCollectionBehavior explicit) { |
| 93 | + return explicit != null ? explicit : EMPTY_DENY_LIST; |
| 94 | + } |
| 95 | + |
| 96 | + private boolean isHttpBodyEnabled( |
| 97 | + final @NotNull HttpBodyType bodyType, final boolean legacyFallback) { |
| 98 | + final @NotNull DataCollection dataCollection = options.getDataCollection(); |
| 99 | + final @Nullable Set<HttpBodyType> httpBodies = dataCollection.getHttpBodies(); |
| 100 | + if (httpBodies != null) { |
| 101 | + return httpBodies.contains(bodyType); |
| 102 | + } |
| 103 | + return isDataCollectionConfigured() || legacyFallback; |
| 104 | + } |
| 105 | +} |
0 commit comments