Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set ip_address = {{auto}} by default #2860

Merged
merged 3 commits into from
Jul 24, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Breaking changes:
- Capture failed HTTP requests by default ([#2794](https://github.com/getsentry/sentry-java/pull/2794))
- Reduce flush timeout to 4s on Android to avoid ANRs ([#2858](https://github.com/getsentry/sentry-java/pull/2858))
- Set ip_address to {{auto}} by default, even if sendDefaultPII is disabled ([#2860](https://github.com/getsentry/sentry-java/pull/2860))
- Instead use the "Prevent Storing of IP Addresses" option in the "Security & Privacy" project settings on sentry.io

### Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,35 +475,19 @@ private void setExceptions(final @NotNull SentryEvent event, final @NotNull Obje
}

private void mergeUser(final @NotNull SentryBaseEvent event) {
if (options.isSendDefaultPii()) {
if (event.getUser() == null) {
final User user = new User();
user.setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
event.setUser(user);
} else if (event.getUser().getIpAddress() == null) {
event.getUser().setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
}
@Nullable User user = event.getUser();
if (user == null) {
user = new User();
event.setUser(user);
}

// userId should be set even if event is Cached as the userId is static and won't change anyway.
final User user = event.getUser();
if (user == null) {
event.setUser(getDefaultUser());
} else if (user.getId() == null) {
if (user.getId() == null) {
user.setId(getDeviceId());
}
}

/**
* Sets the default user which contains only the userId.
*
* @return the User object
*/
private @NotNull User getDefaultUser() {
User user = new User();
user.setId(getDeviceId());

return user;
if (user.getIpAddress() == null) {
user.setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
}
}

private @Nullable String getDeviceId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.sentry.DateUtils;
import io.sentry.EventProcessor;
import io.sentry.Hint;
import io.sentry.IpAddressUtils;
import io.sentry.SentryBaseEvent;
import io.sentry.SentryEvent;
import io.sentry.SentryLevel;
Expand Down Expand Up @@ -165,13 +166,19 @@ private boolean shouldApplyScopeData(
}

private void mergeUser(final @NotNull SentryBaseEvent event) {
// userId should be set even if event is Cached as the userId is static and won't change anyway.
final User user = event.getUser();
@Nullable User user = event.getUser();
if (user == null) {
event.setUser(getDefaultUser());
} else if (user.getId() == null) {
user = new User();
event.setUser(user);
}

// userId should be set even if event is Cached as the userId is static and won't change anyway.
if (user.getId() == null) {
user.setId(getDeviceId());
}
if (user.getIpAddress() == null) {
user.setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
}
}

private void setDevice(
Expand Down Expand Up @@ -728,18 +735,6 @@ private void setAppPackageInfo(final @NotNull App app, final @NotNull PackageInf
}
}

/**
* Sets the default user which contains only the userId.
*
* @return the User object
*/
public @NotNull User getDefaultUser() {
User user = new User();
user.setId(getDeviceId());

return user;
}

private @Nullable String getDeviceId() {
try {
return Installation.id(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ class AnrV2EventProcessorTest {
debugMeta = DebugMeta().apply {
images = listOf(DebugImage().apply { type = DebugImage.PROGUARD; uuid = "uuid1" })
}
user = User().apply {
id = "42"
ipAddress = "2.4.8.16"
}
}

assertEquals("NotAndroid", processed.platform)
Expand All @@ -427,6 +431,9 @@ class AnrV2EventProcessorTest {
assertEquals(2, processed.debugMeta!!.images!!.size)
assertEquals("uuid1", processed.debugMeta!!.images!![0].uuid)
assertEquals("uuid", processed.debugMeta!!.images!![1].uuid)

assertEquals("42", processed.user!!.id)
assertEquals("2.4.8.16", processed.user!!.ipAddress)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,31 @@ class DefaultAndroidEventProcessorTest {
}
}

@Test
fun `when event user data does not have ip address set, sets {{auto}} as the ip address`() {
val sut = fixture.getSut(context)
val event = SentryEvent().apply {
user = User()
}
sut.process(event, Hint())
assertNotNull(event.user) {
assertEquals("{{auto}}", it.ipAddress)
}
}

@Test
fun `when event has ip address set, keeps original ip address`() {
val sut = fixture.getSut(context)
val event = SentryEvent()
event.user = User().apply {
ipAddress = "192.168.0.1"
}
sut.process(event, Hint())
assertNotNull(event.user) {
assertEquals("192.168.0.1", it.ipAddress)
}
}

@Test
fun `Executor service should be called on ctor`() {
val sut = fixture.getSut(context)
Expand Down
15 changes: 7 additions & 8 deletions sentry/src/main/java/io/sentry/MainEventProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,13 @@ private void setTags(final @NotNull SentryBaseEvent event) {
}

private void mergeUser(final @NotNull SentryBaseEvent event) {
if (options.isSendDefaultPii()) {
if (event.getUser() == null) {
final User user = new User();
user.setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
event.setUser(user);
} else if (event.getUser().getIpAddress() == null) {
event.getUser().setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
}
@Nullable User user = event.getUser();
if (user == null) {
user = new User();
event.setUser(user);
}
if (user.getIpAddress() == null) {
user.setIpAddress(IpAddressUtils.DEFAULT_IP_ADDRESS);
}
}

Expand Down
15 changes: 2 additions & 13 deletions sentry/src/test/java/io/sentry/MainEventProcessorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class MainEventProcessorTest {
}

@Test
fun `when event does not have ip address set and sendDefaultPii is set to true, sets {{auto}} as the ip address`() {
fun `when event does not have ip address set, sets {{auto}} as the ip address`() {
val sut = fixture.getSut(sendDefaultPii = true)
val event = SentryEvent()
sut.process(event, Hint())
Expand All @@ -307,7 +307,7 @@ class MainEventProcessorTest {
}

@Test
fun `when event has ip address set and sendDefaultPii is set to true, keeps original ip address`() {
fun `when event has ip address set, keeps original ip address`() {
val sut = fixture.getSut(sendDefaultPii = true)
val event = SentryEvent()
event.user = User().apply {
Expand All @@ -319,17 +319,6 @@ class MainEventProcessorTest {
}
}

@Test
fun `when event does not have ip address set and sendDefaultPii is set to false, does not set ip address`() {
val sut = fixture.getSut(sendDefaultPii = false)
val event = SentryEvent()
event.user = User()
sut.process(event, Hint())
assertNotNull(event.user) {
assertNull(it.ipAddress)
}
}

@Test
fun `when event has environment set, does not overwrite environment`() {
val sut = fixture.getSut(environment = null)
Expand Down
Loading