@@ -4,21 +4,34 @@ import (
44 "context"
55 "errors"
66 "fmt"
7+ "image/gif"
78 "io/ioutil"
89 "os"
910 "strings"
1011 "sync"
1112 "time"
1213
14+ "camlistore.org/pkg/images"
1315 "github.com/keybase/client/go/chat/attachments"
1416 "github.com/keybase/client/go/chat/globals"
17+ "github.com/keybase/client/go/chat/storage"
1518 "github.com/keybase/client/go/chat/types"
1619 "github.com/keybase/client/go/chat/utils"
1720 "github.com/keybase/client/go/libkb"
1821 "github.com/keybase/client/go/protocol/chat1"
1922 "github.com/keybase/client/go/protocol/gregor1"
2023 "github.com/keybase/client/go/protocol/keybase1"
21- "github.com/kyokomi/emoji"
24+ )
25+
26+ const (
27+ minShortNameLength = 2
28+ maxShortNameLength = 48
29+ minEmojiSize = 512 // min size for reading mime type
30+ maxEmojiSize = 256 * 1000 // 256kb
31+ minEmojiWidth = 16
32+ minEmojiHeight = 16
33+ maxEmojiWidth = 128
34+ maxEmojiHeight = 128
2235)
2336
2437type DevConvEmojiSource struct {
@@ -91,28 +104,86 @@ func (s *DevConvEmojiSource) addAdvanced(ctx context.Context, uid gregor1.UID,
91104}
92105
93106func (s * DevConvEmojiSource ) isStockEmoji (alias string ) bool {
94- _ , ok := emoji .CodeMap ()[":" + alias + ":" ]
95- if ! ok {
96- _ , ok = emoji .CodeMap ()[":" + strings .ReplaceAll (alias , "-" , "_" )+ ":" ]
107+ alias = fmt .Sprintf (":%s:" , alias )
108+ alias2 := strings .ReplaceAll (alias , "-" , "_" )
109+ return storage .EmojiExists (alias ) || storage .EmojiExists (alias2 )
110+ }
111+
112+ func (s * DevConvEmojiSource ) validateShortName (shortName string ) (string , error ) {
113+ shortName = strings .ReplaceAll (shortName , ":" , "" ) // drop any colons from alias
114+ if s .isStockEmoji (shortName ) {
115+ return "" , errors .New ("cannot use existing stock emoji short name" )
116+ }
117+ if len (shortName ) > maxShortNameLength || len (shortName ) < minShortNameLength {
118+ return "" , fmt .Errorf ("short name %q (length %d) not within bounds %d,%d" ,
119+ shortName , len (shortName ), minShortNameLength , maxShortNameLength )
97120 }
98- return ok
121+ if strings .Contains (shortName , "#" ) {
122+ return "" , errors .New ("invalid character in emoji alias" )
123+ }
124+ return shortName , nil
99125}
100126
101- func (s * DevConvEmojiSource ) validateAlias ( alias string ) (string , error ) {
102- alias = strings . ReplaceAll ( alias , ":" , "" ) // drop any colons from alias
103- if strings . Contains ( alias , "#" ) {
104- return alias , errors . New ( "invalid character in emoji alias" )
127+ func (s * DevConvEmojiSource ) validateCustomEmoji ( ctx context. Context , shortName , filename string ) (string , error ) {
128+ shortName , err := s . validateShortName ( shortName )
129+ if err != nil {
130+ return "" , err
105131 }
106- if s .isStockEmoji (alias ) {
107- return alias , errors .New ("cannot use existing stock emoji alias" )
132+
133+ err = s .validateFile (ctx , filename )
134+ if err != nil {
135+ return "" , err
108136 }
109- return alias , nil
137+ return shortName , nil
138+ }
139+
140+ // validateFile validates the following:
141+ // file size
142+ // dimensions
143+ // format
144+ func (s * DevConvEmojiSource ) validateFile (ctx context.Context , filename string ) error {
145+ finfo , err := attachments .StatOSOrKbfsFile (ctx , s .G ().GlobalContext , filename )
146+ if err != nil {
147+ return err
148+ }
149+ if finfo .IsDir () {
150+ return errors .New ("invalid file type for emoji" )
151+ } else if finfo .Size () > maxEmojiSize || finfo .Size () < minEmojiSize {
152+ return fmt .Errorf ("emoji size %d not within bounds %d,%d" , finfo .Size (), minEmojiSize , maxEmojiSize )
153+ }
154+
155+ src , err := attachments .NewReadCloseResetter (ctx , s .G ().GlobalContext , filename )
156+ if err != nil {
157+ return err
158+ }
159+ defer func () { src .Close () }()
160+ img , _ , err := images .Decode (src , nil )
161+ if err != nil {
162+ if err := src .Reset (); err != nil {
163+ return err
164+ }
165+ g , err := gif .DecodeAll (src )
166+ if err != nil {
167+ return err
168+ }
169+ if len (g .Image ) == 0 {
170+ return errors .New ("no image frames in GIF" )
171+ }
172+ img = g .Image [0 ]
173+ }
174+ bounds := img .Bounds ()
175+ if bounds .Dx () > maxEmojiWidth || bounds .Dx () < minEmojiWidth ||
176+ bounds .Dy () > maxEmojiHeight || bounds .Dy () < minEmojiHeight {
177+ return fmt .Errorf ("invalid dimensions %dx%d not within %dx%d, %dx%d" ,
178+ bounds .Dx (), bounds .Dy (), maxEmojiWidth , maxEmojiHeight , minEmojiWidth , minEmojiHeight )
179+ }
180+ return nil
110181}
111182
112183func (s * DevConvEmojiSource ) Add (ctx context.Context , uid gregor1.UID , convID chat1.ConversationID ,
113184 alias , filename string ) (res chat1.EmojiRemoteSource , err error ) {
114185 defer s .Trace (ctx , func () error { return err }, "Add" )()
115- if alias , err = s .validateAlias ( alias ); err != nil {
186+ if alias , err = s .validateCustomEmoji ( ctx , alias , filename ); err != nil {
116187 return res , err
117188 }
118189 storage := s .makeStorage (chat1 .TopicType_EMOJI )
@@ -122,7 +193,7 @@ func (s *DevConvEmojiSource) Add(ctx context.Context, uid gregor1.UID, convID ch
122193func (s * DevConvEmojiSource ) AddAlias (ctx context.Context , uid gregor1.UID , convID chat1.ConversationID ,
123194 newAlias , existingAlias string ) (res chat1.EmojiRemoteSource , err error ) {
124195 defer s .Trace (ctx , func () error { return err }, "AddAlias" )()
125- if newAlias , err = s .validateAlias (newAlias ); err != nil {
196+ if newAlias , err = s .validateShortName (newAlias ); err != nil {
126197 return res , err
127198 }
128199 var stored chat1.EmojiStorage
0 commit comments