fix: Make sure hive keys only contain valid characters

There are some matrix IDs like room IDs which contain for example emojis.
This is valid from the spec and some people hack their Synapse to have this.
This commit is contained in:
Christian Pauly 2021-06-19 10:20:20 +02:00
parent c37c5a962e
commit 4dc62f6150
1 changed files with 36 additions and 28 deletions

View File

@ -224,7 +224,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
if (multiKey.parts.first != roomId) continue; if (multiKey.parts.first != roomId) continue;
await _roomAccountDataBox.delete(key); await _roomAccountDataBox.delete(key);
} }
await _roomsBox.delete(roomId); await _roomsBox.delete(roomId.toHiveKey);
} }
@override @override
@ -287,7 +287,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
String roomId, String roomId,
String sessionId, String sessionId,
) async { ) async {
final raw = await _inboundGroupSessionsBox.get(sessionId); final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey);
if (raw == null) return null; if (raw == null) return null;
return StoredInboundGroupSession.fromJson(convertToJson(raw)); return StoredInboundGroupSession.fromJson(convertToJson(raw));
} }
@ -321,21 +321,22 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> storeOlmSession(int clientId, String identityKey, Future<void> storeOlmSession(int clientId, String identityKey,
String sessionId, String pickle, int lastReceived) async { String sessionId, String pickle, int lastReceived) async {
final rawSessions = (await _olmSessionsBox.get(identityKey) as Map) ?? {}; final rawSessions =
(await _olmSessionsBox.get(identityKey.toHiveKey) as Map) ?? {};
rawSessions[sessionId] = <String, dynamic>{ rawSessions[sessionId] = <String, dynamic>{
'identity_key': identityKey, 'identity_key': identityKey,
'pickle': pickle, 'pickle': pickle,
'session_id': sessionId, 'session_id': sessionId,
'last_received': lastReceived, 'last_received': lastReceived,
}; };
await _olmSessionsBox.put(identityKey, rawSessions); await _olmSessionsBox.put(identityKey.toHiveKey, rawSessions);
return; return;
} }
@override @override
Future<List<OlmSession>> getOlmSessions( Future<List<OlmSession>> getOlmSessions(
int clientId, String identityKey, String userId) async { int clientId, String identityKey, String userId) async {
final rawSessions = await _olmSessionsBox.get(identityKey) as Map; final rawSessions = await _olmSessionsBox.get(identityKey.toHiveKey) as Map;
if (rawSessions?.isEmpty ?? true) return <OlmSession>[]; if (rawSessions?.isEmpty ?? true) return <OlmSession>[];
return rawSessions.values return rawSessions.values
.map((json) => OlmSession.fromJson(convertToJson(json), userId)) .map((json) => OlmSession.fromJson(convertToJson(json), userId))
@ -353,7 +354,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<OutboundGroupSession> getOutboundGroupSession( Future<OutboundGroupSession> getOutboundGroupSession(
int clientId, String roomId, String userId) async { int clientId, String roomId, String userId) async {
final raw = await _outboundGroupSessionsBox.get(roomId); final raw = await _outboundGroupSessionsBox.get(roomId.toHiveKey);
if (raw == null) return null; if (raw == null) return null;
return OutboundGroupSession.fromJson(convertToJson(raw), userId); return OutboundGroupSession.fromJson(convertToJson(raw), userId);
} }
@ -543,14 +544,14 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> markInboundGroupSessionAsUploaded( Future<void> markInboundGroupSessionAsUploaded(
int clientId, String roomId, String sessionId) async { int clientId, String roomId, String sessionId) async {
final raw = await _inboundGroupSessionsBox.get(sessionId); final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey);
if (raw == null) { if (raw == null) {
Logs().w( Logs().w(
'Tried to mark inbound group session as uploaded which was not found in the database!'); 'Tried to mark inbound group session as uploaded which was not found in the database!');
return; return;
} }
raw['uploaded'] = true; raw['uploaded'] = true;
await _inboundGroupSessionsBox.put(sessionId, raw); await _inboundGroupSessionsBox.put(sessionId.toHiveKey, raw);
return; return;
} }
@ -582,7 +583,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> removeOutboundGroupSession(int clientId, String roomId) async { Future<void> removeOutboundGroupSession(int clientId, String roomId) async {
await _outboundGroupSessionsBox.delete(roomId); await _outboundGroupSessionsBox.delete(roomId.toHiveKey);
return; return;
} }
@ -603,10 +604,10 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> resetNotificationCount(int clientId, String roomId) async { Future<void> resetNotificationCount(int clientId, String roomId) async {
final raw = await _roomsBox.get(roomId); final raw = await _roomsBox.get(roomId.toHiveKey);
if (raw == null) return; if (raw == null) return;
raw['notification_count'] = raw['highlight_count'] = 0; raw['notification_count'] = raw['highlight_count'] = 0;
await _roomsBox.put(roomId, raw); await _roomsBox.put(roomId.toHiveKey, raw);
return; return;
} }
@ -663,11 +664,11 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> setRoomPrevBatch( Future<void> setRoomPrevBatch(
String prevBatch, int clientId, String roomId) async { String prevBatch, int clientId, String roomId) async {
final raw = await _roomsBox.get(roomId); final raw = await _roomsBox.get(roomId.toHiveKey);
if (raw == null) return; if (raw == null) return;
final room = Room.fromJson(convertToJson(raw)); final room = Room.fromJson(convertToJson(raw));
room.prev_batch = prevBatch; room.prev_batch = prevBatch;
await _roomsBox.put(roomId, room.toJson()); await _roomsBox.put(roomId.toHiveKey, room.toJson());
return; return;
} }
@ -701,7 +702,8 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> storeAccountData( Future<void> storeAccountData(
int clientId, String type, String content) async { int clientId, String type, String content) async {
await _accountDataBox.put(type, convertToJson(jsonDecode(content))); await _accountDataBox.put(
type.toHiveKey, convertToJson(jsonDecode(content)));
return; return;
} }
@ -816,7 +818,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
String senderKey, String senderKey,
String senderClaimedKey) async { String senderClaimedKey) async {
await _inboundGroupSessionsBox.put( await _inboundGroupSessionsBox.put(
sessionId, sessionId.toHiveKey,
StoredInboundGroupSession( StoredInboundGroupSession(
clientId: clientId, clientId: clientId,
roomId: roomId, roomId: roomId,
@ -840,7 +842,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
String deviceIds, String deviceIds,
int creationTime, int creationTime,
int sentMessages) async { int sentMessages) async {
await _outboundGroupSessionsBox.put(roomId, <String, dynamic>{ await _outboundGroupSessionsBox.put(roomId.toHiveKey, <String, dynamic>{
'room_id': roomId, 'room_id': roomId,
'pickle': pickle, 'pickle': pickle,
'device_ids': deviceIds, 'device_ids': deviceIds,
@ -865,9 +867,9 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
return; return;
} }
// Make sure room exists // Make sure room exists
if (!_roomsBox.containsKey(roomUpdate.id)) { if (!_roomsBox.containsKey(roomUpdate.id.toHiveKey)) {
await _roomsBox.put( await _roomsBox.put(
roomUpdate.id, roomUpdate.id.toHiveKey,
Room( Room(
id: roomUpdate.id, id: roomUpdate.id,
membership: roomUpdate.membership, membership: roomUpdate.membership,
@ -877,10 +879,10 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
summary: roomUpdate.summary, summary: roomUpdate.summary,
).toJson()); ).toJson());
} else { } else {
final currentRawRoom = await _roomsBox.get(roomUpdate.id); final currentRawRoom = await _roomsBox.get(roomUpdate.id.toHiveKey);
final currentRoom = Room.fromJson(convertToJson(currentRawRoom)); final currentRoom = Room.fromJson(convertToJson(currentRawRoom));
await _roomsBox.put( await _roomsBox.put(
roomUpdate.id, roomUpdate.id.toHiveKey,
Room( Room(
id: roomUpdate.id, id: roomUpdate.id,
membership: roomUpdate.membership ?? currentRoom.membership, membership: roomUpdate.membership ?? currentRoom.membership,
@ -957,7 +959,7 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> storeUserDeviceKeysInfo( Future<void> storeUserDeviceKeysInfo(
int clientId, String userId, bool outdated) async { int clientId, String userId, bool outdated) async {
await _userDeviceKeysOutdatedBox.put(userId, outdated); await _userDeviceKeysOutdatedBox.put(userId.toHiveKey, outdated);
return; return;
} }
@ -993,38 +995,38 @@ class FamedlySdkHiveDatabase extends DatabaseApi {
@override @override
Future<void> updateInboundGroupSessionAllowedAtIndex(String allowedAtIndex, Future<void> updateInboundGroupSessionAllowedAtIndex(String allowedAtIndex,
int clientId, String roomId, String sessionId) async { int clientId, String roomId, String sessionId) async {
final raw = await _inboundGroupSessionsBox.get(sessionId); final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey);
if (raw == null) { if (raw == null) {
Logs().w( Logs().w(
'Tried to update inbound group session as uploaded which wasnt found in the database!'); 'Tried to update inbound group session as uploaded which wasnt found in the database!');
return; return;
} }
raw['allowed_at_index'] = allowedAtIndex; raw['allowed_at_index'] = allowedAtIndex;
await _inboundGroupSessionsBox.put(sessionId, raw); await _inboundGroupSessionsBox.put(sessionId.toHiveKey, raw);
return; return;
} }
@override @override
Future<void> updateInboundGroupSessionIndexes( Future<void> updateInboundGroupSessionIndexes(
String indexes, int clientId, String roomId, String sessionId) async { String indexes, int clientId, String roomId, String sessionId) async {
final raw = await _inboundGroupSessionsBox.get(sessionId); final raw = await _inboundGroupSessionsBox.get(sessionId.toHiveKey);
if (raw == null) { if (raw == null) {
Logs().w( Logs().w(
'Tried to update inbound group session indexes of a session which was not found in the database!'); 'Tried to update inbound group session indexes of a session which was not found in the database!');
return; return;
} }
raw['indexes'] = indexes; raw['indexes'] = indexes;
await _inboundGroupSessionsBox.put(sessionId, raw); await _inboundGroupSessionsBox.put(sessionId.toHiveKey, raw);
return; return;
} }
@override @override
Future<void> updateRoomSortOrder(double oldestSortOrder, Future<void> updateRoomSortOrder(double oldestSortOrder,
double newestSortOrder, int clientId, String roomId) async { double newestSortOrder, int clientId, String roomId) async {
final raw = await _roomsBox.get(roomId); final raw = await _roomsBox.get(roomId.toHiveKey);
raw['oldest_sort_order'] = oldestSortOrder; raw['oldest_sort_order'] = oldestSortOrder;
raw['newest_sort_order'] = newestSortOrder; raw['newest_sort_order'] = newestSortOrder;
await _roomsBox.put(roomId, raw); await _roomsBox.put(roomId.toHiveKey, raw);
return; return;
} }
} }
@ -1054,8 +1056,14 @@ class MultiKey {
: parts = multiKeyString.split('|'); : parts = multiKeyString.split('|');
@override @override
String toString() => parts.join('|'); String toString() => parts.map((s) => s.toHiveKey).join('|');
@override @override
bool operator ==(other) => parts.toString() == other.toString(); bool operator ==(other) => parts.toString() == other.toString();
} }
extension HiveKeyExtension on String {
String get toHiveKey => isValidMatrixId
? '$sigil${Uri.encodeComponent(localpart)}:${Uri.encodeComponent(domain)}'
: Uri.encodeComponent(this);
}