-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathppom-middleware.ts
More file actions
83 lines (78 loc) · 2.73 KB
/
ppom-middleware.ts
File metadata and controls
83 lines (78 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { PPOM } from '@blockaid/ppom_release';
import { PPOMController } from '@metamask/ppom-validator';
import { NetworkController } from '@metamask/network-controller';
import {
BlockaidReason,
BlockaidResultType,
} from '../../../../shared/constants/security-provider';
import { CHAIN_IDS } from '../../../../shared/constants/network';
import PreferencesController from '../../controllers/preferences';
const { sentry } = global as any;
const ConfirmationMethods = Object.freeze([
'eth_sendRawTransaction',
'eth_sendTransaction',
'eth_sign',
'eth_signTypedData',
'eth_signTypedData_v1',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
'personal_sign',
]);
export const SUPPORTED_CHAIN_IDS: string[] = [
CHAIN_IDS.MAINNET,
CHAIN_IDS.BSC,
CHAIN_IDS.POLYGON,
CHAIN_IDS.ARBITRUM,
CHAIN_IDS.OPTIMISM,
CHAIN_IDS.AVALANCHE,
CHAIN_IDS.LINEA_MAINNET,
];
/**
* Middleware function that handles JSON RPC requests.
* This function will be called for every JSON RPC request.
* It will call the PPOM to check if the request is malicious or benign.
* If the request is benign, it will be forwarded to the next middleware.
* If the request is malicious or warning, it will trigger the PPOM alert dialog,
* after the user has confirmed or rejected the request,
* the request will be forwarded to the next middleware, together with the PPOM response.
*
* @param ppomController - Instance of PPOMController.
* @param preferencesController - Instance of PreferenceController.
* @param networkController - Instance of NetworkController.
* @returns PPOMMiddleware function.
*/
export function createPPOMMiddleware(
ppomController: PPOMController,
preferencesController: PreferencesController,
networkController: NetworkController,
) {
return async (req: any, _res: any, next: () => void) => {
try {
const securityAlertsEnabled =
preferencesController.store.getState()?.securityAlertsEnabled;
const { chainId } = networkController.state.providerConfig;
if (
securityAlertsEnabled &&
ConfirmationMethods.includes(req.method) &&
SUPPORTED_CHAIN_IDS.includes(chainId)
) {
// eslint-disable-next-line require-atomic-updates
req.securityAlertResponse = await ppomController.usePPOM(
async (ppom: PPOM) => {
return ppom.validateJsonRpc(req);
},
);
}
} catch (error: any) {
sentry?.captureException(error);
console.error('Error validating JSON RPC using PPOM: ', error);
req.securityAlertResponse = {
result_type: BlockaidResultType.Failed,
reason: BlockaidReason.failed,
description: 'Validating the confirmation failed by throwing error.',
};
} finally {
next();
}
};
}