-
Notifications
You must be signed in to change notification settings - Fork 244
/
message_validator.go
326 lines (253 loc) · 8.13 KB
/
message_validator.go
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
package protocol
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/v1"
)
const maxChatMessageTextLength = 4096
const maxStatusMessageText = 128
// 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 {
return errors.New("clock can't be 0")
}
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
}
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
}
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)
}
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
}
func ValidateReceivedPairInstallation(message *protobuf.PairInstallation, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
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
}
func ValidateReceivedSendTransaction(message *protobuf.SendTransaction, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
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
}
func ValidateReceivedRequestAddressForTransaction(message *protobuf.RequestAddressForTransaction, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
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
}
func ValidateReceivedRequestTransaction(message *protobuf.RequestTransaction, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
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
}
func ValidateReceivedAcceptRequestAddressForTransaction(message *protobuf.AcceptRequestAddressForTransaction, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
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
}
func ValidateReceivedDeclineRequestAddressForTransaction(message *protobuf.DeclineRequestAddressForTransaction, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
if len(message.Id) == 0 {
return errors.New("messageID can't be empty")
}
return nil
}
func ValidateReceivedDeclineRequestTransaction(message *protobuf.DeclineRequestTransaction, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
if len(message.Id) == 0 {
return errors.New("messageID can't be empty")
}
return nil
}
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
}
func ValidateReceivedChatMessage(message *protobuf.ChatMessage, whisperTimestamp uint64) error {
if err := validateClockValue(message.Clock, whisperTimestamp); err != nil {
return err
}
if message.Timestamp == 0 {
return errors.New("timestamp can't be 0")
}
if err := ValidateText(message.Text); err != nil {
return err
}
if len(message.ChatId) == 0 {
return errors.New("chatId 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")
}
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:
if message.Payload == nil {
return errors.New("no sticker content")
}
sticker := message.GetSticker()
if sticker == nil {
return errors.New("no sticker content")
}
if len(sticker.Hash) == 0 {
return errors.New("sticker hash not set")
}
case protobuf.ChatMessage_IMAGE:
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")
}
if image.Type == protobuf.ImageType_UNKNOWN_IMAGE_TYPE {
return errors.New("image type unknown")
}
}
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")
}
}
return nil
}
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
}
func ValidateReceivedGroupChatInvitation(invitation *protobuf.GroupChatInvitation) error {
if len(invitation.ChatId) == 0 {
return errors.New("chat-id can't be empty")
}
return nil
}