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

fix(sdk): Set {{auto}} if user.ip_address is undefined #4466

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- Use proper SDK name for Session Replay tags ([#4428](https://github.com/getsentry/sentry-react-native/pull/4428))
- 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))
- Set `{{auto}}` if `user.ip_address` is `undefined` ([#4466](https://github.com/getsentry/sentry-react-native/pull/4466))

### Changes

Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/js/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { eventFromException, eventFromMessage } from '@sentry/browser';
import {
addAutoIpAddressToSession,
addAutoIpAddressToUser,
eventFromException,
eventFromMessage,
} from '@sentry/browser';
import type {
ClientReportEnvelope,
ClientReportItem,
Expand Down Expand Up @@ -48,6 +53,9 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
super(options);

this._outcomesBuffer = [];

this.on('postprocessEvent', addAutoIpAddressToUser);
this.on('beforeSendSession', addAutoIpAddressToSession);
}
Comment on lines +57 to +58
Copy link

@timfish timfish Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you only want to add these hooks if sendDefaultPii is true!


/**
Expand Down
71 changes: 71 additions & 0 deletions packages/core/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,77 @@ describe('Tests ReactNativeClient', () => {
client.recordDroppedEvent('before_send', 'error');
}
});

describe('ipAddress', () => {
let mockTransportSend: jest.Mock;
let client: ReactNativeClient;

beforeEach(() => {
mockTransportSend = jest.fn(() => Promise.resolve());
client = new ReactNativeClient({
...DEFAULT_OPTIONS,
dsn: EXAMPLE_DSN,
transport: () => ({
send: mockTransportSend,
flush: jest.fn(),
}),
});
});

test('preserves ip_address null', () => {
client.captureEvent({
user: {
ip_address: null,
},
});

expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
expect.objectContaining({ ip_address: null }),
);
});

test('preserves ip_address value if set', () => {
client.captureEvent({
user: {
ip_address: '203.45.167.89',
},
});

expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
expect.objectContaining({ ip_address: '203.45.167.89' }),
);
});

test('adds ip_address {{auto}} to user if set to undefined', () => {
client.captureEvent({
user: {
ip_address: undefined,
},
});

expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
expect.objectContaining({ ip_address: '{{auto}}' }),
);
});

test('adds ip_address {{auto}} to user if not set', () => {
client.captureEvent({
user: {},
});

expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
expect.objectContaining({ ip_address: '{{auto}}' }),
);
});

test('adds ip_address {{auto}} to undefined user', () => {
client.captureEvent({});

expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toEqual(
expect.objectContaining({ ip_address: '{{auto}}' }),
);
});
});
});

function mockedOptions(options: Partial<ReactNativeClientOptions>): ReactNativeClientOptions {
Expand Down
Loading