Skip to content

Commit 839c3e0

Browse files
adonesky1jiexi
andauthored
fix: isInternalAccountInPermittedAccountIds case where only wallet:eip155 permission exists for a given address (MetaMask#5980)
## Explanation We aren't handling the case where a permitted account with the `wallet:eip155` scope prefix is passed into `isInternalAccountInPermittedAccountIds` - where it is the only representation of its address in the permission set. In this case we wouldn't find a permitted account matching a passed in internal account and would [ultimately throw an error](https://github.com/MetaMask/metamask-mobile/blob/97486f7d2aff5d075865e93126727590c88c0c4d/app/core/Permissions/index.ts#L145) resulting in the app crashing. ## References * Fixes error [reported here](https://consensys.slack.com/archives/C08UFPWB3GB/p1749821334454809) ## Changelog ### `@metamask/chain-agnostic-permission` - Fix `isInternalAccountInPermittedAccountIds` and `isCaipAccountIdInPermittedAccountIds` to correctly handle comparison against `permittedAccounts` values of the `wallet:<namespace>:<address>` format ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs), highlighting breaking changes as necessary - [ ] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes --------- Co-authored-by: Jiexi Luan <jiexiluan@gmail.com>
1 parent 4a936da commit 839c3e0

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

packages/chain-agnostic-permission/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Bump `@metamask/network-controller` to `^23.6.0` ([#5935](https://github.com/MetaMask/core/pull/5935),[#5882](https://github.com/MetaMask/core/pull/5882))
1414
- Change `caip25CaveatBuilder` to list unsupported scopes in the unsupported scopes error ([#5806](https://github.com/MetaMask/core/pull/5806))
1515

16+
### Fixed
17+
18+
- Fix `isInternalAccountInPermittedAccountIds` and `isCaipAccountIdInPermittedAccountIds` to correctly handle comparison against `permittedAccounts` values of the `wallet:<namespace>:<address>` format ([#5980](https://github.com/MetaMask/core/pull/5980))
19+
1620
## [0.7.0]
1721

1822
### Changed

packages/chain-agnostic-permission/src/adapters/caip-permission-adapter-accounts.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,42 @@ describe('CAIP-25 eth_accounts adapters', () => {
637637
);
638638
expect(result).toBe(false);
639639
});
640+
641+
it('returns true if a wallet:eip155 namespaced address is permitted and a matching (case insensitive) internal account with eip155:0 scope exists', () => {
642+
const result = isInternalAccountInPermittedAccountIds(
643+
// @ts-expect-error partial internal account
644+
{
645+
scopes: ['eip155:0'],
646+
address: '0xDeAdBeEf',
647+
},
648+
['wallet:eip155:0xdeadbeef'],
649+
);
650+
expect(result).toBe(true);
651+
});
652+
653+
it('returns true if a wallet:<non-evm-namespace> namespaced account is permitted and a matching (case sensitive) internal account with solana namespaced scope exists', () => {
654+
const result = isInternalAccountInPermittedAccountIds(
655+
// @ts-expect-error partial internal account
656+
{
657+
scopes: ['solana:0'],
658+
address: 'abC123',
659+
},
660+
['wallet:solana:abC123'],
661+
);
662+
expect(result).toBe(true);
663+
});
664+
665+
it('returns false if a wallet:<non-evm-namespace> namespaced account is permitted and a matching (case sensitive) internal account with same address but different namespace', () => {
666+
const result = isInternalAccountInPermittedAccountIds(
667+
// @ts-expect-error partial internal account
668+
{
669+
scopes: ['solana:0'],
670+
address: 'abC123',
671+
},
672+
['wallet:notsolana:abC123'],
673+
);
674+
expect(result).toBe(false);
675+
});
640676
});
641677

642678
describe('isCaipAccountIdInPermittedAccountIds', () => {

packages/chain-agnostic-permission/src/adapters/caip-permission-adapter-accounts.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,41 @@ function isAddressWithParsedScopesInPermittedAccountIds(
319319
const parsedPermittedAccount = parseCaipAccountId(account);
320320

321321
return parsedAccountScopes.some(({ namespace, reference }) => {
322-
if (namespace !== parsedPermittedAccount.chain.namespace) {
322+
if (
323+
namespace !== parsedPermittedAccount.chain.namespace &&
324+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
325+
parsedPermittedAccount.chain.namespace !== KnownCaipNamespace.Wallet
326+
) {
327+
return false;
328+
}
329+
330+
// handle wallet:<namespace>:<address> case where namespaces are mismatched but addresses match
331+
// i.e. wallet:notSolana:12389812309123 and solana:0:12389812309123
332+
if (
333+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
334+
parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet &&
335+
namespace !== parsedPermittedAccount.chain.reference
336+
) {
323337
return false;
324338
}
325339

326340
// handle eip155:0 case and insensitive evm address comparison
327-
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
328341
if (namespace === KnownCaipNamespace.Eip155) {
329342
return (
330343
(reference === '0' ||
331344
reference === parsedPermittedAccount.chain.reference) &&
332345
isEqualCaseInsensitive(address, parsedPermittedAccount.address)
333346
);
334347
}
348+
349+
// handle wallet:<namespace>:<address> case
350+
if (
351+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
352+
parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet
353+
) {
354+
return address === parsedPermittedAccount.address;
355+
}
356+
335357
return (
336358
reference === parsedPermittedAccount.chain.reference &&
337359
address === parsedPermittedAccount.address

0 commit comments

Comments
 (0)