2021-02-04 19:15:23 +01:00
|
|
|
import * as Sentry from '@sentry/browser';
|
|
|
|
import { Dedupe, ExtraErrorData } from '@sentry/integrations';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2022-08-23 20:44:14 +02:00
|
|
|
import { FilterEvents } from './sentry-filter-events';
|
2021-02-04 19:15:23 +01:00
|
|
|
import extractEthjsErrorMessage from './extractEthjsErrorMessage';
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2020-10-08 18:22:14 +02:00
|
|
|
/* eslint-disable prefer-destructuring */
|
|
|
|
// Destructuring breaks the inlining of the environment variables
|
2021-02-04 19:15:23 +01:00
|
|
|
const METAMASK_DEBUG = process.env.METAMASK_DEBUG;
|
|
|
|
const METAMASK_ENVIRONMENT = process.env.METAMASK_ENVIRONMENT;
|
2022-09-16 18:58:43 +02:00
|
|
|
const SENTRY_DSN_DEV =
|
|
|
|
process.env.SENTRY_DSN_DEV ||
|
|
|
|
'https://f59f3dd640d2429d9d0e2445a87ea8e1@sentry.io/273496';
|
2021-10-25 18:57:30 +02:00
|
|
|
const METAMASK_BUILD_TYPE = process.env.METAMASK_BUILD_TYPE;
|
2022-08-26 01:07:31 +02:00
|
|
|
const IN_TEST = process.env.IN_TEST;
|
2020-10-08 18:22:14 +02:00
|
|
|
/* eslint-enable prefer-destructuring */
|
2018-01-17 23:59:15 +01:00
|
|
|
|
2022-12-16 16:26:23 +01:00
|
|
|
export const ERROR_URL_ALLOWLIST = {
|
|
|
|
CRYPTOCOMPARE: 'cryptocompare.com',
|
|
|
|
COINGECKO: 'coingecko.com',
|
|
|
|
ETHERSCAN: 'etherscan.io',
|
|
|
|
CODEFI: 'codefi.network',
|
|
|
|
SEGMENT: 'segment.io',
|
|
|
|
};
|
|
|
|
|
2023-08-16 19:10:44 +02:00
|
|
|
// This describes the subset of background controller state attached to errors
|
|
|
|
// sent to Sentry These properties have some potential to be useful for
|
|
|
|
// debugging, and they do not contain any identifiable information.
|
|
|
|
export const SENTRY_BACKGROUND_STATE = {
|
2023-08-16 19:51:18 +02:00
|
|
|
AccountTracker: {
|
|
|
|
currentBlockGasLimit: true,
|
|
|
|
},
|
|
|
|
AlertController: {
|
|
|
|
alertEnabledness: true,
|
|
|
|
},
|
|
|
|
AppMetadataController: {
|
|
|
|
currentAppVersion: true,
|
|
|
|
previousAppVersion: true,
|
|
|
|
previousMigrationVersion: true,
|
|
|
|
currentMigrationVersion: true,
|
|
|
|
},
|
|
|
|
AppStateController: {
|
|
|
|
connectedStatusPopoverHasBeenShown: true,
|
|
|
|
defaultHomeActiveTabName: true,
|
|
|
|
},
|
|
|
|
CurrencyController: {
|
|
|
|
conversionDate: true,
|
|
|
|
conversionRate: true,
|
|
|
|
currentCurrency: true,
|
|
|
|
nativeCurrency: true,
|
|
|
|
},
|
|
|
|
DecryptMessageController: {
|
|
|
|
unapprovedDecryptMsgCount: true,
|
|
|
|
},
|
|
|
|
DesktopController: {
|
|
|
|
desktopEnabled: true,
|
|
|
|
},
|
|
|
|
EncryptionPublicKeyController: {
|
|
|
|
unapprovedEncryptionPublicKeyMsgCount: true,
|
|
|
|
},
|
|
|
|
KeyringController: {
|
|
|
|
isUnlocked: true,
|
|
|
|
},
|
|
|
|
MetaMetricsController: {
|
|
|
|
metaMetricsId: true,
|
|
|
|
participateInMetaMetrics: true,
|
|
|
|
},
|
|
|
|
NetworkController: {
|
|
|
|
networkId: true,
|
|
|
|
networkStatus: true,
|
|
|
|
providerConfig: {
|
|
|
|
nickname: true,
|
|
|
|
ticker: true,
|
|
|
|
type: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
OnboardingController: {
|
|
|
|
completedOnboarding: true,
|
|
|
|
firstTimeFlowType: true,
|
|
|
|
seedPhraseBackedUp: true,
|
|
|
|
},
|
|
|
|
PreferencesController: {
|
|
|
|
currentLocale: true,
|
|
|
|
featureFlags: true,
|
|
|
|
forgottenPassword: true,
|
|
|
|
ipfsGateway: true,
|
|
|
|
preferences: true,
|
|
|
|
useBlockie: true,
|
|
|
|
useNonceField: true,
|
|
|
|
usePhishDetect: true,
|
|
|
|
},
|
|
|
|
SignatureController: {
|
|
|
|
unapprovedMsgCount: true,
|
|
|
|
unapprovedPersonalMsgCount: true,
|
|
|
|
unapprovedTypedMessagesCount: true,
|
|
|
|
},
|
2023-08-16 19:10:44 +02:00
|
|
|
};
|
|
|
|
|
2023-08-16 19:51:18 +02:00
|
|
|
const flattenedBackgroundStateMask = Object.values(
|
|
|
|
SENTRY_BACKGROUND_STATE,
|
|
|
|
).reduce((partialBackgroundState, controllerState) => {
|
|
|
|
return {
|
|
|
|
...partialBackgroundState,
|
|
|
|
...controllerState,
|
|
|
|
};
|
|
|
|
}, {});
|
|
|
|
|
2020-07-17 17:09:38 +02:00
|
|
|
// This describes the subset of Redux state attached to errors sent to Sentry
|
|
|
|
// These properties have some potential to be useful for debugging, and they do
|
|
|
|
// not contain any identifiable information.
|
2023-08-16 19:10:44 +02:00
|
|
|
export const SENTRY_UI_STATE = {
|
2020-07-17 17:09:38 +02:00
|
|
|
gas: true,
|
|
|
|
history: true,
|
2023-08-16 19:51:18 +02:00
|
|
|
metamask: {
|
|
|
|
...flattenedBackgroundStateMask,
|
|
|
|
// This property comes from the background but isn't in controller state
|
|
|
|
isInitialized: true,
|
|
|
|
// These properties are in the `metamask` slice but not in the background state
|
|
|
|
customNonceValue: true,
|
|
|
|
isAccountMenuOpen: true,
|
|
|
|
nextNonce: true,
|
|
|
|
welcomeScreenSeen: true,
|
|
|
|
},
|
2020-07-17 17:09:38 +02:00
|
|
|
unconnectedAccount: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-07-17 17:09:38 +02:00
|
|
|
|
2023-08-18 10:17:23 +02:00
|
|
|
/**
|
|
|
|
* Returns whether MetaMetrics is enabled, given the application state.
|
|
|
|
*
|
|
|
|
* @param {{ state: unknown} | { persistedState: unknown }} appState - Application state
|
|
|
|
* @returns `true` if MetaMask's state has been initialized, and MetaMetrics
|
|
|
|
* is enabled, `false` otherwise.
|
|
|
|
*/
|
|
|
|
function getMetaMetricsEnabledFromAppState(appState) {
|
|
|
|
// during initialization after loading persisted state
|
|
|
|
if (appState.persistedState) {
|
|
|
|
return getMetaMetricsEnabledFromPersistedState(appState.persistedState);
|
|
|
|
// After initialization
|
|
|
|
} else if (appState.state) {
|
|
|
|
// UI
|
|
|
|
if (appState.state.metamask) {
|
|
|
|
return Boolean(appState.state.metamask.participateInMetaMetrics);
|
|
|
|
}
|
|
|
|
// background
|
|
|
|
return Boolean(
|
|
|
|
appState.state.MetaMetricsController?.participateInMetaMetrics,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// during initialization, before first persisted state is read
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether MetaMetrics is enabled, given the persisted state.
|
|
|
|
*
|
|
|
|
* @param {unknown} persistedState - Application state
|
|
|
|
* @returns `true` if MetaMask's state has been initialized, and MetaMetrics
|
|
|
|
* is enabled, `false` otherwise.
|
|
|
|
*/
|
|
|
|
function getMetaMetricsEnabledFromPersistedState(persistedState) {
|
|
|
|
return Boolean(
|
|
|
|
persistedState?.data?.MetaMetricsController?.participateInMetaMetrics,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether onboarding has completed, given the application state.
|
|
|
|
*
|
|
|
|
* @param {Record<string, unknown>} appState - Application state
|
|
|
|
* @returns `true` if MetaMask's state has been initialized, and MetaMetrics
|
|
|
|
* is enabled, `false` otherwise.
|
|
|
|
*/
|
|
|
|
function getOnboardingCompleteFromAppState(appState) {
|
|
|
|
// during initialization after loading persisted state
|
|
|
|
if (appState.persistedState) {
|
|
|
|
return Boolean(
|
|
|
|
appState.persistedState.data?.OnboardingController?.completedOnboarding,
|
|
|
|
);
|
|
|
|
// After initialization
|
|
|
|
} else if (appState.state) {
|
|
|
|
// UI
|
|
|
|
if (appState.state.metamask) {
|
|
|
|
return Boolean(appState.state.metamask.completedOnboarding);
|
|
|
|
}
|
|
|
|
// background
|
|
|
|
return Boolean(appState.state.OnboardingController?.completedOnboarding);
|
|
|
|
}
|
|
|
|
// during initialization, before first persisted state is read
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function setupSentry({ release, getState }) {
|
2022-04-04 21:14:32 +02:00
|
|
|
if (!release) {
|
|
|
|
throw new Error('Missing release');
|
2022-08-26 01:07:31 +02:00
|
|
|
} else if (METAMASK_DEBUG && !IN_TEST) {
|
|
|
|
/**
|
|
|
|
* Workaround until the following issue is resolved
|
|
|
|
* https://github.com/MetaMask/metamask-extension/issues/15691
|
|
|
|
* The IN_TEST condition allows the e2e tests to run with both
|
|
|
|
* yarn start:test and yarn build:test
|
|
|
|
*/
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
2022-01-03 14:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const environment =
|
2023-04-25 16:32:51 +02:00
|
|
|
METAMASK_BUILD_TYPE === 'main'
|
2022-01-03 14:59:44 +01:00
|
|
|
? METAMASK_ENVIRONMENT
|
|
|
|
: `${METAMASK_ENVIRONMENT}-${METAMASK_BUILD_TYPE}`;
|
|
|
|
|
2022-04-04 21:14:32 +02:00
|
|
|
let sentryTarget;
|
2022-01-03 14:59:44 +01:00
|
|
|
if (METAMASK_ENVIRONMENT === 'production') {
|
2020-07-29 18:14:08 +02:00
|
|
|
if (!process.env.SENTRY_DSN) {
|
2020-11-03 00:41:28 +01:00
|
|
|
throw new Error(
|
|
|
|
`Missing SENTRY_DSN environment variable in production environment`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-07-29 18:14:08 +02:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
console.log(
|
2022-01-03 14:59:44 +01:00
|
|
|
`Setting up Sentry Remote Error Reporting for '${environment}': SENTRY_DSN`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
sentryTarget = process.env.SENTRY_DSN;
|
2020-07-29 18:14:08 +02:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
console.log(
|
2022-01-03 14:59:44 +01:00
|
|
|
`Setting up Sentry Remote Error Reporting for '${environment}': SENTRY_DSN_DEV`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
sentryTarget = SENTRY_DSN_DEV;
|
2018-01-17 23:59:15 +01:00
|
|
|
}
|
|
|
|
|
2022-08-23 20:44:14 +02:00
|
|
|
/**
|
2023-08-18 10:17:23 +02:00
|
|
|
* Returns whether MetaMetrics is enabled. If the application hasn't yet
|
|
|
|
* been initialized, the persisted state will be used (if any).
|
2022-08-23 20:44:14 +02:00
|
|
|
*
|
2023-08-18 10:17:23 +02:00
|
|
|
* @returns `true` if MetaMetrics is enabled, `false` otherwise.
|
2022-08-23 20:44:14 +02:00
|
|
|
*/
|
2023-07-31 23:19:32 +02:00
|
|
|
async function getMetaMetricsEnabled() {
|
|
|
|
const appState = getState();
|
2023-08-18 10:17:23 +02:00
|
|
|
if (appState.state || appState.persistedState) {
|
|
|
|
return getMetaMetricsEnabledFromAppState(appState);
|
2023-07-31 23:19:32 +02:00
|
|
|
}
|
2023-08-18 10:17:23 +02:00
|
|
|
// If we reach here, it means the error was thrown before initialization
|
|
|
|
// completed, and before we loaded the persisted state for the first time.
|
2023-07-31 23:19:32 +02:00
|
|
|
try {
|
|
|
|
const persistedState = await globalThis.stateHooks.getPersistedState();
|
2023-08-18 10:17:23 +02:00
|
|
|
return getMetaMetricsEnabledFromPersistedState(persistedState);
|
2023-07-31 23:19:32 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2022-08-23 20:44:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 08:22:50 +02:00
|
|
|
Sentry.init({
|
|
|
|
dsn: sentryTarget,
|
|
|
|
debug: METAMASK_DEBUG,
|
2023-07-26 14:13:28 +02:00
|
|
|
/**
|
|
|
|
* autoSessionTracking defaults to true and operates by sending a session
|
|
|
|
* packet to sentry. This session packet does not appear to be filtered out
|
|
|
|
* via our beforeSend or FilterEvents integration. To avoid sending a
|
|
|
|
* request before we have the state tree and can validate the users
|
|
|
|
* preferences, we initiate this to false. Later, in startSession and
|
|
|
|
* endSession we modify this option and start the session or end the
|
|
|
|
* session manually.
|
|
|
|
*
|
|
|
|
* In sentry-install we call toggleSession after the page loads and state
|
|
|
|
* is available, this handles initiating the session for a user who has
|
|
|
|
* opted into MetaMetrics. This script is ran in both the background and UI
|
|
|
|
* so it should be effective at starting the session in both places.
|
|
|
|
*
|
|
|
|
* In the MetaMetricsController the session is manually started or stopped
|
|
|
|
* when the user opts in or out of MetaMetrics. This occurs in the
|
|
|
|
* setParticipateInMetaMetrics function which is exposed to the UI via the
|
|
|
|
* MetaMaskController.
|
|
|
|
*
|
|
|
|
* In actions.ts, after sending the updated participateInMetaMetrics flag
|
|
|
|
* to the background, we call toggleSession to ensure sentry is kept in
|
|
|
|
* sync with the user's preference.
|
|
|
|
*
|
|
|
|
* Types for the global Sentry object, and the new methods added as part of
|
|
|
|
* this effort were added to global.d.ts in the types folder.
|
|
|
|
*/
|
|
|
|
autoSessionTracking: false,
|
2021-10-25 18:57:30 +02:00
|
|
|
environment,
|
2022-08-23 20:44:14 +02:00
|
|
|
integrations: [
|
2023-07-26 14:13:28 +02:00
|
|
|
/**
|
|
|
|
* Filtering of events must happen in this FilterEvents custom
|
|
|
|
* integration instead of in the beforeSend handler because the Dedupe
|
|
|
|
* integration is unaware of the beforeSend functionality. If an event is
|
|
|
|
* queued in the sentry context, additional events of the same name will
|
|
|
|
* be filtered out by Dedupe even if the original event was not sent due
|
|
|
|
* to the beforeSend method returning null.
|
|
|
|
*
|
|
|
|
* @see https://github.com/MetaMask/metamask-extension/pull/15677
|
|
|
|
*/
|
2022-08-23 20:44:14 +02:00
|
|
|
new FilterEvents({ getMetaMetricsEnabled }),
|
|
|
|
new Dedupe(),
|
|
|
|
new ExtraErrorData(),
|
|
|
|
],
|
2018-08-09 22:49:40 +02:00
|
|
|
release,
|
2022-12-16 16:26:23 +01:00
|
|
|
beforeSend: (report) => rewriteReport(report, getState),
|
2023-02-20 18:13:12 +01:00
|
|
|
beforeBreadcrumb: beforeBreadcrumb(getState),
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-07-19 08:22:56 +02:00
|
|
|
|
2023-07-26 14:13:28 +02:00
|
|
|
/**
|
|
|
|
* As long as a reference to the Sentry Hub can be found, and the user has
|
|
|
|
* opted into MetaMetrics, change the autoSessionTracking option and start
|
|
|
|
* a new sentry session.
|
|
|
|
*/
|
2023-07-31 23:19:32 +02:00
|
|
|
const startSession = async () => {
|
2023-07-26 14:13:28 +02:00
|
|
|
const hub = Sentry.getCurrentHub?.();
|
|
|
|
const options = hub.getClient?.().getOptions?.() ?? {};
|
2023-07-31 23:19:32 +02:00
|
|
|
if (hub && (await getMetaMetricsEnabled()) === true) {
|
2023-07-26 14:13:28 +02:00
|
|
|
options.autoSessionTracking = true;
|
|
|
|
hub.startSession();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* As long as a reference to the Sentry Hub can be found, and the user has
|
|
|
|
* opted out of MetaMetrics, change the autoSessionTracking option and end
|
|
|
|
* the current sentry session.
|
|
|
|
*/
|
2023-07-31 23:19:32 +02:00
|
|
|
const endSession = async () => {
|
2023-07-26 14:13:28 +02:00
|
|
|
const hub = Sentry.getCurrentHub?.();
|
|
|
|
const options = hub.getClient?.().getOptions?.() ?? {};
|
2023-07-31 23:19:32 +02:00
|
|
|
if (hub && (await getMetaMetricsEnabled()) === false) {
|
2023-07-26 14:13:28 +02:00
|
|
|
options.autoSessionTracking = false;
|
|
|
|
hub.endSession();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the appropriate method (either startSession or endSession) depending
|
|
|
|
* on the state of metaMetrics optin and the state of autoSessionTracking on
|
|
|
|
* the Sentry client.
|
|
|
|
*/
|
2023-07-31 23:19:32 +02:00
|
|
|
const toggleSession = async () => {
|
2023-07-26 14:13:28 +02:00
|
|
|
const hub = Sentry.getCurrentHub?.();
|
|
|
|
const options = hub.getClient?.().getOptions?.() ?? {
|
|
|
|
autoSessionTracking: false,
|
|
|
|
};
|
2023-07-31 23:19:32 +02:00
|
|
|
const isMetaMetricsEnabled = await getMetaMetricsEnabled();
|
2023-07-26 14:13:28 +02:00
|
|
|
if (
|
|
|
|
isMetaMetricsEnabled === true &&
|
|
|
|
options.autoSessionTracking === false
|
|
|
|
) {
|
2023-07-31 23:19:32 +02:00
|
|
|
await startSession();
|
2023-07-26 14:13:28 +02:00
|
|
|
} else if (
|
|
|
|
isMetaMetricsEnabled === false &&
|
|
|
|
options.autoSessionTracking === true
|
|
|
|
) {
|
2023-07-31 23:19:32 +02:00
|
|
|
await endSession();
|
2023-07-26 14:13:28 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...Sentry,
|
|
|
|
startSession,
|
|
|
|
endSession,
|
|
|
|
toggleSession,
|
|
|
|
};
|
2022-12-16 16:26:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives a string and returns that string if it is a
|
|
|
|
* regex match for a url with a `chrome-extension` or `moz-extension`
|
|
|
|
* protocol, and an empty string otherwise.
|
|
|
|
*
|
|
|
|
* @param {string} url - The URL to check.
|
|
|
|
* @returns {string} An empty string if the URL was internal, or the unmodified URL otherwise.
|
|
|
|
*/
|
|
|
|
function hideUrlIfNotInternal(url) {
|
|
|
|
const re = /^(chrome-extension|moz-extension):\/\//u;
|
|
|
|
if (!url.match(re)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2023-02-20 18:13:12 +01:00
|
|
|
/**
|
|
|
|
* Returns a method that handles the Sentry breadcrumb using a specific method to get the extension state
|
|
|
|
*
|
|
|
|
* @param {Function} getState - A method that returns the state of the extension
|
|
|
|
* @returns {(breadcrumb: object) => object} A method that modifies a Sentry breadcrumb object
|
|
|
|
*/
|
|
|
|
export function beforeBreadcrumb(getState) {
|
|
|
|
return (breadcrumb) => {
|
2023-08-18 10:17:23 +02:00
|
|
|
if (!getState) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const appState = getState();
|
|
|
|
if (
|
|
|
|
!getMetaMetricsEnabledFromAppState(appState) ||
|
|
|
|
!getOnboardingCompleteFromAppState(appState) ||
|
|
|
|
breadcrumb?.category === 'ui.input'
|
|
|
|
) {
|
2023-02-20 18:13:12 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const newBreadcrumb = removeUrlsFromBreadCrumb(breadcrumb);
|
|
|
|
return newBreadcrumb;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-16 16:26:23 +01:00
|
|
|
/**
|
|
|
|
* Receives a Sentry breadcrumb object and potentially removes urls
|
|
|
|
* from its `data` property, it particular those possibly found at
|
|
|
|
* data.from, data.to and data.url
|
|
|
|
*
|
|
|
|
* @param {object} breadcrumb - A Sentry breadcrumb object: https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/
|
|
|
|
* @returns {object} A modified Sentry breadcrumb object.
|
|
|
|
*/
|
|
|
|
export function removeUrlsFromBreadCrumb(breadcrumb) {
|
|
|
|
if (breadcrumb?.data?.url) {
|
|
|
|
breadcrumb.data.url = hideUrlIfNotInternal(breadcrumb.data.url);
|
|
|
|
}
|
|
|
|
if (breadcrumb?.data?.to) {
|
|
|
|
breadcrumb.data.to = hideUrlIfNotInternal(breadcrumb.data.to);
|
|
|
|
}
|
|
|
|
if (breadcrumb?.data?.from) {
|
|
|
|
breadcrumb.data.from = hideUrlIfNotInternal(breadcrumb.data.from);
|
|
|
|
}
|
|
|
|
return breadcrumb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives a Sentry event object and modifies it before the
|
|
|
|
* error is sent to Sentry. Modifications include both sanitization
|
|
|
|
* of data via helper methods and addition of state data from the
|
|
|
|
* return value of the second parameter passed to the function.
|
|
|
|
*
|
|
|
|
* @param {object} report - A Sentry event object: https://develop.sentry.dev/sdk/event-payloads/
|
|
|
|
* @param {Function} getState - A function that should return an object representing some amount
|
|
|
|
* of app state that we wish to submit with our error reports
|
|
|
|
* @returns {object} A modified Sentry event object.
|
|
|
|
*/
|
|
|
|
export function rewriteReport(report, getState) {
|
|
|
|
try {
|
|
|
|
// simplify certain complex error messages (e.g. Ethjs)
|
|
|
|
simplifyErrorMessages(report);
|
|
|
|
// remove urls from error message
|
|
|
|
sanitizeUrlsFromErrorMessages(report);
|
|
|
|
// Remove evm addresses from error message.
|
|
|
|
// Note that this is redundent with data scrubbing we do within our sentry dashboard,
|
|
|
|
// but putting the code here as well gives public visibility to how we are handling
|
|
|
|
// privacy with respect to sentry.
|
|
|
|
sanitizeAddressesFromErrorMessages(report);
|
|
|
|
// modify report urls
|
|
|
|
rewriteReportUrls(report);
|
|
|
|
// append app state
|
|
|
|
if (getState) {
|
|
|
|
const appState = getState();
|
|
|
|
if (!report.extra) {
|
|
|
|
report.extra = {};
|
2020-07-17 17:09:38 +02:00
|
|
|
}
|
2022-12-16 16:26:23 +01:00
|
|
|
report.extra.appState = appState;
|
2018-10-20 08:22:50 +02:00
|
|
|
}
|
2022-12-16 16:26:23 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
2018-10-20 08:22:50 +02:00
|
|
|
}
|
2022-12-16 16:26:23 +01:00
|
|
|
return report;
|
|
|
|
}
|
2018-03-23 23:24:32 +01:00
|
|
|
|
2022-12-16 16:26:23 +01:00
|
|
|
/**
|
|
|
|
* Receives a Sentry event object and modifies it so that urls are removed from any of its
|
|
|
|
* error messages.
|
|
|
|
*
|
|
|
|
* @param {object} report - the report to modify
|
|
|
|
*/
|
|
|
|
function sanitizeUrlsFromErrorMessages(report) {
|
|
|
|
rewriteErrorMessages(report, (errorMessage) => {
|
|
|
|
let newErrorMessage = errorMessage;
|
|
|
|
const re = /(([-.+a-zA-Z]+:\/\/)|(www\.))\S+[@:.]\S+/gu;
|
|
|
|
const urlsInMessage = newErrorMessage.match(re) || [];
|
|
|
|
urlsInMessage.forEach((url) => {
|
|
|
|
try {
|
|
|
|
const urlObj = new URL(url);
|
|
|
|
const { hostname } = urlObj;
|
|
|
|
if (
|
|
|
|
!Object.values(ERROR_URL_ALLOWLIST).some(
|
|
|
|
(allowedHostname) =>
|
|
|
|
hostname === allowedHostname ||
|
|
|
|
hostname.endsWith(`.${allowedHostname}`),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
newErrorMessage = newErrorMessage.replace(url, '**');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
newErrorMessage = newErrorMessage.replace(url, '**');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return newErrorMessage;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives a Sentry event object and modifies it so that ethereum addresses are removed from
|
|
|
|
* any of its error messages.
|
|
|
|
*
|
|
|
|
* @param {object} report - the report to modify
|
|
|
|
*/
|
|
|
|
function sanitizeAddressesFromErrorMessages(report) {
|
|
|
|
rewriteErrorMessages(report, (errorMessage) => {
|
|
|
|
const newErrorMessage = errorMessage.replace(/0x[A-Fa-f0-9]{40}/u, '0x**');
|
|
|
|
return newErrorMessage;
|
|
|
|
});
|
2018-04-30 21:10:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function simplifyErrorMessages(report) {
|
2018-05-01 01:05:01 +02:00
|
|
|
rewriteErrorMessages(report, (errorMessage) => {
|
|
|
|
// simplify ethjs error messages
|
2021-02-04 19:15:23 +01:00
|
|
|
let simplifiedErrorMessage = extractEthjsErrorMessage(errorMessage);
|
2018-05-01 01:05:01 +02:00
|
|
|
// simplify 'Transaction Failed: known transaction'
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
simplifiedErrorMessage.indexOf(
|
|
|
|
'Transaction Failed: known transaction',
|
|
|
|
) === 0
|
|
|
|
) {
|
2018-05-01 01:05:01 +02:00
|
|
|
// cut the hash from the error message
|
2021-02-04 19:15:23 +01:00
|
|
|
simplifiedErrorMessage = 'Transaction Failed: known transaction';
|
2018-05-01 01:05:01 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return simplifiedErrorMessage;
|
|
|
|
});
|
2018-05-01 01:05:01 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function rewriteErrorMessages(report, rewriteFn) {
|
2018-05-01 01:05:01 +02:00
|
|
|
// rewrite top level message
|
2019-11-20 01:03:20 +01:00
|
|
|
if (typeof report.message === 'string') {
|
2021-02-04 19:15:23 +01:00
|
|
|
report.message = rewriteFn(report.message);
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-05-01 01:05:01 +02:00
|
|
|
// rewrite each exception message
|
2018-04-30 21:07:48 +02:00
|
|
|
if (report.exception && report.exception.values) {
|
2020-02-15 21:34:12 +01:00
|
|
|
report.exception.values.forEach((item) => {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (typeof item.value === 'string') {
|
2021-02-04 19:15:23 +01:00
|
|
|
item.value = rewriteFn(item.value);
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-30 21:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function rewriteReportUrls(report) {
|
2023-02-20 18:13:12 +01:00
|
|
|
if (report.request?.url) {
|
|
|
|
// update request url
|
|
|
|
report.request.url = toMetamaskUrl(report.request.url);
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:24:32 +01:00
|
|
|
// update exception stack trace
|
2018-04-29 22:46:07 +02:00
|
|
|
if (report.exception && report.exception.values) {
|
2020-02-15 21:34:12 +01:00
|
|
|
report.exception.values.forEach((item) => {
|
2020-03-12 14:51:05 +01:00
|
|
|
if (item.stacktrace) {
|
|
|
|
item.stacktrace.frames.forEach((frame) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
frame.filename = toMetamaskUrl(frame.filename);
|
|
|
|
});
|
2020-03-12 14:51:05 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-04-29 22:46:07 +02:00
|
|
|
}
|
2018-03-23 23:24:32 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function toMetamaskUrl(origUrl) {
|
2023-02-20 18:13:12 +01:00
|
|
|
if (!globalThis.location?.origin) {
|
|
|
|
return origUrl;
|
|
|
|
}
|
|
|
|
|
2022-12-16 16:26:23 +01:00
|
|
|
const filePath = origUrl?.split(globalThis.location.origin)[1];
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!filePath) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return origUrl;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2023-07-21 21:03:25 +02:00
|
|
|
const metamaskUrl = `/metamask${filePath}`;
|
2021-02-04 19:15:23 +01:00
|
|
|
return metamaskUrl;
|
2018-03-23 23:24:32 +01:00
|
|
|
}
|