From 313af8558bd24da360923a79efac5cf19ac384f4 Mon Sep 17 00:00:00 2001 From: Olusegun Akintayo Date: Wed, 22 Nov 2023 17:16:56 +0300 Subject: [PATCH 1/2] fix: After triggering a Transaction, a Signature request metrics event is also triggered (#21753) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** Problem: after triggering any transaction (not a signature) we can see how now a Signature request is also triggered. Additionally, the information about RPC requests is held in the Signature request (which should not be there) instead of in the transaction request. ## **Expected behavior** No Signature event should be triggered if we perform a transaction RPC information should be held inside the transaction request, instead of the signature, for transactions ## **Fix** We are sending signature events on confirm page, we shouldn't. This call is now removed. Then on Transaction Events, we should add the blockaid RPC calls. There's already a code for this in metrics helper. We just need to call this in metrics to add the blockaid params. ## **Related issues** Fixes: #21739 ## **Manual testing steps** 1. Enable a metrics project / or look the background console 2. Select Mainnet 3. Go to the test dapp 4. Trigger a transaction 5. See how no info about RPC is held in the transaction event 6. See how a Signature request is also triggered, alongside the transaction 7. Checkout this branch 8. Repeat step 1-4 9. See how RPC info is sent in Transaction Added event 10. See how no signature request event is triggered. ## **Screenshots/Recordings** ### **Before** https://github.com/MetaMask/metamask-extension/assets/54408225/96293fc6-507c-41d1-9fbc-e38a011ff845 ### **After** https://github.com/MetaMask/metamask-extension/assets/44811/44bbd5f6-247a-4369-8b2c-9f3c8c180075 ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've clearly explained what problem this PR is solving and how it is solved. - [x] I've linked related issues - [x] I've included manual testing steps - [x] I've included screenshots/recordings if applicable - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. - [x] I’ve properly set the pull request status: - [ ] In case it's not yet "ready for review", I've set it to "draft". - [x] In case it's "ready for review", I've changed it from "draft" to "non-draft". ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Signed-off-by: Akintayo A. Olusegun --- app/scripts/lib/transaction/metrics.test.ts | 32 +++++++++++++-- app/scripts/lib/transaction/metrics.ts | 40 +++++++------------ .../signature-request-siwe.js | 2 +- .../transaction-alerts/transaction-alerts.js | 21 ---------- ui/helpers/utils/metrics.js | 4 ++ .../confirm-approve-content.component.js | 25 ------------ ui/pages/token-allowance/token-allowance.js | 29 -------------- 7 files changed, 49 insertions(+), 104 deletions(-) diff --git a/app/scripts/lib/transaction/metrics.test.ts b/app/scripts/lib/transaction/metrics.test.ts index 82c781461d49..b4fb52e4d366 100644 --- a/app/scripts/lib/transaction/metrics.test.ts +++ b/app/scripts/lib/transaction/metrics.test.ts @@ -17,7 +17,10 @@ import { } from '../../../../shared/constants/metametrics'; import { TRANSACTION_ENVELOPE_TYPE_NAMES } from '../../../../shared/lib/transactions-controller-utils'; ///: BEGIN:ONLY_INCLUDE_IN(blockaid) -import { BlockaidReason } from '../../../../shared/constants/security-provider'; +import { + BlockaidReason, + BlockaidResultType, +} from '../../../../shared/constants/security-provider'; ///: END:ONLY_INCLUDE_IN(blockaid) import { handleTransactionAdded, @@ -107,8 +110,9 @@ describe('Transaction metrics', () => { // copy mockTransactionMeta and add blockaid data mockTransactionMetaWithBlockaid = { ...JSON.parse(JSON.stringify(mockTransactionMeta)), - securityProviderResponse: { - flagAsDangerous: 1, + securityAlertResponse: { + result_type: BlockaidResultType.Malicious, + reason: BlockaidReason.maliciousDomain, providerRequestsCount: { eth_call: 5, eth_getCode: 3, @@ -230,6 +234,8 @@ describe('Transaction metrics', () => { persist: true, properties: { ...expectedProperties, + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ui_customizations: ['flagged_as_malicious'], ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, @@ -315,6 +321,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -330,6 +338,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -442,6 +452,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -460,6 +472,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -627,6 +641,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -647,6 +663,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -750,6 +768,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -769,6 +789,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -867,6 +889,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, @@ -882,6 +906,8 @@ describe('Transaction metrics', () => { properties: { ...expectedProperties, ui_customizations: ['flagged_as_malicious'], + security_alert_reason: BlockaidReason.maliciousDomain, + security_alert_response: 'Malicious', ppom_eth_call_count: 5, ppom_eth_getCode_count: 3, }, diff --git a/app/scripts/lib/transaction/metrics.ts b/app/scripts/lib/transaction/metrics.ts index 90fed3dbdd8e..5805362c4948 100644 --- a/app/scripts/lib/transaction/metrics.ts +++ b/app/scripts/lib/transaction/metrics.ts @@ -37,6 +37,7 @@ import { BlockaidReason, BlockaidResultType, } from '../../../../shared/constants/security-provider'; +import { getBlockaidMetricsParams } from '../../../../ui/helpers/utils/metrics'; ///: END:ONLY_INCLUDE_IN import { getSnapAndHardwareInfoForMetrics, @@ -926,36 +927,25 @@ async function buildEventFragmentProperties({ } let uiCustomizations; + let additionalBlockaidParams; + + // eslint-disable-next-line no-lonely-if + if (securityProviderResponse?.flagAsDangerous === 1) { + uiCustomizations = ['flagged_as_malicious']; + } else if (securityProviderResponse?.flagAsDangerous === 2) { + uiCustomizations = ['flagged_as_safety_unknown']; + } else { + uiCustomizations = null; + } ///: BEGIN:ONLY_INCLUDE_IN(blockaid) if (securityAlertResponse?.result_type === BlockaidResultType.Failed) { uiCustomizations = ['security_alert_failed']; } else { - ///: END:ONLY_INCLUDE_IN - // eslint-disable-next-line no-lonely-if - if (securityProviderResponse?.flagAsDangerous === 1) { - uiCustomizations = ['flagged_as_malicious']; - } else if (securityProviderResponse?.flagAsDangerous === 2) { - uiCustomizations = ['flagged_as_safety_unknown']; - } else { - uiCustomizations = null; - } - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) + additionalBlockaidParams = getBlockaidMetricsParams(securityAlertResponse); + uiCustomizations = additionalBlockaidParams?.ui_customizations ?? null; } - ///: END:ONLY_INCLUDE_IN - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) - const additionalBlockaidParams = {} as Record; - - if (securityProviderResponse?.providerRequestsCount) { - Object.keys(securityProviderResponse.providerRequestsCount).forEach( - (key) => { - const metricKey = `ppom_${key}_count`; - additionalBlockaidParams[metricKey] = - securityProviderResponse.providerRequestsCount[key]; - }, - ); - } ///: END:ONLY_INCLUDE_IN if (simulationFails) { @@ -985,13 +975,13 @@ async function buildEventFragmentProperties({ token_standard: tokenStandard, transaction_type: transactionType, transaction_speed_up: type === TransactionType.retry, - ui_customizations: uiCustomizations, + ...additionalBlockaidParams, + ui_customizations: uiCustomizations?.length > 0 ? uiCustomizations : null, ///: BEGIN:ONLY_INCLUDE_IN(blockaid) security_alert_response: securityAlertResponse?.result_type ?? BlockaidResultType.NotApplicable, security_alert_reason: securityAlertResponse?.reason ?? BlockaidReason.notApplicable, - ...additionalBlockaidParams, ///: END:ONLY_INCLUDE_IN gas_estimation_failed: Boolean(simulationFails), } as Record; diff --git a/ui/components/app/signature-request-siwe/signature-request-siwe.js b/ui/components/app/signature-request-siwe/signature-request-siwe.js index 7d85ea1cf159..e4efee227615 100644 --- a/ui/components/app/signature-request-siwe/signature-request-siwe.js +++ b/ui/components/app/signature-request-siwe/signature-request-siwe.js @@ -89,7 +89,7 @@ export default function SignatureRequestSIWE({ txData }) { }, }); } - }, [txData?.securityAlertResponse]); + }, []); ///: END:ONLY_INCLUDE_IN const { diff --git a/ui/components/app/transaction-alerts/transaction-alerts.js b/ui/components/app/transaction-alerts/transaction-alerts.js index 00f0c6ab58ac..3596aee31b0b 100644 --- a/ui/components/app/transaction-alerts/transaction-alerts.js +++ b/ui/components/app/transaction-alerts/transaction-alerts.js @@ -2,7 +2,6 @@ import React, { ///: BEGIN:ONLY_INCLUDE_IN(blockaid) useCallback, useContext, - useEffect, ///: END:ONLY_INCLUDE_IN } from 'react'; import PropTypes from 'prop-types'; @@ -33,7 +32,6 @@ import { MetaMetricsEventCategory, MetaMetricsEventName, } from '../../../../shared/constants/metametrics'; -import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics'; ///: END:ONLY_INCLUDE_IN const TransactionAlerts = ({ @@ -74,25 +72,6 @@ const TransactionAlerts = ({ const trackEvent = useContext(MetaMetricsContext); ///: END:ONLY_INCLUDE_IN - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) - useEffect(() => { - if (txData.securityAlertResponse) { - const blockaidMetricsParams = getBlockaidMetricsParams( - txData.securityAlertResponse, - ); - - trackEvent({ - category: MetaMetricsEventCategory.Transactions, - event: MetaMetricsEventName.SignatureRequested, - properties: { - action: 'Sign Request', - ...blockaidMetricsParams, - }, - }); - } - }, [txData?.securityAlertResponse]); - ///: END:ONLY_INCLUDE_IN - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) const onClickSupportLink = useCallback(() => { trackEvent({ diff --git a/ui/helpers/utils/metrics.js b/ui/helpers/utils/metrics.js index 796377bcdee6..65f1642767a7 100644 --- a/ui/helpers/utils/metrics.js +++ b/ui/helpers/utils/metrics.js @@ -39,6 +39,10 @@ export const getBlockaidMetricsParams = (securityAlertResponse = null) => { additionalParams.ui_customizations = ['flagged_as_malicious']; } + if (resultType === BlockaidResultType.Failed) { + additionalParams.ui_customizations = ['security_alert_failed']; + } + if (resultType !== BlockaidResultType.Benign) { additionalParams.security_alert_reason = BlockaidReason.notApplicable; diff --git a/ui/pages/confirm-approve/confirm-approve-content/confirm-approve-content.component.js b/ui/pages/confirm-approve/confirm-approve-content/confirm-approve-content.component.js index fe2820637278..e435b9206891 100644 --- a/ui/pages/confirm-approve/confirm-approve-content/confirm-approve-content.component.js +++ b/ui/pages/confirm-approve/confirm-approve-content/confirm-approve-content.component.js @@ -23,11 +23,6 @@ import { ConfirmPageContainerWarning } from '../../../components/app/confirm-pag import LedgerInstructionField from '../../../components/app/ledger-instruction-field'; ///: BEGIN:ONLY_INCLUDE_IN(blockaid) import BlockaidBannerAlert from '../../../components/app/security-provider-banner-alert/blockaid-banner-alert/blockaid-banner-alert'; -import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics'; -import { - MetaMetricsEventCategory, - MetaMetricsEventName, -} from '../../../../shared/constants/metametrics'; ///: END:ONLY_INCLUDE_IN import { isSuspiciousResponse } from '../../../../shared/modules/security-provider.utils'; @@ -103,26 +98,6 @@ export default class ConfirmApproveContent extends Component { setShowContractDetails: false, }; - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) - componentDidMount() { - const { txData } = this.props; - if (txData.securityAlertResponse) { - const blockaidMetricsParams = getBlockaidMetricsParams( - txData.securityAlertResponse, - ); - - this.context.trackEvent({ - category: MetaMetricsEventCategory.Transactions, - event: MetaMetricsEventName.SignatureRequested, - properties: { - action: 'Sign Request', - ...blockaidMetricsParams, - }, - }); - } - } - ///: END:ONLY_INCLUDE_IN - renderApproveContentCard({ showHeader = true, symbol, diff --git a/ui/pages/token-allowance/token-allowance.js b/ui/pages/token-allowance/token-allowance.js index 78ac171323db..2bae6f067205 100644 --- a/ui/pages/token-allowance/token-allowance.js +++ b/ui/pages/token-allowance/token-allowance.js @@ -63,12 +63,6 @@ import { import { isSuspiciousResponse } from '../../../shared/modules/security-provider.utils'; ///: BEGIN:ONLY_INCLUDE_IN(blockaid) import BlockaidBannerAlert from '../../components/app/security-provider-banner-alert/blockaid-banner-alert/blockaid-banner-alert'; -import { getBlockaidMetricsParams } from '../../helpers/utils/metrics'; -import { - MetaMetricsEventCategory, - MetaMetricsEventName, -} from '../../../shared/constants/metametrics'; -import { MetaMetricsContext } from '../../contexts/metametrics'; ///: END:ONLY_INCLUDE_IN import { ConfirmPageContainerNavigation } from '../../components/app/confirm-page-container'; import { useSimulationFailureWarning } from '../../hooks/useSimulationFailureWarning'; @@ -143,29 +137,6 @@ export default function TokenAllowance({ const nextNonce = useSelector(getNextSuggestedNonce); const customNonceValue = useSelector(getCustomNonceValue); - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) - const trackEvent = useContext(MetaMetricsContext); - ///: END:ONLY_INCLUDE_IN - - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) - useEffect(() => { - if (txData.securityAlertResponse) { - const blockaidMetricsParams = getBlockaidMetricsParams( - txData.securityAlertResponse, - ); - - trackEvent({ - category: MetaMetricsEventCategory.Transactions, - event: MetaMetricsEventName.SignatureRequested, - properties: { - action: 'Sign Request', - ...blockaidMetricsParams, - }, - }); - } - }, [txData?.securityAlertResponse]); - ///: END:ONLY_INCLUDE_IN - /** * We set the customSpendingCap to the dappProposedTokenAmount, if provided, rather than setting customTokenAmount * because customTokenAmount is reserved for custom user input. This is only set once when the component is mounted. From 3ef4a2cc548485c3d0d32ef52109cc0de6364ce4 Mon Sep 17 00:00:00 2001 From: Olusegun Akintayo Date: Thu, 23 Nov 2023 11:21:29 +0300 Subject: [PATCH 2/2] fix: 2 Duplicated Signature requests events are triggered for any signature (#21955) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** For some reason, [this merge](https://github.com/MetaMask/metamask-extension/pull/21791) is showing this warning on Github Screenshot 2023-11-23 at 02 53 46 So I cherry picked it to develop and created this new PR. This is basically a copy and paste of #21791 but this time using develop as base. Below is the original Description copied over from 21791 for completion ---------- When you click on a signature request that is flagged by PPOM, 2 events are sent to metrics instead of 1. ### Expected behavior Just 1 Signature request should be triggered for a Signature event flagged by PPOM ## **Related issues** Fixes: #21770 ## **Manual testing steps** 1. Launch MM 2. Open background console and open the network app, make sure it's recording network requests 3. Open testdapp 4. Click on any of the signature requests for PPOM e.g Malicious Trade Order 5. Go to background console network tab and see 2 signature requests called as shown in the video 6. Checkout this branch, build and run MM 7. Repeat steps 2-4 8. On the background console, you should now see only 1 signature request sent. ## **Screenshots/Recordings** ### **Before** [](https://github.com/MetaMask/metamask-extension/assets/54408225/926b1da1-deac-45b3-91d9-f55f5da6cbe6) ### **After** https://github.com/MetaMask/metamask-extension/assets/44811/f7f32a58-dcff-4a76-8bdb-6579d6693b88 ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've clearly explained what problem this PR is solving and how it is solved. - [x] I've linked related issues - [x] I've included manual testing steps - [x] I've included screenshots/recordings if applicable - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. - [x] I’ve properly set the pull request status: - [ ] In case it's not yet "ready for review", I've set it to "draft". - [x] In case it's "ready for review", I've changed it from "draft" to "non-draft". ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- .../signature-request-original.component.js | 25 ------------------- .../signature-request/signature-request.js | 22 ---------------- 2 files changed, 47 deletions(-) diff --git a/ui/components/app/signature-request-original/signature-request-original.component.js b/ui/components/app/signature-request-original/signature-request-original.component.js index 176bd5c3bce5..b53499c03f7b 100644 --- a/ui/components/app/signature-request-original/signature-request-original.component.js +++ b/ui/components/app/signature-request-original/signature-request-original.component.js @@ -44,11 +44,6 @@ import { ///: END:ONLY_INCLUDE_IN } from '../../component-library'; ///: BEGIN:ONLY_INCLUDE_IN(blockaid) -import { - MetaMetricsEventCategory, - MetaMetricsEventName, -} from '../../../../shared/constants/metametrics'; -import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics'; import BlockaidBannerAlert from '../security-provider-banner-alert/blockaid-banner-alert/blockaid-banner-alert'; ///: END:ONLY_INCLUDE_IN import ConfirmPageContainerNavigation from '../confirm-page-container/confirm-page-container-navigation'; @@ -96,26 +91,6 @@ export default class SignatureRequestOriginal extends Component { showSignatureRequestWarning: false, }; - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) - componentDidMount() { - const { txData } = this.props; - if (txData.securityAlertResponse) { - const blockaidMetricsParams = getBlockaidMetricsParams( - txData.securityAlertResponse, - ); - - this.context.trackEvent({ - category: MetaMetricsEventCategory.Transactions, - event: MetaMetricsEventName.SignatureRequested, - properties: { - action: 'Sign Request', - ...blockaidMetricsParams, - }, - }); - } - } - ///: END:ONLY_INCLUDE_IN - msgHexToText = (hex) => { try { const stripped = stripHexPrefix(hex); diff --git a/ui/components/app/signature-request/signature-request.js b/ui/components/app/signature-request/signature-request.js index b3ec8c83eb9f..e5de3f67cca0 100644 --- a/ui/components/app/signature-request/signature-request.js +++ b/ui/components/app/signature-request/signature-request.js @@ -96,7 +96,6 @@ import { showCustodyConfirmLink } from '../../../store/institutional/institution import { useMMICustodySignMessage } from '../../../hooks/useMMICustodySignMessage'; ///: END:ONLY_INCLUDE_IN ///: BEGIN:ONLY_INCLUDE_IN(blockaid) -import { getBlockaidMetricsParams } from '../../../helpers/utils/metrics'; import BlockaidBannerAlert from '../security-provider-banner-alert/blockaid-banner-alert/blockaid-banner-alert'; ///: END:ONLY_INCLUDE_IN @@ -256,27 +255,6 @@ const SignatureRequest = ({ txData }) => { ]); ///: END:ONLY_INCLUDE_IN - ///: BEGIN:ONLY_INCLUDE_IN(blockaid) - useEffect(() => { - if (txData.securityAlertResponse) { - const blockaidMetricsParams = getBlockaidMetricsParams( - txData.securityAlertResponse, - ); - - trackEvent({ - category: MetaMetricsEventCategory.Transactions, - event: MetaMetricsEventName.SignatureRequested, - properties: { - action: 'Sign Request', - type, - version, - ...blockaidMetricsParams, - }, - }); - } - }, []); - ///: END:ONLY_INCLUDE_IN - return (