|
1 | 1 | /** biome-ignore-all lint/suspicious/noExplicitAny: test code */ |
2 | 2 | /** biome-ignore-all lint/suspicious/noShadowRestrictedNames: test code */ |
3 | | -import { type ConnectionMode, type IKeyManager, type IKVStore, type KeyPair, type SessionRequest, SessionStore, WebSocketTransport } from "@metamask/mobile-wallet-protocol-core"; |
| 3 | +import { type ConnectionMode, ErrorCode, type IKeyManager, type IKVStore, type KeyPair, type SessionRequest, SessionStore, WebSocketTransport } from "@metamask/mobile-wallet-protocol-core"; |
4 | 4 | import { DappClient, type OtpRequiredPayload } from "@metamask/mobile-wallet-protocol-dapp-client"; |
5 | 5 | import { WalletClient } from "@metamask/mobile-wallet-protocol-wallet-client"; |
6 | 6 | import { decrypt, encrypt, PrivateKey, PublicKey } from "eciesjs"; |
@@ -190,6 +190,34 @@ t.describe("E2E Integration Test", () => { |
190 | 190 | await t.expect(messageFromWalletPromise).resolves.toEqual(responsePayload); |
191 | 191 | }); |
192 | 192 |
|
| 193 | + t.test("should reject inbound messages on an expired session", async () => { |
| 194 | + await connectClients(dappClient, walletClient, "trusted"); |
| 195 | + |
| 196 | + // Verify a message works pre-expiry |
| 197 | + const preExpiryPayload = { method: "pre_expiry_check" }; |
| 198 | + const preExpiryPromise = new Promise((resolve) => walletClient.on("message", resolve)); |
| 199 | + await dappClient.sendRequest(preExpiryPayload); |
| 200 | + await t.expect(preExpiryPromise).resolves.toEqual(preExpiryPayload); |
| 201 | + |
| 202 | + // Force-expire the wallet's session |
| 203 | + (walletClient as any).session.expiresAt = Date.now() - 1000; |
| 204 | + |
| 205 | + const errorPromise = new Promise<any>((resolve) => { |
| 206 | + walletClient.once("error", resolve); |
| 207 | + }); |
| 208 | + |
| 209 | + // Send another message from dapp |
| 210 | + await dappClient.sendRequest({ method: "post_expiry_check" }); |
| 211 | + |
| 212 | + // Wallet should emit SESSION_EXPIRED |
| 213 | + const error = await errorPromise; |
| 214 | + t.expect(error.code).toBe(ErrorCode.SESSION_EXPIRED); |
| 215 | + |
| 216 | + // Wait briefly, confirm the message was NOT delivered |
| 217 | + const walletMessagePromise = new Promise((resolve) => walletClient.once("message", resolve)); |
| 218 | + await assertPromiseNotResolve(walletMessagePromise, 500, "Wallet should not receive messages on expired session"); |
| 219 | + }); |
| 220 | + |
193 | 221 | t.test("should successfully resume a previously established session", async () => { |
194 | 222 | await connectClients(dappClient, walletClient, "untrusted"); |
195 | 223 | const sessionId = (await dappSessionStore.list())[0].id; |
|
0 commit comments