forked from cwtch.im/cwtch
Adding maxlength check
This commit is contained in:
parent
746c5397fd
commit
1e2ecb8e88
|
@ -36,6 +36,10 @@ type Profile struct {
|
|||
lock sync.Mutex
|
||||
}
|
||||
|
||||
// MaxGroupMessageLength is the maximum length of a message posted to a server group.
|
||||
// TODO: Should this be per server?
|
||||
const MaxGroupMessageLength = 1024
|
||||
|
||||
func (p *PublicProfile) init() {
|
||||
p.Attributes = make(map[string]string)
|
||||
}
|
||||
|
@ -354,6 +358,11 @@ func getRandomness(arr *[]byte) {
|
|||
// EncryptMessageToGroup when given a message and a group, encrypts and signs the message under the group and
|
||||
// profile
|
||||
func (p *Profile) EncryptMessageToGroup(message string, groupID string) ([]byte, []byte, error) {
|
||||
|
||||
if len(message) > MaxGroupMessageLength {
|
||||
return nil, nil, errors.New("group message is too long")
|
||||
}
|
||||
|
||||
group := p.GetGroupByGroupID(groupID)
|
||||
if group != nil {
|
||||
timestamp := time.Now().Unix()
|
||||
|
@ -365,7 +374,7 @@ func (p *Profile) EncryptMessageToGroup(message string, groupID string) ([]byte,
|
|||
prevSig = group.SignedGroupID
|
||||
}
|
||||
|
||||
lenPadding := 1024 - len(message)
|
||||
lenPadding := MaxGroupMessageLength - len(message)
|
||||
padding := make([]byte, lenPadding)
|
||||
getRandomness(&padding)
|
||||
|
||||
|
|
|
@ -151,6 +151,11 @@ func TestProfileGroup(t *testing.T) {
|
|||
t.Errorf("Initial Message was not stored properly: %v", im)
|
||||
}
|
||||
|
||||
_, _, err := sarah.EncryptMessageToGroup(string(make([]byte, MaxGroupMessageLength*2)), group2.GroupID)
|
||||
if err == nil {
|
||||
t.Errorf("Overly long message should have returned an error")
|
||||
}
|
||||
|
||||
bob := GenerateNewProfile("bob")
|
||||
bob.AddContact(alice.Onion, &alice.PublicProfile)
|
||||
bob.ProcessInvite(gci2.GetGroupChatInvite(), alice.Onion)
|
||||
|
|
Loading…
Reference in New Issue