-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathsetupSentry.test.js
More file actions
594 lines (548 loc) · 19.2 KB
/
Copy pathsetupSentry.test.js
File metadata and controls
594 lines (548 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
import { cloneDeep } from 'lodash';
import {
removeUrlsFromBreadCrumb,
rewriteReport,
rewriteTransactionReport,
shouldCreateSpanForRequest,
} from './setupSentry';
describe('Setup Sentry', () => {
describe('rewriteReport', () => {
it('should remove urls from error messages', () => {
const testReport = {
message: 'This report has a test url: http://example.com',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report has a test url: **',
);
});
it('should remove urls from error reports that have an exception with an array of values', () => {
const testReport = {
exception: {
values: [
{
value: 'This report has a test url: http://example.com',
},
{
value: 'https://example.com is another url',
},
],
},
request: {},
};
rewriteReport(testReport);
expect(testReport.exception.values).toStrictEqual([
{
value: 'This report has a test url: **',
},
{
value: '** is another url',
},
]);
});
it('should remove ethereum addresses from error messages', () => {
const testReport = {
message:
'There is an ethereum address 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235 in this message',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'There is an ethereum address 0x** in this message',
);
});
it('should not remove urls from our allow list', () => {
const testReport = {
message: 'This report has an allowed url: https://codefi.network/',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report has an allowed url: https://codefi.network/',
);
});
it('should not remove urls at subdomains of the urls in the allow list', () => {
const testReport = {
message:
'This report has an allowed url: https://subdomain.codefi.network/',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report has an allowed url: https://subdomain.codefi.network/',
);
});
it('should remove urls very similar to, but different from, those in our allow list', () => {
const testReport = {
message:
'This report does not have an allowed url: https://nodefi.network/',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report does not have an allowed url: **',
);
});
it('should remove urls with allow list urls in their domain path', () => {
const testReport = {
message:
'This report does not have an allowed url: https://codefi.network.another.domain.com/',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report does not have an allowed url: **',
);
});
it('should remove urls have allowed urls in their URL path', () => {
const testReport = {
message:
'This report does not have an allowed url: https://example.com/test?redirect=http://codefi.network',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report does not have an allowed url: **',
);
});
it('should remove urls with subdomains', () => {
const testReport = {
message:
'This report does not have an allowed url: https://subdomain.example.com/',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report does not have an allowed url: **',
);
});
it('should remove invalid urls', () => {
const testReport = {
message:
'This report does not have an allowed url: https://example.%%%/',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This report does not have an allowed url: **',
);
});
it('should remove urls and ethereum addresses from error messages', () => {
const testReport = {
message:
'This 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235 address used http://example.com on Saturday',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'This 0x** address used ** on Saturday',
);
});
it('should not modify an error message with no urls or addresses', () => {
const testReport = {
message: 'This is a simple report',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual('This is a simple report');
});
it('removes Solana addresses from error messages', () => {
const testReport = {
message:
'There is a Solana address 7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs in this message',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'There is a Solana address ** in this message',
);
});
it('removes Tron addresses from error messages', () => {
const testReport = {
message:
'There is a Tron address TJRyWwFs9wTFGZg3JbrVriFbNfCug5tDeC in this message',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'There is a Tron address ** in this message',
);
});
it('removes Stellar (XLM) addresses from error messages', () => {
const testReport = {
message:
'There is a Stellar address GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX in this message',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'There is a Stellar address ** in this message',
);
});
it('removes Bitcoin bech32 addresses from error messages', () => {
const testReport = {
message:
'There is a Bitcoin address bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq in this message',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'There is a Bitcoin address ** in this message',
);
});
it('removes Bitcoin legacy addresses from error messages', () => {
const testReport = {
message:
'There is a Bitcoin address 17VZNX1SN5NtKa8UQFxwQbFeFc3iqRYhem in this message',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'There is a Bitcoin address ** in this message',
);
});
it('removes multiple EVM addresses from a single error message', () => {
const testReport = {
message:
'Addresses 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235 and 0x1234567890123456789012345678901234567890 failed',
request: {},
};
rewriteReport(testReport);
expect(testReport.message).toStrictEqual(
'Addresses 0x** and 0x** failed',
);
});
it('removes addresses from report.extra parameters', () => {
const testReport = {
message: 'An error occurred',
extra: {
accountAddress: '7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs',
nested: {
evmAddress: '0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
},
},
request: {},
};
rewriteReport(testReport);
expect(testReport.extra.accountAddress).toStrictEqual('**');
expect(testReport.extra.nested.evmAddress).toStrictEqual('0x**');
});
it('removes addresses from an array shared across multiple properties', () => {
const sharedAddresses = ['7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs'];
const testReport = {
message: 'An error occurred',
extra: {
first: sharedAddresses,
second: sharedAddresses,
},
request: {},
};
rewriteReport(testReport);
expect(testReport.extra.first).toStrictEqual(['**']);
expect(testReport.extra.second).toStrictEqual(['**']);
});
it('removes addresses from report.contexts parameters', () => {
const testReport = {
message: 'An error occurred',
contexts: {
account: {
address: 'TJRyWwFs9wTFGZg3JbrVriFbNfCug5tDeC',
},
},
request: {},
};
rewriteReport(testReport);
expect(testReport.contexts.account.address).toStrictEqual('**');
});
it('removes addresses from breadcrumbs when sending an error report', () => {
const testReport = {
message: 'An error occurred',
breadcrumbs: [
{
message: 'console.error',
data: {
arguments: [
new Error(
'Failed for 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
),
],
},
},
],
request: {},
};
rewriteReport(testReport);
expect(testReport.breadcrumbs[0].data.arguments[0].message).toStrictEqual(
'Failed for 0x**',
);
});
it('scrubs breadcrumbs without mutating live source objects', () => {
const liveError = new Error(
'Failed for 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
);
const liveArgs = [liveError];
const testReport = cloneDeep({
message: 'An error occurred',
breadcrumbs: [
{
message: 'console.error',
data: { arguments: liveArgs, logger: 'console' },
},
],
request: {},
});
rewriteReport(testReport);
expect(testReport.breadcrumbs[0].data.arguments[0].message).toStrictEqual(
'Failed for 0x**',
);
expect(liveError.message).toStrictEqual(
'Failed for 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
);
});
});
describe('rewriteTransactionReport', () => {
it('removes addresses from breadcrumbs when sending a transaction', () => {
const testReport = {
type: 'transaction',
transaction: 'ui.popup',
breadcrumbs: [
{
message: 'fetch',
data: {
url: 'https://api.example.com/0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
},
},
],
};
rewriteTransactionReport(testReport);
expect(testReport.breadcrumbs[0].data.url).toStrictEqual('');
});
});
describe('shouldCreateSpanForRequest', () => {
it('should return false for snap manifest fetches', () => {
expect(
shouldCreateSpanForRequest(
'chrome-extension://abcdefg/snaps/npm:@metamask/preinstalled-example-snap/snap.manifest.json',
),
).toStrictEqual(false);
expect(
shouldCreateSpanForRequest(
'moz-extension://abcdefg/snaps/npm:@metamask/message-signing-snap/snap.manifest.json',
),
).toStrictEqual(false);
});
it('should return false for locale file fetches', () => {
expect(
shouldCreateSpanForRequest(
'chrome-extension://abcdefg/_locales/en/messages.json',
),
).toStrictEqual(false);
expect(
shouldCreateSpanForRequest(
'moz-extension://abcdefg/_locales/es/messages.json',
),
).toStrictEqual(false);
});
it('should return false for root content-hashed json fetches (preinstalled snap bundles)', () => {
// These are the webpack `asset/resource` preinstalled-snap bundles that
// sit at the extension root and slipped past the old `/snaps/` regex.
expect(
shouldCreateSpanForRequest('chrome-extension://abc/deadbeef.json'),
).toStrictEqual(false);
expect(
shouldCreateSpanForRequest('moz-extension://abc/deadbeef.json'),
).toStrictEqual(false);
});
it('should return true for other local extension file fetches (only hashed json is blocked)', () => {
// Non-hashed local fetches still get spans — the block is scoped to the
// content-hashed preinstalled-snap bundles, not all local files.
expect(
shouldCreateSpanForRequest('chrome-extension://abc/home.html'),
).toStrictEqual(true);
expect(
shouldCreateSpanForRequest(
'chrome-extension://abcdefg/scripts/ppom-validator.wasm',
),
).toStrictEqual(true);
// A non-hex-named root json (e.g. a config file) is also still traced.
expect(
shouldCreateSpanForRequest('chrome-extension://abc/manifest.json'),
).toStrictEqual(true);
});
it('should return false for sentry.io domains', () => {
expect(
shouldCreateSpanForRequest('https://sentry.io/api/123'),
).toStrictEqual(false);
expect(
shouldCreateSpanForRequest('https://o123.ingest.sentry.io/envelope'),
).toStrictEqual(false);
});
it('should return false for segment.io domains', () => {
expect(
shouldCreateSpanForRequest('https://api.segment.io/v1/batch'),
).toStrictEqual(false);
});
it('should return false for static config domains', () => {
expect(
shouldCreateSpanForRequest('https://chainid.network/chains.json'),
).toStrictEqual(false);
expect(
shouldCreateSpanForRequest(
'https://acl.execution.metamask.io/latest/registry.json',
),
).toStrictEqual(false);
expect(
shouldCreateSpanForRequest(
'https://acl.execution.metamask.io/latest/signature.json',
),
).toStrictEqual(false);
});
it('should return true for external API URLs', () => {
expect(
shouldCreateSpanForRequest('https://mainnet.infura.io/v3/abc'),
).toStrictEqual(true);
expect(
shouldCreateSpanForRequest('https://api.coingecko.com/v3/simple/price'),
).toStrictEqual(true);
expect(
shouldCreateSpanForRequest('https://example.com/foo'),
).toStrictEqual(true);
// Other cx.metamask.io APIs are real calls — still traced (only the static
// `acl.execution.metamask.io` config is excluded, not all of metamask.io).
expect(
shouldCreateSpanForRequest(
'https://accounts.api.cx.metamask.io/v2/supportedNetworks',
),
).toStrictEqual(true);
expect(
shouldCreateSpanForRequest('https://token.api.cx.metamask.io/tokens/1'),
).toStrictEqual(true);
});
});
describe('removeUrlsFromBreadCrumb', () => {
it('should hide the breadcrumb data url', () => {
const testBreadcrumb = {
data: {
url: 'https://example.com',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data.url).toStrictEqual('');
});
it('should hide the breadcrumb data "to" page', () => {
const testBreadcrumb = {
data: {
to: 'https://example.com',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data.to).toStrictEqual('');
});
it('should hide the breadcrumb data "from" page', () => {
const testBreadcrumb = {
data: {
from: 'https://example.com',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data.from).toStrictEqual('');
});
it('should NOT hide the breadcrumb data url if the url is on the extension protocol', () => {
const testBreadcrumb = {
data: {
url: 'chrome-extension://abcefg/home.html',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data.url).toStrictEqual(
'chrome-extension://abcefg/home.html',
);
});
it('should NOT hide the breadcrumb data "to" page if the url is on the extension protocol', () => {
const testBreadcrumb = {
data: {
to: 'chrome-extension://abcefg/home.html',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data.to).toStrictEqual(
'chrome-extension://abcefg/home.html',
);
});
it('should NOT hide the breadcrumb data "from" page if the url is on the extension protocol', () => {
const testBreadcrumb = {
data: {
from: 'chrome-extension://abcefg/home.html',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data.from).toStrictEqual(
'chrome-extension://abcefg/home.html',
);
});
it('should hide "to" but not "from" or url if "to" is the only one not matching an internal url', () => {
const testBreadcrumb = {
data: {
url: 'chrome-extension://abcefg/home.html',
to: 'https://example.com',
from: 'chrome-extension://abcefg/home.html',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data).toStrictEqual({
url: 'chrome-extension://abcefg/home.html',
to: '',
from: 'chrome-extension://abcefg/home.html',
});
});
it('removes addresses from the breadcrumb message', () => {
const testBreadcrumb = {
message:
'Selected account 7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs',
data: {
address: '0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.message).toStrictEqual('Selected account **');
});
it('removes addresses from the breadcrumb data', () => {
const testBreadcrumb = {
message:
'Selected account 7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs',
data: {
address: '0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
},
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
expect(rewrittenBreadcrumb.data.address).toStrictEqual('0x**');
});
it('redacts the breadcrumb data without mutating the live source objects', () => {
const liveError = new Error(
'Failed for 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
);
const liveArgs = [
liveError,
'7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs',
];
const testBreadcrumb = {
message: 'console.error',
data: { arguments: liveArgs, logger: 'console' },
};
const rewrittenBreadcrumb = removeUrlsFromBreadCrumb(testBreadcrumb);
// The breadcrumb sent to Sentry is redacted...
expect(rewrittenBreadcrumb.data.arguments[0].message).toStrictEqual(
'Failed for 0x**',
);
expect(rewrittenBreadcrumb.data.arguments[1]).toStrictEqual('**');
// ...but the live Error and array the extension still holds are untouched.
expect(liveError.message).toStrictEqual(
'Failed for 0x790A8A9E9bc1C9dB991D8721a92e461Db4CfB235',
);
expect(liveArgs[1]).toStrictEqual(
'7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs',
);
});
});
});