Improves notify props validation (#24031)
* Adds the channel member notify props max runes restriction * Fix translations
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
3c31629813
Коммит
4803889158
@@ -1338,14 +1338,17 @@ func (a *App) UpdateChannelMemberNotifyProps(c request.CTX, data map[string]stri
|
||||
member, err := a.Srv().Store().Channel().UpdateMemberNotifyProps(channelID, userID, filteredProps)
|
||||
if err != nil {
|
||||
var appErr *model.AppError
|
||||
var invErr *store.ErrInvalidInput
|
||||
var nfErr *store.ErrNotFound
|
||||
switch {
|
||||
case errors.As(err, &appErr):
|
||||
return nil, appErr
|
||||
case errors.As(err, &invErr):
|
||||
return nil, model.NewAppError("updateMemberNotifyProps", "app.channel.update_member.notify_props_limit_exceeded.app_error", nil, "", http.StatusBadRequest).Wrap(err)
|
||||
case errors.As(err, &nfErr):
|
||||
return nil, model.NewAppError("updateMemberNotifyProps", MissingChannelMemberError, nil, "", http.StatusNotFound).Wrap(err)
|
||||
default:
|
||||
return nil, model.NewAppError("updateMemberNotifyProps", "app.channel.get_member.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
return nil, model.NewAppError("updateMemberNotifyProps", "app.channel.update_member.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -390,7 +390,6 @@ func (a *App) buildUserChannelMemberships(userID string, teamID string) (*[]impo
|
||||
}
|
||||
|
||||
func (a *App) buildUserNotifyProps(notifyProps model.StringMap) *imports.UserNotifyPropsImportData {
|
||||
|
||||
getProp := func(key string) *string {
|
||||
if v, ok := notifyProps[key]; ok {
|
||||
return &v
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
sq "github.com/mattermost/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
@@ -1916,10 +1917,15 @@ func (s SqlChannelStore) UpdateMemberNotifyProps(channelID, userID string, props
|
||||
}
|
||||
defer finalizeTransactionX(tx, &err)
|
||||
|
||||
jsonNotifyProps := model.MapToJSON(props)
|
||||
if utf8.RuneCountInString(jsonNotifyProps) > model.ChannelMemberNotifyPropsMaxRunes {
|
||||
return nil, store.NewErrInvalidInput("ChannelMember", "NotifyProps", props)
|
||||
}
|
||||
|
||||
if s.DriverName() == model.DatabaseDriverPostgres {
|
||||
sql, args, err2 := s.getQueryBuilder().
|
||||
Update("channelmembers").
|
||||
Set("notifyprops", sq.Expr("notifyprops || ?::jsonb", model.MapToJSON(props))).
|
||||
Set("notifyprops", sq.Expr("notifyprops || ?::jsonb", jsonNotifyProps)).
|
||||
Where(sq.Eq{
|
||||
"userid": userID,
|
||||
"channelid": channelID,
|
||||
|
||||
@@ -1082,6 +1082,15 @@ func testChannelSaveMember(t *testing.T, ss store.Store) {
|
||||
require.IsType(t, &store.ErrConflict{}, nErr)
|
||||
})
|
||||
|
||||
t.Run("should fail if notify props are too big", func(t *testing.T) {
|
||||
channelID := model.NewId()
|
||||
props := model.GetDefaultChannelNotifyProps()
|
||||
props["property"] = strings.Repeat("Z", model.ChannelMemberNotifyPropsMaxRunes)
|
||||
member := &model.ChannelMember{ChannelId: channelID, UserId: u1.Id, NotifyProps: props}
|
||||
_, nErr := ss.Channel().SaveMember(member)
|
||||
require.ErrorContains(t, nErr, "channel_member.is_valid.notify_props")
|
||||
})
|
||||
|
||||
t.Run("insert member correctly (in channel without channel scheme and team without scheme)", func(t *testing.T) {
|
||||
team := &model.Team{
|
||||
DisplayName: "Name",
|
||||
@@ -1581,6 +1590,15 @@ func testChannelSaveMultipleMembers(t *testing.T, ss store.Store) {
|
||||
require.IsType(t, &store.ErrConflict{}, nErr)
|
||||
})
|
||||
|
||||
t.Run("should fail if notify props are too big", func(t *testing.T) {
|
||||
channelID := model.NewId()
|
||||
props := model.GetDefaultChannelNotifyProps()
|
||||
props["property"] = strings.Repeat("Z", model.ChannelMemberNotifyPropsMaxRunes)
|
||||
member := &model.ChannelMember{ChannelId: channelID, UserId: u1.Id, NotifyProps: props}
|
||||
_, nErr := ss.Channel().SaveMultipleMembers([]*model.ChannelMember{member})
|
||||
require.ErrorContains(t, nErr, "channel_member.is_valid.notify_props")
|
||||
})
|
||||
|
||||
t.Run("insert members correctly (in channel without channel scheme and team without scheme)", func(t *testing.T) {
|
||||
team := &model.Team{
|
||||
DisplayName: "Name",
|
||||
@@ -2109,6 +2127,18 @@ func testChannelUpdateMember(t *testing.T, ss store.Store) {
|
||||
require.Equal(t, "model.channel_member.is_valid.channel_id.app_error", appErr.Id)
|
||||
})
|
||||
|
||||
t.Run("should fail with invalid error if notify props are too big", func(t *testing.T) {
|
||||
props := model.GetDefaultChannelNotifyProps()
|
||||
props["property"] = strings.Repeat("Z", model.ChannelMemberNotifyPropsMaxRunes)
|
||||
|
||||
member := &model.ChannelMember{ChannelId: model.NewId(), UserId: u1.Id, NotifyProps: props}
|
||||
_, nErr := ss.Channel().UpdateMember(member)
|
||||
require.Error(t, nErr)
|
||||
var appErr *model.AppError
|
||||
require.ErrorAs(t, nErr, &appErr)
|
||||
require.Equal(t, "model.channel_member.is_valid.notify_props.app_error", appErr.Id)
|
||||
})
|
||||
|
||||
t.Run("insert member correctly (in channel without channel scheme and team without scheme)", func(t *testing.T) {
|
||||
team := &model.Team{
|
||||
DisplayName: "Name",
|
||||
@@ -2614,6 +2644,18 @@ func testChannelUpdateMultipleMembers(t *testing.T, ss store.Store) {
|
||||
require.IsType(t, &store.ErrConflict{}, nErr)
|
||||
})
|
||||
|
||||
t.Run("should fail with invalid error if notify props are too big", func(t *testing.T) {
|
||||
props := model.GetDefaultChannelNotifyProps()
|
||||
props["property"] = strings.Repeat("Z", model.ChannelMemberNotifyPropsMaxRunes)
|
||||
|
||||
member := &model.ChannelMember{ChannelId: model.NewId(), UserId: u1.Id, NotifyProps: props}
|
||||
_, nErr := ss.Channel().SaveMultipleMembers([]*model.ChannelMember{member})
|
||||
require.Error(t, nErr)
|
||||
var appErr *model.AppError
|
||||
require.ErrorAs(t, nErr, &appErr)
|
||||
require.Equal(t, "model.channel_member.is_valid.notify_props.app_error", appErr.Id)
|
||||
})
|
||||
|
||||
t.Run("insert members correctly (in channel without channel scheme and team without scheme)", func(t *testing.T) {
|
||||
team := &model.Team{
|
||||
DisplayName: "Name",
|
||||
@@ -3152,6 +3194,14 @@ func testChannelUpdateMemberNotifyProps(t *testing.T, ss store.Store) {
|
||||
require.NoError(t, nErr)
|
||||
// Verify props.
|
||||
assert.Equal(t, props, member.NotifyProps)
|
||||
|
||||
t.Run("should fail with invalid input if the notify props are too big", func(t *testing.T) {
|
||||
props["property"] = strings.Repeat("Z", model.ChannelMemberNotifyPropsMaxRunes)
|
||||
member, err = ss.Channel().UpdateMemberNotifyProps(member.ChannelId, member.UserId, props)
|
||||
var invErr *store.ErrInvalidInput
|
||||
require.ErrorAs(t, err, &invErr)
|
||||
require.Nil(t, member)
|
||||
})
|
||||
}
|
||||
|
||||
func testChannelRemoveMember(t *testing.T, ss store.Store) {
|
||||
|
||||
Ссылка в новой задаче
Block a user