Skip to content

Commit 170d5ea

Browse files
authored
Fix the issue with changing immutable metadata structure in the contructor of ReactNativeClient (#5202)
* Fix the issue with changing immutable metadata structure in the contructor of `ReactNativeClient` * lint fix * Test updates * Test fixes * Updated changelog message * Message fix * lint fixes
1 parent 955f2eb commit 170d5ea

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### Fixes
1616

1717
- Fixes .env file loading in Expo sourcemap uploads ([#5210](https://github.com/getsentry/sentry-react-native/pull/5210))
18+
- Fixes the issue with changing immutable metadata structure in the contructor of `ReactNativeClient`. This structure is getting re-created instead of being modified to ensure IP address is only inferred by Relay if `sendDefaultPii` is `true` ([#5202](https://github.com/getsentry/sentry-react-native/pull/5202))
1819

1920
### Dependencies
2021

packages/core/src/js/client.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ export class ReactNativeClient extends Client<ReactNativeClientOptions> {
4949
*/
5050
public constructor(options: ReactNativeClientOptions) {
5151
ignoreRequireCycleLogs(ReactNativeLibraries.ReactNativeVersion?.version);
52-
options._metadata = options._metadata || {};
53-
options._metadata.sdk = options._metadata.sdk || defaultSdkInfo;
54-
55-
// Only allow IP inferral by Relay if sendDefaultPii is true
56-
if (options._metadata?.sdk) {
57-
options._metadata.sdk.settings = {
58-
infer_ip: options.sendDefaultPii ? 'auto' : 'never',
59-
...options._metadata.sdk.settings,
60-
};
61-
}
52+
options._metadata = {
53+
...options._metadata,
54+
sdk: {
55+
...(options._metadata?.sdk || defaultSdkInfo),
56+
settings: {
57+
// Only allow IP inferral by Relay if sendDefaultPii is true
58+
infer_ip: options.sendDefaultPii ? 'auto' : 'never',
59+
...options._metadata?.sdk?.settings,
60+
},
61+
},
62+
};
6263

6364
// We default this to true, as it is the safer scenario
6465
options.parentSpanIsAlwaysRootSpan =

packages/core/test/client.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ describe('Tests ReactNativeClient', () => {
679679
);
680680
});
681681

682-
test('adds ip_address {{auto}} to user if set to undefined', () => {
682+
test("doesn't change infer_ip if the ip_address is set to undefined", () => {
683683
client.captureEvent({
684684
user: {
685685
ip_address: undefined,
@@ -692,13 +692,13 @@ describe('Tests ReactNativeClient', () => {
692692
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].sdk).toEqual(
693693
expect.objectContaining({
694694
settings: {
695-
infer_ip: 'never',
695+
infer_ip: 'auto',
696696
},
697697
}),
698698
);
699699
});
700700

701-
test('adds ip_address undefined to user if not set', () => {
701+
test("doesn't change infer_ip if the user is not set", () => {
702702
client.captureEvent({
703703
user: {},
704704
});
@@ -707,20 +707,20 @@ describe('Tests ReactNativeClient', () => {
707707
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].sdk).toEqual(
708708
expect.objectContaining({
709709
settings: {
710-
infer_ip: 'never',
710+
infer_ip: 'auto',
711711
},
712712
}),
713713
);
714714
});
715715

716-
test('leaves ip_address undefined to undefined user', () => {
716+
test("doesn't change infer_ip if the event is empty", () => {
717717
client.captureEvent({});
718718

719719
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].user).toBeUndefined();
720720
expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload].sdk).toEqual(
721721
expect.objectContaining({
722722
settings: {
723-
infer_ip: 'never',
723+
infer_ip: 'auto',
724724
},
725725
}),
726726
);

0 commit comments

Comments
 (0)