Skip to content

Commit b12a241

Browse files
authored
ref: Make sure that captureMessage input is a primitive (#1825)
1 parent 3c45c2f commit b12a241

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
`7` lines pre/post
1111
- [core]: ref: Change way how transports are initialized
1212
- [core]: ref: Rename `RequestBuffer` to `PromiseBuffer`, also introduce limit
13+
- [core]: ref: Make sure that captureMessage input is a primitive
1314
- [browser] fix: Prevent empty exception values
1415

1516
## 4.4.2

packages/core/src/baseclient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
Status,
1212
} from '@sentry/types';
1313
import { forget } from '@sentry/utils/async';
14+
import { isPrimitive } from '@sentry/utils/is';
1415
import { logger } from '@sentry/utils/logger';
1516
import { consoleSandbox, uuid4 } from '@sentry/utils/misc';
1617
import { truncate } from '@sentry/utils/string';
@@ -155,7 +156,10 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
155156
): Promise<SentryResponse> {
156157
return this.buffer.add(
157158
(async () => {
158-
const event = await this.getBackend().eventFromMessage(message, level, hint);
159+
const event = isPrimitive(message)
160+
? await this.getBackend().eventFromMessage(`${message}`, level, hint)
161+
: await this.getBackend().eventFromException(message, hint);
162+
159163
return this.captureEvent(event, hint, scope);
160164
})(),
161165
);

packages/core/test/lib/base.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ describe('BaseClient', () => {
191191
message: 'test message',
192192
});
193193
});
194+
195+
test.only('should call eventFromException if input to captureMessage is not a primitive', async () => {
196+
const client = new TestClient({ dsn: PUBLIC_DSN });
197+
const scope = new Scope();
198+
const spy = jest.spyOn(TestBackend.instance!, 'eventFromException');
199+
200+
await client.captureMessage('foo', undefined, undefined, scope);
201+
await client.captureMessage(null, undefined, undefined, scope);
202+
await client.captureMessage(undefined, undefined, undefined, scope);
203+
await client.captureMessage(1, undefined, undefined, scope);
204+
await client.captureMessage(false, undefined, undefined, scope);
205+
expect(spy.mock.calls.length).toEqual(0);
206+
207+
await client.captureMessage({}, undefined, undefined, scope);
208+
await client.captureMessage([], undefined, undefined, scope);
209+
expect(spy.mock.calls.length).toEqual(2);
210+
});
194211
});
195212

196213
describe('captureEvent() / prepareEvent()', () => {

packages/utils/src/is.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ export function isString(wat: any): boolean {
8484
return Object.prototype.toString.call(wat) === '[object String]';
8585
}
8686

87+
/**
88+
* Checks whether given value's is a primitive (undefined, null, number, boolean, string)
89+
* {@link isPrimitive}.
90+
*
91+
* @param wat A value to be checked.
92+
* @returns A boolean representing the result.
93+
*/
94+
export function isPrimitive(wat: any): boolean {
95+
return wat === null || (typeof wat !== 'object' && typeof wat !== 'function');
96+
}
97+
8798
/**
8899
* Checks whether given value's type is an array
89100
* {@link isArray}.

packages/utils/test/is.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isDOMError, isDOMException, isError, isErrorEvent } from '../src/is';
1+
import { isDOMError, isDOMException, isError, isErrorEvent, isPrimitive } from '../src/is';
22
import { supportsDOMError, supportsDOMException, supportsErrorEvent } from '../src/supports';
33

44
class SentryError extends Error {
@@ -56,3 +56,18 @@ if (supportsErrorEvent()) {
5656
});
5757
});
5858
}
59+
60+
describe('isPrimitive()', () => {
61+
test('should work as advertised', () => {
62+
expect(isPrimitive(undefined)).toEqual(true);
63+
expect(isPrimitive(null)).toEqual(true);
64+
expect(isPrimitive(true)).toEqual(true);
65+
expect(isPrimitive('foo')).toEqual(true);
66+
expect(isPrimitive(42)).toEqual(true);
67+
68+
expect(isPrimitive({})).toEqual(false);
69+
expect(isPrimitive([])).toEqual(false);
70+
expect(isPrimitive(new Error('foo'))).toEqual(false);
71+
expect(isPrimitive(new Date())).toEqual(false);
72+
});
73+
});

0 commit comments

Comments
 (0)