Skip to content

Commit

Permalink
add one more restriction on the length of exception #1990 (#2006)
Browse files Browse the repository at this point in the history
  • Loading branch information
siyuniu-ms authored Mar 6, 2023
2 parents 044384b + 0363f2b commit ed8b351
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
40 changes: 39 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,44 @@ 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);

for (let i = 0; i < exceptionDetails.parsedStack.length; i++) {
Assert.ok(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

0 comments on commit ed8b351

Please sign in to comment.