-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathvalidated_commit.rs
1050 lines (956 loc) · 37.2 KB
/
validated_commit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::HashSet;
use openmls::{
credentials::{errors::BasicCredentialError, BasicCredential, Credential as OpenMlsCredential},
extensions::{Extension, Extensions, UnknownExtension},
group::{MlsGroup as OpenMlsGroup, StagedCommit},
messages::proposals::Proposal,
prelude::{LeafNodeIndex, Sender},
treesync::LeafNode,
};
use prost::Message;
use thiserror::Error;
#[cfg(doc)]
use xmtp_id::associations::AssociationState;
use xmtp_id::{associations::MemberIdentifier, InboxId};
use xmtp_proto::xmtp::{
identity::MlsCredential,
mls::message_contents::{
group_updated::{Inbox as InboxProto, MetadataFieldChange as MetadataFieldChangeProto},
GroupMembershipChanges, GroupUpdated as GroupUpdatedProto,
},
};
use crate::{
configuration::GROUP_MEMBERSHIP_EXTENSION_ID,
identity_updates::{InstallationDiff, InstallationDiffError},
storage::{db_connection::DbConnection, StorageError},
};
use xmtp_common::{retry::RetryableError, retryable};
use super::{
group_membership::{GroupMembership, MembershipDiff},
group_metadata::{DmMembers, GroupMetadata, GroupMetadataError},
group_mutable_metadata::{
find_mutable_metadata_extension, GroupMutableMetadata, GroupMutableMetadataError,
MetadataField,
},
group_permissions::{
extract_group_permissions, GroupMutablePermissions, GroupMutablePermissionsError,
},
ScopedGroupClient, MAX_GROUP_DESCRIPTION_LENGTH, MAX_GROUP_IMAGE_URL_LENGTH,
MAX_GROUP_NAME_LENGTH,
};
#[derive(Debug, Error)]
pub enum CommitValidationError {
#[error("Actor could not be found")]
ActorCouldNotBeFound,
// Subject of the proposal has an invalid credential
#[error("Inbox validation failed for {0}")]
InboxValidationFailed(String),
// Not used yet, but seems obvious enough to include now
#[error("Insufficient permissions")]
InsufficientPermissions,
#[error("Invalid version format: {0}")]
InvalidVersionFormat(String),
#[error("Minimum supported protocol version {0} exceeds current version")]
MinimumSupportedProtocolVersionExceedsCurrentVersion(String),
// TODO: We will need to relax this once we support external joins
#[error("Actor not a member of the group")]
ActorNotMember,
#[error("Subject not a member of the group")]
SubjectDoesNotExist,
// Current behaviour is to error out if a Commit includes proposals from multiple actors
// TODO: We should relax this once we support self remove
#[error("Multiple actors in commit")]
MultipleActors,
#[error("Missing group membership")]
MissingGroupMembership,
#[error("Missing mutable metadata")]
MissingMutableMetadata,
#[error("Unexpected installations added:")]
UnexpectedInstallationAdded(Vec<Vec<u8>>),
#[error("Sequence ID can only increase")]
SequenceIdDecreased,
#[error("Unexpected installations removed: {0:?}")]
UnexpectedInstallationsRemoved(Vec<Vec<u8>>),
#[error(transparent)]
GroupMetadata(#[from] GroupMetadataError),
#[error(transparent)]
MlsCredential(#[from] BasicCredentialError),
#[error(transparent)]
GroupMutableMetadata(#[from] GroupMutableMetadataError),
#[error(transparent)]
ProtoDecode(#[from] prost::DecodeError),
#[error(transparent)]
InstallationDiff(#[from] InstallationDiffError),
#[error("Failed to parse group mutable permissions: {0}")]
GroupMutablePermissions(#[from] GroupMutablePermissionsError),
#[error("PSKs are not support")]
NoPSKSupport,
#[error(transparent)]
StorageError(#[from] StorageError),
#[error("Exceeded max characters for this field. Must be under: {length}")]
TooManyCharacters { length: usize },
#[error("Version part missing")]
VersionMissing,
}
impl RetryableError for CommitValidationError {
fn is_retryable(&self) -> bool {
match self {
CommitValidationError::InstallationDiff(diff_error) => retryable!(diff_error),
_ => false,
}
}
}
#[derive(Clone, PartialEq, Hash)]
pub struct CommitParticipant {
pub inbox_id: String,
pub installation_id: Vec<u8>,
pub is_creator: bool,
pub is_admin: bool,
pub is_super_admin: bool,
}
impl std::fmt::Debug for CommitParticipant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
ref inbox_id,
ref installation_id,
ref is_creator,
ref is_admin,
ref is_super_admin,
} = self;
write!(f, "CommitParticipant {{ inbox_id={}, installation_id={}, is_creator={}, is_admin={}, is_super_admin={} }}",
inbox_id,
hex::encode(installation_id),
is_creator,
is_admin,
is_super_admin,
)
}
}
impl CommitParticipant {
pub fn build(
inbox_id: String,
installation_id: Vec<u8>,
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Self {
let is_creator = inbox_id == immutable_metadata.creator_inbox_id;
let is_admin = mutable_metadata.is_admin(&inbox_id);
let is_super_admin = mutable_metadata.is_super_admin(&inbox_id);
Self {
inbox_id,
installation_id,
is_creator,
is_admin,
is_super_admin,
}
}
pub fn from_leaf_node(
leaf_node: &LeafNode,
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Result<Self, CommitValidationError> {
let inbox_id = inbox_id_from_credential(leaf_node.credential())?;
let installation_id = leaf_node.signature_key().as_slice().to_vec();
Ok(Self::build(
inbox_id,
installation_id,
immutable_metadata,
mutable_metadata,
))
}
}
#[derive(Debug, Clone, Default)]
pub struct MutableMetadataValidationInfo {
pub metadata_field_changes: Vec<MetadataFieldChange>,
pub admins_added: Vec<Inbox>,
pub admins_removed: Vec<Inbox>,
pub super_admins_added: Vec<Inbox>,
pub super_admins_removed: Vec<Inbox>,
pub num_super_admins: u32,
pub minimum_supported_protocol_version: Option<String>,
}
impl MutableMetadataValidationInfo {
pub fn is_empty(&self) -> bool {
self.metadata_field_changes.is_empty()
&& self.admins_added.is_empty()
&& self.admins_removed.is_empty()
&& self.super_admins_added.is_empty()
&& self.super_admins_removed.is_empty()
&& self.minimum_supported_protocol_version.is_none()
}
}
#[derive(Debug, Clone)]
pub struct Inbox {
pub inbox_id: String,
#[allow(dead_code)]
pub is_creator: bool,
pub is_admin: bool,
pub is_super_admin: bool,
}
#[derive(Debug, Clone)]
pub struct MetadataFieldChange {
pub field_name: String,
#[allow(dead_code)]
pub old_value: Option<String>,
#[allow(dead_code)]
pub new_value: Option<String>,
}
impl MetadataFieldChange {
pub fn new(field_name: String, old_value: Option<String>, new_value: Option<String>) -> Self {
Self {
field_name,
old_value,
new_value,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct LibXMTPVersion {
major: u32,
minor: u32,
patch: u32,
suffix: Option<String>,
}
impl LibXMTPVersion {
pub fn parse(version_str: &str) -> Result<Self, CommitValidationError> {
let parts: Vec<&str> = version_str.split('.').collect();
if parts.len() != 3 {
return Err(CommitValidationError::InvalidVersionFormat(
version_str.to_string(),
));
}
let major = parts
.first()
.ok_or(CommitValidationError::VersionMissing)?
.parse()
.map_err(|_| CommitValidationError::InvalidVersionFormat(version_str.to_string()))?;
let minor = parts
.get(1)
.ok_or(CommitValidationError::VersionMissing)?
.parse()
.map_err(|_| CommitValidationError::InvalidVersionFormat(version_str.to_string()))?;
let patch_and_suffix = parts
.get(2)
.ok_or(CommitValidationError::VersionMissing)?
.split('-')
.collect::<Vec<_>>();
let patch = patch_and_suffix
.first()
.ok_or(CommitValidationError::VersionMissing)?
.parse()
.map_err(|_| CommitValidationError::InvalidVersionFormat(version_str.to_string()))?;
Ok(LibXMTPVersion {
major,
minor,
patch,
suffix: patch_and_suffix.get(1).map(ToString::to_string),
})
}
}
/**
* A [`ValidatedCommit`] is a summary of changes coming from a MLS commit, after all of our validation rules have been applied
*
* Commit Validation Rules:
* 1. If the `sequence_id` for an inbox has changed, it can only increase
* 2. The client must create an expected diff of installations added and removed based on the difference between the current
* [`GroupMembership`] and the [`GroupMembership`] found in the [`StagedCommit`]
* 3. Installations may only be added or removed in the commit if they were added/removed in the expected diff
* 4. For updates (either updating a path or via an Update Proposal) clients must verify that the `installation_id` is
* present in the [`AssociationState`] for the `inbox_id` presented in the credential at the `to_sequence_id` found in the
* new [`GroupMembership`].
* 5. All proposals in a commit must come from the same installation
* 6. No PSK proposals will be allowed
* 7. New installations may be missing from the commit but still be present in the expected diff.
* 8. Confirms metadata character limit is not exceeded
*/
#[derive(Debug, Clone)]
pub struct ValidatedCommit {
pub actor: CommitParticipant,
pub added_inboxes: Vec<Inbox>,
pub removed_inboxes: Vec<Inbox>,
pub metadata_validation_info: MutableMetadataValidationInfo,
pub permissions_changed: bool,
pub dm_members: Option<DmMembers<String>>,
}
impl ValidatedCommit {
pub async fn from_staged_commit(
client: impl ScopedGroupClient,
conn: &DbConnection,
staged_commit: &StagedCommit,
openmls_group: &OpenMlsGroup,
) -> Result<Self, CommitValidationError> {
// Get the immutable and mutable metadata
let extensions = openmls_group.extensions();
let immutable_metadata: GroupMetadata = extensions.try_into()?;
let mutable_metadata: GroupMutableMetadata = extensions.try_into()?;
let group_permissions: GroupMutablePermissions = extensions.try_into()?;
let current_group_members = get_current_group_members(openmls_group);
let existing_group_extensions = openmls_group.extensions();
let new_group_extensions = staged_commit.group_context().extensions();
let metadata_validation_info = extract_metadata_changes(
&immutable_metadata,
&mutable_metadata,
existing_group_extensions,
new_group_extensions,
)?;
// Enforce character limits for specific metadata fields
for field_change in &metadata_validation_info.metadata_field_changes {
if let Some(new_value) = &field_change.new_value {
match field_change.field_name.as_str() {
val if val == MetadataField::Description.as_str()
&& new_value.len() > MAX_GROUP_DESCRIPTION_LENGTH =>
{
return Err(CommitValidationError::TooManyCharacters {
length: MAX_GROUP_DESCRIPTION_LENGTH,
});
}
val if val == MetadataField::GroupName.as_str()
&& new_value.len() > MAX_GROUP_NAME_LENGTH =>
{
return Err(CommitValidationError::TooManyCharacters {
length: MAX_GROUP_NAME_LENGTH,
});
}
val if val == MetadataField::GroupImageUrlSquare.as_str()
&& new_value.len() > MAX_GROUP_IMAGE_URL_LENGTH =>
{
return Err(CommitValidationError::TooManyCharacters {
length: MAX_GROUP_IMAGE_URL_LENGTH,
});
}
_ => {}
}
}
}
let permissions_changed =
extract_permissions_changed(&group_permissions, new_group_extensions)?;
// Get the actor who created the commit.
// Because we don't allow for multiple actors in a commit, this will error if two proposals come from different authors.
let actor = extract_actor(
staged_commit,
openmls_group,
&immutable_metadata,
&mutable_metadata,
)?;
// Block any psk proposals
if staged_commit.psk_proposals().any(|_| true) {
return Err(CommitValidationError::NoPSKSupport);
}
// Get the installations actually added and removed in the commit
let ProposalChanges {
added_installations,
removed_installations,
mut credentials_to_verify,
} = get_proposal_changes(
staged_commit,
openmls_group,
&immutable_metadata,
&mutable_metadata,
)?;
// Get the expected diff of installations added and removed based on the difference between the current
// group membership and the new group membership.
// Also gets back the added and removed inbox ids from the expected diff
let expected_diff =
ExpectedDiff::from_staged_commit(&client, conn, staged_commit, openmls_group).await?;
let ExpectedDiff {
new_group_membership,
expected_installation_diff,
added_inboxes,
removed_inboxes,
} = expected_diff;
// Ensure that the expected diff matches the added/removed installations in the proposals
expected_diff_matches_commit(
&expected_installation_diff,
added_installations,
removed_installations,
current_group_members,
)?;
credentials_to_verify.push(actor.clone());
// Verify the credentials of the following entities
// 1. The actor who created the commit
// 2. Anyone referenced in an update proposal
// Satisfies Rule 4
for participant in credentials_to_verify {
let to_sequence_id = new_group_membership
.get(&participant.inbox_id)
.ok_or(CommitValidationError::SubjectDoesNotExist)?;
let inbox_state = client
.get_association_state(conn, &participant.inbox_id, Some(*to_sequence_id as i64))
.await
.map_err(InstallationDiffError::from)?;
if inbox_state
.get(&MemberIdentifier::installation(participant.installation_id))
.is_none()
{
return Err(CommitValidationError::InboxValidationFailed(
participant.inbox_id,
));
}
}
let verified_commit = Self {
actor,
added_inboxes,
removed_inboxes,
metadata_validation_info,
permissions_changed,
dm_members: immutable_metadata.dm_members,
};
let policy_set = extract_group_permissions(openmls_group)?;
if !policy_set.policies.evaluate_commit(&verified_commit) {
return Err(CommitValidationError::InsufficientPermissions);
}
if let Some(min_version) = &verified_commit
.metadata_validation_info
.minimum_supported_protocol_version
{
let current_version = LibXMTPVersion::parse(client.version_info().pkg_version())?;
let min_supported_version = LibXMTPVersion::parse(min_version)?;
tracing::info!(
"Validating commit with min_supported_version: {:?}, current_version: {:?}",
min_supported_version,
current_version
);
if min_supported_version > current_version {
return Err(
CommitValidationError::MinimumSupportedProtocolVersionExceedsCurrentVersion(
min_version.clone(),
),
);
}
}
Ok(verified_commit)
}
pub fn is_empty(&self) -> bool {
self.added_inboxes.is_empty()
&& self.removed_inboxes.is_empty()
&& self.metadata_validation_info.is_empty()
}
pub fn actor_inbox_id(&self) -> InboxId {
self.actor.inbox_id.clone()
}
pub fn actor_installation_id(&self) -> Vec<u8> {
self.actor.installation_id.clone()
}
}
impl From<ValidatedCommit> for GroupMembershipChanges {
fn from(_commit: ValidatedCommit) -> Self {
// TODO: Use new GroupMembershipChanges
GroupMembershipChanges {
members_added: vec![],
members_removed: vec![],
installations_added: vec![],
installations_removed: vec![],
}
}
}
struct ProposalChanges {
added_installations: HashSet<Vec<u8>>,
removed_installations: HashSet<Vec<u8>>,
credentials_to_verify: Vec<CommitParticipant>,
}
/**
* Extracts the installations added and removed via proposals in the commit.
* Also returns a list of credentials from existing members that need verification (caused by update proposals)
*/
fn get_proposal_changes(
staged_commit: &StagedCommit,
openmls_group: &OpenMlsGroup,
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Result<ProposalChanges, CommitValidationError> {
// The actual installations added and removed via proposals in the commit
let mut added_installations: HashSet<Vec<u8>> = HashSet::new();
let mut removed_installations: HashSet<Vec<u8>> = HashSet::new();
let mut credentials_to_verify: Vec<CommitParticipant> = vec![];
for proposal in staged_commit.queued_proposals() {
match proposal.proposal() {
// For update proposals, we need to validate that the credential and installation key
// are valid for the inbox_id in the current group membership state
Proposal::Update(update_proposal) => {
credentials_to_verify.push(CommitParticipant::from_leaf_node(
update_proposal.leaf_node(),
immutable_metadata,
mutable_metadata,
)?);
}
// For Add Proposals, all we need to do is validate that the installation_id is in the expected diff
Proposal::Add(add_proposal) => {
// We don't need to validate the credential here, since we've already validated it as part of
// building the expected installation diff
let leaf_node = add_proposal.key_package().leaf_node();
let installation_id = leaf_node.signature_key().as_slice().to_vec();
added_installations.insert(installation_id);
}
// For Remove Proposals, all we need to do is validate that the installation_id is in the expected diff
Proposal::Remove(remove_proposal) => {
let leaf_node = openmls_group
.member_at(remove_proposal.removed())
.ok_or(CommitValidationError::SubjectDoesNotExist)?;
let installation_id = leaf_node.signature_key.to_vec();
removed_installations.insert(installation_id);
}
_ => continue,
}
}
Ok(ProposalChanges {
added_installations,
removed_installations,
credentials_to_verify,
})
}
/**
* Extracts the latest `GroupMembership` from the staged commit.
*
* Returns an error if the extension is not found.
*/
fn get_latest_group_membership(
staged_commit: &StagedCommit,
) -> Result<GroupMembership, CommitValidationError> {
for proposal in staged_commit.queued_proposals() {
match proposal.proposal() {
Proposal::GroupContextExtensions(group_context_extensions) => {
let new_group_membership: GroupMembership =
extract_group_membership(group_context_extensions.extensions())?;
tracing::info!(
"Group context extensions proposal found: {:?}",
new_group_membership
);
return Ok(new_group_membership);
}
_ => continue,
}
}
extract_group_membership(staged_commit.group_context().extensions())
}
struct ExpectedDiff {
new_group_membership: GroupMembership,
expected_installation_diff: InstallationDiff,
added_inboxes: Vec<Inbox>,
removed_inboxes: Vec<Inbox>,
}
impl ExpectedDiff {
pub(super) async fn from_staged_commit(
client: impl ScopedGroupClient,
conn: &DbConnection,
staged_commit: &StagedCommit,
openmls_group: &OpenMlsGroup,
) -> Result<Self, CommitValidationError> {
// Get the immutable and mutable metadata
let extensions = openmls_group.extensions();
let immutable_metadata: GroupMetadata = extensions.try_into()?;
let mutable_metadata: GroupMutableMetadata = extensions.try_into()?;
// Block any psk proposals
if staged_commit.psk_proposals().any(|_| true) {
return Err(CommitValidationError::NoPSKSupport);
}
let expected_diff = Self::extract_expected_diff(
conn,
&client,
staged_commit,
extensions,
&immutable_metadata,
&mutable_metadata,
)
.await?;
Ok(expected_diff)
}
/// Generates an expected diff of installations added and removed based on the difference between the current
/// [`GroupMembership`] and the [`GroupMembership`] found in the [`StagedCommit`].
/// This requires loading the Inbox state from the network.
/// Satisfies Rule 2
async fn extract_expected_diff(
conn: &DbConnection,
client: impl ScopedGroupClient,
staged_commit: &StagedCommit,
existing_group_extensions: &Extensions,
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Result<ExpectedDiff, CommitValidationError> {
let old_group_membership = extract_group_membership(existing_group_extensions)?;
let new_group_membership = get_latest_group_membership(staged_commit)?;
let membership_diff = old_group_membership.diff(&new_group_membership);
validate_membership_diff(
&old_group_membership,
&new_group_membership,
&membership_diff,
)?;
let added_inboxes = membership_diff
.added_inboxes
.iter()
.map(|inbox_id| build_inbox(inbox_id, immutable_metadata, mutable_metadata))
.collect::<Vec<Inbox>>();
let removed_inboxes = membership_diff
.removed_inboxes
.iter()
.map(|inbox_id| build_inbox(inbox_id, immutable_metadata, mutable_metadata))
.collect::<Vec<Inbox>>();
let expected_installation_diff = client
.get_installation_diff(
conn,
&old_group_membership,
&new_group_membership,
&membership_diff,
)
.await?;
Ok(ExpectedDiff {
new_group_membership,
expected_installation_diff,
added_inboxes,
removed_inboxes,
})
}
}
/// Compare the list of installations added and removed in the commit to the expected diff based on the changes
/// to the inbox state.
/// Satisfies Rule 3 and Rule 7
fn expected_diff_matches_commit(
expected_diff: &InstallationDiff,
added_installations: HashSet<Vec<u8>>,
removed_installations: HashSet<Vec<u8>>,
existing_installation_ids: HashSet<Vec<u8>>,
) -> Result<(), CommitValidationError> {
// Check and make sure that any added installations are either:
// 1. In the expected diff
// 2. Already a member of the group (for example, the group creator is already a member on the first commit)
let unknown_adds = added_installations
.into_iter()
.filter(|installation_id| {
!expected_diff.added_installations.contains(installation_id)
&& !existing_installation_ids.contains(installation_id)
})
.collect::<Vec<Vec<u8>>>();
if !unknown_adds.is_empty() {
return Err(CommitValidationError::UnexpectedInstallationAdded(
unknown_adds,
));
}
if removed_installations.ne(&expected_diff.removed_installations) {
return Err(CommitValidationError::UnexpectedInstallationsRemoved(
removed_installations
.difference(&expected_diff.removed_installations)
.cloned()
.collect::<Vec<Vec<u8>>>(),
));
}
Ok(())
}
fn get_current_group_members(openmls_group: &OpenMlsGroup) -> HashSet<Vec<u8>> {
openmls_group
.members()
.map(|member| member.signature_key)
.collect()
}
/// Validate that the new group membership is a valid state transition from the old group membership.
/// Enforces Rule 1 from above
fn validate_membership_diff(
old_membership: &GroupMembership,
new_membership: &GroupMembership,
diff: &MembershipDiff<'_>,
) -> Result<(), CommitValidationError> {
for inbox_id in diff.updated_inboxes.iter() {
let old_sequence_id = old_membership
.get(inbox_id)
.ok_or(CommitValidationError::SubjectDoesNotExist)?;
let new_sequence_id = new_membership
.get(inbox_id)
.ok_or(CommitValidationError::SubjectDoesNotExist)?;
if new_sequence_id.lt(old_sequence_id) {
return Err(CommitValidationError::SequenceIdDecreased);
}
}
Ok(())
}
/// Extracts the [`CommitParticipant`] from the [`LeafNodeIndex`]
fn extract_commit_participant(
leaf_index: &LeafNodeIndex,
group: &OpenMlsGroup,
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Result<CommitParticipant, CommitValidationError> {
if let Some(leaf_node) = group.member_at(*leaf_index) {
let installation_id = leaf_node.signature_key.to_vec();
let inbox_id = inbox_id_from_credential(&leaf_node.credential)?;
Ok(CommitParticipant::build(
inbox_id,
installation_id,
immutable_metadata,
mutable_metadata,
))
} else {
// TODO: Handle external joins/commits
Err(CommitValidationError::ActorNotMember)
}
}
/// Get the [`GroupMembership`] from a [`GroupContext`] struct by iterating through all extensions
/// until a match is found
#[tracing::instrument(level = "trace", skip_all)]
pub fn extract_group_membership(
extensions: &Extensions,
) -> Result<GroupMembership, CommitValidationError> {
for extension in extensions.iter() {
if let Extension::Unknown(
GROUP_MEMBERSHIP_EXTENSION_ID,
UnknownExtension(group_membership),
) = extension
{
return Ok(GroupMembership::try_from(group_membership.clone())?);
}
}
Err(CommitValidationError::MissingGroupMembership)
}
/**
* Extracts the changes to the mutable metadata in the commit.
*
* Returns an error if the extension is not found in either the old or new group context.
*/
fn extract_metadata_changes(
immutable_metadata: &GroupMetadata,
// We already have the old mutable metadata, so save parsing it a second time
old_mutable_metadata: &GroupMutableMetadata,
old_group_extensions: &Extensions,
new_group_extensions: &Extensions,
) -> Result<MutableMetadataValidationInfo, CommitValidationError> {
let old_mutable_metadata_ext = find_mutable_metadata_extension(old_group_extensions)
.ok_or(CommitValidationError::MissingMutableMetadata)?;
let new_mutable_metadata_ext = find_mutable_metadata_extension(new_group_extensions)
.ok_or(CommitValidationError::MissingMutableMetadata)?;
// Before even decoding the new metadata, make sure that something has changed. Otherwise we know there is
// nothing to do
if old_mutable_metadata_ext.eq(new_mutable_metadata_ext) {
let minimum_supported_protocol_version: Option<String> = old_mutable_metadata
.attributes
.get(MetadataField::MinimumSupportedProtocolVersion.as_str())
.map(|s| s.to_string());
return Ok(MutableMetadataValidationInfo {
minimum_supported_protocol_version,
..Default::default()
});
}
let new_mutable_metadata: GroupMutableMetadata = new_mutable_metadata_ext.try_into()?;
let metadata_field_changes =
mutable_metadata_field_changes(old_mutable_metadata, &new_mutable_metadata);
Ok(MutableMetadataValidationInfo {
metadata_field_changes,
admins_added: get_added_members(
&old_mutable_metadata.admin_list,
&new_mutable_metadata.admin_list,
immutable_metadata,
old_mutable_metadata,
),
admins_removed: get_removed_members(
&old_mutable_metadata.admin_list,
&new_mutable_metadata.admin_list,
immutable_metadata,
old_mutable_metadata,
),
super_admins_added: get_added_members(
&old_mutable_metadata.super_admin_list,
&new_mutable_metadata.super_admin_list,
immutable_metadata,
old_mutable_metadata,
),
super_admins_removed: get_removed_members(
&old_mutable_metadata.super_admin_list,
&new_mutable_metadata.super_admin_list,
immutable_metadata,
old_mutable_metadata,
),
num_super_admins: new_mutable_metadata.super_admin_list.len() as u32,
minimum_supported_protocol_version: new_mutable_metadata
.attributes
.get(MetadataField::MinimumSupportedProtocolVersion.as_str())
.map(|s| s.to_string()),
})
}
// Returns true if the permissions have changed, false otherwise
fn extract_permissions_changed(
old_group_permissions: &GroupMutablePermissions,
new_group_extensions: &Extensions,
) -> Result<bool, CommitValidationError> {
let new_group_permissions: GroupMutablePermissions = new_group_extensions.try_into()?;
Ok(!old_group_permissions.eq(&new_group_permissions))
}
/**
* Gets the list of inboxes present in the new group membership that are not present in the old group membership.
*/
fn get_added_members(
old: &[String],
new: &[String],
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Vec<Inbox> {
new.iter()
.filter(|new_inbox| !old.contains(new_inbox))
.map(|inbox_id| build_inbox(inbox_id, immutable_metadata, mutable_metadata))
.collect()
}
/**
* Gets the list of inboxes present in the old group membership that are not present in the new group membership.
*/
fn get_removed_members(
old: &[String],
new: &[String],
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Vec<Inbox> {
old.iter()
.filter(|old_inbox| !new.contains(old_inbox))
.map(|inbox_id| build_inbox(inbox_id, immutable_metadata, mutable_metadata))
.collect()
}
fn build_inbox(
inbox_id: &String,
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Inbox {
Inbox {
inbox_id: inbox_id.to_string(),
is_admin: mutable_metadata.is_admin(inbox_id),
is_super_admin: mutable_metadata.is_super_admin(inbox_id),
is_creator: immutable_metadata.creator_inbox_id.eq(inbox_id),
}
}
/**
* Extracts the changes to the mutable metadata in the commit.
*/
fn mutable_metadata_field_changes(
old_metadata: &GroupMutableMetadata,
new_metadata: &GroupMutableMetadata,
) -> Vec<MetadataFieldChange> {
let all_keys = old_metadata
.attributes
.keys()
.chain(new_metadata.attributes.keys())
.fold(HashSet::new(), |mut key_set, key| {
key_set.insert(key);
key_set
});
all_keys
.into_iter()
.filter_map(|key| {
let old_val = old_metadata.attributes.get(key);
let new_val = new_metadata.attributes.get(key);
if old_val.ne(&new_val) {
Some(MetadataFieldChange::new(
key.clone(),
old_val.cloned(),
new_val.cloned(),
))
} else {
None
}
})
.collect()
}
/// Extracts the inbox ID from a credential.
fn inbox_id_from_credential(
credential: &OpenMlsCredential,
) -> Result<String, CommitValidationError> {
let basic_credential = BasicCredential::try_from(credential.clone())?;
let identity_bytes = basic_credential.identity();
let decoded = MlsCredential::decode(identity_bytes)?;
Ok(decoded.inbox_id)
}
/// Takes a [`StagedCommit`] and tries to extract the actor who created the commit.
/// In the case of a self-update, which does not contain any proposals, this will come from the update_path.
/// In the case of a commit with proposals, it will be the creator of all the proposals.
/// Satisfies Rule 5 by erroring if any proposals have different actors
fn extract_actor(
staged_commit: &StagedCommit,
openmls_group: &OpenMlsGroup,
immutable_metadata: &GroupMetadata,
mutable_metadata: &GroupMutableMetadata,
) -> Result<CommitParticipant, CommitValidationError> {
// If there was a path update, get the leaf node that was updated
let path_update_leaf_node: Option<&LeafNode> = staged_commit.update_path_leaf_node();
// Iterate through the proposals and get the sender of the proposal.
// Error if there are multiple senders found
let proposal_author_leaf_index = staged_commit
.queued_proposals()
.try_fold::<Option<&LeafNodeIndex>, _, _>(
None,
|existing_value, proposal| match proposal.sender() {
Sender::Member(member_leaf_node_index) => match existing_value {
Some(existing_member) => {
if existing_member.ne(member_leaf_node_index) {
return Err(CommitValidationError::MultipleActors);
}
Ok(existing_value)
}
None => Ok(Some(member_leaf_node_index)),
},
_ => Err(CommitValidationError::ActorNotMember),
},
)?;
// If there is both a path update and there are proposals we need to make sure that they are from the same actor
if let (Some(path_update_leaf_node), Some(proposal_author_leaf_index)) =
(path_update_leaf_node, proposal_author_leaf_index)
{
let proposal_author = openmls_group
.member_at(*proposal_author_leaf_index)
.ok_or(CommitValidationError::ActorCouldNotBeFound)?;
// Verify that the signature keys are the same
if path_update_leaf_node
.signature_key()
.as_slice()
.to_vec()
.ne(&proposal_author.signature_key)
{
return Err(CommitValidationError::MultipleActors);
}
}
// Convert the path update leaf node to a [`CommitParticipant`]
if let Some(path_update_leaf_node) = path_update_leaf_node {
return CommitParticipant::from_leaf_node(
path_update_leaf_node,
immutable_metadata,
mutable_metadata,
);
}
// Convert the proposal author leaf index to a [`CommitParticipant`]