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: remove supported chains check #29773

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 1 addition & 6 deletions app/scripts/lib/ppom/ppom-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { createPPOMMiddleware, PPOMMiddlewareRequest } from './ppom-middleware';
import {
generateSecurityAlertId,
handlePPOMError,
isChainSupported,
validateRequestWithPPOM,
} from './ppom-util';
import { SecurityAlertResponse } from './types';
Expand Down Expand Up @@ -106,15 +105,13 @@ const createMiddleware = (
describe('PPOMMiddleware', () => {
const generateSecurityAlertIdMock = jest.mocked(generateSecurityAlertId);
const handlePPOMErrorMock = jest.mocked(handlePPOMError);
const isChainSupportedMock = jest.mocked(isChainSupported);
const detectSIWEMock = jest.mocked(detectSIWE);

beforeEach(() => {
jest.resetAllMocks();

generateSecurityAlertIdMock.mockReturnValue(SECURITY_ALERT_ID_MOCK);
handlePPOMErrorMock.mockReturnValue(SECURITY_ALERT_RESPONSE_MOCK);
isChainSupportedMock.mockResolvedValue(true);
detectSIWEMock.mockReturnValue({ isSIWEMessage: false } as SIWEMessage);

globalThis.sentry = {
Expand Down Expand Up @@ -145,9 +142,7 @@ describe('PPOMMiddleware', () => {
() => undefined,
);

expect(req.securityAlertResponse?.reason).toBe(
BlockaidReason.checkingChain,
);
expect(req.securityAlertResponse?.reason).toBe(BlockaidReason.inProgress);
expect(req.securityAlertResponse?.result_type).toBe(
BlockaidResultType.Loading,
);
Expand Down
10 changes: 5 additions & 5 deletions app/scripts/lib/ppom/ppom-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ 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 { SECURITY_ALERT_RESPONSE_CHECKING_CHAIN } from '../../../../shared/constants/security-provider';
import { getProviderConfig } from '../../../../shared/modules/selectors/networks';
import { trace, TraceContext, TraceName } from '../../../../shared/lib/trace';
import { LOADING_SECURITY_ALERT_RESPONSE } from '../../../../shared/constants/security-provider';
import {
generateSecurityAlertId,
handlePPOMError,
Expand Down Expand Up @@ -118,18 +118,18 @@ export function createPPOMMiddleware<
}),
);

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

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

req.securityAlertResponse = securityAlertResponseCheckingChain;
req.securityAlertResponse = securityAlertResponseLoading;
} catch (error) {
req.securityAlertResponse = handlePPOMError(
error,
Expand Down
55 changes: 0 additions & 55 deletions app/scripts/lib/ppom/ppom-util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import {
BlockaidReason,
BlockaidResultType,
LOADING_SECURITY_ALERT_RESPONSE,
SECURITY_ALERT_RESPONSE_CHAIN_NOT_SUPPORTED,
SecurityAlertSource,
} from '../../../../shared/constants/security-provider';
import { AppStateController } from '../../controllers/app-state-controller';
import {
generateSecurityAlertId,
isChainSupported,
METHOD_SIGN_TYPED_DATA_V3,
METHOD_SIGN_TYPED_DATA_V4,
updateSecurityAlertResponse,
Expand Down Expand Up @@ -114,10 +112,6 @@ describe('PPOM Utils', () => {
const normalizeTransactionParamsMock = jest.mocked(
normalizeTransactionParams,
);
const getSupportedChainIdsMock = jest.spyOn(
securityAlertAPI,
'getSecurityAlertsAPISupportedChainIds',
);
let isSecurityAlertsEnabledMock: jest.SpyInstance;

const updateSecurityAlertResponseMock = jest.fn();
Expand Down Expand Up @@ -308,23 +302,6 @@ describe('PPOM Utils', () => {
});
},
);

it('updates response indicating chain is not supported', async () => {
const ppomController = {} as PPOMController;
const CHAIN_ID_UNSUPPORTED_MOCK = '0x2';

await validateRequestWithPPOM({
...validateRequestWithPPOMOptionsBase,
ppomController,
chainId: CHAIN_ID_UNSUPPORTED_MOCK,
});

expect(updateSecurityAlertResponseMock).toHaveBeenCalledWith(
validateRequestWithPPOMOptionsBase.request.method,
SECURITY_ALERT_ID_MOCK,
SECURITY_ALERT_RESPONSE_CHAIN_NOT_SUPPORTED,
);
});
});

describe('generateSecurityAlertId', () => {
Expand Down Expand Up @@ -457,36 +434,4 @@ describe('PPOM Utils', () => {
);
});
});

describe('isChainSupported', () => {
describe('when security alerts API is enabled', () => {
beforeEach(async () => {
isSecurityAlertsEnabledMock.mockReturnValue(true);
getSupportedChainIdsMock.mockResolvedValue([CHAIN_ID_MOCK]);
});

it('returns true if chain is supported', async () => {
expect(await isChainSupported(CHAIN_ID_MOCK)).toStrictEqual(true);
});

it('returns false if chain is not supported', async () => {
expect(await isChainSupported('0x2')).toStrictEqual(false);
});

it('returns correctly if security alerts API throws', async () => {
getSupportedChainIdsMock.mockRejectedValue(new Error('Test Error'));
expect(await isChainSupported(CHAIN_ID_MOCK)).toStrictEqual(true);
});
});

describe('when security alerts API is disabled', () => {
it('returns true if chain is supported', async () => {
expect(await isChainSupported(CHAIN_ID_MOCK)).toStrictEqual(true);
});

it('returns false if chain is not supported', async () => {
expect(await isChainSupported('0x2')).toStrictEqual(false);
});
});
});
});
28 changes: 0 additions & 28 deletions app/scripts/lib/ppom/ppom-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import {
BlockaidReason,
BlockaidResultType,
LOADING_SECURITY_ALERT_RESPONSE,
SECURITY_ALERT_RESPONSE_CHAIN_NOT_SUPPORTED,
SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS_FALLBACK_LIST,
SecurityAlertSource,
} from '../../../../shared/constants/security-provider';
import { SIGNING_METHODS } from '../../../../shared/constants/transaction';
import { AppStateController } from '../../controllers/app-state-controller';
import { SecurityAlertResponse, UpdateSecurityAlertResponse } from './types';
import {
getSecurityAlertsAPISupportedChainIds,
isSecurityAlertsAPIEnabled,
SecurityAlertsAPIRequest,
validateWithSecurityAlertsAPI,
Expand Down Expand Up @@ -56,15 +53,6 @@ export async function validateRequestWithPPOM({
updateSecurityAlertResponse: UpdateSecurityAlertResponse;
}) {
try {
if (!(await isChainSupported(chainId))) {
await updateSecurityResponse(
request.method,
securityAlertId,
SECURITY_ALERT_RESPONSE_CHAIN_NOT_SUPPORTED,
);
return;
}

await updateSecurityResponse(
request.method,
securityAlertId,
Expand Down Expand Up @@ -151,22 +139,6 @@ export function handlePPOMError(
};
}

export async function isChainSupported(chainId: Hex): Promise<boolean> {
let supportedChainIds = SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS_FALLBACK_LIST;

try {
if (isSecurityAlertsAPIEnabled()) {
supportedChainIds = await getSecurityAlertsAPISupportedChainIds();
}
} catch (error: unknown) {
handlePPOMError(
error,
`Error fetching supported chains from security alerts API`,
);
}
return supportedChainIds.includes(chainId as Hex);
}

function normalizePPOMRequest(
request: PPOMRequest | JsonRpcRequest,
): PPOMRequest | JsonRpcRequest {
Expand Down
28 changes: 0 additions & 28 deletions app/scripts/lib/ppom/security-alerts-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
import {
getSecurityAlertsAPISupportedChainIds,
isSecurityAlertsAPIEnabled,
validateWithSecurityAlertsAPI,
} from './security-alerts-api';
Expand Down Expand Up @@ -95,31 +94,4 @@ describe('Security Alerts API', () => {
expect(isEnabled).toBe(false);
});
});

describe('getSecurityAlertsAPISupportedChainIds', () => {
it('sends GET request', async () => {
const SUPPORTED_CHAIN_IDS_MOCK = ['0x1', '0x2'];
fetchMock.mockResolvedValue({
ok: true,
json: async () => SUPPORTED_CHAIN_IDS_MOCK,
});
const response = await getSecurityAlertsAPISupportedChainIds();

expect(response).toEqual(SUPPORTED_CHAIN_IDS_MOCK);

expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(
`${BASE_URL}/supportedChains`,
undefined,
);
});

it('throws an error if response is not ok', async () => {
fetchMock.mockResolvedValue({ ok: false, status: 404 });

await expect(getSecurityAlertsAPISupportedChainIds()).rejects.toThrow(
'Security alerts API request failed with status: 404',
);
});
});
});
7 changes: 1 addition & 6 deletions app/scripts/lib/ppom/security-alerts-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Hex, JsonRpcRequest } from '@metamask/utils';
import { JsonRpcRequest } from '@metamask/utils';
import { SecurityAlertResponse } from './types';

const ENDPOINT_VALIDATE = 'validate';
const ENDPOINT_SUPPORTED_CHAINS = 'supportedChains';

type SecurityAlertsAPIRequestBody = {
method: string;
Expand Down Expand Up @@ -36,10 +35,6 @@ export async function validateWithSecurityAlertsAPI(
});
}

