Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add origin throttling modal #29656

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 160 additions & 1 deletion app/scripts/controllers/app-state-controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ControllerMessenger } from '@metamask/base-controller';
import { errorCodes } from '@metamask/rpc-errors';
import type {
AcceptRequest,
AddApprovalRequest,
ApprovalAcceptedEvent,
ApprovalRejectedEvent,
ApprovalRequest,
} from '@metamask/approval-controller';
import { KeyringControllerQRKeyringStateChangeEvent } from '@metamask/keyring-controller';
import { Browser } from 'webextension-polyfill';
import { waitFor } from '@testing-library/react';
import {
ENVIRONMENT_TYPE_POPUP,
ORIGIN_METAMASK,
Expand All @@ -17,6 +22,7 @@ import type {
AppStateControllerActions,
AppStateControllerEvents,
AppStateControllerOptions,
AppStateControllerState,
} from './app-state-controller';
import type {
PreferencesControllerState,
Expand Down Expand Up @@ -553,11 +559,157 @@ describe('AppStateController', () => {
});
});
});

describe('throttledOrigins', () => {
describe('resetOriginThrottlingState', () => {
it('should reset the throttling state for a given origin', async () => {
await withController(
{
state: {
throttledOrigins: {
'example.com': { rejections: 3, lastRejection: Date.now() },
},
},
},
({ controller }) => {
controller.resetOriginThrottlingState('example.com');
expect(
controller.state.throttledOrigins['example.com'],
).toBeUndefined();
},
);
});
});

describe('isOriginBlockedForConfirmations', () => {
it('should return false if the origin is not throttled', async () => {
await withController(({ controller }) => {
expect(
controller.isOriginBlockedForConfirmations('example.com'),
).toBe(false);
});
});

it('should return true if the origin is throttled and within the blocking threshold', async () => {
await withController(
{
state: {
throttledOrigins: {
'example.com': {
rejections: 5,
lastRejection: Date.now(),
},
},
},
},
({ controller }) => {
expect(
controller.isOriginBlockedForConfirmations('example.com'),
).toBe(true);
},
);
});

it('should return false if the origin is throttled but outside the blocking threshold', async () => {
await withController(
{
state: {
throttledOrigins: {
'example.com': {
rejections: 5,
lastRejection: Date.now() - 600000, // 10 minutes ago
},
},
},
},
({ controller }) => {
expect(
controller.isOriginBlockedForConfirmations('example.com'),
).toBe(false);
},
);
});
});

describe('ApprovalController:rejected event', () => {
it('should increase rejection count for user rejected errors', async () => {
await withController(
async ({ controller, controllerMessenger: messenger }) => {
const origin = 'example.com';

messenger.publish('ApprovalController:rejected', {
approval: {
origin,
type: 'transaction',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as unknown as ApprovalRequest<any>,
error: {
code: errorCodes.provider.userRejectedRequest,
} as unknown as Error,
});

await waitFor(() => {
expect(controller.state.throttledOrigins[origin].rejections).toBe(
1,
);
});
},
);
});

it('should not increase rejection count for non-user rejected errors', async () => {
await withController(
async ({ controller, controllerMessenger: messenger }) => {
const origin = 'example.com';

messenger.publish('ApprovalController:rejected', {
approval: {
origin,
type: 'transaction',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as unknown as ApprovalRequest<any>,
error: { code: errorCodes.rpc.internal } as unknown as Error,
});

expect(controller.state.throttledOrigins[origin]).toBeUndefined();
},
);
});
});

describe('ApprovalController:accepted event', () => {
it('should reset throttling state on approval acceptance', async () => {
await withController(
{
state: {
throttledOrigins: {
'example.com': { rejections: 3, lastRejection: Date.now() },
},
},
},
({ controller, controllerMessenger: messenger }) => {
messenger.publish('ApprovalController:accepted', {
approval: {
origin: 'example.com',
type: 'transaction',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as unknown as ApprovalRequest<any>,
});

expect(
controller.state.throttledOrigins['example.com'],
).toBeUndefined();
},
);
});
});
});
});

type WithControllerOptions = {
options?: Partial<AppStateControllerOptions>;
addRequestMock?: jest.Mock;
state?: Partial<AppStateControllerState>;
};

type WithControllerCallback<ReturnValue> = ({
Expand All @@ -573,6 +725,8 @@ type WithControllerCallback<ReturnValue> = ({
| AppStateControllerEvents
| PreferencesControllerStateChangeEvent
| KeyringControllerQRKeyringStateChangeEvent
| ApprovalAcceptedEvent
| ApprovalRejectedEvent
>;
}) => ReturnValue;

Expand All @@ -584,7 +738,7 @@ async function withController<ReturnValue>(
...args: WithControllerArgs<ReturnValue>
): Promise<ReturnValue> {
const [{ ...rest }, fn] = args.length === 2 ? args : [{}, args[0]];
const { addRequestMock, options = {} } = rest;
const { addRequestMock, state, options = {} } = rest;

const controllerMessenger = new ControllerMessenger<
| AppStateControllerActions
Expand All @@ -594,6 +748,8 @@ async function withController<ReturnValue>(
| AppStateControllerEvents
| PreferencesControllerStateChangeEvent
| KeyringControllerQRKeyringStateChangeEvent
| ApprovalAcceptedEvent
| ApprovalRejectedEvent
>();
const appStateMessenger = controllerMessenger.getRestricted({
name: 'AppStateController',
Expand All @@ -605,6 +761,8 @@ async function withController<ReturnValue>(
allowedEvents: [
`PreferencesController:stateChange`,
`KeyringController:qrKeyringStateChange`,
`ApprovalController:accepted`,
`ApprovalController:rejected`,
],
});
controllerMessenger.registerActionHandler(
Expand All @@ -627,6 +785,7 @@ async function withController<ReturnValue>(
onInactiveTimeout: jest.fn(),
messenger: appStateMessenger,
extension: extensionMock,
state,
...options,
}),
controllerMessenger,
Expand Down
Loading
Loading