Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Telemetry] Expand field sanitization to codedError.data #14161

Merged
merged 9 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Expand sanitization checks for error telemetry instances",
"packageName": "@react-native-windows/telemetry",
"email": "14967941+danielayala94@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,85 @@ test.each(testTelemetryOptions)(
});
},
);

test.each(testTelemetryOptions)(
'Telemetry run test command end to end with CodedError, verifies PII is scrubbed if present in CodedError.',
async options => {
await TelemetryTest.startTest(options);

const codedErrorInfo = new errorUtils.CodedError(
'MSBuildError', // type
'test error', // message
{
fieldWithPath:
'Test Error occurred at C:\\some\\file\\path\\project.build.appxrecipe', // expectation: replace the whole C:\\... thing with "[path]".
fieldWithNoPath: 'Test Error data', // expectation: no changes to this string.
fieldWithNoString: 14, // expectation: no changes to this value.
arrayField: [
'No path',
15,
'Clean this path: C:\\some\\file\\path2\\project.build.appxrecipe',
[
'No path',
150,
'Also clean this: C:\\some\\file\\path2\\project.build.appxrecipe',
],
],
someObject: {
fieldWithPath:
'Test Error occurred at C:\\some\\file\\path3\\project.build.appxrecipe', // expectation: replace the whole C:\\... thing with "[path]".
fieldWithNoPath: 'Test Error data 2', // expectation: no changes to this string.
fieldWithNoString: 16, // expectation: no changes to this value.
nestedObject: {
fieldWithPath:
'Test Error occurred at C:\\some\\file\\path4\\project.build.appxrecipe', // expectation: replace the whole C:\\... thing with "[path]".
fieldWithNoPath: 'Test Error data 3', // expectation: no changes to this string.
fieldWithNoString: 17, // expectation: no changes to this value.
},
},
}, // data
);

const expectedError = new errorUtils.CodedError(
'MSBuildError', // type
'test error', // message
{
fieldWithPath: 'Test Error occurred at [path]',
fieldWithNoPath: 'Test Error data',
fieldWithNoString: 14,
arrayField: [
'No path',
15,
'Clean this path: [path]',
['No path', 150, 'Also clean this: [path]'],
],
someObject: {
fieldWithPath: 'Test Error occurred at [path]',
fieldWithNoPath: 'Test Error data 2',
fieldWithNoString: 16,
nestedObject: {
fieldWithPath: 'Test Error occurred at [path]',
fieldWithNoPath: 'Test Error data 3',
fieldWithNoString: 17,
},
},
}, // data
);

const caughtErrors: Error[] = [];
TelemetryTest.addTelemetryInitializer(
verifyTestCommandTelemetryProcessor(
caughtErrors,
expectedError.type,
expectedError,
),
);

await runTestCommandE2E(() => testCommandBody(codedErrorInfo));

TelemetryTest.endTest(() => {
// Check if any errors were thrown
expect(caughtErrors).toHaveLength(0);
});
},
);
25 changes: 25 additions & 0 deletions packages/@react-native-windows/telemetry/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ export class Telemetry {
}
}

// Scrub any potential PII present in codedError.data array, as long as the data is a string.
codedErrorStruct.data = Telemetry.sanitizeAny(codedErrorStruct.data);

// Break down TS Error object into Exception Data
const exceptionData = Telemetry.convertErrorIntoExceptionData(error);

Expand Down Expand Up @@ -531,4 +534,26 @@ export class Telemetry {

return exceptionData;
}

static sanitizeAny(data: any): any {
danielayala94 marked this conversation as resolved.
Show resolved Hide resolved
if (Array.isArray(data)) {
// This is an array, sanitize each element recursively.
return data.map(item => Telemetry.sanitizeAny(item));
} else if (typeof data === 'object' && data !== null) {
// This is an object, sanitize each field recursively.
const sanitizedObject: Record<string, any> = {};
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
sanitizedObject[key] = Telemetry.sanitizeAny(data[key]);
}
}
return sanitizedObject;
} else if (typeof data === 'string') {
// The base case: this is a string, sanitize it.
return errorUtils.sanitizeErrorMessage(data);
}

// Not a string, return the data unchanged.
return data;
}
}