github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/chat/msgchecker/boxed_checker.go (about) 1 package msgchecker 2 3 import ( 4 "fmt" 5 6 "github.com/keybase/client/go/protocol/chat1" 7 ) 8 9 type MessageBoxedLengthExceedingError struct { 10 DescriptibleItemName string 11 ActualLength int 12 MaxLength int 13 } 14 15 func (e MessageBoxedLengthExceedingError) Error() string { 16 return fmt.Sprintf("%s exceeds the maximum length (%v > %v)", e.DescriptibleItemName, e.ActualLength, e.MaxLength) 17 } 18 19 func boxedFieldLengthChecker(descriptibleItemName string, actualLength int, maxLength int) error { 20 if actualLength > maxLength { 21 return MessageBoxedLengthExceedingError{ 22 DescriptibleItemName: descriptibleItemName, 23 ActualLength: actualLength, 24 MaxLength: maxLength, 25 } 26 } 27 return nil 28 } 29 30 func checkMessageBoxedLength(msg chat1.MessageBoxed) error { 31 textMsgLength := getBoxedMaxTextLength(msg.ClientHeader.Conv.TopicType) 32 switch msg.GetMessageType() { 33 case chat1.MessageType_ATTACHMENT, 34 chat1.MessageType_DELETE, 35 chat1.MessageType_NONE, 36 chat1.MessageType_TLFNAME, 37 chat1.MessageType_ATTACHMENTUPLOADED, 38 chat1.MessageType_PIN: 39 return boxedFieldLengthChecker("sanity check", len(msg.BodyCiphertext.E), BoxedSanityLength) 40 case chat1.MessageType_TEXT, chat1.MessageType_FLIP, chat1.MessageType_UNFURL: 41 return boxedFieldLengthChecker("TEXT message", len(msg.BodyCiphertext.E), textMsgLength) 42 case chat1.MessageType_EDIT: 43 return boxedFieldLengthChecker("EDIT message", len(msg.BodyCiphertext.E), textMsgLength) 44 case chat1.MessageType_REACTION: 45 return boxedFieldLengthChecker("REACTION message", len(msg.BodyCiphertext.E), BoxedReactionMessageBodyMaxLength) 46 case chat1.MessageType_HEADLINE: 47 return boxedFieldLengthChecker("HEADLINE message", len(msg.BodyCiphertext.E), BoxedHeadlineMessageBodyMaxLength) 48 case chat1.MessageType_METADATA: 49 return boxedFieldLengthChecker("METADATA message", len(msg.BodyCiphertext.E), BoxedMetadataMessageBodyMaxLength) 50 case chat1.MessageType_JOIN: 51 return boxedFieldLengthChecker("JOIN message", len(msg.BodyCiphertext.E), BoxedJoinMessageBodyMaxLength) 52 case chat1.MessageType_LEAVE: 53 return boxedFieldLengthChecker("LEAVE message", len(msg.BodyCiphertext.E), BoxedLeaveMessageBodyMaxLength) 54 case chat1.MessageType_SYSTEM: 55 return boxedFieldLengthChecker("SYSTEM message", len(msg.BodyCiphertext.E), 56 BoxedSystemMessageBodyMaxLength) 57 case chat1.MessageType_DELETEHISTORY: 58 return boxedFieldLengthChecker("DELETEHISTORY message", len(msg.BodyCiphertext.E), 59 BoxedDeleteHistoryMessageBodyMaxLength) 60 case chat1.MessageType_SENDPAYMENT: 61 return boxedFieldLengthChecker("SENDPAYMENT message", len(msg.BodyCiphertext.E), 62 BoxedSendPaymentMessageBodyMaxLength) 63 case chat1.MessageType_REQUESTPAYMENT: 64 return boxedFieldLengthChecker("REQUESTPAYMENT message", len(msg.BodyCiphertext.E), 65 BoxedRequestPaymentMessageBodyMaxLength) 66 default: 67 return fmt.Errorf("unknown message type: %v", msg.GetMessageType()) 68 } 69 } 70 71 func CheckMessageBoxed(msg chat1.MessageBoxed) error { 72 return checkMessageBoxedLength(msg) 73 }