Repository navigation
fix(connect-multichain): wipe MWP pairing on full disconnect (MCWP-822) - #348
Conversation
Full disconnect() previously only called dappClient.disconnect(), which is a no-op when dappClient's in-memory session is already null (e.g. after a prior failed resume()). That left the pairing (relay channel + ECIES keys) in SessionStore, so a subsequent connect() would resume an already-revoked pairing via getActiveSession(), time out, and leave the dapp stuck even after a page refresh. - Add #clearMwpPairing(): on full disconnect, list+delete every SessionStore entry directly (independent of dappClient.session), then still call dappClient.disconnect() for transport/channel cleanup. Its rejection is intentionally left to propagate so existing disconnect-error behavior is unchanged. - Full disconnect now also clears pending_session_request so a later connect() can't pick up a dead handshake URI. - Defense in depth: if #resumeSession fails, delete that specific pairing from SessionStore before rethrowing (complements PR #180's reject-path cleanup on #startSession). - Add disconnect() unit test coverage: full disconnect wipes the pairing and getActiveSession() resolves undefined afterward; partial disconnect leaves the pairing untouched; a subsequent connect() takes #startSession rather than resume. - Add CHANGELOG entry. Co-authored-by: Cursor <cursoragent@cursor.com>
Real validation of the fix (real
|
| if (session.id) { | ||
| await this.#deleteMwpSession(session.id); | ||
| } |
There was a problem hiding this comment.
I think this will force a fresh QR/deeplink on any resume failure, not just a dead pairing. connect() already wipes SessionStore via dappClient.disconnect() when the in-memory session is still set, including the 10s timeout path. The hole is only resume() nulling that session without deleting storage, so move this delete into the dappClient.resume().catch.
There was a problem hiding this comment.
The wipe now runs only when dappClient.resume() rejects. Timeouts and #onResumeHandler failures no longer touch SessionStore. Addressed in 6861123. Added a test for the hung-resume timeout.
| } catch (error) { | ||
| logger('error clearing MWP pairing from SessionStore', error); | ||
| } |
There was a problem hiding this comment.
I don't think we want to swallow list/delete failures here. If dappClient.session is already null, the disconnect() below is a no-op, so we return success while the pairing is still in SessionStore.
There was a problem hiding this comment.
#clearMwpPairing() now propagates list() and delete() failures, and dappClient.disconnect() runs only after the wipe succeeds. Addressed in 2802639. Added a test for a rejected delete().
| * @param sessionId - The id of the `SessionStore` entry to delete. | ||
| * @returns Nothing | ||
| */ | ||
| async #deleteMwpSession(sessionId: string): Promise<void> { |
There was a problem hiding this comment.
Multiple sessions aren't actually supported on the dapp side. I wonder if it makes sense to just use #clearMwpPairing instead which deletes all sessions just to be safe
There was a problem hiding this comment.
Removed #deleteMwpSession(). A rejected resume() now clears every stored pairing, since getActiveSession() reads list()[0]. Addressed in 4380b15. Added a test with two pairings.
Move the failed-resume cleanup from the outer race catch into the dappClient.resume() rejection handler, so timeouts no longer delete the pairing and trigger a fresh QR. Co-authored-by: Cursor <cursoragent@cursor.com>
Use the delete-all pairing cleanup when dappClient.resume() rejects instead of deleting only the resumed session id, since the dapp side does not support multiple pairings. Co-authored-by: Cursor <cursoragent@cursor.com>
Stop swallowing SessionStore list/delete failures in the MWP pairing wipe so disconnect() rejects instead of reporting success while the pairing remains. Adds tests for propagated delete failures, clearing every pairing on resume rejection, and not clearing on resume timeout. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Tested this by hand in the browser playground, connecting over QR code with Ethereum Mainnet and the extension turned off. On this branch, a QR connect followed by disconnect removes the For the case in the ticket, I paused |
- Notify listeners of the empty session even if full-disconnect cleanup rejects, then rethrow the original error. - Add a test that the SessionStore wipe survives a dappClient.disconnect() rejection, and one for the notification on cleanup failure. - Document why the double dappClient.disconnect() on a rejected resume is harmless. - Shorten the changelog entry and link it to the PR. Co-authored-by: Cursor <cursoragent@cursor.com>
NicolasMassart
left a comment
There was a problem hiding this comment.
Thanks for the quick fixes. All my comments are addressed. The security-scan failure is a SARIF upload server error, so a re-run should clear it.
Explanation
MWPTransport.disconnect()'s full-disconnect branch only calleddappClient.disconnect(), relying on the wallet-protocol client's in-memorysessionto be non-null.dappClient.disconnect()is a no-op when that in-memory session is alreadynull— e.g. after a prior failedresume(), which nulls the session without clearingSessionStore. In that case the pairing (relay channel + ECIES keys) survived inSessionStore.The next
connect()reads that leftover pairing viagetActiveSession()(SessionStore.list()[0]), takes the resume path, and tries to resume a pairing the wallet has already revoked. That resume attempt times out, and the dapp is stuck — surviving even a page refresh, since the CAIP-25 session cache and the MWP pairing are two independent persisted stores. Only wiping storage manually recovered.This reproduced for Fireblocks (
@metamask/connect-multichain@1.2.0) on a successful connect → sign →disconnect()→connect().Fix
MWPTransport.#clearMwpPairing(): on a full disconnect (no remaining scopes), list and delete everySessionStoreentry directly, independent ofdappClient's in-memory session state.dappClient.disconnect()is still called afterward for transport/channel cleanup, and its rejection is intentionally left to propagate — this preserves the existing behavior where a failed disconnect surfaces to callers (seeMWPTransport.disconnect()'s existingshould handle disconnect errorstest).pending_session_request, matching whatconnect()already does in itsfinally, so a laterconnect()can't pick up a dead handshake URI.#resumeSessionfails (e.g. the wallet already revoked the pairing out-of-band), that specific pairing is now deleted fromSessionStorebefore rethrowing. This complements fix(connect-multichain): clean up MWP session on connection rejection #180's existing reject-path cleanup on#startSession.disconnect()inmwp/index.test.ts(previously untested): full disconnect wipes the pairing andgetActiveSession()resolvesundefinedafterward, partial disconnect leaves the pairing untouched, and a subsequentconnect()takes#startSessionrather than resume.Scope is intentionally limited to
@metamask/connect-multichainin this repo — the leftover pairing lives in the dapp-side adapter (localStorage / IndexedDB / AsyncStorage), not in any Mobile wallet-side store, soapp/core/SDKConnectV2inmetamask-mobileis unaffected and out of scope here.References
connectedwith#transportundefined) and MCWP-821 (stale IndexedDB handle) — same underlying report, not fixed by this changeSessionStoreon connect rejection); this PR closes the same gap on the disconnect pathChecklist