Skip to content

Commit 5f6228b

Browse files
authored
sanity check emoji inputs (keybase#23453)
* sanity check emoji inputs * min length check * tweaks * fix tests * review feedback * check err
1 parent e3e832a commit 5f6228b

4 files changed

Lines changed: 98 additions & 27 deletions

File tree

‎go/chat/emojisource.go‎

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2437
type DevConvEmojiSource struct {
@@ -91,28 +104,86 @@ func (s *DevConvEmojiSource) addAdvanced(ctx context.Context, uid gregor1.UID,
91104
}
92105

93106
func (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

112183
func (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
122193
func (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

‎go/chat/emojisource_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestEmojiSourceBasic(t *testing.T) {
7272
uploader := attachments.NewUploader(tc.Context(), store, mockSigningRemote{},
7373
func() chat1.RemoteInterface { return ri }, 1)
7474
tc.ChatG.AttachmentUploader = uploader
75-
filename := "./testdata/ship.jpg"
75+
filename := "./testdata/party_parrot.gif"
7676

7777
conv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT,
7878
chat1.ConversationMembersType_IMPTEAMNATIVE)
@@ -262,7 +262,7 @@ func TestEmojiSourceCrossTeam(t *testing.T) {
262262
uploader := attachments.NewUploader(tc.Context(), store, mockSigningRemote{},
263263
func() chat1.RemoteInterface { return ri }, 1)
264264
tc.ChatG.AttachmentUploader = uploader
265-
filename := "./testdata/ship.jpg"
265+
filename := "./testdata/party_parrot.gif"
266266
t.Logf("uid1: %s", uid1)
267267

268268
aloneConv := mustCreateConversationForTest(t, ctc, users[0], chat1.TopicType_CHAT,

‎go/chat/storage/reacjis.go‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ const (
2626
// If the user has less than 5 favorite reacjis we stuff these defaults in.
2727
var DefaultTopReacjis = []string{":+1:", ":-1:", ":joy:", ":sunglasses:", ":tada:"}
2828

29-
// RevCodeMap gets the underlying map of emoji.
30-
func RevCodeMap() map[string][]string {
31-
return emojiRevCodeMap
32-
}
33-
34-
func AliasList(shortCode string) []string {
29+
func EmojiAliasList(shortCode string) []string {
3530
return emojiRevCodeMap[emojiCodeMap[shortCode]]
3631
}
3732

38-
// HasAlias flags if the given `shortCode` has multiple aliases with other
33+
// EmojiHasAlias flags if the given `shortCode` has multiple aliases with other
3934
// codes.
40-
func HasAlias(shortCode string) bool {
41-
return len(AliasList(shortCode)) > 1
35+
func EmojiHasAlias(shortCode string) bool {
36+
return len(EmojiAliasList(shortCode)) > 1
37+
}
38+
39+
// EmojiExists flags if the given `shortCode` is a valid emoji
40+
func EmojiExists(shortCode string) bool {
41+
return len(EmojiAliasList(shortCode)) > 0
4242
}
4343

4444
// NormalizeShortCode normalizes a given `shortCode` to a deterministic alias.
4545
func NormalizeShortCode(shortCode string) string {
46-
shortLists := AliasList(shortCode)
46+
shortLists := EmojiAliasList(shortCode)
4747
if len(shortLists) == 0 {
4848
return shortCode
4949
}
@@ -236,7 +236,7 @@ func (s *ReacjiStore) populateCacheLocked(ctx context.Context, uid gregor1.UID)
236236
func (s *ReacjiStore) PutReacji(ctx context.Context, uid gregor1.UID, shortCode string) error {
237237
s.Lock()
238238
defer s.Unlock()
239-
if !(HasAlias(shortCode) || globals.EmojiPattern.MatchString(shortCode)) {
239+
if !(EmojiHasAlias(shortCode) || globals.EmojiPattern.MatchString(shortCode)) {
240240
return nil
241241
}
242242
cache := s.populateCacheLocked(ctx, uid)

‎go/chat/testdata/party_parrot.gif‎

4.48 KB
Loading

0 commit comments

Comments
 (0)