Skip to content

Commit fc1ea27

Browse files
authored
Merge pull request #855 from uhoreg/fix_key_requests
More key request fixes
2 parents 77270fa + 8194629 commit fc1ea27

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

spec/unit/crypto.spec.js

+65
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import TestClient from '../TestClient';
1212
import {MatrixEvent} from '../../lib/models/event';
1313
import Room from '../../lib/models/room';
1414
import olmlib from '../../lib/crypto/olmlib';
15+
import lolex from 'lolex';
1516

1617
const EventEmitter = require("events").EventEmitter;
1718

@@ -297,5 +298,69 @@ describe("Crypto", function() {
297298
expect(await cryptoStore.getOutgoingRoomKeyRequest(roomKeyRequestBody))
298299
.toExist();
299300
});
301+
302+
it("uses a new txnid for re-requesting keys", async function() {
303+
const event = new MatrixEvent({
304+
sender: "@bob:example.com",
305+
room_id: "!someroom",
306+
content: {
307+
algorithm: olmlib.MEGOLM_ALGORITHM,
308+
session_id: "sessionid",
309+
sender_key: "senderkey",
310+
},
311+
});
312+
/* return a promise and a function. When the function is called,
313+
* the promise will be resolved.
314+
*/
315+
function awaitFunctionCall() {
316+
let func;
317+
const promise = new Promise((resolve, reject) => {
318+
func = function(...args) {
319+
resolve(args);
320+
return new Promise((resolve, reject) => {
321+
// give us some time to process the result before
322+
// continuing
323+
global.setTimeout(resolve, 1);
324+
});
325+
};
326+
});
327+
return {func, promise};
328+
}
329+
330+
aliceClient.startClient();
331+
332+
const clock = lolex.install();
333+
334+
try {
335+
let promise;
336+
// make a room key request, and record the transaction ID for the
337+
// sendToDevice call
338+
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
339+
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
340+
clock.runToLast();
341+
let args = await promise;
342+
const txnId = args[2];
343+
clock.runToLast();
344+
345+
// give the room key request manager time to update the state
346+
// of the request
347+
await Promise.resolve();
348+
349+
// cancel and resend the room key request
350+
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
351+
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
352+
clock.runToLast();
353+
// the first call to sendToDevice will be the cancellation
354+
args = await promise;
355+
// the second call to sendToDevice will be the key request
356+
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
357+
clock.runToLast();
358+
args = await promise;
359+
clock.runToLast();
360+
expect(args[2]).toNotBe(txnId);
361+
} finally {
362+
clock.uninstall();
363+
}
364+
});
300365
});
301366
});

src/crypto/OutgoingRoomKeyRequestManager.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,15 @@ export default class OutgoingRoomKeyRequestManager {
170170
// existing request and send a new one.
171171
if (resend) {
172172
const state =
173-
ROOM_KEY_REQUEST_STATES.CANCELLATION_PENDING_AND_WILLRESEND;
173+
ROOM_KEY_REQUEST_STATES.CANCELLATION_PENDING_AND_WILL_RESEND;
174174
const updatedReq =
175175
await this._cryptoStore.updateOutgoingRoomKeyRequest(
176176
req.requestId, ROOM_KEY_REQUEST_STATES.SENT, {
177177
state,
178178
cancellationTxnId: this._baseApis.makeTxnId(),
179+
// need to use a new transaction ID so that
180+
// the request gets sent
181+
requestTxnId: this._baseApis.makeTxnId(),
179182
},
180183
);
181184
if (!updatedReq) {
@@ -347,7 +350,7 @@ export default class OutgoingRoomKeyRequestManager {
347350
logger.warn(
348351
`error in OutgoingRoomKeyRequestManager: ${e}`,
349352
);
350-
}).done();
353+
});
351354
};
352355

353356
this._sendOutgoingRoomKeyRequestsTimer = global.setTimeout(
@@ -398,7 +401,7 @@ export default class OutgoingRoomKeyRequestManager {
398401
logger.error("Error sending room key request; will retry later.", e);
399402
this._sendOutgoingRoomKeyRequestsTimer = null;
400403
this._startTimer();
401-
}).done();
404+
});
402405
});
403406
}
404407

@@ -418,7 +421,7 @@ export default class OutgoingRoomKeyRequestManager {
418421
};
419422

420423
return this._sendMessageToDevices(
421-
requestMessage, req.recipients, req.requestId,
424+
requestMessage, req.recipients, req.requestTxnId || req.requestId,
422425
).then(() => {
423426
return this._cryptoStore.updateOutgoingRoomKeyRequest(
424427
req.requestId, ROOM_KEY_REQUEST_STATES.UNSENT,

0 commit comments

Comments
 (0)