From b6218eb42cbb0fbd04bd06e59e475b6148d5916e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Tue, 22 Jun 2021 11:46:07 +0200 Subject: [PATCH 1/2] fix: Prevent circular structure serialization in events --- .../src/integrations/globalhandlers.ts | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/packages/browser/src/integrations/globalhandlers.ts b/packages/browser/src/integrations/globalhandlers.ts index 0217e4226b53..5911bbeac285 100644 --- a/packages/browser/src/integrations/globalhandlers.ts +++ b/packages/browser/src/integrations/globalhandlers.ts @@ -85,17 +85,18 @@ export class GlobalHandlers implements Integration { } const client = currentHub.getClient(); - const event = isPrimitive(error) - ? this._eventFromIncompleteOnError(data.msg, data.url, data.line, data.column) - : this._enhanceEventWithInitialFrame( - eventFromUnknownInput(error, undefined, { - attachStacktrace: client && client.getOptions().attachStacktrace, - rejection: false, - }), - data.url, - data.line, - data.column, - ); + const event = + error === undefined && isString(data.msg) + ? this._eventFromIncompleteOnError(data.msg, data.url, data.line, data.column) + : this._enhanceEventWithInitialFrame( + eventFromUnknownInput(error || data.msg, undefined, { + attachStacktrace: client && client.getOptions().attachStacktrace, + rejection: false, + }), + data.url, + data.line, + data.column, + ); addExceptionMechanism(event, { handled: false, @@ -188,12 +189,10 @@ export class GlobalHandlers implements Integration { let message = isErrorEvent(msg) ? msg.message : msg; let name; - if (isString(message)) { - const groups = message.match(ERROR_TYPES_RE); - if (groups) { - name = groups[1]; - message = groups[2]; - } + const groups = message.match(ERROR_TYPES_RE); + if (groups) { + name = groups[1]; + message = groups[2]; } const event = { From 53cf33808c2ecb356dcd847c5a4d0eb2af48c4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Tue, 22 Jun 2021 14:07:07 +0200 Subject: [PATCH 2/2] Add an appropriate test --- .../test/integration/suites/onerror.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/browser/test/integration/suites/onerror.js b/packages/browser/test/integration/suites/onerror.js index d89b46051676..b32a5e4c67f1 100644 --- a/packages/browser/test/integration/suites/onerror.js +++ b/packages/browser/test/integration/suites/onerror.js @@ -130,6 +130,25 @@ describe("window.onerror", function() { }); }); + it("should onerror calls with non-string first argument gracefully", function() { + return runInSandbox(sandbox, function() { + window.onerror({ + type: "error", + otherKey: "hi", + }); + }).then(function(summary) { + assert.equal(summary.events[0].exception.values[0].type, "Error"); + assert.equal( + summary.events[0].exception.values[0].value, + "Non-Error exception captured with keys: otherKey, type" + ); + assert.deepEqual(summary.events[0].extra.__serialized__, { + type: "error", + otherKey: "hi", + }); + }); + }); + it("should NOT catch an exception already caught [but rethrown] via Sentry.captureException", function() { return runInSandbox(sandbox, function() { try {