Skip to content

feat!: Only collect ip addresses with sendDefaultPii: true #15084

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 21, 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
Add tests
  • Loading branch information
Luca Forstner committed Jan 21, 2025
commit dc8eb118894957c9f8d145a2639ccb688e2c74ad
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sentry.captureException(new Error('woot'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/core';

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

sentryTest('should default user to {{auto}} on errors when sendDefaultPii: true', async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
expect(eventData.user?.ip_address).toBe('{{auto}}');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
tracesSampleRate: 1,
sendDefaultPii: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sentry.startSpan({ name: 'woot' }, () => undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from '@playwright/test';
import { sentryTest } from '../../../../utils/fixtures';
import {
envelopeRequestParser,
shouldSkipTracingTest,
waitForTransactionRequestOnUrl,
} from '../../../../utils/helpers';

sentryTest(
'should default user to {{auto}} on transactions when sendDefaultPii: true',
async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestUrl({ testDir: __dirname });
const req = await waitForTransactionRequestOnUrl(page, url);
const transaction = envelopeRequestParser(req);
expect(transaction.user?.ip_address).toBe('{{auto}}');
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

window.Replay = Sentry.replayIntegration({
flushMinDelay: 200,
flushMaxDelay: 200,
minReplayDuration: 0,
useCompression: false,
blockAllMedia: false,
unmask: ['.sentry-unmask, [data-sentry-unmask]'],
});

Sentry.init({
dsn: 'https://[email protected]/1337',
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
integrations: [window.Replay],
sendDefaultPii: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from '@playwright/test';

import { sentryTest } from '../../../../utils/fixtures';
import { getReplayEvent, shouldSkipReplayTest, waitForReplayRequest } from '../../../../utils/replayHelpers';

sentryTest(
'replay recording should contain default performance spans',
async ({ getLocalTestUrl, page, browserName }) => {
// We only test this against the NPM package and replay bundles
// and only on chromium as most performance entries are only available in chromium
if (shouldSkipReplayTest() || browserName !== 'chromium') {
sentryTest.skip();
}

const reqPromise0 = waitForReplayRequest(page, 0);

const url = await getLocalTestUrl({ testDir: __dirname });

await page.goto(url);
const replayEvent = getReplayEvent(await reqPromise0);

expect(replayEvent.user?.ip_address).toBe('{{auto}}');
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
sendDefaultPii: true,
release: '1.0',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from '@playwright/test';

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

sentryTest(
'should default user to {{auto}} on sessions when sendDefaultPii: true',
async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });
const session = await getFirstSentryEnvelopeRequest(page, url);
expect((session as any).attrs.ip_address).toBe('{{auto}}');
},
);
23 changes: 9 additions & 14 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ export class BrowserClient extends Client<BrowserClientOptions> {

if (this._options.sendDefaultPii) {
this.on('postprocessEvent', event => {
addAutoIpAddressToUser(event);
if (event.user?.ip_address === undefined) {
event.user = {
...event.user,
ip_address: '{{auto}}',
};
}
});

this.on('beforeSendSession', session => {
Expand All @@ -98,7 +103,9 @@ export class BrowserClient extends Client<BrowserClientOptions> {
};
}
} else {
addAutoIpAddressToUser(session);
if (session.ipAddress === undefined) {
session.ipAddress = '{{auto}}';
}
}
});
}
Expand Down Expand Up @@ -136,15 +143,3 @@ export class BrowserClient extends Client<BrowserClientOptions> {
return super._prepareEvent(event, hint, currentScope, isolationScope);
}
}

// 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
function addAutoIpAddressToUser(objWithMaybeUser: { user?: User | null }): void {
if (objWithMaybeUser.user?.ip_address === undefined) {
objWithMaybeUser.user = {
...objWithMaybeUser.user,
ip_address: '{{auto}}',
};
}
}
Loading