1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-29 07:16:36 +01:00
metamask-extension/shared/modules/object.utils.js
Pedro Figueiredo 827c2fb741
feat: Add more state props from controllers to Sentry mask (#20595)
* feat: update sentry mask adding  controller props to improve error monitoring

* fix:remove changes in chrome-driver dependency

* Remove properties from mask

* Add more values to mask

* Sort the mask alphabetically

* Add termsOfUseLastAgreed to mask

* Fix test imports

* Update policy gap test to compare UI mask

* Reorganize tests under one describe block

* Update snapshots

* Mask another timestamp in state snapshots

* Mask browser environment properties

* Add missing UI field mask, and refactor field masking/removal

* Eliminate remaining policy gaps

* Simplify ganache options

* Eliminate extra mask properties

* Update mask to capture dynamic keys

The mask now supports dynamic keys. This lets set more fine-grained rules
for which data to include within dynamic data structures.

The mask has been updated to include just top-level keys for various
token-related data collections in state. This lets us see the chain IDs
that users have tokens on. This will be useful in debugging Sentry
reports of invalid keys in these data structures.

* Add additional 'expected missing state' entries

* Remove unnecessary properties from state snapshot

* Add providerConfig.chainId to state snapshot

* Update error state snapshots

---------

Co-authored-by: Danica Shen <zhaodanica@gmail.com>
Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2023-08-29 20:47:58 -02:30

45 lines
1.7 KiB
JavaScript

/**
* This symbol matches all object properties when used in a mask
*/
export const AllProperties = Symbol('*');
/**
* Return a "masked" copy of the given object. The returned object includes
* only the properties present in the mask.
*
* The mask is an object that mirrors the structure of the given object, except
* the only values are `true`, `false, a sub-mask, or the 'AllProperties"
* symbol. `true` implies the property should be included, and `false` will
* exclude it. A sub-mask implies the property should be further masked
* according to that sub-mask. The "AllProperties" symbol is used for objects
* with dynamic keys, and applies a rule (either `true`, `false`, or a
* sub-mask`) to every property in that object.
*
* If a property is excluded, its type is included instead.
*
* @param {object} object - The object to mask
* @param {Object<object | boolean>} mask - The mask to apply to the object
*/
export function maskObject(object, mask) {
let maskAllProperties = false;
if (Object.keys(mask).includes(AllProperties)) {
if (Object.keys(mask).length > 1) {
throw new Error('AllProperties mask key does not support sibling keys');
}
maskAllProperties = true;
}
return Object.keys(object).reduce((state, key) => {
const maskKey = maskAllProperties ? mask[AllProperties] : mask[key];
if (maskKey === true) {
state[key] = object[key];
} else if (maskKey && typeof maskKey === 'object') {
state[key] = maskObject(object[key], maskKey);
} else if (maskKey === undefined || maskKey === false) {
state[key] = typeof object[key];
} else {
throw new Error(`Unsupported mask entry: ${maskKey}`);
}
return state;
}, {});
}