app/user: ensure that user gets sanitized while calling plugin hooks (#19808)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3e8b81c0c7
Коммит
1c49366f00
@@ -288,7 +288,7 @@ func (a *App) createUserOrGuest(c *request.Context, user *model.User, guest bool
|
|||||||
a.Srv().Go(func() {
|
a.Srv().Go(func() {
|
||||||
pluginContext := pluginContext(c)
|
pluginContext := pluginContext(c)
|
||||||
pluginsEnvironment.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
|
pluginsEnvironment.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
|
||||||
hooks.UserHasBeenCreated(pluginContext, user)
|
hooks.UserHasBeenCreated(pluginContext, ruser)
|
||||||
return true
|
return true
|
||||||
}, plugin.UserHasBeenCreatedID)
|
}, plugin.UserHasBeenCreatedID)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -200,24 +200,70 @@ func TestCreateUser(t *testing.T) {
|
|||||||
th := Setup(t)
|
th := Setup(t)
|
||||||
defer th.TearDown()
|
defer th.TearDown()
|
||||||
|
|
||||||
group := th.CreateGroup()
|
|
||||||
|
|
||||||
id := model.NewId()
|
|
||||||
user := &model.User{
|
|
||||||
Email: "success+" + id + "@simulator.amazonses.com",
|
|
||||||
Username: *group.Name,
|
|
||||||
Nickname: "nn_" + id,
|
|
||||||
Password: "Password1",
|
|
||||||
EmailVerified: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Run("fails if the username matches a group name", func(t *testing.T) {
|
t.Run("fails if the username matches a group name", func(t *testing.T) {
|
||||||
|
group := th.CreateGroup()
|
||||||
|
|
||||||
|
id := model.NewId()
|
||||||
|
user := &model.User{
|
||||||
|
Email: "success+" + id + "@simulator.amazonses.com",
|
||||||
|
Username: *group.Name,
|
||||||
|
Nickname: "nn_" + id,
|
||||||
|
Password: "Password1",
|
||||||
|
EmailVerified: true,
|
||||||
|
}
|
||||||
|
|
||||||
user.Username = *group.Name
|
user.Username = *group.Name
|
||||||
u, err := th.App.CreateUser(th.Context, user)
|
u, err := th.App.CreateUser(th.Context, user)
|
||||||
require.NotNil(t, err)
|
require.NotNil(t, err)
|
||||||
require.Equal(t, "app.user.group_name_conflict", err.Id)
|
require.Equal(t, "app.user.group_name_conflict", err.Id)
|
||||||
require.Nil(t, u)
|
require.Nil(t, u)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("should sanitize user authdata before publishing to plugin hooks", func(t *testing.T) {
|
||||||
|
tearDown, _, _ := SetAppEnvironmentWithPlugins(t,
|
||||||
|
[]string{
|
||||||
|
`
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mattermost/mattermost-server/v6/plugin"
|
||||||
|
"github.com/mattermost/mattermost-server/v6/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MyPlugin struct {
|
||||||
|
plugin.MattermostPlugin
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *MyPlugin) UserHasBeenCreated(c *plugin.Context, user *model.User) {
|
||||||
|
user.Nickname = "sanitized"
|
||||||
|
if len(user.Password) > 0 {
|
||||||
|
user.Nickname = "not-sanitized"
|
||||||
|
}
|
||||||
|
p.API.UpdateUser(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
plugin.ClientMain(&MyPlugin{})
|
||||||
|
}
|
||||||
|
`}, th.App, th.NewPluginAPI)
|
||||||
|
defer tearDown()
|
||||||
|
|
||||||
|
user := &model.User{
|
||||||
|
Email: model.NewId() + "success+test@example.com",
|
||||||
|
Nickname: "Darth Vader",
|
||||||
|
Username: "vader" + model.NewId(),
|
||||||
|
Password: "passwd12345",
|
||||||
|
AuthService: "",
|
||||||
|
}
|
||||||
|
_, err := th.App.CreateUser(th.Context, user)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
user, err = th.App.GetUser(user.Id)
|
||||||
|
require.Nil(t, err)
|
||||||
|
require.Equal(t, "sanitized", user.Nickname)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateUserActive(t *testing.T) {
|
func TestUpdateUserActive(t *testing.T) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user