diff --git a/protocol/identity/social_links.go b/protocol/identity/social_links.go index 0322feda5..daa7b1a0e 100644 --- a/protocol/identity/social_links.go +++ b/protocol/identity/social_links.go @@ -55,21 +55,6 @@ func (s SocialLinks) Equals(rhs SocialLinks) bool { return true } -func (s SocialLinks) EqualsProtobuf(rhs []*protobuf.SocialLink) bool { - if len(s) != len(rhs) { - return false - } - sort.Slice(s, func(i, j int) bool { return s[i].Text < s[j].Text }) - sort.Slice(rhs, func(i, j int) bool { return rhs[i].Text < rhs[j].Text }) - for i := range s { - if s[i].Text != rhs[i].Text || s[i].URL != rhs[i].Url { - return false - } - } - - return true -} - func (s *SocialLinks) Serialize() ([]byte, error) { return json.Marshal(s) } diff --git a/protocol/identity/social_links_test.go b/protocol/identity/social_links_test.go index bd280b83a..a2ec65c59 100644 --- a/protocol/identity/social_links_test.go +++ b/protocol/identity/social_links_test.go @@ -20,16 +20,19 @@ func TestEquals(t *testing.T) { }, } - other := []*protobuf.SocialLink{} - require.False(t, socialLinks.EqualsProtobuf(other)) + protobufLinks := []*protobuf.SocialLink{} + transformedLinks := NewSocialLinks(protobufLinks) + require.False(t, socialLinks.Equals(*transformedLinks)) - other = append(other, &protobuf.SocialLink{Text: "A", Url: "B"}) - other = append(other, &protobuf.SocialLink{Text: "X", Url: "Y"}) - require.True(t, socialLinks.EqualsProtobuf(other)) + protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "A", Url: "B"}) + protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"}) + transformedLinks = NewSocialLinks(protobufLinks) + require.True(t, socialLinks.Equals(*transformedLinks)) // order does not matter - other = []*protobuf.SocialLink{} - other = append(other, &protobuf.SocialLink{Text: "X", Url: "Y"}) - other = append(other, &protobuf.SocialLink{Text: "A", Url: "B"}) - require.True(t, socialLinks.EqualsProtobuf(other)) + protobufLinks = []*protobuf.SocialLink{} + protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "X", Url: "Y"}) + protobufLinks = append(protobufLinks, &protobuf.SocialLink{Text: "A", Url: "B"}) + transformedLinks = NewSocialLinks(protobufLinks) + require.True(t, socialLinks.Equals(*transformedLinks)) } diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index 2624cb5f3..66d2d67a5 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -1887,13 +1887,22 @@ func (m *Messenger) HandleChatIdentity(state *ReceivedMessageState, ci protobuf. contactModified = true } + if err = ValidateBio(&ci.Description); err != nil { + return err + } + if contact.Bio != ci.Description { contact.Bio = ci.Description contactModified = true } - if !contact.SocialLinks.EqualsProtobuf(ci.SocialLinks) { - contact.SocialLinks = *identity.NewSocialLinks(ci.SocialLinks) + socialLinks := identity.NewSocialLinks(ci.SocialLinks) + if err = ValidateSocialLinks(socialLinks); err != nil { + return err + } + + if !contact.SocialLinks.Equals(*socialLinks) { + contact.SocialLinks = *socialLinks contactModified = true } } diff --git a/protocol/messenger_identity.go b/protocol/messenger_identity.go index e2b69cb94..8d479ff89 100644 --- a/protocol/messenger_identity.go +++ b/protocol/messenger_identity.go @@ -10,9 +10,16 @@ import ( "github.com/status-im/status-go/protocol/identity/alias" ) +const ( + maxBioLength = 240 + maxSocialLinkTextLength = 24 +) + var ErrInvalidDisplayNameRegExp = errors.New("only letters, numbers, underscores and hyphens allowed") var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`) var ErrInvalidDisplayNameNotAllowed = errors.New("name is not allowed") +var ErrInvalidBioLength = errors.New("invalid bio length") +var ErrInvalidSocialLinkTextLength = errors.New("invalid social link text length") func ValidateDisplayName(displayName *string) error { name := strings.TrimSpace(*displayName) @@ -72,6 +79,13 @@ func (m *Messenger) SetDisplayName(displayName string) error { return m.publishContactCode() } +func ValidateBio(bio *string) error { + if len(*bio) > maxBioLength { + return ErrInvalidBioLength + } + return nil +} + func (m *Messenger) SetBio(bio string) error { currentBio, err := m.settings.Bio() if err != nil { @@ -82,21 +96,30 @@ func (m *Messenger) SetBio(bio string) error { return nil // Do nothing } - // TODO: add validation - - err = m.settings.SaveSettingField(settings.Bio, bio) - if err != nil { + if err = ValidateBio(&bio); err != nil { return err } - err = m.resetLastPublishedTimeForChatIdentity() - if err != nil { + if err = m.settings.SaveSettingField(settings.Bio, bio); err != nil { + return err + } + + if err = m.resetLastPublishedTimeForChatIdentity(); err != nil { return err } return m.publishContactCode() } +func ValidateSocialLinks(socialLinks *identity.SocialLinks) error { + for _, link := range *socialLinks { + if len(link.Text) > maxSocialLinkTextLength { + return ErrInvalidSocialLinkTextLength + } + } + return nil +} + func (m *Messenger) SetSocialLinks(socialLinks *identity.SocialLinks) error { currentSocialLinks, err := m.settings.GetSocialLinks() if err != nil { @@ -107,15 +130,15 @@ func (m *Messenger) SetSocialLinks(socialLinks *identity.SocialLinks) error { return nil // Do nothing } - // TODO: add validation - - err = m.settings.SetSocialLinks(socialLinks) - if err != nil { + if err = ValidateSocialLinks(socialLinks); err != nil { return err } - err = m.resetLastPublishedTimeForChatIdentity() - if err != nil { + if err = m.settings.SetSocialLinks(socialLinks); err != nil { + return err + } + + if err = m.resetLastPublishedTimeForChatIdentity(); err != nil { return err }