Skip to content

Commit

Permalink
Delete interface when rejecting snap confirmations
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwalsh0 committed Dec 10, 2024
1 parent ec62382 commit ac44d44
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
3 changes: 1 addition & 2 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import {
import { getCurrentChainId } from '../../shared/modules/selectors/networks';
import { addNonceToCsp } from '../../shared/modules/add-nonce-to-csp';
import { checkURLForProviderInjection } from '../../shared/modules/provider-injection';
import { rejectAllApprovals } from '../../shared/lib/approval';
import migrations from './migrations';
import Migrator from './lib/migrator';
import ExtensionPlatform from './platforms/extension';
Expand Down Expand Up @@ -1195,7 +1194,7 @@ export function setupController(
REJECT_NOTIFICATION_CLOSE,
);

rejectAllApprovals(controller.approvalController);
controller.rejectAllPendingApprovals();
}

// Updates the snaps registry and check for newly blocked snaps to block if the user has at least one snap installed that isn't preinstalled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { Json } from '@metamask/utils';
import { ApprovalType } from '@metamask/controller-utils';
import { providerErrors } from '@metamask/rpc-errors';
import { DIALOG_APPROVAL_TYPES } from '@metamask/snaps-rpc-methods';
import { SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES } from '../constants/app';
import { rejectAllApprovals } from './approval';
import { SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES } from '../../../../shared/constants/app';
import { rejectAllApprovals } from './utils';

const ID_MOCK = '123';
const ID_MOCK_2 = '456';
const INTERFACE_ID_MOCK = '789';

function createApprovalControllerMock(
pendingApprovals: Partial<ApprovalRequest<Record<string, Json>>>[],
Expand All @@ -32,7 +33,9 @@ describe('Approval Utils', () => {
{ id: ID_MOCK_2, type: ApprovalType.EthSignTypedData },
]);

rejectAllApprovals(approvalController);
rejectAllApprovals({
approvalController,
});

expect(approvalController.reject).toHaveBeenCalledTimes(2);
expect(approvalController.reject).toHaveBeenCalledWith(
Expand All @@ -55,7 +58,7 @@ describe('Approval Utils', () => {
{ id: ID_MOCK, type },
]);

rejectAllApprovals(approvalController);
rejectAllApprovals({ approvalController });

expect(approvalController.accept).toHaveBeenCalledTimes(1);
expect(approvalController.accept).toHaveBeenCalledWith(ID_MOCK, null);
Expand All @@ -67,15 +70,34 @@ describe('Approval Utils', () => {
SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES.confirmAccountCreation,
SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES.confirmAccountRemoval,
SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES.showSnapAccountRedirect,
])('accepts pending approval if type is %s', () => {
])('accepts pending approval if type is %s', (type: string) => {
const approvalController = createApprovalControllerMock([
{ id: ID_MOCK, type: ApprovalType.SnapDialogConfirmation },
{ id: ID_MOCK, type },
]);

rejectAllApprovals(approvalController);
rejectAllApprovals({ approvalController });

expect(approvalController.accept).toHaveBeenCalledTimes(1);
expect(approvalController.accept).toHaveBeenCalledWith(ID_MOCK, false);
});

// @ts-expect-error This function is missing from the Mocha type definitions
it.each([
ApprovalType.SnapDialogAlert,
ApprovalType.SnapDialogPrompt,
DIALOG_APPROVAL_TYPES.default,
ApprovalType.SnapDialogConfirmation,
])('deletes interface if type is %s', (type: string) => {
const approvalController = createApprovalControllerMock([
{ id: ID_MOCK, type, requestData: { id: INTERFACE_ID_MOCK } },
]);

const deleteInterface = jest.fn();

rejectAllApprovals({ approvalController, deleteInterface });

expect(deleteInterface).toHaveBeenCalledTimes(1);
expect(deleteInterface).toHaveBeenCalledWith(INTERFACE_ID_MOCK);
});
});
});
20 changes: 18 additions & 2 deletions shared/lib/approval.ts → app/scripts/lib/approval/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,52 @@ import { ApprovalType } from '@metamask/controller-utils';
import { DIALOG_APPROVAL_TYPES } from '@metamask/snaps-rpc-methods';
import { providerErrors } from '@metamask/rpc-errors';
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
import { SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES } from '../constants/app';
import { SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES } from '../../../../shared/constants/app';
import { createProjectLogger } from '@metamask/utils';
///: END:ONLY_INCLUDE_IF

