MM-55966 - Update ArrayFromJSON to use LimitedReader (#25510)

* update ArrayFromJSON to use LimitedReader

* update for bad merge

* fix lint errors

* update test code

* update unit tests

* update unit tests

* fix unit tests

* use consts, other cleanup

* add non sorting duplicate check

* set config to default value, then config setting if available

* fix lint errors

* fixes and debugs

* fix log test

* remove setting from Client, add unlimited Parser to client

* a couple more fixes

* another fix

* rename some variables

* remove superflous call

* check for valid MaximumPayloadSize

* update language file

* fix for e2e-tests

* update util function to return error

* lint fix

* update config property name to include unit

* fix for unit test

* add new config to telemetry

* call function to create LimitedReader

* Deprecate old function, use new function name

* return new AppError on failed parse

* return new AppError on failed parse

* return new AppError on failed parse

* add constant for i18n valid constants

* Update server/public/model/utils_test.go

Co-authored-by: Miguel de la Cruz <mgdelacroix@gmail.com>

* Apply suggestions from code review

Co-authored-by: Miguel de la Cruz <mgdelacroix@gmail.com>

* update error variable, remove unnecessary check

* Update function names

* fix errors from merge

* update unit test to create unique ids

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Miguel de la Cruz <mgdelacroix@gmail.com>
Этот коммит содержится в:
Scott Bishel
2024-01-09 10:04:16 -07:00
коммит произвёл GitHub
родитель 4f0598d592
Коммит 82b8d4dc07
22 изменённых файлов: 345 добавлений и 142 удалений

Просмотреть файл

@@ -425,9 +425,18 @@ func restoreChannel(c *Context, w http.ResponseWriter, r *http.Request) {
}
func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
userIds := model.ArrayFromJSON(r.Body)
userIds, err := model.NonSortedArrayFromJSON(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes)
if err != nil {
c.Err = model.NewAppError("createDirectChannel", model.PayloadParseError, nil, "", http.StatusBadRequest).Wrap(err)
return
}
allowed := false
// single userId allowed if creating a self-channel
// NonSortedArrayFromJSON will remove duplicates, so need to add back
if len(userIds) == 1 && userIds[0] == c.AppContext.Session().UserId {
userIds = append(userIds, userIds[0])
}
if len(userIds) != 2 {
c.SetInvalidParam("user_ids")
return
@@ -464,9 +473,9 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
audit.AddEventParameter(auditRec, "user_id", otherUserId)
canSee, err := c.App.UserCanSeeOtherUser(c.AppContext, c.AppContext.Session().UserId, otherUserId)
if err != nil {
c.Err = err
canSee, appErr := c.App.UserCanSeeOtherUser(c.AppContext, c.AppContext.Session().UserId, otherUserId)
if appErr != nil {
c.Err = appErr
return
}
@@ -475,9 +484,9 @@ func createDirectChannel(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
sc, err := c.App.GetOrCreateDirectChannel(c.AppContext, userIds[0], userIds[1])
if err != nil {
c.Err = err
sc, appErr := c.App.GetOrCreateDirectChannel(c.AppContext, userIds[0], userIds[1])
if appErr != nil {
c.Err = appErr
return
}
@@ -511,9 +520,11 @@ func searchGroupChannels(c *Context, w http.ResponseWriter, r *http.Request) {
}
func createGroupChannel(c *Context, w http.ResponseWriter, r *http.Request) {
userIds := model.ArrayFromJSON(r.Body)
if len(userIds) == 0 {
userIds, err := model.SortedArrayFromJSON(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes)
if err != nil {
c.Err = model.NewAppError("createGroupChannel", model.PayloadParseError, nil, "", http.StatusBadRequest).Wrap(err)
return
} else if len(userIds) == 0 {
c.SetInvalidParam("user_ids")
return
}
@@ -561,9 +572,9 @@ func createGroupChannel(c *Context, w http.ResponseWriter, r *http.Request) {
return
}
groupChannel, err := c.App.CreateGroupChannel(c.AppContext, userIds, c.AppContext.Session().UserId)
if err != nil {
c.Err = err
groupChannel, appErr := c.App.CreateGroupChannel(c.AppContext, userIds, c.AppContext.Session().UserId)
if appErr != nil {
c.Err = appErr
return
}
@@ -697,7 +708,12 @@ func getChannelsMemberCount(c *Context, w http.ResponseWriter, r *http.Request)
return
}
channelIDs := model.ArrayFromJSON(r.Body)
channelIDs, sortErr := model.SortedArrayFromJSON(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes)
if sortErr != nil {
c.Err = model.NewAppError("getChannelsMemberCount", model.PayloadParseError, nil, "", http.StatusBadRequest).Wrap(sortErr)
return
}
channels, err := c.App.GetChannels(c.AppContext, channelIDs)
if err != nil {
c.Err = err
@@ -711,10 +727,9 @@ func getChannelsMemberCount(c *Context, w http.ResponseWriter, r *http.Request)
}
}
channelsMemberCount, err := c.App.GetChannelsMemberCount(c.AppContext, channelIDs)
if err != nil {
c.Err = err
channelsMemberCount, appErr := c.App.GetChannelsMemberCount(c.AppContext, channelIDs)
if appErr != nil {
c.Err = appErr
return
}
@@ -899,8 +914,11 @@ func getPublicChannelsByIdsForTeam(c *Context, w http.ResponseWriter, r *http.Re
return
}
channelIds := model.ArrayFromJSON(r.Body)
if len(channelIds) == 0 {
channelIds, err := model.SortedArrayFromJSON(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes)
if err != nil {
c.Err = model.NewAppError("getPublicChannelsByIdsForTeam", model.PayloadParseError, nil, "", http.StatusBadRequest).Wrap(err)
return
} else if len(channelIds) == 0 {
c.SetInvalidParam("channel_ids")
return
}
@@ -917,15 +935,15 @@ func getPublicChannelsByIdsForTeam(c *Context, w http.ResponseWriter, r *http.Re
return
}
channels, err := c.App.GetPublicChannelsByIdsForTeam(c.AppContext, c.Params.TeamId, channelIds)
if err != nil {
c.Err = err
channels, appErr := c.App.GetPublicChannelsByIdsForTeam(c.AppContext, c.Params.TeamId, channelIds)
if appErr != nil {
c.Err = appErr
return
}
err = c.App.FillInChannelsProps(c.AppContext, channels)
if err != nil {
c.Err = err
appErr = c.App.FillInChannelsProps(c.AppContext, channels)
if appErr != nil {
c.Err = appErr
return
}
@@ -1439,8 +1457,11 @@ func getChannelMembersByIds(c *Context, w http.ResponseWriter, r *http.Request)
return
}
userIds := model.ArrayFromJSON(r.Body)
if len(userIds) == 0 {
userIds, err := model.SortedArrayFromJSON(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes)
if err != nil {
c.Err = model.NewAppError("getChannelMembersByIds", model.PayloadParseError, nil, "", http.StatusBadRequest).Wrap(err)
return
} else if len(userIds) == 0 {
c.SetInvalidParam("user_ids")
return
}
@@ -1450,9 +1471,9 @@ func getChannelMembersByIds(c *Context, w http.ResponseWriter, r *http.Request)
return
}
members, err := c.App.GetChannelMembersByIds(c.AppContext, c.Params.ChannelId, userIds)
if err != nil {
c.Err = err
members, appErr := c.App.GetChannelMembersByIds(c.AppContext, c.Params.ChannelId, userIds)
if appErr != nil {
c.Err = appErr
return
}
@@ -1562,14 +1583,16 @@ func viewChannel(c *Context, w http.ResponseWriter, r *http.Request) {
func readMultipleChannels(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireUserId()
var channelIDs []string
err := json.NewDecoder(r.Body).Decode(&channelIDs)
if err != nil || len(channelIDs) == 0 {
c.SetInvalidParamWithErr("channel_ids", err)
channelIds, err := model.SortedArrayFromJSON(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes)
if err != nil {
c.Err = model.NewAppError("readMultipleChannels", model.PayloadParseError, nil, "", http.StatusBadRequest).Wrap(err)
return
} else if len(channelIds) == 0 {
c.SetInvalidParam("channel_ids")
return
}
times, appErr := c.App.MarkChannelsAsViewed(c.AppContext, channelIDs, c.Params.UserId, c.AppContext.Session().Id, true, c.App.IsCRTEnabledForUser(c.AppContext, c.Params.UserId))
times, appErr := c.App.MarkChannelsAsViewed(c.AppContext, channelIds, c.Params.UserId, c.AppContext.Session().Id, true, c.App.IsCRTEnabledForUser(c.AppContext, c.Params.UserId))
if appErr != nil {
c.Err = appErr
return