chore: Implement a proper deep-copy function for json maps

This commit is contained in:
Sorunome 2020-12-28 13:41:54 +01:00
parent c69a8ac927
commit 768baa7602
No known key found for this signature in database
GPG Key ID: B19471D07FC9BE9C
17 changed files with 157 additions and 36 deletions

View File

@ -314,7 +314,7 @@ class Encryption {
}
// we clone the payload as we do not want to remove 'm.relates_to' from the
// original payload passed into this function
payload = Map<String, dynamic>.from(payload);
payload = payload.copy();
final Map<String, dynamic> mRelatesTo = payload.remove('m.relates_to');
final payloadContent = {
'content': payload,

View File

@ -49,10 +49,6 @@ const SSSS_KEY_LENGTH = 32;
const PBKDF2_DEFAULT_ITERATIONS = 500000;
const PBKDF2_SALT_LENGTH = 64;
Map<String, dynamic> _deepcopy(Map<String, dynamic> data) {
return json.decode(json.encode(data));
}
/// SSSS: **S**ecure **S**ecret **S**torage and **S**haring
/// Read more about SSSS at:
/// https://matrix.org/docs/guides/implementing-more-advanced-e-2-ee-features-such-as-cross-signing#3-implementing-ssss
@ -310,7 +306,7 @@ class SSSS {
final encrypted = encryptAes(secret, key, type);
Map<String, dynamic> content;
if (add && client.accountData[type] != null) {
content = _deepcopy(client.accountData[type].content);
content = client.accountData[type].content.copy();
if (!(content['encrypted'] is Map)) {
content['encrypted'] = <String, dynamic>{};
}
@ -341,7 +337,7 @@ class SSSS {
throw Exception('Secrets do not match up!');
}
// now remove all other keys
final content = _deepcopy(client.accountData[type].content);
final content = client.accountData[type].content.copy();
final otherKeys =
Set<String>.from(content['encrypted'].keys.where((k) => k != keyId));
content['encrypted'].removeWhere((k, v) => otherKeys.contains(k));

View File

@ -84,8 +84,7 @@ class SessionKey {
Event.getMapFromPayload(dbEntry.allowedAtIndex);
final parsedSenderClaimedKeys =
Event.getMapFromPayload(dbEntry.senderClaimedKeys);
content =
parsedContent != null ? Map<String, dynamic>.from(parsedContent) : null;
content = parsedContent;
// we need to try...catch as the map used to be <String, int> and that will throw an error.
try {
indexes = parsedIndexes != null

View File

@ -20,6 +20,7 @@ library matrix_api;
export 'matrix_api/matrix_api.dart';
export 'matrix_api/utils/logs.dart';
export 'matrix_api/utils/map_copy_extension.dart';
export 'matrix_api/utils/try_get_map_extension.dart';
export 'matrix_api/model/algorithm_types.dart';
export 'matrix_api/model/basic_event.dart';

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import '../utils/map_copy_extension.dart';
class BasicEvent {
String type;
Map<String, dynamic> content;
@ -27,7 +29,7 @@ class BasicEvent {
BasicEvent.fromJson(Map<String, dynamic> json) {
type = json['type'];
content = Map<String, dynamic>.from(json['content']);
content = (json['content'] as Map<String, dynamic>).copy();
}
Map<String, dynamic> toJson() {
final data = <String, dynamic>{};

View File

@ -17,6 +17,7 @@
*/
import 'matrix_keys.dart';
import '../utils/map_copy_extension.dart';
class KeysQueryResponse {
Map<String, dynamic> failures;
@ -26,9 +27,7 @@ class KeysQueryResponse {
Map<String, MatrixCrossSigningKey> userSigningKeys;
KeysQueryResponse.fromJson(Map<String, dynamic> json) {
failures = json['failures'] != null
? Map<String, dynamic>.from(json['failures'])
: null;
failures = (json['failures'] as Map<String, dynamic>)?.copy();
deviceKeys = json['device_keys'] != null
? (json['device_keys'] as Map).map(
(k, v) => MapEntry(

View File

@ -17,6 +17,7 @@
*/
import 'stripped_state_event.dart';
import '../utils/map_copy_extension.dart';
class MatrixEvent extends StrippedStateEvent {
String eventId;
@ -38,12 +39,8 @@ class MatrixEvent extends StrippedStateEvent {
roomId = json['room_id'];
originServerTs =
DateTime.fromMillisecondsSinceEpoch(json['origin_server_ts']);
unsigned = json['unsigned'] != null
? Map<String, dynamic>.from(json['unsigned'])
: null;
prevContent = json['prev_content'] != null
? Map<String, dynamic>.from(json['prev_content'])
: null;
unsigned = (json['unsigned'] as Map<String, dynamic>)?.copy();
prevContent = (json['prev_content'] as Map<String, dynamic>)?.copy();
redacts = json['redacts'];
}

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import '../utils/map_copy_extension.dart';
class MatrixSignableKey {
String userId;
String identifier;
@ -33,13 +35,12 @@ class MatrixSignableKey {
_json = json;
userId = json['user_id'];
keys = Map<String, String>.from(json['keys']);
// we need to manually copy to ensure that our map is Map<String, Map<String, String>>
signatures = json['signatures'] is Map
? Map<String, Map<String, String>>.from((json['signatures'] as Map)
.map((k, v) => MapEntry(k, Map<String, String>.from(v))))
: null;
unsigned = json['unsigned'] is Map
? Map<String, dynamic>.from(json['unsigned'])
: null;
unsigned = (json['unsigned'] as Map<String, dynamic>)?.copy();
}
Map<String, dynamic> toJson() {

View File

@ -16,13 +16,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import '../utils/map_copy_extension.dart';
class OneTimeKeysClaimResponse {
Map<String, dynamic> failures;
Map<String, Map<String, dynamic>> oneTimeKeys;
OneTimeKeysClaimResponse.fromJson(Map<String, dynamic> json) {
failures = Map<String, dynamic>.from(json['failures'] ?? {});
oneTimeKeys = Map<String, Map<String, dynamic>>.from(json['one_time_keys']);
failures = (json['failures'] as Map<String, dynamic>)?.copy() ?? {};
// We still need a Map<...>.from(...) to ensure all second-level entries are also maps
oneTimeKeys = Map<String, Map<String, dynamic>>.from(
(json['one_time_keys'] as Map<String, dynamic>).copy());
}
Map<String, dynamic> toJson() {

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import '../utils/map_copy_extension.dart';
enum RoomVersionStability { stable, unstable }
class ServerCapabilities {
@ -30,7 +32,7 @@ class ServerCapabilities {
mRoomVersions = json['m.room_versions'] != null
? MRoomVersions.fromJson(json['m.room_versions'])
: null;
customCapabilities = Map<String, dynamic>.from(json);
customCapabilities = json.copy();
customCapabilities.remove('m.change_password');
customCapabilities.remove('m.room_versions');
}

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import '../utils/map_copy_extension.dart';
class ThirdPartyLocation {
String alias;
String protocol;
@ -24,7 +26,7 @@ class ThirdPartyLocation {
ThirdPartyLocation.fromJson(Map<String, dynamic> json) {
alias = json['alias'];
protocol = json['protocol'];
fields = Map<String, dynamic>.from(json['fields']);
fields = (json['fields'] as Map<String, dynamic>).copy();
}
Map<String, dynamic> toJson() {

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import '../utils/map_copy_extension.dart';
class ThirdPartyUser {
String userId;
String protocol;
@ -24,7 +26,7 @@ class ThirdPartyUser {
ThirdPartyUser.fromJson(Map<String, dynamic> json) {
userId = json['userid'];
protocol = json['protocol'];
fields = Map<String, dynamic>.from(json['fields']);
fields = (json['fields'] as Map<String, dynamic>).copy();
}
Map<String, dynamic> toJson() {

View File

@ -0,0 +1,33 @@
/*
* Famedly Matrix SDK
* Copyright (C) 2020 Famedly GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
extension MapCopyExtension on Map<String, dynamic> {
/// Deep-copies a given json map
Map<String, dynamic> copy() {
final copy = Map<String, dynamic>.from(this);
for (final entry in copy.entries) {
if (entry.value is Map<String, dynamic>) {
copy[entry.key] = (entry.value as Map<String, dynamic>).copy();
}
if (entry.value is List) {
copy[entry.key] = List.from(entry.value);
}
}
return copy;
}
}

View File

@ -750,7 +750,7 @@ class Room {
};
}
if (editEventId != null) {
final newContent = Map<String, dynamic>.from(content);
final newContent = content.copy();
content['m.new_content'] = newContent;
content['m.relates_to'] = {
'event_id': editEventId,

View File

@ -145,8 +145,7 @@ abstract class SignableKey extends MatrixSignableKey {
}
MatrixSignableKey cloneForSigning() {
final newKey =
MatrixSignableKey.fromJson(Map<String, dynamic>.from(toJson()));
final newKey = MatrixSignableKey.fromJson(toJson().copy());
newKey.identifier = identifier;
newKey.signatures ??= <String, Map<String, String>>{};
newKey.signatures.clear();
@ -154,7 +153,7 @@ abstract class SignableKey extends MatrixSignableKey {
}
String get signingContent {
final data = Map<String, dynamic>.from(super.toJson());
final data = super.toJson().copy();
// some old data might have the custom verified and blocked keys
data.remove('verified');
data.remove('blocked');
@ -303,7 +302,7 @@ abstract class SignableKey extends MatrixSignableKey {
@override
Map<String, dynamic> toJson() {
final data = Map<String, dynamic>.from(super.toJson());
final data = super.toJson().copy();
// some old data may have the verified and blocked keys which are unneeded now
data.remove('verified');
data.remove('blocked');
@ -336,7 +335,7 @@ class CrossSigningKey extends SignableKey {
}
CrossSigningKey.fromMatrixCrossSigningKey(MatrixCrossSigningKey k, Client cl)
: super.fromJson(Map<String, dynamic>.from(k.toJson()), cl) {
: super.fromJson(k.toJson().copy(), cl) {
final json = toJson();
identifier = k.publicKey;
usage = json['usage'].cast<String>();
@ -352,7 +351,7 @@ class CrossSigningKey extends SignableKey {
}
CrossSigningKey.fromJson(Map<String, dynamic> json, Client cl)
: super.fromJson(Map<String, dynamic>.from(json), cl) {
: super.fromJson(json.copy(), cl) {
final json = toJson();
usage = json['usage'].cast<String>();
if (keys != null && keys.isNotEmpty) {
@ -409,7 +408,7 @@ class DeviceKeys extends SignableKey {
}
DeviceKeys.fromMatrixDeviceKeys(MatrixDeviceKeys k, Client cl)
: super.fromJson(Map<String, dynamic>.from(k.toJson()), cl) {
: super.fromJson(k.toJson().copy(), cl) {
final json = toJson();
identifier = k.deviceId;
algorithms = json['algorithms'].cast<String>();
@ -425,7 +424,7 @@ class DeviceKeys extends SignableKey {
}
DeviceKeys.fromJson(Map<String, dynamic> json, Client cl)
: super.fromJson(Map<String, dynamic>.from(json), cl) {
: super.fromJson(json.copy(), cl) {
final json = toJson();
identifier = json['device_id'];
algorithms = json['algorithms'].cast<String>();

View File

@ -0,0 +1,39 @@
/*
* Famedly Matrix SDK
* Copyright (C) 2020 Famedly GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'package:famedlysdk/matrix_api/utils/map_copy_extension.dart';
import 'package:test/test.dart';
void main() {
group('Map-copy-extension', () {
test('it should work', () {
final original = <String, dynamic>{
'attr': 'fox',
'child': <String, dynamic>{
'attr': 'bunny',
'list': [1, 2],
},
};
final copy = original.copy();
original['child']['attr'] = 'raccoon';
expect(copy['child']['attr'], 'bunny');
original['child']['list'].add(3);
expect(copy['child']['list'], [1, 2]);
});
});
}

View File

@ -0,0 +1,45 @@
/*
* Famedly Matrix SDK
* Copyright (C) 2020 Famedly GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import 'package:famedlysdk/matrix_api/utils/try_get_map_extension.dart';
import 'package:test/test.dart';
void main() {
group('Try-get-map-extension', () {
test('it should work', () {
final data = <String, dynamic>{
'str': 'foxies',
'int': 42,
'list': [2, 3, 4],
'map': <String, dynamic>{
'beep': 'boop',
},
};
expect(data.tryGet<String>('str'), 'foxies');
expect(data.tryGet<int>('str'), null);
expect(data.tryGet<int>('int'), 42);
expect(data.tryGet<List>('list'), [2, 3, 4]);
expect(data.tryGet<Map<String, dynamic>>('map')?.tryGet<String>('beep'),
'boop');
expect(data.tryGet<Map<String, dynamic>>('map')?.tryGet<String>('meep'),
null);
expect(data.tryGet<Map<String, dynamic>>('pam')?.tryGet<String>('beep'),
null);
});
});
}