Skip to content

Commit 2c5e51e

Browse files
committed
Check sendDefaultPii before attaching user attributes to logs
1 parent fb05841 commit 2c5e51e

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

sentry/src/main/java/io/sentry/logger/LoggerApi.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ private void captureLog(
247247
setServerName(attributes);
248248
}
249249

250-
setUser(attributes);
250+
if (scopes.getOptions().isSendDefaultPii()) {
251+
setUser(attributes);
252+
}
251253

252254
return attributes;
253255
}

sentry/src/test/java/io/sentry/ScopesTest.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2913,11 +2913,12 @@ class ScopesTest {
29132913
}
29142914

29152915
@Test
2916-
fun `adds user fields to log attributes`() {
2916+
fun `adds user fields to log attributes if sendDefaultPii is true`() {
29172917
val (sut, mockClient) =
29182918
getEnabledScopes {
29192919
it.logs.isEnabled = true
29202920
it.distinctId = "distinctId"
2921+
it.isSendDefaultPii = true
29212922
}
29222923

29232924
sut.configureScope { scope ->
@@ -2951,6 +2952,37 @@ class ScopesTest {
29512952
)
29522953
}
29532954

2955+
@Test
2956+
fun `does not add user fields to log attributes by default`() {
2957+
val (sut, mockClient) =
2958+
getEnabledScopes {
2959+
it.logs.isEnabled = true
2960+
it.distinctId = "distinctId"
2961+
}
2962+
2963+
sut.configureScope { scope ->
2964+
scope.user =
2965+
User().also {
2966+
it.id = "usrid"
2967+
it.username = "usrname"
2968+
it.email = "[email protected]"
2969+
}
2970+
}
2971+
sut.logger().log(SentryLogLevel.WARN, "log message")
2972+
2973+
verify(mockClient)
2974+
.captureLog(
2975+
check {
2976+
assertEquals("log message", it.body)
2977+
2978+
assertNull(it.attributes?.get("user.id"))
2979+
assertNull(it.attributes?.get("user.name"))
2980+
assertNull(it.attributes?.get("user.email"))
2981+
},
2982+
anyOrNull(),
2983+
)
2984+
}
2985+
29542986
@Test
29552987
fun `unset user does provide distinct-id as user-id`() {
29562988
val (sut, mockClient) =

0 commit comments

Comments
 (0)