Skip to content

Commit 3b4f18f

Browse files
authored
update error path processing to include current working directory for anonymization (#2832)
* update error path processing to include current working directory for anonymization * add test logs * try this * try that * try this * hopefully fixes tests * regex... * add more logs * try that * hopefully last try * forgot some
1 parent 17fe9cb commit 3b4f18f

File tree

5 files changed

+158
-5
lines changed

5 files changed

+158
-5
lines changed

.changeset/four-tigers-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/platform-core': patch
3+
---
4+
5+
Update error path processing to include current working directory for anonymization

packages/platform-core/src/telemetry/serializable_error.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ void describe('serializable error', () => {
5050
);
5151
});
5252

53+
void test('that error message does not contain current working directory', () => {
54+
const error = new Error(`${process.cwd()} test error`);
55+
const serializableError = new SerializableError(error);
56+
assert.ok(serializableError.message);
57+
const matches = [
58+
...serializableError.message.matchAll(new RegExp(process.cwd(), 'g')),
59+
];
60+
assert.ok(
61+
matches.length === 0,
62+
`${process.cwd()} is included in ${serializableError.message}`,
63+
);
64+
});
65+
5366
void test('that error message does not contain file url path with user homedir', () => {
5467
const error = new Error(
5568
`${pathToFileURL(process.cwd()).toString()} test error`,
@@ -65,6 +78,21 @@ void describe('serializable error', () => {
6578
);
6679
});
6780

81+
void test('that error message does not contain file url path with current working directory', () => {
82+
const error = new Error(
83+
`${pathToFileURL(process.cwd()).toString()} test error`,
84+
);
85+
const serializableError = new SerializableError(error);
86+
assert.ok(serializableError.message);
87+
const matches = [
88+
...serializableError.message.matchAll(new RegExp(process.cwd(), 'g')),
89+
];
90+
assert.ok(
91+
matches.length === 0,
92+
`${process.cwd()} is included in ${serializableError.message}`,
93+
);
94+
});
95+
6896
void test('that error message does not contain arns', () => {
6997
const error = new Error(
7098
'User: arn:aws:iam::123456789012:user/test is not authorized to perform: test-action',
@@ -102,6 +130,20 @@ void describe('serializable error', () => {
102130
);
103131
});
104132

133+
void test('that error stack does not contain current working directory', () => {
134+
const error = new Error(`${process.cwd()} test error`);
135+
error.stack = `${error.stack} at methodName (${process.cwd()}:12:34)\n`;
136+
const serializableError = new SerializableError(error);
137+
assert.ok(serializableError.stack);
138+
const matches = [
139+
...serializableError.stack.matchAll(new RegExp(process.cwd(), 'g')),
140+
];
141+
assert.ok(
142+
matches.length === 0,
143+
`${process.cwd()} is included in ${serializableError.stack}`,
144+
);
145+
});
146+
105147
void test('that error stack does not contain file url path with user homedir', () => {
106148
const error = new Error(
107149
`${pathToFileURL(process.cwd()).toString()} test error`,
@@ -126,6 +168,30 @@ void describe('serializable error', () => {
126168
);
127169
});
128170

171+
void test('that error stack does not contain file url path with current working directory', () => {
172+
const error = new Error(
173+
`${pathToFileURL(process.cwd()).toString()} test error`,
174+
);
175+
error.stack = `${error.stack} at methodName (${pathToFileURL(
176+
process.cwd(),
177+
).toString()}/node_modules/@aws-amplify/test-package/lib/test.js:12:34)\n`;
178+
const serializableError = new SerializableError(error);
179+
assert.ok(serializableError.stack);
180+
const matches = [
181+
...serializableError.stack.matchAll(new RegExp(process.cwd(), 'g')),
182+
];
183+
assert.ok(
184+
matches.length === 0,
185+
`${process.cwd()} is included in ${serializableError.stack}`,
186+
);
187+
const expectedFilePath =
188+
'node_modules/@aws-amplify/test-package/lib/test.js';
189+
assert.ok(
190+
serializableError.stack.includes(expectedFilePath),
191+
`${expectedFilePath} is not found in ${serializableError.stack}`,
192+
);
193+
});
194+
129195
void test('that error stack does not contain stacks', () => {
130196
const error = new Error('test error');
131197
error.stack =

packages/platform-core/src/telemetry/serializable_error.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export class SerializableError {
1212

1313
// breakdown of filePathRegex:
1414
// (file:/+)? -> matches optional file url prefix
15-
// homedir() -> users home directory, replacing \ with /
15+
// homedir()/process.cwd() -> users home directory or current working directory, replacing \ with /
1616
// [\\w.\\-_@\\\\/]+ -> matches nested directories and file name
1717
private filePathRegex = new RegExp(
18-
`(file:/+)?${homedir().replaceAll('\\', '/')}[\\w.\\-_@\\\\/]+`,
18+
`(file:/+)?(${homedir().replaceAll('\\', '/')}|${process.cwd().replaceAll('\\', '/')})[\\w.\\-_@\\\\/]+`,
1919
'g',
2020
);
2121
private arnRegex =
@@ -41,7 +41,6 @@ export class SerializableError {
4141
for (const match of matches) {
4242
result = result.replace(match[0], this.processPaths([match[0]])[0]);
4343
}
44-
4544
return result;
4645
};
4746

packages/platform-core/src/usage-data/serializable_error.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ void describe('serializable error', () => {
2727
});
2828
});
2929

30+
void test('that regular stack trace does not contain current working directory', () => {
31+
const error = new Error('test error');
32+
const serializableError = new SerializableError(error);
33+
assert.ok(serializableError.trace);
34+
serializableError.trace?.forEach((trace) => {
35+
assert.ok(
36+
trace.file.includes(process.cwd()) == false,
37+
`${process.cwd()} is included in the ${trace.file}`,
38+
);
39+
});
40+
});
41+
3042
void test('that regular stack trace does not contain user homedir for file url paths', () => {
3143
const error = new Error('test error');
3244
error.stack = `at methodName (${pathToFileURL(
@@ -42,6 +54,21 @@ void describe('serializable error', () => {
4254
});
4355
});
4456

57+
void test('that regular stack trace does not contain current working directory for file url paths', () => {
58+
const error = new Error('test error');
59+
error.stack = `at methodName (${pathToFileURL(
60+
process.cwd(),
61+
).toString()}/node_modules/@aws-amplify/test-package/lib/test.js:12:34)\n`;
62+
const serializableError = new SerializableError(error);
63+
assert.ok(serializableError.trace);
64+
serializableError.trace?.forEach((trace) => {
65+
assert.ok(
66+
trace.file.includes(process.cwd()) == false,
67+
`${process.cwd()} is included in the ${trace.file}`,
68+
);
69+
});
70+
});
71+
4572
void test('that if code is available it is used as the error name', () => {
4673
const error = new ErrorWithDetailsAndCode(
4774
'some error message',
@@ -125,6 +152,18 @@ void describe('serializable error', () => {
125152
);
126153
});
127154

155+
void test('that error message does not contain current working directory', () => {
156+
const error = new ErrorWithDetailsAndCode(`${process.cwd()} test error`);
157+
const serializableError = new SerializableError(error);
158+
const matches = [
159+
...serializableError.message.matchAll(new RegExp(process.cwd(), 'g')),
160+
];
161+
assert.ok(
162+
matches.length === 0,
163+
`${process.cwd()} is included in ${serializableError.message}`,
164+
);
165+
});
166+
128167
void test('that error message does not contain file url path with user homedir', () => {
129168
const error = new ErrorWithDetailsAndCode(
130169
`${pathToFileURL(process.cwd()).toString()} test error`,
@@ -139,6 +178,20 @@ void describe('serializable error', () => {
139178
);
140179
});
141180

181+
void test('that error message does not contain file url path with current working directory', () => {
182+
const error = new ErrorWithDetailsAndCode(
183+
`${pathToFileURL(process.cwd()).toString()} test error`,
184+
);
185+
const serializableError = new SerializableError(error);
186+
const matches = [
187+
...serializableError.message.matchAll(new RegExp(process.cwd(), 'g')),
188+
];
189+
assert.ok(
190+
matches.length === 0,
191+
`${process.cwd()} is included in ${serializableError.message}`,
192+
);
193+
});
194+
142195
void test('that error details do not contain user homedir', () => {
143196
const error = new ErrorWithDetailsAndCode(
144197
'test error',
@@ -154,6 +207,21 @@ void describe('serializable error', () => {
154207
);
155208
});
156209

210+
void test('that error details do not contain current working directory', () => {
211+
const error = new ErrorWithDetailsAndCode(
212+
'test error',
213+
`${process.cwd()} test details`,
214+
);
215+
const serializableError = new SerializableError(error);
216+
const matches = serializableError.details
217+
? [...serializableError.details.matchAll(new RegExp(process.cwd(), 'g'))]
218+
: [];
219+
assert.ok(
220+
serializableError.details && matches.length === 0,
221+
`${process.cwd()} is included in ${serializableError.details}`,
222+
);
223+
});
224+
157225
void test('that error details do not contain file url path with user homedir', () => {
158226
const error = new ErrorWithDetailsAndCode(
159227
'test error',
@@ -169,6 +237,21 @@ void describe('serializable error', () => {
169237
);
170238
});
171239

240+
void test('that error details do not contain file url path with current working directory', () => {
241+
const error = new ErrorWithDetailsAndCode(
242+
'test error',
243+
`${pathToFileURL(process.cwd()).toString()} test details`,
244+
);
245+
const serializableError = new SerializableError(error);
246+
const matches = serializableError.details
247+
? [...serializableError.details.matchAll(new RegExp(process.cwd(), 'g'))]
248+
: [];
249+
assert.ok(
250+
serializableError.details && matches.length === 0,
251+
`${process.cwd()} is included in ${serializableError.details}`,
252+
);
253+
});
254+
172255
void test('that error details do not contain AWS ARNs or stacks', () => {
173256
const error = new ErrorWithDetailsAndCode(
174257
'test error',

packages/platform-core/src/usage-data/serializable_error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export class SerializableError {
1313

1414
// breakdown of filePathRegex:
1515
// (file:/+)? -> matches optional file url prefix
16-
// homedir() -> users home directory, replacing \ with /
16+
// homedir()/process.cwd() -> users home directory or current working directory, replacing \ with /
1717
// [\\w.\\-_@\\\\/]+ -> matches nested directories and file name
1818
private filePathRegex = new RegExp(
19-
`(file:/+)?${homedir().replaceAll('\\', '/')}[\\w.\\-_@\\\\/]+`,
19+
`(file:/+)?(${homedir().replaceAll('\\', '/')}|${process.cwd().replaceAll('\\', '/')})[\\w.\\-_@\\\\/]+`,
2020
'g',
2121
);
2222
private stackTraceRegex =

0 commit comments

Comments
 (0)