Skip to content

Commit ac3cf20

Browse files
authored
fix: Prevent circular structure serialization in events (#3727)
* fix: Prevent circular structure serialization in events
1 parent 0d0aba1 commit ac3cf20

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,18 @@ export class GlobalHandlers implements Integration {
8585
}
8686

8787
const client = currentHub.getClient();
88-
const event = isPrimitive(error)
89-
? this._eventFromIncompleteOnError(data.msg, data.url, data.line, data.column)
90-
: this._enhanceEventWithInitialFrame(
91-
eventFromUnknownInput(error, undefined, {
92-
attachStacktrace: client && client.getOptions().attachStacktrace,
93-
rejection: false,
94-
}),
95-
data.url,
96-
data.line,
97-
data.column,
98-
);
88+
const event =
89+
error === undefined && isString(data.msg)
90+
? this._eventFromIncompleteOnError(data.msg, data.url, data.line, data.column)
91+
: this._enhanceEventWithInitialFrame(
92+
eventFromUnknownInput(error || data.msg, undefined, {
93+
attachStacktrace: client && client.getOptions().attachStacktrace,
94+
rejection: false,
95+
}),
96+
data.url,
97+
data.line,
98+
data.column,
99+
);
99100

100101
addExceptionMechanism(event, {
101102
handled: false,
@@ -188,12 +189,10 @@ export class GlobalHandlers implements Integration {
188189
let message = isErrorEvent(msg) ? msg.message : msg;
189190
let name;
190191

191-
if (isString(message)) {
192-
const groups = message.match(ERROR_TYPES_RE);
193-
if (groups) {
194-
name = groups[1];
195-
message = groups[2];
196-
}
192+
const groups = message.match(ERROR_TYPES_RE);
193+
if (groups) {
194+
name = groups[1];
195+
message = groups[2];
197196
}
198197

199198
const event = {

packages/browser/test/integration/suites/onerror.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ describe("window.onerror", function() {
130130
});
131131
});
132132

133+
it("should onerror calls with non-string first argument gracefully", function() {
134+
return runInSandbox(sandbox, function() {
135+
window.onerror({
136+
type: "error",
137+
otherKey: "hi",
138+
});
139+
}).then(function(summary) {
140+
assert.equal(summary.events[0].exception.values[0].type, "Error");
141+
assert.equal(
142+
summary.events[0].exception.values[0].value,
143+
"Non-Error exception captured with keys: otherKey, type"
144+
);
145+
assert.deepEqual(summary.events[0].extra.__serialized__, {
146+
type: "error",
147+
otherKey: "hi",
148+
});
149+
});
150+
});
151+
133152
it("should NOT catch an exception already caught [but rethrown] via Sentry.captureException", function() {
134153
return runInSandbox(sandbox, function() {
135154
try {

0 commit comments

Comments
 (0)