Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: better submitRequest validation
  • Loading branch information
mathieuartu committed Dec 4, 2025
commit 54975680dc6c55af78bd882446893fb397c38ced
15 changes: 15 additions & 0 deletions packages/keyring-api/src/eth/rpc/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ export type EthEip7702Authorization = Infer<
typeof EthEip7702AuthorizationStruct
>;

/**
* A struct for getEncryptionPublicKey options.
*/
export const EthGetEncryptionPublicKeyOptionsStruct = optional(
record(string(), unknown()),
);

// ============================================================================
// RPC Method Parameter Structs
// ============================================================================
Expand Down Expand Up @@ -214,3 +221,11 @@ export const EthSignEip7702AuthorizationParamsStruct = tuple([
export type EthSignEip7702AuthorizationParams = Infer<
typeof EthSignEip7702AuthorizationParamsStruct
>;

/**
* Parameters for `eth_getEncryptionPublicKey`.
*/
export const EthGetEncryptionPublicKeyParamsStruct = tuple([
EthAddressStruct, // address
EthGetEncryptionPublicKeyOptionsStruct, // options
]);
4 changes: 2 additions & 2 deletions packages/keyring-api/src/eth/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { object } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import { nonempty, array, enums, literal } from '@metamask/superstruct';
import { definePattern } from '@metamask/utils';
import { definePattern, type Hex } from '@metamask/utils';

import { EthScope } from '.';
import {
Expand All @@ -12,7 +12,7 @@ import {

export const EthBytesStruct = definePattern('EthBytes', /^0x[0-9a-f]*$/iu);

export const EthAddressStruct = definePattern(
export const EthAddressStruct = definePattern<Hex>(
'EthAddress',
/^0x[0-9a-f]{40}$/iu,
);
Expand Down
32 changes: 8 additions & 24 deletions packages/keyring-api/src/eth/v2/eth-keyring-wrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,16 @@ describe('EthKeyringWrapper', () => {
);

const result = await wrapper.submitRequest(
createMockRequest(EthKeyringMethod.GetEncryptionPublicKey, []),
createMockRequest(EthKeyringMethod.GetEncryptionPublicKey, [
MOCK_ADDRESS,
{}, // options
]),
);

expect(mockGetEncryptionPublicKey).toHaveBeenCalledWith(MOCK_ADDRESS);
expect(mockGetEncryptionPublicKey).toHaveBeenCalledWith(
MOCK_ADDRESS,
{},
);
expect(result).toBe('pubkey');
});

Expand All @@ -435,28 +441,6 @@ describe('EthKeyringWrapper', () => {
),
).rejects.toThrow('Keyring does not support getEncryptionPublicKey');
});

it('handles request with undefined params', async () => {
const mockGetEncryptionPublicKey = jest
.fn()
.mockResolvedValue('pubkey');
const wrapper = new TestEthKeyringWrapper(
createMockKeyring({
getEncryptionPublicKey: mockGetEncryptionPublicKey,
}),
);

const request: KeyringRequest = {
id: '00000000-0000-0000-0000-000000000000',
scope: EthScope.Eoa,
account: MOCK_ACCOUNT_ID,
origin: 'https://example.com',
request: { method: EthKeyringMethod.GetEncryptionPublicKey },
};

const result = await wrapper.submitRequest(request);
expect(result).toBe('pubkey');
});
});

describe('eth_getAppKeyAddress', () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/keyring-api/src/eth/v2/eth-keyring-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import {
EthDecryptParamsStruct,
EthGetAppKeyAddressParamsStruct,
EthGetEncryptionPublicKeyParamsStruct,
EthPersonalSignParamsStruct,
EthSignEip7702AuthorizationParamsStruct,
EthSignParamsStruct,
Expand Down Expand Up @@ -79,7 +80,7 @@ export abstract class EthKeyringWrapper<
* @returns The result of the signing operation.
*/
async submitRequest(request: KeyringRequest): Promise<Json> {
const { method, params = [] } = request.request;
const { method, params } = request.request;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing default value for params may cause unexpected errors

The params destructuring lacks a default value. The previous implementation in HdKeyringV2.submitRequest used const { method, params = [] } = request.request; but EthKeyringWrapper uses const { method, params } = request.request;. Since params is defined as exactOptional in KeyringRequestStruct, requests without params are valid and will result in params being undefined. This could cause confusing validation errors when assert is called on undefined instead of an empty array.

Fix in Cursor Fix in Web


const { address, methods } = await this.getAccount(request.account);
const hexAddress = this.toHexAddress(address);
Expand Down Expand Up @@ -182,7 +183,9 @@ export abstract class EthKeyringWrapper<
if (!this.inner.getEncryptionPublicKey) {
throw new Error('Keyring does not support getEncryptionPublicKey');
}
return this.inner.getEncryptionPublicKey(hexAddress);
assert(params, EthGetEncryptionPublicKeyParamsStruct);
const [, options] = params;
return this.inner.getEncryptionPublicKey(hexAddress, options);
}

case `${EthKeyringMethod.GetAppKeyAddress}`: {
Expand All @@ -200,10 +203,7 @@ export abstract class EthKeyringWrapper<
}
assert(params, EthSignEip7702AuthorizationParamsStruct);
const [authorization] = params;
return this.inner.signEip7702Authorization(
hexAddress,
authorization as [number, Hex, number],
);
return this.inner.signEip7702Authorization(hexAddress, authorization);
}

default:
Expand Down
4 changes: 2 additions & 2 deletions packages/keyring-eth-hd/src/hd-keyring-v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ describe('HdKeyringV2', () => {
const request = createMockRequest(
accountId,
'eth_getEncryptionPublicKey',
[],
['0x0000000000000000000000000000000000000000'],
);

const result = await wrapper.submitRequest(request);
Expand All @@ -920,7 +920,7 @@ describe('HdKeyringV2', () => {
const pubKeyRequest = createMockRequest(
accountId,
'eth_getEncryptionPublicKey',
[],
['0x0000000000000000000000000000000000000000', {}],
);

const pubKey = await wrapper.submitRequest(pubKeyRequest);
Expand Down
Loading