Skip to content

Commit 7e68e24

Browse files
FIX: missing ip settings from SDK package (#390)
* ported ip fix to cordova * changelog * Update src/js/integrations/sdkinfo.ts Co-authored-by: Lukas Stracke <[email protected]> --------- Co-authored-by: Lukas Stracke <[email protected]>
1 parent 2e3a067 commit 7e68e24

File tree

3 files changed

+148
-13
lines changed

3 files changed

+148
-13
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Important Changes
6+
7+
- **fix(browser): Ensure IP address is only inferred by Relay if `sendDefaultPii` is `true`** ([#390](https://github.com/getsentry/sentry-cordova/pull/390))
8+
9+
This release includes a fix for a behaviour change
10+
that was originally introduced with the newer JavaScript SDK: User IP Addresses should only be added to Sentry events automatically,
11+
if `sendDefaultPii` was set to `true`.
12+
13+
To avoid making a major bump, the fix was patched on the current version and not by bumping to V10.
14+
There is _no API_ breakage involved and hence it is safe to update.
15+
However, after updating the SDK, events (errors, traces, replays, etc.) sent from the browser, will only include
16+
user IP addresses, if you set `sendDefaultPii: true` in your `Sentry.init` options.
17+
18+
We apologize for any inconvenience caused!
19+
320
## 1.6.0
421

522
### Fixes
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { addEventProcessor, getClient } from '@sentry/core';
2+
3+
import { SdkInfo } from '../../integrations/sdkinfo';
4+
import { SDK_NAME, SDK_VERSION } from '../../version';
5+
6+
jest.mock('@sentry/core', () => ({
7+
addEventProcessor: jest.fn(),
8+
getClient: jest.fn(),
9+
}));
10+
11+
describe('SdkInfo Integration', () => {
12+
beforeEach(() => {
13+
jest.clearAllMocks();
14+
});
15+
16+
it('should have correct id and name', () => {
17+
const sdkInfo = new SdkInfo();
18+
expect(SdkInfo.id).toBe('SdkInfo');
19+
expect(sdkInfo.name).toBe('SdkInfo');
20+
});
21+
22+
it('should add an event processor', () => {
23+
(getClient as jest.Mock).mockReturnValue({
24+
getOptions: () => ({ sendDefaultPii: true }),
25+
});
26+
27+
const sdkInfo = new SdkInfo();
28+
sdkInfo.setupOnce();
29+
30+
expect(addEventProcessor).toHaveBeenCalled();
31+
});
32+
33+
it('should patch event sdk info correctly with sendDefaultPii true', async () => {
34+
(getClient as jest.Mock).mockReturnValue({
35+
getOptions: () => ({ sendDefaultPii: true }),
36+
});
37+
38+
const sdkInfo = new SdkInfo();
39+
sdkInfo.setupOnce();
40+
41+
const eventProcessor = (addEventProcessor as jest.Mock).mock.calls[0][0];
42+
43+
const event = { sdk: { packages: [] } };
44+
const processedEvent = await eventProcessor(event);
45+
46+
expect(processedEvent.platform).toBe('javascript');
47+
expect(processedEvent.sdk.name).toBe(SDK_NAME);
48+
expect(processedEvent.sdk.version).toBe(SDK_VERSION);
49+
expect(processedEvent.sdk.packages).toContainEqual({
50+
name: 'npm:sentry-cordova',
51+
version: SDK_VERSION,
52+
});
53+
expect(processedEvent.sdk.settings?.infer_ip).toBe('auto');
54+
});
55+
56+
it('should patch event sdk info correctly with sendDefaultPii false', async () => {
57+
(getClient as jest.Mock).mockReturnValue({
58+
getOptions: () => ({ sendDefaultPii: false }),
59+
});
60+
61+
const sdkInfo = new SdkInfo();
62+
sdkInfo.setupOnce();
63+
64+
const eventProcessor = (addEventProcessor as jest.Mock).mock.calls[0][0];
65+
66+
const event = { sdk: {} };
67+
const processedEvent = await eventProcessor(event);
68+
69+
expect(processedEvent.sdk.settings?.infer_ip).toBe('never');
70+
});
71+
72+
it('should preserve existing sdk settings', async () => {
73+
(getClient as jest.Mock).mockReturnValue({
74+
getOptions: () => ({ sendDefaultPii: true }),
75+
});
76+
77+
const sdkInfo = new SdkInfo();
78+
sdkInfo.setupOnce();
79+
80+
const eventProcessor = (addEventProcessor as jest.Mock).mock.calls[0][0];
81+
82+
const event = {
83+
sdk: {
84+
packages: [],
85+
settings: { debug: true },
86+
},
87+
};
88+
const processedEvent = await eventProcessor(event);
89+
90+
expect(processedEvent.sdk.settings).toEqual({
91+
infer_ip: 'auto',
92+
debug: true,
93+
});
94+
});
95+
});

src/js/integrations/sdkinfo.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import { addEventProcessor } from '@sentry/core';
2-
import type { Integration } from '@sentry/types';
1+
import { addEventProcessor, getClient } from '@sentry/core';
2+
import type { Integration, SdkInfo as SdkInfoType} from '@sentry/types';
33

44
import { SDK_NAME, SDK_VERSION } from '../version';
55

6+
interface IpPatchedSdkInfo extends SdkInfoType {
7+
settings?: {
8+
infer_ip?: 'auto' | 'never';
9+
};
10+
}
11+
612
/** Default SdkInfo instrumentation */
713
export class SdkInfo implements Integration {
814
/**
@@ -19,21 +25,38 @@ export class SdkInfo implements Integration {
1925
* @inheritDoc
2026
*/
2127
public setupOnce(): void {
28+
29+
let defaultPii: boolean | undefined = undefined;
30+
31+
const client = getClient();
32+
if (client) {
33+
const options = client.getOptions();
34+
defaultPii = options.sendDefaultPii;
35+
}
36+
2237
addEventProcessor(async (event) => {
2338
event.platform = event.platform || 'javascript';
24-
event.sdk = {
25-
...event.sdk,
26-
name: SDK_NAME,
27-
packages: [
28-
...((event.sdk && event.sdk.packages) || []),
29-
{
30-
name: 'npm:sentry-cordova',
31-
version: SDK_VERSION,
32-
},
33-
],
34-
version: SDK_VERSION,
39+
const sdk = (event.sdk || {}) as IpPatchedSdkInfo;
40+
41+
sdk.name = sdk.name || SDK_NAME;
42+
sdk.packages = [
43+
...((event.sdk && event.sdk.packages) || []),
44+
{
45+
name: 'npm:sentry-cordova',
46+
version: SDK_VERSION,
47+
},
48+
];
49+
sdk.version = SDK_VERSION;
50+
51+
// Patch missing infer_ip.
52+
sdk.settings = {
53+
infer_ip: defaultPii ? 'auto' : 'never',
54+
// purposefully allowing already passed settings to override the default
55+
...sdk.settings
3556
};
3657

58+
event.sdk = sdk;
59+
3760
return event;
3861
});
3962
}

0 commit comments

Comments
 (0)