@@ -13,6 +13,7 @@ import {
13
13
} from '@metamask/utils' ;
14
14
15
15
import type { Block } from './types' ;
16
+ import { stripArrayTypeIfPresent } from './utils/common' ;
16
17
import { normalizeTypedMessage , parseTypedMessage } from './utils/normalize' ;
17
18
18
19
/*
@@ -243,6 +244,7 @@ WalletMiddlewareOptions): JsonRpcMiddleware<any, Block> {
243
244
244
245
const address = await validateAndNormalizeKeyholder ( params [ 0 ] , req ) ;
245
246
const message = normalizeTypedMessage ( params [ 1 ] ) ;
247
+ validatePrimaryType ( message ) ;
246
248
validateVerifyingContract ( message ) ;
247
249
const version = 'V3' ;
248
250
const msgParams : TypedMessageParams = {
@@ -274,6 +276,7 @@ WalletMiddlewareOptions): JsonRpcMiddleware<any, Block> {
274
276
275
277
const address = await validateAndNormalizeKeyholder ( params [ 0 ] , req ) ;
276
278
const message = normalizeTypedMessage ( params [ 1 ] ) ;
279
+ validatePrimaryType ( message ) ;
277
280
validateVerifyingContract ( message ) ;
278
281
const version = 'V4' ;
279
282
const msgParams : TypedMessageParams = {
@@ -457,6 +460,27 @@ WalletMiddlewareOptions): JsonRpcMiddleware<any, Block> {
457
460
}
458
461
}
459
462
463
+ /**
464
+ * Validates primary of typedSignMessage, to ensure that it's type definition is present in message.
465
+ *
466
+ * @param data - The data passed in typedSign request.
467
+ */
468
+ function validatePrimaryType ( data : string ) {
469
+ const { primaryType, types } = parseTypedMessage ( data ) ;
470
+ if ( ! types ) {
471
+ throw rpcErrors . invalidInput ( ) ;
472
+ }
473
+
474
+ // Primary type can be an array.
475
+ const baseType = stripArrayTypeIfPresent ( primaryType ) ;
476
+
477
+ // Return if the base type is not defined in the types
478
+ const baseTypeDefinitions = types [ baseType ] ;
479
+ if ( ! baseTypeDefinitions ) {
480
+ throw rpcErrors . invalidInput ( ) ;
481
+ }
482
+ }
483
+
460
484
/**
461
485
* Validates verifyingContract of typedSignMessage.
462
486
*
0 commit comments