export async function getSecurityAlertsAPISupportedChainIds(): Promise<Hex[]> {
return request(ENDPOINT_SUPPORTED_CHAINS);
}

async function request(endpoint: string, options?: RequestInit) {
const url = getUrl(endpoint);

Expand Down
5 changes: 1 addition & 4 deletions app/scripts/lib/transaction/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { UserOperationController } from '@metamask/user-operation-controller';
import { cloneDeep } from 'lodash';
import {
generateSecurityAlertId,
isChainSupported,
validateRequestWithPPOM,
} from '../ppom/ppom-util';
import {
Expand Down Expand Up @@ -100,7 +99,6 @@ describe('Transaction Utils', () => {
let userOperationController: jest.Mocked<UserOperationController>;
const validateRequestWithPPOMMock = jest.mocked(validateRequestWithPPOM);
const generateSecurityAlertIdMock = jest.mocked(generateSecurityAlertId);
const isChainSupportedMock = jest.mocked(isChainSupported);

beforeEach(() => {
jest.resetAllMocks();
Expand All @@ -125,7 +123,6 @@ describe('Transaction Utils', () => {
});

generateSecurityAlertIdMock.mockReturnValue(SECURITY_ALERT_ID_MOCK);
isChainSupportedMock.mockResolvedValue(true);

request.transactionController = transactionController;
request.userOperationController = userOperationController;
Expand Down Expand Up @@ -399,7 +396,7 @@ describe('Transaction Utils', () => {
).toHaveBeenCalledWith(TRANSACTION_PARAMS_MOCK, {
...TRANSACTION_OPTIONS_MOCK,
securityAlertResponse: {
reason: BlockaidReason.checkingChain,
reason: BlockaidReason.inProgress,
result_type: BlockaidResultType.Loading,
securityAlertId: SECURITY_ALERT_ID_MOCK,
},
Expand Down
8 changes: 4 additions & 4 deletions app/scripts/lib/transaction/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
UpdateSecurityAlertResponse,
} from '../ppom/types';
import {
SECURITY_ALERT_RESPONSE_CHECKING_CHAIN,
LOADING_SECURITY_ALERT_RESPONSE,
SECURITY_PROVIDER_EXCLUDED_TRANSACTION_TYPES,
} from '../../../../shared/constants/security-provider';
import { endTrace, TraceName } from '../../../../shared/lib/trace';
Expand Down Expand Up @@ -287,13 +287,13 @@ async function validateSecurity(request: AddTransactionRequest) {
updateSecurityAlertResponse,
});

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

request.transactionOptions.securityAlertResponse =
securityAlertResponseCheckingChain;
securityAlertResponseLoading;
} catch (error) {
handlePPOMError(error, 'Error validating JSON RPC using PPOM: ');
}
Expand Down
32 changes: 0 additions & 32 deletions shared/constants/security-provider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Hex } from '@metamask/utils';
import {
SecurityAlertResponse,
TransactionType,
} from '@metamask/transaction-controller';
import { CHAIN_IDS } from './network';

export enum SecurityProvider {
Blockaid = 'blockaid',
Expand Down Expand Up @@ -57,8 +55,6 @@ export enum BlockaidReason {
errored = 'Error',
notApplicable = 'NotApplicable',
inProgress = 'validation_in_progress',
checkingChain = 'CheckingChain',
chainNotSupported = 'ChainNotSupported',
}

export enum BlockaidResultType {
Expand Down Expand Up @@ -91,23 +87,6 @@ export const FALSE_POSITIVE_REPORT_BASE_URL =

export const SECURITY_PROVIDER_UTM_SOURCE = 'metamask-ppom';

export const SECURITY_PROVIDER_SUPPORTED_CHAIN_IDS_FALLBACK_LIST: Hex[] = [
CHAIN_IDS.ARBITRUM,
CHAIN_IDS.AVALANCHE,
CHAIN_IDS.BASE,
CHAIN_IDS.BSC,
CHAIN_IDS.LINEA_MAINNET,
CHAIN_IDS.MAINNET,
CHAIN_IDS.OPBNB,
CHAIN_IDS.OPTIMISM,
CHAIN_IDS.POLYGON,
CHAIN_IDS.SEPOLIA,
CHAIN_IDS.ZKSYNC_ERA,
CHAIN_IDS.SCROLL,
CHAIN_IDS.BERACHAIN,
CHAIN_IDS.METACHAIN_ONE,
];

export const SECURITY_PROVIDER_EXCLUDED_TRANSACTION_TYPES = [
TransactionType.swap,
TransactionType.swapApproval,
Expand All @@ -121,17 +100,6 @@ export const LOADING_SECURITY_ALERT_RESPONSE: SecurityAlertResponse = {
reason: BlockaidReason.inProgress,
};

export const SECURITY_ALERT_RESPONSE_CHECKING_CHAIN: SecurityAlertResponse = {
result_type: BlockaidResultType.Loading,
reason: BlockaidReason.checkingChain,
};

export const SECURITY_ALERT_RESPONSE_CHAIN_NOT_SUPPORTED: SecurityAlertResponse =
{
result_type: BlockaidResultType.Benign,
reason: BlockaidReason.chainNotSupported,
};

export enum SecurityAlertSource {
/** Validation performed remotely using the Security Alerts API. */
API = 'api',
Expand Down
2 changes: 1 addition & 1 deletion test/data/confirmations/typed_sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export const seaportSignatureMsg = {
networkClientId: 'mainnet',
securityAlertResponse: {
result_type: 'loading',
reason: 'CheckingChain',
reason: 'validation_in_progress',
securityAlertId: 'def3b0ef-c96b-4c87-b1b1-c69cc02a0f78',
},
status: 'unapproved',
Expand Down
Loading
Loading