Skip to content

feat(browser): Set user.ip_address explicitly to {{auto}} #15008

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

Merged
merged 6 commits into from
Jan 20, 2025
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
Prev Previous commit
Next Next commit
fix & tests
  • Loading branch information
mydea committed Jan 20, 2025
commit d704c9e31a019eb98b3f864f5bd5a3c67da0ebd8
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ sentryTest('should capture feedback with custom button', async ({ getLocalTestUr
event_id: expect.stringMatching(/\w{32}/),
environment: 'production',
tags: {},
user: {
ip_address: '{{auto}}',
},
sdk: {
integrations: expect.arrayContaining(['Feedback']),
version: expect.any(String),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ sentryTest('should capture feedback', async ({ getLocalTestUrl, page }) => {
'User-Agent': expect.stringContaining(''),
},
},
user: {
ip_address: '{{auto}}',
},
platform: 'javascript',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ sentryTest('should capture feedback', async ({ getLocalTestUrl, page }) => {
'User-Agent': expect.stringContaining(''),
},
},
user: {
ip_address: '{{auto}}',
},
platform: 'javascript',
});
const cspViolation = await page.evaluate<boolean>('window.__CSPVIOLATION__');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ sentryTest('allows to setup a client manually & capture exceptions', async ({ ge
'User-Agent': expect.any(String),
}),
},
user: {
ip_address: '{{auto}}',
},
timestamp: expect.any(Number),
environment: 'local',
release: '0.0.1',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sentry.setUser({
id: 'foo',
ip_address: null,
});

Sentry.captureMessage('first_user');
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/core';

import { sentryTest } from '../../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers';

sentryTest('should allow to set ip_address to null', async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);

expect(eventData.message).toBe('first_user');
expect(eventData.user).toEqual({
id: 'foo',
ip_address: null,
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ sentryTest('should unset user', async ({ getLocalTestUrl, page }) => {
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 3, { url });

expect(eventData[0].message).toBe('no_user');
expect(eventData[0].user).toBeUndefined();
expect(eventData[0].user).toEqual({ ip_address: '{{auto}}' });

expect(eventData[1].message).toBe('user');
expect(eventData[1].user).toMatchObject({
expect(eventData[1].user).toEqual({
id: 'foo',
ip_address: 'bar',
other_key: 'baz',
});

expect(eventData[2].message).toBe('unset_user');
expect(eventData[2].user).toBeUndefined();
expect(eventData[2].user).toEqual({
ip_address: '{{auto}}',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ sentryTest('should update user', async ({ getLocalTestUrl, page }) => {
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 2, { url });

expect(eventData[0].message).toBe('first_user');
expect(eventData[0].user).toMatchObject({
expect(eventData[0].user).toEqual({
id: 'foo',
ip_address: 'bar',
});

expect(eventData[1].message).toBe('second_user');
expect(eventData[1].user).toMatchObject({
expect(eventData[1].user).toEqual({
id: 'baz',
ip_address: '{{auto}}',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ sentryTest('should allow nested scoping', async ({ getLocalTestUrl, page }) => {
const eventData = await getMultipleSentryEnvelopeRequests<Event>(page, 5, { url });

expect(eventData[0].message).toBe('root_before');
expect(eventData[0].user).toMatchObject({ id: 'qux' });
expect(eventData[0].user).toEqual({ id: 'qux' });
expect(eventData[0].tags).toBeUndefined();

expect(eventData[1].message).toBe('outer_before');
expect(eventData[1].user).toMatchObject({ id: 'qux' });
expect(eventData[1].user).toEqual({ id: 'qux' });
expect(eventData[1].tags).toMatchObject({ foo: false });

expect(eventData[2].message).toBe('inner');
expect(eventData[2].user).toBeUndefined();
expect(eventData[2].tags).toMatchObject({ foo: false, bar: 10 });

expect(eventData[3].message).toBe('outer_after');
expect(eventData[3].user).toMatchObject({ id: 'baz' });
expect(eventData[3].user).toEqual({ id: 'baz' });
expect(eventData[3].tags).toMatchObject({ foo: false });

expect(eventData[4].message).toBe('root_after');
expect(eventData[4].user).toMatchObject({ id: 'qux' });
expect(eventData[4].user).toEqual({ id: 'qux' });
expect(eventData[4].tags).toBeUndefined();
});
24 changes: 16 additions & 8 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,22 @@ export class BrowserClient extends Client<BrowserClientOptions> {
): PromiseLike<Event | null> {
event.platform = event.platform || 'javascript';

// By default, we want to infer the IP address, unless this is explicitly set to `null`
if (typeof event.user?.ip_address === 'undefined') {
event.user = {
...event.user,
ip_address: '{{auto}}',
};
}
return super._prepareEvent(event, hint, currentScope, isolationScope).then(prepared => {
if (!prepared) {
return prepared;
}

// By default, we want to infer the IP address, unless this is explicitly set to `null`
// We do this after all other processing is done
// If `ip_address` is explicitly set to `null` or a value, we leave it as is
if (prepared.user?.ip_address === undefined) {
prepared.user = {
...prepared.user,
ip_address: '{{auto}}',
};
}

return super._prepareEvent(event, hint, currentScope, isolationScope);
return prepared;
});
}
}