export function rejectAllApprovals(approvalController: ApprovalController) {
const log = createProjectLogger('approval-utils');

export function rejectAllApprovals({
approvalController,
deleteInterface,
}: {
approvalController: ApprovalController;
deleteInterface?: (id: string) => void;
}) {
const approvalRequestsById = approvalController.state.pendingApprovals;
const approvalRequests = Object.values(approvalRequestsById);

for (const approvalRequest of approvalRequests) {
const { id, type } = approvalRequest;
const interfaceId = approvalRequest.requestData?.id as string;

switch (type) {
case ApprovalType.SnapDialogAlert:
case ApprovalType.SnapDialogPrompt:
case DIALOG_APPROVAL_TYPES.default:
log('Rejecting snap dialog', { id, interfaceId });
approvalController.accept(id, null);
deleteInterface?.(interfaceId);
break;

case ApprovalType.SnapDialogConfirmation:
log('Rejecting snap confirmation', { id, interfaceId });
approvalController.accept(id, false);
deleteInterface?.(interfaceId);
break;

///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
case SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES.confirmAccountCreation:
case SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES.confirmAccountRemoval:
case SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES.showSnapAccountRedirect:
log('Rejecting snap account confirmation', { id });
approvalController.accept(id, false);
break;
///: END:ONLY_INCLUDE_IF

default:
log('Rejecting pending approval', { id });
approvalController.reject(id, providerErrors.userRejectedRequest());
break;
}
Expand Down
17 changes: 15 additions & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ import { isSnapId } from '../../ui/helpers/utils/snaps';
import { BridgeStatusAction } from '../../shared/types/bridge-status';
import { ENVIRONMENT } from '../../development/build/constants';
import fetchWithCache from '../../shared/lib/fetch-with-cache';
import { rejectAllApprovals } from '../../shared/lib/approval';
import { BalancesController as MultichainBalancesController } from './lib/accounts/BalancesController';
import {
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
Expand Down Expand Up @@ -381,6 +380,7 @@ import { PatchStore } from './lib/PatchStore';
import { sanitizeUIState } from './lib/state-utils';
import BridgeStatusController from './controllers/bridge-status/bridge-status-controller';
import { BRIDGE_STATUS_CONTROLLER_NAME } from './controllers/bridge-status/constants';
import { rejectAllApprovals } from './lib/approval/utils';

const { TRIGGER_TYPES } = NotificationServicesController.Constants;
export const METAMASK_CONTROLLER_EVENTS = {
Expand Down Expand Up @@ -4163,8 +4163,8 @@ export default class MetamaskController extends EventEmitter {
trackInsightSnapView: this.trackInsightSnapView.bind(this),

// ApprovalController
rejectAllPendingApprovals: this.rejectAllPendingApprovals.bind(this),
rejectPendingApproval: this.rejectPendingApproval,
rejectAllApprovals: rejectAllApprovals.bind(null, approvalController),
requestUserApproval:
approvalController.addAndShowApprovalRequest.bind(approvalController),
resolvePendingApproval: this.resolvePendingApproval,
Expand Down Expand Up @@ -7060,6 +7060,19 @@ export default class MetamaskController extends EventEmitter {
}
};

rejectAllPendingApprovals() {
const deleteInterface = (id) =>
this.controllerMessenger.call(
'SnapInterfaceController:deleteInterface',
id,
);

rejectAllApprovals({
approvalController: this.approvalController,
deleteInterface,
});
}

async _onAccountChange(newAddress) {
const permittedAccountsMap = getPermittedAccountsByOrigin(
this.permissionController.state,
Expand Down
1 change: 1 addition & 0 deletions ui/pages/confirmations/components/confirm/nav/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const Nav = ({ confirmationId }: NavProps) => {
width={BlockSize.Full}
style={{
zIndex: 2,
position: 'relative',
}}
>
<Box alignItems={AlignItems.center} display={Display.Flex}>
Expand Down
2 changes: 1 addition & 1 deletion ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5306,7 +5306,7 @@ export function requestUserApproval({

export function rejectAllApprovals() {
return async (dispatch: MetaMaskReduxDispatch) => {
await submitRequestToBackground('rejectAllApprovals');
await submitRequestToBackground('rejectAllPendingApprovals');

const { pendingApprovals } = await forceUpdateMetamaskState(dispatch);

Expand Down

0 comments on commit ac44d44

Please sign in to comment.