2019-12-02 15:34:05 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-09-01 14:38:36 +00:00
|
|
|
"fmt"
|
2020-01-10 18:59:01 +00:00
|
|
|
"strconv"
|
2019-12-02 15:34:05 +00:00
|
|
|
"strings"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2020-01-20 16:44:32 +00:00
|
|
|
"github.com/status-im/status-go/protocol/v1"
|
2019-12-02 15:34:05 +00:00
|
|
|
)
|
|
|
|
|
2020-09-01 14:38:36 +00:00
|
|
|
const maxChatMessageTextLength = 4096
|
2021-07-22 17:41:49 +00:00
|
|
|
const maxStatusMessageText = 128
|
2020-09-01 14:38:36 +00:00
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
// maxWhisperDrift is how many milliseconds we allow the clock value to differ
|
|
|
|
// from whisperTimestamp
|
|
|
|
const maxWhisperFutureDriftMs uint64 = 120000
|
|
|
|
|
|
|
|
func validateClockValue(clock uint64, whisperTimestamp uint64) error {
|
|
|
|
if clock == 0 {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("clock can't be 0")
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
if clock > whisperTimestamp && clock-whisperTimestamp > maxWhisperFutureDriftMs {
|
|
|
|
return errors.New("clock value too high")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateMembershipUpdateMessage(message *protocol.MembershipUpdateMessage, timeNowMs uint64) error {
|
|
|
|
|
|
|
|
for _, e := range message.Events {
|
|
|
|
if err := validateClockValue(e.ClockValue, timeNowMs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
func ValidateStatusUpdate(message protobuf.StatusUpdate) error {
|
|
|
|
if message.Clock == 0 {
|
|
|
|
return errors.New("clock can't be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.StatusType == protobuf.StatusUpdate_UNKNOWN_STATUS_TYPE {
|
|
|
|
return errors.New("unknown status type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len([]rune(message.CustomText)) > maxStatusMessageText {
|
|
|
|
return fmt.Errorf("custom text shouldn't be longer than %d", maxStatusMessageText)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-25 10:20:40 +00:00
|
|
|
func ValidateEditMessage(message protobuf.EditMessage) error {
|
|
|
|
if message.Clock == 0 {
|
|
|
|
return errors.New("clock can't be 0")
|
|
|
|
}
|
|
|
|
if len(message.ChatId) == 0 {
|
|
|
|
return errors.New("chat-id can't be empty")
|
|
|
|
}
|
|
|
|
if len(message.MessageId) == 0 {
|
|
|
|
return errors.New("message-id can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.MessageType == protobuf.MessageType_UNKNOWN_MESSAGE_TYPE || message.MessageType == protobuf.MessageType_SYSTEM_MESSAGE_PRIVATE_GROUP {
|
|
|
|
return errors.New("unknown message type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ValidateText(message.Text)
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:06:32 +00:00
|
|
|
func ValidateDeleteMessage(message protobuf.DeleteMessage) error {
|
|
|
|
if len(message.ChatId) == 0 {
|
|
|
|
return errors.New("chat-id can't be empty")
|
|
|
|
}
|
|
|
|
if len(message.MessageId) == 0 {
|
|
|
|
return errors.New("message-id can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.MessageType == protobuf.MessageType_UNKNOWN_MESSAGE_TYPE || message.MessageType == protobuf.MessageType_SYSTEM_MESSAGE_PRIVATE_GROUP {
|
|
|
|
return errors.New("unknown message type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:42:17 +00:00
|
|
|
func ValidateDeleteForMeMessage(message protobuf.DeleteForMeMessage) error {
|
|
|
|
if len(message.MessageId) == 0 {
|
|
|
|
return errors.New("message-id can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedPairInstallation(message *protobuf.PairInstallation, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
if len(strings.TrimSpace(message.Name)) == 0 {
|
|
|
|
return errors.New("name can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(message.DeviceType)) == 0 {
|
|
|
|
return errors.New("device type can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(message.InstallationId)) == 0 {
|
|
|
|
return errors.New("installationId can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedSendTransaction(message *protobuf.SendTransaction, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(message.TransactionHash)) == 0 {
|
|
|
|
return errors.New("transaction hash can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.Signature == nil {
|
|
|
|
return errors.New("signature can't be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedRequestAddressForTransaction(message *protobuf.RequestAddressForTransaction, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(message.Value)) == 0 {
|
|
|
|
return errors.New("value can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := strconv.ParseFloat(message.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedRequestTransaction(message *protobuf.RequestTransaction, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(message.Value)) == 0 {
|
|
|
|
return errors.New("value can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(message.Address)) == 0 {
|
|
|
|
return errors.New("address can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := strconv.ParseFloat(message.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedAcceptRequestAddressForTransaction(message *protobuf.AcceptRequestAddressForTransaction, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(message.Id) == 0 {
|
|
|
|
return errors.New("messageID can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(strings.TrimSpace(message.Address)) == 0 {
|
|
|
|
return errors.New("address can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedDeclineRequestAddressForTransaction(message *protobuf.DeclineRequestAddressForTransaction, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(message.Id) == 0 {
|
|
|
|
return errors.New("messageID can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedDeclineRequestTransaction(message *protobuf.DeclineRequestTransaction, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(message.Id) == 0 {
|
|
|
|
return errors.New("messageID can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 10:15:37 +00:00
|
|
|
func ValidateText(text string) error {
|
|
|
|
if len(strings.TrimSpace(text)) == 0 {
|
|
|
|
return errors.New("text can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len([]rune(text)) > maxChatMessageTextLength {
|
|
|
|
return fmt.Errorf("text shouldn't be longer than %d", maxChatMessageTextLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
func ValidateReceivedChatMessage(message *protobuf.ChatMessage, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if message.Timestamp == 0 {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("timestamp can't be 0")
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 16:06:50 +00:00
|
|
|
if message.ContentType != protobuf.ChatMessage_DISCORD_MESSAGE && (message.ContentType != protobuf.ChatMessage_IMAGE || message.Text != "") {
|
2022-09-29 11:50:23 +00:00
|
|
|
if err := ValidateText(message.Text); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-01 14:38:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
if len(message.ChatId) == 0 {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("chatId can't be empty")
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 11:46:43 +00:00
|
|
|
if message.MessageType == protobuf.MessageType_UNKNOWN_MESSAGE_TYPE || message.MessageType == protobuf.MessageType_SYSTEM_MESSAGE_PRIVATE_GROUP {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("unknown message type")
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 13:00:58 +00:00
|
|
|
switch message.ContentType {
|
|
|
|
case protobuf.ChatMessage_UNKNOWN_CONTENT_TYPE:
|
|
|
|
return errors.New("unknown content type")
|
|
|
|
|
|
|
|
case protobuf.ChatMessage_TRANSACTION_COMMAND:
|
|
|
|
return errors.New("can't receive request address for transaction from others")
|
|
|
|
|
|
|
|
case protobuf.ChatMessage_STICKER:
|
2019-12-02 15:34:05 +00:00
|
|
|
if message.Payload == nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("no sticker content")
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
sticker := message.GetSticker()
|
|
|
|
if sticker == nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("no sticker content")
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
if len(sticker.Hash) == 0 {
|
2020-01-10 18:59:01 +00:00
|
|
|
return errors.New("sticker hash not set")
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
2020-05-13 13:16:17 +00:00
|
|
|
|
2020-07-10 13:00:58 +00:00
|
|
|
case protobuf.ChatMessage_IMAGE:
|
2020-05-13 13:16:17 +00:00
|
|
|
if message.Payload == nil {
|
|
|
|
return errors.New("no image content")
|
|
|
|
}
|
|
|
|
image := message.GetImage()
|
|
|
|
if image == nil {
|
|
|
|
return errors.New("no image content")
|
|
|
|
}
|
|
|
|
if len(image.Payload) == 0 {
|
|
|
|
return errors.New("image payload empty")
|
|
|
|
}
|
2020-10-26 13:01:46 +00:00
|
|
|
if image.Type == protobuf.ImageType_UNKNOWN_IMAGE_TYPE {
|
2020-05-13 13:16:17 +00:00
|
|
|
return errors.New("image type unknown")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 18:55:49 +00:00
|
|
|
if message.ContentType == protobuf.ChatMessage_AUDIO {
|
|
|
|
if message.Payload == nil {
|
|
|
|
return errors.New("no audio content")
|
|
|
|
}
|
|
|
|
audio := message.GetAudio()
|
|
|
|
if audio == nil {
|
|
|
|
return errors.New("no audio content")
|
|
|
|
}
|
|
|
|
if len(audio.Payload) == 0 {
|
|
|
|
return errors.New("audio payload empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if audio.Type == protobuf.AudioMessage_UNKNOWN_AUDIO_TYPE {
|
|
|
|
return errors.New("audio type unknown")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:13:10 +00:00
|
|
|
if err := ValidateDisplayName(&message.DisplayName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-28 07:53:32 +00:00
|
|
|
|
|
|
|
func ValidateReceivedEmojiReaction(emoji *protobuf.EmojiReaction, whisperTimestamp uint64) error {
|
|
|
|
if err := validateClockValue(emoji.Clock, whisperTimestamp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(emoji.MessageId) == 0 {
|
|
|
|
return errors.New("message-id can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(emoji.ChatId) == 0 {
|
|
|
|
return errors.New("chat-id can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if emoji.Type == protobuf.EmojiReaction_UNKNOWN_EMOJI_REACTION_TYPE {
|
|
|
|
return errors.New("unknown emoji reaction type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if emoji.MessageType == protobuf.MessageType_UNKNOWN_MESSAGE_TYPE {
|
|
|
|
return errors.New("unknown message type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-07 13:49:37 +00:00
|
|
|
|
|
|
|
func ValidateReceivedGroupChatInvitation(invitation *protobuf.GroupChatInvitation) error {
|
|
|
|
|
|
|
|
if len(invitation.ChatId) == 0 {
|
|
|
|
return errors.New("chat-id can't be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|