refactor: Simpler update user device keys
This commit is contained in:
parent
96f1f056e9
commit
0008ae380f
|
|
@ -2235,8 +2235,8 @@ class Client extends MatrixApi {
|
||||||
await dispose();
|
await dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
_id = accessToken = _syncFilterId =
|
_id = accessToken = _syncFilterId = homeserver =
|
||||||
homeserver = _userID = _deviceID = _deviceName = _prevBatch = null;
|
_userID = _deviceID = _deviceName = _prevBatch = _trackedUserIds = null;
|
||||||
_rooms = [];
|
_rooms = [];
|
||||||
_eventsPendingDecryption.clear();
|
_eventsPendingDecryption.clear();
|
||||||
await encryption?.dispose();
|
await encryption?.dispose();
|
||||||
|
|
@ -2547,6 +2547,7 @@ class Client extends MatrixApi {
|
||||||
for (final userId in deviceLists.left ?? []) {
|
for (final userId in deviceLists.left ?? []) {
|
||||||
if (_userDeviceKeys.containsKey(userId)) {
|
if (_userDeviceKeys.containsKey(userId)) {
|
||||||
_userDeviceKeys.remove(userId);
|
_userDeviceKeys.remove(userId);
|
||||||
|
_trackedUserIds?.remove(userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2815,16 +2816,26 @@ class Client extends MatrixApi {
|
||||||
final callEvents = <Event>[];
|
final callEvents = <Event>[];
|
||||||
|
|
||||||
for (var event in events) {
|
for (var event in events) {
|
||||||
// The client must ignore any new m.room.encryption event to prevent
|
if (event.type == EventTypes.Encryption) {
|
||||||
// man-in-the-middle attacks!
|
// The client must ignore any new m.room.encryption event to prevent
|
||||||
if ((event.type == EventTypes.Encryption &&
|
// man-in-the-middle attacks!
|
||||||
room.encrypted &&
|
if ((room.encrypted &&
|
||||||
event.content.tryGet<String>('algorithm') !=
|
event.content.tryGet<String>('algorithm') !=
|
||||||
room
|
room
|
||||||
.getState(EventTypes.Encryption)
|
.getState(EventTypes.Encryption)
|
||||||
?.content
|
?.content
|
||||||
.tryGet<String>('algorithm'))) {
|
.tryGet<String>('algorithm'))) {
|
||||||
continue;
|
Logs().wtf(
|
||||||
|
'Received an `m.room.encryption` event in a room, where encryption is already enabled! This event must be ignored as it could be an attack!',
|
||||||
|
jsonEncode(event.toJson()),
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
// Encryption has been enabled in a room -> Reset tracked user IDs so
|
||||||
|
// sync they can be calculated again.
|
||||||
|
Logs().i('End to end encryption enabled in', room.id);
|
||||||
|
_trackedUserIds = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event is MatrixEvent &&
|
if (event is MatrixEvent &&
|
||||||
|
|
@ -3058,10 +3069,20 @@ class Client extends MatrixApi {
|
||||||
final event = Event.fromMatrixEvent(eventUpdate, room);
|
final event = Event.fromMatrixEvent(eventUpdate, room);
|
||||||
|
|
||||||
// Update the room state:
|
// Update the room state:
|
||||||
if (event.stateKey != null &&
|
final stateKey = event.stateKey;
|
||||||
|
if (stateKey != null &&
|
||||||
(!room.partial || importantStateEvents.contains(event.type))) {
|
(!room.partial || importantStateEvents.contains(event.type))) {
|
||||||
room.setState(event);
|
room.setState(event);
|
||||||
|
|
||||||
|
if (room.encrypted &&
|
||||||
|
event.type == EventTypes.RoomMember &&
|
||||||
|
{'join', 'invite'}
|
||||||
|
.contains(event.content.tryGet<String>('membership'))) {
|
||||||
|
// New members should be added to the tracked user IDs for encryption:
|
||||||
|
_trackedUserIds?.add(stateKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type != EventUpdateType.timeline) break;
|
if (type != EventUpdateType.timeline) break;
|
||||||
|
|
||||||
// Is this event redacting the last event?
|
// Is this event redacting the last event?
|
||||||
|
|
@ -3197,13 +3218,11 @@ class Client extends MatrixApi {
|
||||||
for (final room in rooms) {
|
for (final room in rooms) {
|
||||||
if (room.encrypted && room.membership == Membership.join) {
|
if (room.encrypted && room.membership == Membership.join) {
|
||||||
try {
|
try {
|
||||||
final userList = await room.requestParticipants();
|
final userList = await room.requestParticipants(
|
||||||
for (final user in userList) {
|
[Membership.join, Membership.invite],
|
||||||
if ([Membership.join, Membership.invite]
|
true,
|
||||||
.contains(user.membership)) {
|
);
|
||||||
userIds.add(user.id);
|
userIds.addAll(userList.map((user) => user.id));
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e, s) {
|
} catch (e, s) {
|
||||||
Logs().e('[E2EE] Failed to fetch participants', e, s);
|
Logs().e('[E2EE] Failed to fetch participants', e, s);
|
||||||
}
|
}
|
||||||
|
|
@ -3214,13 +3233,25 @@ class Client extends MatrixApi {
|
||||||
|
|
||||||
final Map<String, DateTime> _keyQueryFailures = {};
|
final Map<String, DateTime> _keyQueryFailures = {};
|
||||||
|
|
||||||
|
/// These are the user IDs we share an encrypted room with and need to track
|
||||||
|
/// the devices from, cached here for performance reasons.
|
||||||
|
/// It gets initialized after the first sync of every
|
||||||
|
/// instance and then updated on member changes or sync device changes.
|
||||||
|
Set<String>? _trackedUserIds;
|
||||||
|
|
||||||
Future<void> updateUserDeviceKeys({Set<String>? additionalUsers}) async {
|
Future<void> updateUserDeviceKeys({Set<String>? additionalUsers}) async {
|
||||||
try {
|
try {
|
||||||
final database = this.database;
|
final database = this.database;
|
||||||
if (!isLogged()) return;
|
if (!isLogged()) return;
|
||||||
final dbActions = <Future<dynamic> Function()>[];
|
final dbActions = <Future<dynamic> Function()>[];
|
||||||
final trackedUserIds = await _getUserIdsInEncryptedRooms();
|
final trackedUserIds =
|
||||||
if (!isLogged()) return;
|
_trackedUserIds ??= await _getUserIdsInEncryptedRooms();
|
||||||
|
if (!isLogged()) {
|
||||||
|
// For the case we get logged out while `_getUserIdsInEncryptedRooms()`
|
||||||
|
// was already started.
|
||||||
|
_trackedUserIds = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
trackedUserIds.add(userID!);
|
trackedUserIds.add(userID!);
|
||||||
if (additionalUsers != null) trackedUserIds.addAll(additionalUsers);
|
if (additionalUsers != null) trackedUserIds.addAll(additionalUsers);
|
||||||
|
|
||||||
|
|
@ -3760,6 +3791,7 @@ class Client extends MatrixApi {
|
||||||
Future<void> clearCache() async {
|
Future<void> clearCache() async {
|
||||||
await abortSync();
|
await abortSync();
|
||||||
_prevBatch = null;
|
_prevBatch = null;
|
||||||
|
_trackedUserIds = null;
|
||||||
rooms.clear();
|
rooms.clear();
|
||||||
await database.clearCache();
|
await database.clearCache();
|
||||||
encryption?.keyManager.clearOutboundGroupSessions();
|
encryption?.keyManager.clearOutboundGroupSessions();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue