Skip to content

Commit dcff708

Browse files
committed
fix(mobile-web): reject stale targets before awaiting the Relay socket
1 parent 5800b8a commit dcff708

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

‎src/mobile-web/src/services/RelayHttpClient.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ export class RelayHttpClient {
395395
const targetEpoch = this.controlTargetEpochValue;
396396
return this.withAccount(async (identity) => {
397397
const messageKey = await this.deviceMessageKey(identity, targetDeviceId);
398+
if (identity.generation !== this.identityGeneration || targetEpoch !== this.controlTargetEpochValue) {
399+
throw new AccountIdentityChangedError();
400+
}
398401
const connection = this.realtime;
399402
if (!connection) throw new AccountIdentityChangedError();
400403
await connection.ready();

‎src/web-ui/src/app/components/RemoteConnectDialog/RelayHttpClient.generation.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ describe.each(['https://remote.openbitfun.com/v/1.0.2', 'http://192.168.1.9:9700
9696
? Response.json({ device_id: 'desktop', public_key: toB64(peerPublicKey) })
9797
: Response.json({ encrypted_data: encrypted.data, nonce: encrypted.nonce }));
9898
vi.stubGlobal('fetch', fetch);
99+
vi.spyOn(AccountRealtime.prototype, 'ready').mockResolvedValue();
99100
const rpc = vi.spyOn(AccountRealtime.prototype, 'call').mockResolvedValue({ encrypted_data: encrypted.data, nonce: encrypted.nonce });
100101
await expect(client.sendDeviceRpc('desktop', { cmd: 'cancel_task' })).rejects.toThrow(message);
101102
expect(fetch).toHaveBeenCalledTimes(1);
@@ -106,11 +107,14 @@ describe.each(['https://remote.openbitfun.com/v/1.0.2', 'http://192.168.1.9:9700
106107
});
107108
it('does not post a command after the target changes during key lookup', async () => {
108109
const pending = deferred<Response>(); const fetch = vi.fn(() => pending.promise); vi.stubGlobal('fetch', fetch);
110+
const ready = vi.spyOn(AccountRealtime.prototype, 'ready').mockResolvedValue();
109111
const client = new RelayHttpClient(relayUrl, identity()); client.setTargetDeviceId('desktop');
110112
const result = client.sendDeviceRpc('desktop', { cmd: 'cancel_task' });
111113
client.setTargetDeviceId('another');
112114
pending.resolve(Response.json({ device_id: 'desktop', public_key: toB64(peerPublicKey) }));
113115
await expect(result).rejects.toBeInstanceOf(AccountIdentityChangedError);
116+
expect(ready).not.toHaveBeenCalled();
117+
client.resetConnectionIdentity();
114118
expect(fetch).toHaveBeenCalledTimes(1);
115119
});
116120
it('stops transient directory retries when the account changes during backoff', async () => {

0 commit comments

Comments
 (0)