Skip to content

Commit ebba343

Browse files
authored
fix(utils): Make new non-enumerable properties mutable (#4528)
There are a few places in the SDK where we add properties to an object in order to store data for internal use. In cases where there's a chance that object might be serialized, we need to make sure the new properties are non-enumerable, which we do with the (appropriately-named) function `addNonEnumerableProperty`. Currently, the non-enumerable properties we add are also immutable, even though nothing requires them to be, simply because that's the default when using `Object.defineProperty()`. This blocks use-cases in which a user might choose to modify an internal property's value in order to change how the SDK behaves. (See the attached issue for an example of such a use-case.) This PR explicitly makes these properties mutable. It also changes the docstring to reflect the fact that the function can be used for more than just setting properties on function objects. Fixes #4525.
1 parent 164c09c commit ebba343

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

packages/utils/src/misc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export function checkOrSetAlreadyCaught(exception: unknown): boolean {
277277
try {
278278
// set it this way rather than by assignment so that it's not ennumerable and therefore isn't recorded by the
279279
// `ExtraErrorData` integration
280-
addNonEnumerableProperty(exception, '__sentry_captured__', true);
280+
addNonEnumerableProperty(exception as { [key: string]: unknown }, '__sentry_captured__', true);
281281
} catch (err) {
282282
// `exception` is a primitive, so we can't mark it seen
283283
}

packages/utils/src/object.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,18 @@ export function fill(source: { [key: string]: any }, name: string, replacementFa
4242
}
4343

4444
/**
45-
* Defines a non enumerable property. This creates a non enumerable property on an object.
45+
* Defines a non-enumerable property on the given object.
4646
*
47-
* @param func The function to set a property to
48-
* @param name the name of the special sentry property
49-
* @param value the property to define
47+
* @param obj The object on which to set the property
48+
* @param name The name of the property to be set
49+
* @param value The value to which to set the property
5050
*/
51-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
52-
export function addNonEnumerableProperty(func: any, name: string, value: any): void {
53-
Object.defineProperty(func, name, {
51+
export function addNonEnumerableProperty(obj: { [key: string]: unknown }, name: string, value: unknown): void {
52+
Object.defineProperty(obj, name, {
53+
// enumerable: false, // the default, so we can save on bundle size by not explicitly setting it
5454
value: value,
55+
writable: true,
56+
configurable: true,
5557
});
5658
}
5759

0 commit comments

Comments
 (0)