Skip to content

Commit 7669fb0

Browse files
fix(sdk): Set {{auto}} if user.ip_address is undefined
1 parent 90aa098 commit 7669fb0

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ Change `Cold/Warm App Start` span description to `Cold/Warm Start` ([#4636](http
290290
- Add mechanism field to unhandled rejection errors ([#4457](https://github.com/getsentry/sentry-react-native/pull/4457))
291291
- Use proper SDK name for Session Replay tags ([#4428](https://github.com/getsentry/sentry-react-native/pull/4428))
292292
- Use `makeDsn` from `core` to extract the URL from DSN avoiding unimplemented `URL.protocol` errors ([#4395](https://github.com/getsentry/sentry-react-native/pull/4395))
293+
- Set `{{auto}}` if `user.ip_address` is `undefined` ([#4466](https://github.com/getsentry/sentry-react-native/pull/4466))
293294

294295
### Changes
295296

packages/core/src/js/client.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { eventFromException, eventFromMessage } from '@sentry/browser';
1+
import {
2+
addAutoIpAddressToSession,
3+
addAutoIpAddressToUser,
4+
eventFromException,
5+
eventFromMessage,
6+
} from '@sentry/browser';
27
import type {
38
ClientReportEnvelope,
49
ClientReportItem,
@@ -48,6 +53,9 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
4853
super(options);
4954

5055
this._outcomesBuffer = [];
56+
57+
this.on('postprocessEvent', addAutoIpAddressToUser);
58+
this.on('beforeSendSession', addAutoIpAddressToSession);
5159
}
5260

5361
/**

packages/core/test/client.test.ts

+71
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,77 @@ describe('Tests ReactNativeClient', () => {
625625
client.recordDroppedEvent('before_send', 'error');
626626
}
627627
});
628+
629+
describe('ipAddress', () => {
630+
let mockTransportSend: jest.Mock;
631+
let client: ReactNativeClient;
632+
633+
beforeEach(() => {
634+
mockTransportSend = jest.fn(() => Promise.resolve());
635+
client = new ReactNativeClient({
636+
...DEFAULT_OPTIONS,
637+
dsn: EXAMPLE_DSN,
638+
transport: () => ({
639+
send: mockTransportSend,
640+
flush: jest.fn(),
641+
}),
642+
});
643+
});
644+
645+
test('preserves ip_address null', () => {
646+
client.captureEvent({
647+
user: {
648+
ip_address: null,
649+
},
650+
});
651+
652+
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
653+
expect.objectContaining({ ip_address: null }),
654+
);
655+
});
656+
657+
test('preserves ip_address value if set', () => {
658+
client.captureEvent({
659+
user: {
660+
ip_address: '203.45.167.89',
661+
},
662+
});
663+
664+
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
665+
expect.objectContaining({ ip_address: '203.45.167.89' }),
666+
);
667+
});
668+
669+
test('adds ip_address {{auto}} to user if set to undefined', () => {
670+
client.captureEvent({
671+
user: {
672+
ip_address: undefined,
673+
},
674+
});
675+
676+
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
677+
expect.objectContaining({ ip_address: '{{auto}}' }),
678+
);
679+
});
680+
681+
test('adds ip_address {{auto}} to user if not set', () => {
682+
client.captureEvent({
683+
user: {},
684+
});
685+
686+
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
687+
expect.objectContaining({ ip_address: '{{auto}}' }),
688+
);
689+
});
690+
691+
test('adds ip_address {{auto}} to undefined user', () => {
692+
client.captureEvent({});
693+
694+
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
695+
expect.objectContaining({ ip_address: '{{auto}}' }),
696+
);
697+
});
698+
});
628699
});
629700

630701
function mockedOptions(options: Partial<ReactNativeClientOptions>): ReactNativeClientOptions {

0 commit comments

Comments
 (0)