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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ function verifyTestCommandTelemetryProcessor(
: 'Unknown',
);

// NOTE: At this point, expectedError has been modified by trackException().
// If the exception type is not CodedError but any data got copied into envelope.CodedError.data,
// for instance autolinking error info, build the expected CodedError.data.
let expectedCodedErrorData = {};
Expand Down Expand Up @@ -740,3 +741,54 @@ 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 expectedError = 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',
],
nestedField: {
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.
anotherNestedField: {
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 caughtErrors: Error[] = [];
TelemetryTest.addTelemetryInitializer(
verifyTestCommandTelemetryProcessor(
caughtErrors,
expectedError.type,
expectedError,
),
);

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

TelemetryTest.endTest(() => {
// Check if any errors were thrown
expect(caughtErrors).toHaveLength(0);
});
},
);
29 changes: 29 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.
Telemetry.traverseCodedErrorStruct(codedErrorStruct.data);

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

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

return exceptionData;
}

static traverseCodedErrorStruct(
danielayala94 marked this conversation as resolved.
Show resolved Hide resolved
data: Record<string, any>,
path: string[] = [],
) {
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
const value = data[key];
const currentPath = [...path, key];

if (Array.isArray(value)) {
// Replace the array by reading all elements in it,
// then check if they are strings. If that's the case, sanitize.
data[key] = value.map(item =>
danielayala94 marked this conversation as resolved.
Show resolved Hide resolved
typeof item === 'string'
? errorUtils.sanitizeErrorMessage(item)
: item,
);
} else if (typeof value === 'object' && value !== null) {
Telemetry.traverseCodedErrorStruct(value, currentPath);
} else if (typeof value === 'string') {
data[key] = errorUtils.sanitizeErrorMessage(value);
}
}
}
}
}
Loading