Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Create user and set ip address if missing
  • Loading branch information
denrase committed Oct 3, 2023
commit 24f77a212ca077767d4053ed6737378683f5046c
16 changes: 4 additions & 12 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import 'client_reports/discard_reason.dart';
import 'transport/data_category.dart';

/// Default value for [User.ipAddress]. It gets set when an event does not have
/// a user and IP address. Only applies if [SentryOptions.sendDefaultPii] is set
/// to true.
/// a user and IP address.
const _defaultIpAddress = '{{auto}}';

/// Logs crash reports and events to the Sentry.io service.
Expand Down Expand Up @@ -142,7 +141,7 @@ class SentryClient {
platform: event.platform ?? sdkPlatform(_options.platformChecker.isWeb),
);

event = _applyDefaultPii(event);
event = _createUserOrSetDefaultIpAddress(event);

if (event is SentryTransaction) {
return event;
Expand Down Expand Up @@ -222,20 +221,13 @@ class SentryClient {
return event;
}

/// This modifies the users IP address according
/// to [SentryOptions.sendDefaultPii].
SentryEvent _applyDefaultPii(SentryEvent event) {
if (!_options.sendDefaultPii) {
return event;
}
SentryEvent _createUserOrSetDefaultIpAddress(SentryEvent event) {
var user = event.user;
if (user == null) {
user = SentryUser(ipAddress: _defaultIpAddress);
return event.copyWith(user: user);
return event.copyWith(user: SentryUser(ipAddress: _defaultIpAddress));
} else if (event.user?.ipAddress == null) {
return event.copyWith(user: user.copyWith(ipAddress: _defaultIpAddress));
}

return event;
}

Expand Down
19 changes: 4 additions & 15 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -826,25 +826,14 @@ void main() {
});
});

group('SentryClient: apply default pii', () {
group('SentryClient: sets user & user ip', () {
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('sendDefaultPii is disabled', () async {
final client = fixture.getSut(sendDefaultPii: false);

await client.captureEvent(fakeEvent);

final capturedEnvelope = fixture.transport.envelopes.first;
final capturedEvent = await eventFromEnvelope(capturedEnvelope);

expect(capturedEvent.user?.toJson(), fakeEvent.user?.toJson());
});

test('sendDefaultPii is enabled and event has no user', () async {
test('event has no user', () async {
final client = fixture.getSut(sendDefaultPii: true);
var fakeEvent = SentryEvent();

Expand All @@ -858,7 +847,7 @@ void main() {
expect(capturedEvent.user?.ipAddress, '{{auto}}');
});

test('sendDefaultPii is enabled and event has a user with IP address',
test('event has a user with IP address',
() async {
final client = fixture.getSut(sendDefaultPii: true);

Expand All @@ -875,7 +864,7 @@ void main() {
expect(capturedEvent.user?.email, fakeEvent.user!.email);
});

test('sendDefaultPii is enabled and event has a user without IP address',
test('event has a user without IP address',
() async {
final client = fixture.getSut(sendDefaultPii: true);

Expand Down