Skip to content

Commit 1d66012

Browse files
authored
check username against advertised commands (keybase#19858)
* check username against advertised commands * fix test
1 parent 01b3962 commit 1d66012

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎go/chat/bots/utils.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,16 @@ func ApplyTeamBotSettings(ctx context.Context, g *globals.Context, botUID gregor
9494
if !botSettings.Cmds {
9595
return false, nil
9696
}
97+
unn, err := g.GetUPAKLoader().LookupUsername(ctx, keybase1.UID(botUID.String()))
98+
if err != nil {
99+
return false, err
100+
}
97101
cmds, err := g.BotCommandManager.ListCommands(ctx, conv.GetConvID())
98102
if err != nil {
99103
return false, nil
100104
}
101105
for _, cmd := range cmds {
102-
if strings.HasPrefix(matchText, fmt.Sprintf("!%s", cmd.Name)) {
106+
if unn.String() == cmd.Username && strings.HasPrefix(matchText, fmt.Sprintf("!%s", cmd.Name)) {
103107
return true, nil
104108
}
105109
}

‎go/chat/bots/utils_test.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/keybase/client/go/chat/types"
99
"github.com/keybase/client/go/chat/utils"
1010
"github.com/keybase/client/go/externalstest"
11+
"github.com/keybase/client/go/libkb"
1112
"github.com/keybase/client/go/protocol/chat1"
1213
"github.com/keybase/client/go/protocol/gregor1"
1314
"github.com/keybase/client/go/protocol/keybase1"
@@ -22,11 +23,20 @@ func (m MockBotCommandManager) ListCommands(context.Context, chat1.ConversationI
2223
return mockCmdOutput, nil
2324
}
2425

26+
type mockUPAKLoader struct {
27+
libkb.UPAKLoader
28+
}
29+
30+
func (m mockUPAKLoader) LookupUsername(ctx context.Context, uid keybase1.UID) (libkb.NormalizedUsername, error) {
31+
return libkb.NewNormalizedUsername("botua"), nil
32+
}
33+
2534
func TestApplyTeamBotSettings(t *testing.T) {
2635
tc := externalstest.SetupTest(t, "chat-utils", 0)
2736
defer tc.Cleanup()
2837

2938
g := globals.NewContext(tc.G, &globals.ChatContext{})
39+
tc.G.OverrideUPAKLoader(mockUPAKLoader{})
3040
debugLabeler := utils.NewDebugLabeler(g.GetLog(), "ApplyTeamBotSettings", false)
3141
ctx := context.TODO()
3242
convID := chat1.ConversationID([]byte("conv"))
@@ -120,7 +130,8 @@ func TestApplyTeamBotSettings(t *testing.T) {
120130
g.BotCommandManager = &MockBotCommandManager{}
121131
mockCmdOutput = []chat1.UserBotCommandOutput{
122132
{
123-
Name: "status",
133+
Name: "status",
134+
Username: "botua",
124135
},
125136
}
126137
msg.MessageBody = chat1.NewMessageBodyWithText(chat1.MessageText{

0 commit comments

Comments
 (0)