fix: Ratchets are not removed when using removeAllRatchets
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
PapaTutuWawa 2023-01-07 22:14:10 +01:00
parent de85ab7955
commit cc31475671
2 changed files with 89 additions and 5 deletions

View File

@ -277,7 +277,7 @@ class OmemoManager {
if (rawKey.kex) { if (rawKey.kex) {
// If the ratchet already existed, we store it. If it didn't, oldRatchet will stay // If the ratchet already existed, we store it. If it didn't, oldRatchet will stay
// null. // null.
final oldRatchet = _getRatchet(ratchetKey)?.clone(); final oldRatchet = getRatchet(ratchetKey)?.clone();
final kex = OmemoKeyExchange.fromBuffer(decodedRawKey); final kex = OmemoKeyExchange.fromBuffer(decodedRawKey);
authMessage = kex.message!; authMessage = kex.message!;
message = OmemoMessage.fromBuffer(authMessage.message!); message = OmemoMessage.fromBuffer(authMessage.message!);
@ -341,7 +341,7 @@ class OmemoManager {
} }
// We can guarantee that the ratchet exists at this point in time // We can guarantee that the ratchet exists at this point in time
final ratchet = _getRatchet(ratchetKey)!; final ratchet = getRatchet(ratchetKey)!;
oldRatchet ??= ratchet.clone(); oldRatchet ??= ratchet.clone();
try { try {
@ -378,7 +378,8 @@ class OmemoManager {
/// Returns, if it exists, the ratchet associated with [key]. /// Returns, if it exists, the ratchet associated with [key].
/// NOTE: Must be called from within the ratchet critical section. /// NOTE: Must be called from within the ratchet critical section.
OmemoDoubleRatchet? _getRatchet(RatchetMapKey key) => _ratchetMap[key]; @visibleForTesting
OmemoDoubleRatchet? getRatchet(RatchetMapKey key) => _ratchetMap[key];
/// Figure out what bundles we have to still build a session with. /// Figure out what bundles we have to still build a session with.
Future<List<OmemoBundle>> _fetchNewBundles(String jid) async { Future<List<OmemoBundle>> _fetchNewBundles(String jid) async {
@ -594,7 +595,7 @@ class OmemoManager {
} }
// Check if the ratchet is acked // Check if the ratchet is acked
final ratchet = _getRatchet(ratchetKey); final ratchet = getRatchet(ratchetKey);
assert(ratchet != null, 'We decrypted the message, so the ratchet must exist'); assert(ratchet != null, 'We decrypted the message, so the ratchet must exist');
if (ratchet!.acknowledged) { if (ratchet!.acknowledged) {
@ -762,7 +763,7 @@ class OmemoManager {
for (final deviceId in _deviceList[jid]!) { for (final deviceId in _deviceList[jid]!) {
// Remove the ratchet and commit it // Remove the ratchet and commit it
_ratchetMap.remove(jid); _ratchetMap.remove(RatchetMapKey(jid, deviceId));
_eventStreamController.add(RatchetRemovedEvent(jid, deviceId)); _eventStreamController.add(RatchetRemovedEvent(jid, deviceId));
} }

View File

@ -942,4 +942,87 @@ void main() {
expect(aliceReceivedMessage.payload, messageText); expect(aliceReceivedMessage.payload, messageText);
} }
}); });
test('Test removing all ratchets and sending a message', () async {
const aliceJid = 'alice@server1';
const bobJid = 'bob@server2';
final aliceDevice = await OmemoDevice.generateNewDevice(aliceJid, opkAmount: 1);
final bobDevice = await OmemoDevice.generateNewDevice(bobJid, opkAmount: 1);
final aliceManager = OmemoManager(
aliceDevice,
AlwaysTrustingTrustManager(),
(result, recipientJid) async {},
(jid) async {
expect(jid, bobJid);
return [bobDevice.id];
},
(jid, id) async {
expect(jid, bobJid);
return bobDevice.toBundle();
},
(jid) async {},
);
final bobManager = OmemoManager(
bobDevice,
AlwaysTrustingTrustManager(),
(result, recipientJid) async {},
(jid) async => null,
(jid, id) async => null,
(jid) async {},
);
// Alice encrypts a message for Bob
final aliceResult1 = await aliceManager.onOutgoingStanza(
const OmemoOutgoingStanza(
[bobJid],
'Hello Bob!',
),
);
// And Bob decrypts it
await bobManager.onIncomingStanza(
OmemoIncomingStanza(
aliceJid,
aliceDevice.id,
DateTime.now().millisecondsSinceEpoch,
aliceResult1.encryptedKeys,
base64.encode(aliceResult1.ciphertext!),
),
);
// Ratchets are acked
await aliceManager.ratchetAcknowledged(
bobJid,
bobDevice.id,
);
// Alice now removes all ratchets for Bob and sends another new message
await aliceManager.removeAllRatchets(bobJid);
expect(aliceManager.getRatchet(RatchetMapKey(bobJid, bobDevice.id)), null);
final aliceResult2 = await aliceManager.onOutgoingStanza(
const OmemoOutgoingStanza(
[bobJid],
'I did not trust your last device, Bob!',
),
);
// Bob decrypts it
final bobResult2 = await bobManager.onIncomingStanza(
OmemoIncomingStanza(
aliceJid,
aliceDevice.id,
DateTime.now().millisecondsSinceEpoch,
aliceResult2.encryptedKeys,
base64.encode(aliceResult2.ciphertext!),
),
);
expect(bobResult2.payload, 'I did not trust your last device, Bob!');
});
} }