Skip to content

Commit fbf3ad8

Browse files
authored
Fix multiple word command matching for restricted bots (keybase#20469)
1 parent 1afa7d6 commit fbf3ad8

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

‎go/chat/bots/utils.go‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"fmt"
66
"regexp"
7-
"strings"
7+
"sort"
88

99
"github.com/keybase/client/go/chat/globals"
1010
"github.com/keybase/client/go/chat/utils"
@@ -30,6 +30,15 @@ func MakeConversationCommandGroups(cmds []chat1.UserBotCommandOutput) chat1.Conv
3030
})
3131
}
3232

33+
func SortCommandsForMatching(cmds []chat1.UserBotCommandOutput) {
34+
// sort commands by reverse command length to prefer specificity (i.e. if
35+
// there's a longer command that matches the prefix, we'll match that
36+
// first.)
37+
sort.SliceStable(cmds, func(i, j int) bool {
38+
return len(cmds[i].Name) > len(cmds[j].Name)
39+
})
40+
}
41+
3342
func ApplyTeamBotSettings(ctx context.Context, g *globals.Context, botUID gregor1.UID,
3443
botSettings keybase1.TeamBotSettings,
3544
msg chat1.MessagePlaintext, convID *chat1.ConversationID,
@@ -114,8 +123,9 @@ func ApplyTeamBotSettings(ctx context.Context, g *globals.Context, botUID gregor
114123
if err != nil {
115124
return false, nil
116125
}
126+
SortCommandsForMatching(cmds)
117127
for _, cmd := range cmds {
118-
if unn.String() == cmd.Username && strings.HasPrefix(matchText, fmt.Sprintf("!%s", cmd.Name)) {
128+
if unn.String() == cmd.Username && cmd.Matches(matchText) {
119129
return true, nil
120130
}
121131
}

‎go/chat/bots/utils_test.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,32 @@ func TestApplyTeamBotSettings(t *testing.T) {
125125
g.BotCommandManager = &MockBotCommandManager{}
126126
mockCmdOutput = []chat1.UserBotCommandOutput{
127127
{
128-
Name: "status",
128+
Name: "remind me",
129129
Username: "botua",
130130
},
131131
}
132132
msg.MessageBody = chat1.NewMessageBodyWithText(chat1.MessageText{
133-
Body: "!status",
133+
Body: "!remind me ",
134134
})
135135
assertMatch(false)
136136
botSettings.Cmds = true
137137
assertMatch(true)
138+
139+
// make sure we only match if the given bot username also matches
140+
mockCmdOutput = []chat1.UserBotCommandOutput{
141+
{
142+
Name: "remind me",
143+
Username: "notbotua",
144+
},
145+
}
146+
msg.MessageBody = chat1.NewMessageBodyWithText(chat1.MessageText{
147+
Body: "!remind me ",
148+
})
149+
assertMatch(false)
150+
151+
// make sure we don't match an erroneous command
138152
msg.MessageBody = chat1.NewMessageBodyWithText(chat1.MessageText{
139-
Body: "!help",
153+
Body: "!help ",
140154
})
141155
assertMatch(false)
142156
}

‎go/chat/commands/bot.go‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package commands
33
import (
44
"context"
55
"errors"
6-
"sort"
76
"strings"
87
"sync"
98

9+
"github.com/keybase/client/go/chat/bots"
1010
"github.com/keybase/client/go/chat/globals"
1111
"github.com/keybase/client/go/protocol/chat1"
1212
"github.com/keybase/client/go/protocol/gregor1"
@@ -68,19 +68,13 @@ func (b *Bot) Preview(ctx context.Context, uid gregor1.UID, convID chat1.Convers
6868
return
6969
}
7070

71-
// sort commands by reverse command length to prefer specificity
72-
// (i.e. if there's a longer command that matches the prefix, show the help text for that one)
73-
sort.SliceStable(cmds, func(i, j int) bool {
74-
l := cmds[i]
75-
r := cmds[j]
76-
return len(l.Name) > len(r.Name)
77-
})
71+
bots.SortCommandsForMatching(cmds)
7872

7973
// Since we have a list of all valid commands for this conversation, don't do any tokenizing
8074
// Instead, just check if any valid bot command (followed by a space) is a prefix of this message
8175
for _, cmd := range cmds {
8276
// If we decide to support the !<command>@<username> syntax, we can just add another check here
83-
if strings.HasPrefix(text, "!"+cmd.Name+" ") && cmd.ExtendedDescription != nil {
77+
if cmd.Matches(text) && cmd.ExtendedDescription != nil {
8478
var body string
8579
if b.G().IsMobileAppType() {
8680
body = cmd.ExtendedDescription.MobileBody

‎go/protocol/chat1/extras.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,3 +2648,7 @@ func (e OutboxErrorType) IsBadgableError() bool {
26482648
return false
26492649
}
26502650
}
2651+
2652+
func (c UserBotCommandOutput) Matches(text string) bool {
2653+
return strings.HasPrefix(text, fmt.Sprintf("!%s ", c.Name))
2654+
}

0 commit comments

Comments
 (0)