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

fix (cherry-pick): get supportedChains to avoid blocking the confirmation process #28422

Merged
Show file tree
Hide file tree
Changes from all 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
78 changes: 4 additions & 74 deletions app/scripts/lib/ppom/ppom-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
BlockaidReason,
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
import { flushPromises } from '../../../../test/lib/timer-helpers';
import { mockNetworkState } from '../../../../test/stub/networks';
import { createPPOMMiddleware, PPOMMiddlewareRequest } from './ppom-middleware';
import {
Expand Down Expand Up @@ -105,7 +104,6 @@ const createMiddleware = (
};

describe('PPOMMiddleware', () => {
const validateRequestWithPPOMMock = jest.mocked(validateRequestWithPPOM);
const generateSecurityAlertIdMock = jest.mocked(generateSecurityAlertId);
const handlePPOMErrorMock = jest.mocked(handlePPOMError);
const isChainSupportedMock = jest.mocked(isChainSupported);
Expand All @@ -114,7 +112,6 @@ describe('PPOMMiddleware', () => {
beforeEach(() => {
jest.resetAllMocks();

validateRequestWithPPOMMock.mockResolvedValue(SECURITY_ALERT_RESPONSE_MOCK);
generateSecurityAlertIdMock.mockReturnValue(SECURITY_ALERT_ID_MOCK);
handlePPOMErrorMock.mockReturnValue(SECURITY_ALERT_RESPONSE_MOCK);
isChainSupportedMock.mockResolvedValue(true);
Expand All @@ -129,38 +126,13 @@ describe('PPOMMiddleware', () => {
};
});

it('updates alert response after validating request', async () => {
it('adds checking chain response to confirmation requests while validation is in progress', async () => {
const updateSecurityAlertResponse = jest.fn();

const middlewareFunction = createMiddleware({
updateSecurityAlertResponse,
});

const req = {
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

await flushPromises();

expect(updateSecurityAlertResponse).toHaveBeenCalledTimes(1);
expect(updateSecurityAlertResponse).toHaveBeenCalledWith(
req.method,
SECURITY_ALERT_ID_MOCK,
SECURITY_ALERT_RESPONSE_MOCK,
);
});

it('adds loading response to confirmation requests while validation is in progress', async () => {
const middlewareFunction = createMiddleware();

const req: PPOMMiddlewareRequest<(string | { to: string })[]> = {
...REQUEST_MOCK,
method: 'eth_sendTransaction',
Expand All @@ -173,7 +145,9 @@ describe('PPOMMiddleware', () => {
() => undefined,
);

expect(req.securityAlertResponse?.reason).toBe(BlockaidReason.inProgress);
expect(req.securityAlertResponse?.reason).toBe(
BlockaidReason.checkingChain,
);
expect(req.securityAlertResponse?.result_type).toBe(
BlockaidResultType.Loading,
);
Expand All @@ -197,50 +171,6 @@ describe('PPOMMiddleware', () => {
expect(validateRequestWithPPOM).not.toHaveBeenCalled();
});

it('does not do validation if unable to get the chainId from the network provider config', async () => {
isChainSupportedMock.mockResolvedValue(false);
const middlewareFunction = createMiddleware({
chainId: null,
});

const req = {
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

expect(req.securityAlertResponse).toBeUndefined();
expect(validateRequestWithPPOM).not.toHaveBeenCalled();
});

it('does not do validation if user is not on a supported network', async () => {
isChainSupportedMock.mockResolvedValue(false);
const middlewareFunction = createMiddleware({
chainId: '0x2',
});

const req = {
...REQUEST_MOCK,
method: 'eth_sendTransaction',
securityAlertResponse: undefined,
};

await middlewareFunction(
req,
{ ...JsonRpcResponseStruct.TYPE },
() => undefined,
);

expect(req.securityAlertResponse).toBeUndefined();
expect(validateRequestWithPPOM).not.toHaveBeenCalled();
});

it('does not do validation when request is not for confirmation method', async () => {
const middlewareFunction = createMiddleware();

Expand Down
30 changes: 9 additions & 21 deletions app/scripts/lib/ppom/ppom-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ import { MESSAGE_TYPE } from '../../../../shared/constants/app';
import { SIGNING_METHODS } from '../../../../shared/constants/transaction';
import { PreferencesController } from '../../controllers/preferences-controller';
import { AppStateController } from '../../controllers/app-state-controller';
import { LOADING_SECURITY_ALERT_RESPONSE } from '../../../../shared/constants/security-provider';
import { SECURITY_ALERT_RESPONSE_CHECKING_CHAIN } from '../../../../shared/constants/security-provider';
// eslint-disable-next-line import/no-restricted-paths
import { getProviderConfig } from '../../../../ui/ducks/metamask/metamask';
import { trace, TraceContext, TraceName } from '../../../../shared/lib/trace';
import {
generateSecurityAlertId,
handlePPOMError,
isChainSupported,
validateRequestWithPPOM,
} from './ppom-util';
import { SecurityAlertResponse } from './types';
import { SecurityAlertResponse, UpdateSecurityAlertResponse } from './types';

const CONFIRMATION_METHODS = Object.freeze([
'eth_sendRawTransaction',
Expand Down Expand Up @@ -64,11 +63,7 @@ export function createPPOMMiddleware<
networkController: NetworkController,
appStateController: AppStateController,
accountsController: AccountsController,
updateSecurityAlertResponse: (
method: string,
signatureAlertId: string,
securityAlertResponse: SecurityAlertResponse,
) => void,
updateSecurityAlertResponse: UpdateSecurityAlertResponse,
) {
return async (
req: PPOMMiddlewareRequest<Params>,
Expand All @@ -88,9 +83,7 @@ export function createPPOMMiddleware<

if (
!securityAlertsEnabled ||
!CONFIRMATION_METHODS.includes(req.method) ||
// Do not move this call above this check because it will result in unnecessary calls
!(await isChainSupported(chainId))
!CONFIRMATION_METHODS.includes(req.method)
) {
return;
}
Expand Down Expand Up @@ -122,27 +115,22 @@ export function createPPOMMiddleware<
request: req,
securityAlertId,
chainId,
}).then((securityAlertResponse) => {
updateSecurityAlertResponse(
req.method,
securityAlertId,
securityAlertResponse,
);
updateSecurityAlertResponse,
}),
);

const loadingSecurityAlertResponse: SecurityAlertResponse = {
...LOADING_SECURITY_ALERT_RESPONSE,
const securityAlertResponseCheckingChain: SecurityAlertResponse = {
...SECURITY_ALERT_RESPONSE_CHECKING_CHAIN,
securityAlertId,
};

if (SIGNING_METHODS.includes(req.method)) {
appStateController.addSignatureSecurityAlertResponse(
loadingSecurityAlertResponse,
securityAlertResponseCheckingChain,
);
}

req.securityAlertResponse = loadingSecurityAlertResponse;
req.securityAlertResponse = securityAlertResponseCheckingChain;
} catch (error) {
req.securityAlertResponse = handlePPOMError(
error,
Expand Down
Loading