-
Notifications
You must be signed in to change notification settings - Fork 14
CP-14604: reject WalletConnect signing requests from accounts not granted to the session #3994
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
Open
bogdandobritoiu
wants to merge
4
commits into
main
Choose a base branch
from
CP-14604
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5f39dbf
restoring WalletConnect session-account authorization so dApps can on…
bogdandobritoiu 0efb698
CP-14604: fail closed when the WC session is missing or the client th…
bogdandobritoiu 056b381
CP-14604: make isAddressApproved namespace-aware (exact match for non…
bogdandobritoiu 732958b
CP-14604: enforce WalletConnect account grant for avalanche_signMessa…
B0Y3R-AVA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 101 additions & 1 deletion
102
packages/core-mobile/app/store/rpc/utils/isAccountApproved/isAccountApproved.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...store/rpc/utils/isAvalancheSignMessageAuthorized/isAvalancheSignMessageAuthorized.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { AvalancheCaip2ChainId } from '@avalabs/core-chains-sdk' | ||
| import { RpcMethod } from '@avalabs/vm-module-types' | ||
| import { SessionTypes } from '@walletconnect/types' | ||
| import { CoreAccountAddresses } from 'store/rpc/handlers/wc_sessionRequest/utils' | ||
| import { isAvalancheSignMessageAuthorized } from './isAvalancheSignMessageAuthorized' | ||
|
|
||
| const X = AvalancheCaip2ChainId.X | ||
|
|
||
| const GRANTED_AVM = 'X-fuji1granted00000000000000000000000000000' | ||
| const UNGRANTED_AVM = 'X-fuji1ungranted000000000000000000000000000' | ||
|
|
||
| const account = (addressAVM: string): CoreAccountAddresses => ({ | ||
| addressC: '0xc0000000000000000000000000000000000000c0', | ||
| addressBTC: 'bc1qbtc', | ||
| addressCoreEth: '0xcoreeth', | ||
| addressAVM, | ||
| addressPVM: 'P-fuji1pvm', | ||
| addressSVM: 'svmAddr' | ||
| }) | ||
|
|
||
| const sessionGranting = (addresses: string[]): SessionTypes.Struct => | ||
| ({ | ||
| namespaces: { | ||
| avax: { | ||
| accounts: addresses.map(a => `${X}:${a}`), | ||
| methods: ['avalanche_signMessage'], | ||
| events: [] | ||
| } | ||
| } | ||
| } as unknown as SessionTypes.Struct) | ||
|
|
||
| // Explicit index 1 → an ungranted account; anything else → a granted account. | ||
| const getAccountByIndex = (index: number): CoreAccountAddresses | undefined => | ||
| index === 1 ? account(UNGRANTED_AVM) : account(GRANTED_AVM) | ||
|
|
||
| const base = { | ||
| method: RpcMethod.AVALANCHE_SIGN_MESSAGE as string, | ||
| isInAppRequest: false, | ||
| params: ['hello', 1] as unknown, | ||
| caip2ChainId: X as string, | ||
| activeAccount: account(GRANTED_AVM), | ||
| getAccountByIndex, | ||
| getSession: (): SessionTypes.Struct | undefined => | ||
| sessionGranting([GRANTED_AVM]) | ||
| } | ||
|
|
||
| describe('isAvalancheSignMessageAuthorized', () => { | ||
| it('is a no-op (authorized) for any method other than avalanche_signMessage', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| method: RpcMethod.ETH_SIGN, | ||
| // would fail closed if the guard applied, proving it is skipped | ||
| getSession: () => { | ||
| throw new Error('should not be consulted') | ||
| } | ||
| }) | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('is a no-op (authorized) for in-app / injected-browser requests', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| isInAppRequest: true, | ||
| getSession: () => { | ||
| throw new Error('should not be consulted') | ||
| } | ||
| }) | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects when the dApp-supplied account index resolves to an ungranted account', () => { | ||
| // params [message, 1] → getAccountByIndex(1) → UNGRANTED_AVM | ||
| expect(isAvalancheSignMessageAuthorized(base)).toBe(false) | ||
| }) | ||
|
|
||
| it('allows when the dApp-supplied account index resolves to a granted account', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ ...base, params: ['hello', 0] }) | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('falls back to the active account when no index is supplied (active granted → allow)', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ ...base, params: ['hello'] }) | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('falls back to the active account when no index is supplied (active ungranted → reject)', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| params: ['hello'], | ||
| activeAccount: account(UNGRANTED_AVM) | ||
| }) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('fails closed when no WC session exists for the topic', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| params: ['hello', 0], | ||
| getSession: () => undefined | ||
| }) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('fails closed when getSession throws (WC client not initialized)', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| params: ['hello', 0], | ||
| getSession: () => { | ||
| throw new Error('WalletConnect client is not initialized') | ||
| } | ||
| }) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects a fractional index whose accounts[0] fallback is ungranted (schema allows non-integers)', () => { | ||
| // 0.5 passes the module schema (nonnegative, not .int()); selectAccountByIndex | ||
| // finds no match and signs with accounts[0]. The guard must check that same | ||
| // resolved account, not the (granted) active account — else the bypass reopens. | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| params: ['hello', 0.5], | ||
| getAccountByIndex: () => account(UNGRANTED_AVM) | ||
| }) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('allows a fractional index only when its resolved account is granted', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| params: ['hello', 0.5], | ||
| getAccountByIndex: () => account(GRANTED_AVM) | ||
| }) | ||
| ).toBe(true) | ||
| }) | ||
|
|
||
| it('fails closed when the account index resolves to no account', () => { | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| params: ['hello', 5], | ||
| getAccountByIndex: () => undefined | ||
| }) | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('treats an invalid (negative) index as absent and checks the active account', () => { | ||
| // module schema rejects negatives; our resolution mirrors the "no index" | ||
| // path (active account) rather than trusting a bogus dApp value | ||
| expect( | ||
| isAvalancheSignMessageAuthorized({ | ||
| ...base, | ||
| params: ['hello', -1], | ||
| activeAccount: account(GRANTED_AVM) | ||
| }) | ||
| ).toBe(true) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.