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

add one more restriction on the length of exception #1990 #2006

Merged
merged 11 commits into from
Mar 6, 2023
42 changes: 41 additions & 1 deletion shared/AppInsightsCommon/Tests/Unit/src/Exception.tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Assert, AITestClass } from "@microsoft/ai-test-framework";
import { Exception } from "../../../src/applicationinsights-common"
import { DataSanitizer, Exception } from "../../../src/applicationinsights-common"
import { DiagnosticLogger } from "@microsoft/applicationinsights-core-js";
import { IExceptionInternal, IExceptionDetailsInternal, IExceptionStackFrameInternal } from "../../../src/Interfaces/IExceptionTelemetry";
import { _ExceptionDetails, _StackFrame } from "../../../src/Telemetry/Exception";
Expand Down Expand Up @@ -61,6 +61,46 @@ export class ExceptionTests extends AITestClass {
}
});

this.testCase({
name: "ExceptionDetails: ExceptionDetails assembly field will be truncated",
test: () => {
try {
// const define
const MAX_STRING_LENGTH = DataSanitizer.MAX_STRING_LENGTH;
const messageLong = new Array(MAX_STRING_LENGTH + 10).join("abc");

const messageFollowRegex = "at functionName (a.js:1:1)"
const longMessageFollowRegex = messageFollowRegex.replace("functionName", messageLong)

let errObj = {
reason:{
message: "message",
stack: longMessageFollowRegex + "\n" + longMessageFollowRegex + "\n" + longMessageFollowRegex
}
};

let exception = Exception.CreateAutoException("message",
"url",
9,
0,
errObj
);
const exceptionDetails = new _ExceptionDetails(this.logger, exception);
console.log("exception", exceptionDetails.parsedStack)
console.log(exceptionDetails.parsedStack.length)
for (let i = 0; i < exceptionDetails.parsedStack.length; i++) {
Assert.ok(exceptionDetails.parsedStack[i].assembly.length <= MAX_STRING_LENGTH);
console.log(exceptionDetails.parsedStack[i].assembly.length, MAX_STRING_LENGTH)
}
} catch (e) {
console.log(e.stack);
console.log(e.toString());
Assert.ok(false, e.toString());
}
}
});


this.testCase({
name: "StackFrame: StackFrame can be exported to interface format",
test: () => {
Expand Down
6 changes: 6 additions & 0 deletions shared/AppInsightsCommon/src/Telemetry/Exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,12 @@ export class _ExceptionDetails implements IExceptionDetails, ISerializable {
_self.message = dataSanitizeMessage(logger, _formatMessage(exception || error, _self.typeName)) || strNotSpecified;
const stack = exception[strStackDetails] || _getStackFromErrorObj(exception);
_self.parsedStack = _parseStack(stack);

// after parsedStack is inited, iterate over each frame object, sanitize its assembly field
if (isArray(_self.parsedStack)){
arrMap(_self.parsedStack, (frame: _StackFrame) => frame.assembly = dataSanitizeString(logger, frame.assembly));
}

_self[strStack] = dataSanitizeException(logger, _formatStackTrace(stack));
_self.hasFullStack = isArray(_self.parsedStack) && _self.parsedStack.length > 0;

Expand Down