MM-14722 - Don't let bots create other bot accounts (#10808)
Этот коммит содержится в:
коммит произвёл
Hanzei
родитель
1a4ea94805
Коммит
ae951f6449
14
api4/bot.go
14
api4/bot.go
@@ -36,6 +36,13 @@ func createBot(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if user, err := c.App.GetUser(c.App.Session.UserId); err == nil {
|
||||
if user.IsBot {
|
||||
c.SetPermissionError(model.PERMISSION_CREATE_BOT)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if !*c.App.Config().ServiceSettings.CreateBotAccounts {
|
||||
c.Err = model.NewAppError("createBot", "api.bot.create_disabled", nil, "", http.StatusForbidden)
|
||||
return
|
||||
@@ -195,6 +202,13 @@ func assignBot(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if user, err := c.App.GetUser(userId); err == nil {
|
||||
if user.IsBot {
|
||||
c.SetPermissionError(model.PERMISSION_ASSIGN_BOT)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
bot, err := c.App.UpdateBotOwner(botUserId, userId)
|
||||
if err != nil {
|
||||
c.Err = err
|
||||
|
||||
@@ -91,6 +91,42 @@ func TestCreateBot(t *testing.T) {
|
||||
|
||||
CheckErrorMessage(t, resp, "model.bot.is_valid.description.app_error")
|
||||
})
|
||||
|
||||
t.Run("bot attempt to create bot fails", func(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.CreateBotAccounts = true
|
||||
})
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableUserAccessTokens = true })
|
||||
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.TEAM_USER_ROLE_ID)
|
||||
th.AddPermissionToRole(model.PERMISSION_EDIT_OTHER_USERS.Id, model.TEAM_USER_ROLE_ID)
|
||||
th.App.UpdateUserRoles(th.BasicUser.Id, model.TEAM_USER_ROLE_ID+" "+model.SYSTEM_USER_ACCESS_TOKEN_ROLE_ID, false)
|
||||
|
||||
bot, resp := th.Client.CreateBot(&model.Bot{
|
||||
Username: GenerateTestUsername(),
|
||||
DisplayName: "a bot",
|
||||
Description: "bot",
|
||||
})
|
||||
CheckCreatedStatus(t, resp)
|
||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||
th.App.UpdateUserRoles(bot.UserId, model.TEAM_USER_ROLE_ID+" "+model.SYSTEM_USER_ACCESS_TOKEN_ROLE_ID, false)
|
||||
|
||||
rtoken, resp := th.Client.CreateUserAccessToken(bot.UserId, "test token")
|
||||
CheckNoError(t, resp)
|
||||
th.Client.AuthToken = rtoken.Token
|
||||
|
||||
_, resp = th.Client.CreateBot(&model.Bot{
|
||||
Username: GenerateTestUsername(),
|
||||
OwnerId: bot.UserId,
|
||||
DisplayName: "a bot2",
|
||||
Description: "bot2",
|
||||
})
|
||||
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestPatchBot(t *testing.T) {
|
||||
@@ -1021,6 +1057,37 @@ func TestAssignBot(t *testing.T) {
|
||||
CheckOKStatus(t, resp)
|
||||
require.Equal(t, th.BasicUser2.Id, after.OwnerId)
|
||||
})
|
||||
|
||||
t.Run("bot assigned to bot fails", func(t *testing.T) {
|
||||
defer th.RestoreDefaultRolePermissions(th.SaveDefaultRolePermissions())
|
||||
|
||||
th.AddPermissionToRole(model.PERMISSION_CREATE_BOT.Id, model.SYSTEM_USER_ROLE_ID)
|
||||
th.AddPermissionToRole(model.PERMISSION_READ_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
||||
th.AddPermissionToRole(model.PERMISSION_READ_OTHERS_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
||||
th.AddPermissionToRole(model.PERMISSION_MANAGE_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
||||
th.AddPermissionToRole(model.PERMISSION_MANAGE_OTHERS_BOTS.Id, model.SYSTEM_USER_ROLE_ID)
|
||||
|
||||
bot := &model.Bot{
|
||||
Username: GenerateTestUsername(),
|
||||
Description: "bot",
|
||||
}
|
||||
bot, resp := th.Client.CreateBot(bot)
|
||||
CheckCreatedStatus(t, resp)
|
||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||
|
||||
bot2, resp := th.Client.CreateBot(&model.Bot{
|
||||
Username: GenerateTestUsername(),
|
||||
DisplayName: "a bot",
|
||||
Description: "bot",
|
||||
})
|
||||
|
||||
CheckCreatedStatus(t, resp)
|
||||
defer th.App.PermanentDeleteBot(bot2.UserId)
|
||||
|
||||
_, resp = th.Client.AssignBot(bot.UserId, bot2.UserId)
|
||||
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func sToP(s string) *string {
|
||||
|
||||
Ссылка в новой задаче
Block a user