From 69aa718d7f60129fabca24d043284ade56f9345d Mon Sep 17 00:00:00 2001 From: Vinicius Stevam Date: Wed, 27 Nov 2024 10:34:44 +0000 Subject: [PATCH] fix: add source when local PPOM fails --- app/lib/ppom/ppom-util.test.ts | 27 +++++++++++++++++++++++++-- app/lib/ppom/ppom-util.ts | 19 ++++++++++++------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/lib/ppom/ppom-util.test.ts b/app/lib/ppom/ppom-util.test.ts index 95241217c88..f7fcea3b703 100644 --- a/app/lib/ppom/ppom-util.test.ts +++ b/app/lib/ppom/ppom-util.test.ts @@ -13,6 +13,7 @@ import { RpcEndpointType, } from '@metamask/network-controller'; import { NETWORKS_CHAIN_ID } from '../../constants/network'; +import { SecurityAlertSource } from '../../components/Views/confirmations/components/BlockaidBanner/BlockaidBanner.types'; const CHAIN_ID_MOCK = '0x1'; @@ -176,8 +177,8 @@ describe('PPOM Utils', () => { MockEngine.context.PreferencesController.state.securityAlertsEnabled = false; await PPOMUtil.validateRequest(mockRequest, CHAIN_ID_MOCK); - expect(MockEngine.context.PPOMController?.usePPOM).toBeCalledTimes(0); - expect(spyTransactionAction).toBeCalledTimes(0); + expect(MockEngine.context.PPOMController?.usePPOM).toHaveBeenCalledTimes(0); + expect(spyTransactionAction).toHaveBeenCalledTimes(0); }); it('should not validate if request is send to users own account ', async () => { @@ -385,5 +386,27 @@ describe('PPOM Utils', () => { await PPOMUtil.validateRequest(mockRequest, CHAIN_ID_MOCK); expect(spy).toHaveBeenCalledTimes(2); }); + + it('sets security alerts response to failed when security alerts API and controller PPOM throws', async () => { + const spy = jest.spyOn( + TransactionActions, + 'setTransactionSecurityAlertResponse', + ); + + const validateMock = () => new Error('Test Error'); + + const ppomMock = { + validateJsonRpc: validateMock, + }; + + MockEngine.context.PPOMController?.usePPOM.mockImplementation( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (callback: any) => callback(ppomMock), + ); + + await PPOMUtil.validateRequest(mockRequest, CHAIN_ID_MOCK); + expect(spy).toHaveBeenCalledTimes(2); + expect(spy).toHaveBeenCalledWith(CHAIN_ID_MOCK, {chainId: CHAIN_ID_MOCK, req: { ...mockRequest } , source: SecurityAlertSource.Local}); + }); }); }); diff --git a/app/lib/ppom/ppom-util.ts b/app/lib/ppom/ppom-util.ts index 2385ac5a6c4..c605b885a08 100644 --- a/app/lib/ppom/ppom-util.ts +++ b/app/lib/ppom/ppom-util.ts @@ -155,14 +155,19 @@ async function validateWithController( ppomController: PPOMController, request: PPOMRequest, ): Promise { - const response = (await ppomController.usePPOM((ppom) => - ppom.validateJsonRpc(request as unknown as Record), - )) as SecurityAlertResponse; + try{ + const response = (await ppomController.usePPOM((ppom) => + ppom.validateJsonRpc(request as unknown as Record), + )) as SecurityAlertResponse; - return { - ...response, - source: SecurityAlertSource.Local, - }; + return { + ...response, + source: SecurityAlertSource.Local, + }; + } catch (e) { + Logger.log(`Error validating request with PPOM: ${e}`); + return {...SECURITY_ALERT_RESPONSE_FAILED, source: SecurityAlertSource.Local,}; + } } async function validateWithAPI(