forked from NomicFoundation/hardhat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanonymizer.ts
395 lines (330 loc) · 14.1 KB
/
anonymizer.ts
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
import { assert } from "chai";
import * as path from "path";
import { Anonymizer } from "../../../src/internal/sentry/anonymizer";
const PROJECT_ROOT = "/path/to/project";
class MockedAnonymizer extends Anonymizer {
public getFilePackageJsonPathResult: string | undefined = undefined;
protected _getFilePackageJsonPath(_: string): string | undefined {
return this.getFilePackageJsonPathResult;
}
}
describe("Anonymizer", () => {
it("should anonymize paths of the user's project", () => {
const anonymizer = new Anonymizer();
const anonymizationResult = anonymizer.anonymizeFilename(
path.join(PROJECT_ROOT, "src", "someFile.js")
);
assert.equal(anonymizationResult.anonymizedFilename, "<user-file>");
assert.isTrue(anonymizationResult.anonymizeContent);
});
it("should anonymize paths of the user's project in the project's root", () => {
const anonymizer = new Anonymizer();
const anonymizationResult = anonymizer.anonymizeFilename(
path.join(PROJECT_ROOT, "someFile.js")
);
assert.equal(anonymizationResult.anonymizedFilename, "<user-file>");
assert.isTrue(anonymizationResult.anonymizeContent);
});
it("should not anonymize paths of hardhat files", () => {
const anonymizer = new Anonymizer();
const hardhatFilePath = path.join(
"node_modules",
"@nomiclabs",
"hardhat",
"someHardhatFile.js"
);
const anonymizationResult = anonymizer.anonymizeFilename(
path.join(PROJECT_ROOT, hardhatFilePath)
);
assert.equal(
anonymizationResult.anonymizedFilename,
path.join(hardhatFilePath)
);
assert.isFalse(anonymizationResult.anonymizeContent);
});
it("should not anonymize internal node paths", () => {
const anonymizer = new Anonymizer();
const internalNodePath = path.join("internal", "some", "module", "main.js");
const anonymizationResult = anonymizer.anonymizeFilename(internalNodePath);
assert.equal(anonymizationResult.anonymizedFilename, internalNodePath);
assert.isFalse(anonymizationResult.anonymizeContent);
});
describe("hardhat config", () => {
it("should return only the config's relative path", () => {
const pathToHardhatConfig = path.join(PROJECT_ROOT, "hardhat.config.ts");
const anonymizer = new MockedAnonymizer(pathToHardhatConfig);
anonymizer.getFilePackageJsonPathResult = path.join(
PROJECT_ROOT,
"package.json"
);
const anonymizationResult =
anonymizer.anonymizeFilename(pathToHardhatConfig);
assert.equal(anonymizationResult.anonymizedFilename, "hardhat.config.ts");
assert.isTrue(anonymizationResult.anonymizeContent);
});
it("should return only the config's relative path when it's not in the root", () => {
const pathToHardhatConfig = path.join(
PROJECT_ROOT,
"config",
"hardhat.config.ts"
);
const anonymizer = new MockedAnonymizer(pathToHardhatConfig);
anonymizer.getFilePackageJsonPathResult = path.join(
PROJECT_ROOT,
"package.json"
);
const anonymizationResult =
anonymizer.anonymizeFilename(pathToHardhatConfig);
assert.equal(
anonymizationResult.anonymizedFilename,
path.join("config", "hardhat.config.ts")
);
assert.isTrue(anonymizationResult.anonymizeContent);
});
it("should return only the config's base name if package.json cannot be found", () => {
const pathToHardhatConfig = path.join(
PROJECT_ROOT,
"config",
"hardhat.config.ts"
);
const anonymizer = new MockedAnonymizer(pathToHardhatConfig);
const anonymizationResult =
anonymizer.anonymizeFilename(pathToHardhatConfig);
assert.equal(anonymizationResult.anonymizedFilename, "hardhat.config.ts");
assert.isTrue(anonymizationResult.anonymizeContent);
});
});
describe("error message", () => {
it("should return the same message if there are no paths", () => {
const anonymizer = new Anonymizer();
const errorMessage = "Something happened";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(anonymizedErrorMessage, errorMessage);
});
it("should anonymize a single path", () => {
const anonymizer = new Anonymizer();
const errorMessage = `Something happened at file ${__filename}`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"Something happened at file <user-file>"
);
});
it("should anonymize a path between parentheses", () => {
const anonymizer = new Anonymizer();
const errorMessage = `Something happened (${__filename})`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(anonymizedErrorMessage, "Something happened <user-file>");
});
it("should anonymize multiple paths", () => {
const anonymizer = new Anonymizer();
const file1 = __filename;
const file2 = path.resolve(__filename, "..", "some-other-file.js");
const errorMessage = `Something happened at file ${file1} and at file ${file2}`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"Something happened at file <user-file> and at file <user-file>"
);
});
it("should anonymize multiline errors", () => {
const anonymizer = new Anonymizer();
const file1 = __filename;
const file2 = path.resolve(__filename, "..", "some-other-file.js");
const errorMessage = `Something happened at file ${file1} and\nsomething else happened at file ${file2}`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
`Something happened at file <user-file> and\nsomething else happened at file <user-file>`
);
});
it("should anonymize files that end with ellipsis", () => {
const anonymizer = new Anonymizer();
const file = `${__filename.slice(0, __filename.length - 5)}...`;
const errorMessage = `Something happened at file ${file}: something`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"Something happened at file <user-file> something"
);
});
it("should anonymize a file that doesn't have a path separator", () => {
const anonymizer = new Anonymizer();
const errorMessage = "Something happened at file foo.json";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"Something happened at file <user-file>"
);
});
it("should anonymize multiple files that don't have a path separator", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"Something happened at file foo.json and at file bar.ts";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"Something happened at file <user-file> and at file <user-file>"
);
});
it("shouldn't anonymize stand-alone extensions", () => {
const anonymizer = new Anonymizer();
const errorMessage = "The .json extension is not supported";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(anonymizedErrorMessage, errorMessage);
});
it("shouldn't interpret periods as files with extensions", () => {
const anonymizer = new Anonymizer();
const errorMessage = "This is a sentence. This is another sentence.";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(anonymizedErrorMessage, errorMessage);
});
it("should hide a private key that starts with 0x", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"My PK is 0x3ecf4eda095143894fef9fc0c2480d44c4c7e40bf340448e3039da92888b3096";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"My PK is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
});
it("should hide a private key that doesn't start with 0x", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"My PK is 3ecf4eda095143894fef9fc0c2480d44c4c7e40bf340448e3039da92888b3096";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"My PK is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
});
it("should hide multiple private keys", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"My PKs are 0x3ecf4eda095143894fef9fc0c2480d44c4c7e40bf340448e3039da92888b3096\nand 0x7b63fe0cc949ac8fd4161a943b7ab26156c06315c2a9030e064980e7bcc7a056";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"My PKs are xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\nand xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
});
it("should hide addresses", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"My addresses are 0xf658b4176b77f6d6439D249D04f166eF315d69FC and 30444113Ad783A6bd92E057a21572a70890e7768";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"My addresses are xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx and xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
);
});
it("should hide mnemonic phrases", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"My mnemonic phrase is test test test test test test test test test test test test. This is an error message.";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"My mnemonic phrase is <mnemonic>. This is an error message."
);
});
it("should hide mnemonic phrases that start with spaces", () => {
const anonymizer = new Anonymizer();
const errorMessage =
" My mnemonic phrase is test test test test test test test test test test test test. This is an error message.";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
" My mnemonic phrase is <mnemonic>. This is an error message."
);
});
it("should hide mnemonic phrases that start with punctuation", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"[module] My mnemonic phrase is test test test test test test test test test test test test. This is an error message.";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"[module] My mnemonic phrase is <mnemonic>. This is an error message."
);
});
it("should hide mnemonic phrases with typos", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"My mnemonic phrase is test test test test test tset test test test test test test. This is an error message.";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"My mnemonic phrase is <mnemonic> tset <mnemonic>. This is an error message."
);
});
it("should hide mnemonic phrases with ideograms", () => {
const anonymizer = new Anonymizer();
const errorMessage =
"My mnemonic phrase is 就 就 就 就 就 就 就 就 就 就 就 就. This is an error message.";
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
"My mnemonic phrase is <mnemonic>. This is an error message."
);
});
it("should hide all mnemonic phrase fragments", () => {
const anonymizer = new Anonymizer();
const errorMessage = `My mnemonic phrase is test test test test test test test test test test test test. This is an error message.
And here is some more more more more`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
`My mnemonic phrase is <mnemonic>. This is an error message.
And here is some <mnemonic>`
);
});
it("should hide all mnemonic phrase fragments, even when one fragment is a subset of another one", () => {
const anonymizer = new Anonymizer();
const errorMessage = `My mnemonic phrase is test test test test test test test test test test test test. This is an error message.
And here is some test test test test. And then there is more more more more.
And a bit more more more more more more more more more more more more.`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
`My mnemonic phrase is <mnemonic>. This is an error message.
And here is some <mnemonic>. And then there is <mnemonic>.
And a bit <mnemonic>.`
);
});
it("should hide mnemonic phrase fragments, even when there's several kinds of whitespace", () => {
const anonymizer = new Anonymizer();
const errorMessage = `My mnemonic phrase is test test test test
test\ttest \r
test test test test test test. This is an error message.`;
const anonymizedErrorMessage =
anonymizer.anonymizeErrorMessage(errorMessage);
assert.equal(
anonymizedErrorMessage,
`My mnemonic phrase is <mnemonic>. This is an error message.`
);
});
});
});