Skip to content

Commit

Permalink
add check on commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Feb 4, 2025
1 parent 31abcdb commit 52dbbff
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion xmtp_mls/src/groups/validated_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ use super::{
group_metadata::{DmMembers, GroupMetadata, GroupMetadataError},
group_mutable_metadata::{
find_mutable_metadata_extension, GroupMutableMetadata, GroupMutableMetadataError,
MetadataField,
},
group_permissions::{
extract_group_permissions, GroupMutablePermissions, GroupMutablePermissionsError,
},
ScopedGroupClient,
GroupError, ScopedGroupClient, MAX_GROUP_DESCRIPTION_LENGTH, MAX_GROUP_IMAGE_URL_LENGTH,

Check failure

Code scanning / clippy

unused import: GroupError Error

unused import: GroupError

Check failure

Code scanning / clippy

unused import: GroupError Error

unused import: GroupError
MAX_GROUP_NAME_LENGTH,
};

#[derive(Debug, Error)]
Expand Down Expand Up @@ -85,6 +87,8 @@ pub enum CommitValidationError {
NoPSKSupport,
#[error(transparent)]
StorageError(#[from] StorageError),
#[error("Exceeded max characters for this field. Must be under: {length}")]
TooManyCharacters { length: usize },
}

impl RetryableError for CommitValidationError {
Expand Down Expand Up @@ -223,6 +227,7 @@ impl MetadataFieldChange {
* 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 {
Expand Down Expand Up @@ -258,6 +263,36 @@ impl ValidatedCommit {
new_group_extensions,
)?;

// Enforce character limits for specific metadata fields
for field_change in &metadata_changes.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.
Expand Down

0 comments on commit 52dbbff

Please sign in to comment.