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: bring back exception if invalid address passed in eth_accounts #30213

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
69 changes: 69 additions & 0 deletions app/scripts/controllers/permissions/specifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,72 @@ export const unrestrictedMethods = Object.freeze([
'metamaskinstitutional_openAddHardwareWallet',
///: END:ONLY_INCLUDE_IF
]);

/**
* Validates the accounts associated with a caveat. In essence, ensures that
* the accounts value is an array of non-empty strings, and that each string
* corresponds to a PreferencesController identity.
*
* @param {string[]} accounts - The accounts associated with the caveat.
* @param {() => Record<string, import('@metamask/keyring-internal-api').InternalAccount>} getInternalAccounts -
* Gets all AccountsController InternalAccounts.
* TODO: Remove this function once the CAIP-25 permission refactor/factory differ work is merged into main
*/
export function validateCaveatAccounts(accounts, getInternalAccounts) {
if (!Array.isArray(accounts) || accounts.length === 0) {
throw new Error(
`${PermissionNames.eth_accounts} error: Expected non-empty array of Ethereum addresses.`,
);
}

const internalAccounts = getInternalAccounts();
accounts.forEach((address) => {
if (!address || typeof address !== 'string') {
throw new Error(
`${PermissionNames.eth_accounts} error: Expected an array of Ethereum addresses. Received: "${address}".`,
);
}

if (
!internalAccounts.some(
(internalAccount) =>
internalAccount.address.toLowerCase() === address.toLowerCase(),
)
) {
throw new Error(
`${PermissionNames.eth_accounts} error: Received unrecognized address: "${address}".`,
);
}
});
}

/**
* Validates the networks associated with a caveat. Ensures that
* the networks value is an array of valid chain IDs.
*
* @param {string[]} chainIdsForCaveat - The list of chain IDs to validate.
* @param {function(string): string} findNetworkClientIdByChainId - Function to find network client ID by chain ID.
* @throws {Error} If the chainIdsForCaveat is not a non-empty array of valid chain IDs.
* TODO: Remove this function once the CAIP-25 permission refactor/factory differ work is merged into main
*/
export function validateCaveatNetworks(
chainIdsForCaveat,
findNetworkClientIdByChainId,
) {
if (!Array.isArray(chainIdsForCaveat) || chainIdsForCaveat.length === 0) {
throw new Error(
`${PermissionNames.permittedChains} error: Expected non-empty array of chainIds.`,
);
}

chainIdsForCaveat.forEach((chainId) => {
try {
findNetworkClientIdByChainId(chainId);
} catch (e) {
console.error(e);
throw new Error(
`${PermissionNames.permittedChains} error: Received unrecognized chainId: "${chainId}". Please try adding the network first via wallet_addEthereumChain.`,
);
}
});
}
38 changes: 28 additions & 10 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ import {
NOTIFICATION_NAMES,
unrestrictedMethods,
PermissionNames,
validateCaveatAccounts,
validateCaveatNetworks,
} from './controllers/permissions';
import { MetaMetricsDataDeletionController } from './controllers/metametrics-data-deletion/metametrics-data-deletion';
import { DataDeletionService } from './services/data-deletion-service';
Expand Down Expand Up @@ -5270,6 +5272,32 @@ export default class MetamaskController extends EventEmitter {
PermissionNames.permittedChains,
]);

const requestedAccounts =
permissions[RestrictedMethods.eth_accounts]?.caveats?.find(
(caveat) => caveat.type === CaveatTypes.restrictReturnedAccounts,
)?.value ?? [];

const requestedChains =
permissions[PermissionNames.permittedChains]?.caveats?.find(
(caveat) => caveat.type === CaveatTypes.restrictNetworkSwitching,
)?.value ?? [];

if (permissions[RestrictedMethods.eth_accounts]?.caveats) {
validateCaveatAccounts(
requestedAccounts,
this.accountsController.listAccounts.bind(this.accountsController),
);
}

if (permissions[PermissionNames.permittedChains]?.caveats) {
validateCaveatNetworks(
requestedChains,
this.networkController.findNetworkClientIdByChainId.bind(
this.networkController,
),
);
}

if (!permissions[RestrictedMethods.eth_accounts]) {
permissions[RestrictedMethods.eth_accounts] = {};
}
Expand All @@ -5282,16 +5310,6 @@ export default class MetamaskController extends EventEmitter {
delete permissions[PermissionNames.permittedChains];
}

const requestedChains =
permissions[PermissionNames.permittedChains]?.caveats?.find(
(caveat) => caveat.type === CaveatTypes.restrictNetworkSwitching,
)?.value ?? [];

const requestedAccounts =
permissions[PermissionNames.eth_accounts]?.caveats?.find(
(caveat) => caveat.type === CaveatTypes.restrictReturnedAccounts,
)?.value ?? [];

const newCaveatValue = {
requiredScopes: {},
optionalScopes: {
Expand Down
Loading
Loading