MM 55199 Limit User Preferences (#25579)

* limit size and number of preferences in updatePreferences

* update unit tests

* fix another location and add tests

* update utility function to return an error

* update unit tests

* fix review comments

* revert package-lock.json

* more review mods

* update number of preferences allowed

* update unit tests for new limit

* update name of config setting

* Update server/public/model/utils.go

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>

* add check for error id

* use existing utility function, remove model.ObjectFromJSON()

* Update preference.go - remove blank line

---------

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Scott Bishel
2024-02-01 15:33:24 -07:00
коммит произвёл GitHub
родитель a57be19da4
Коммит a38c1c54b4
3 изменённых файлов: 90 добавлений и 4 удалений

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

@@ -12,6 +12,8 @@ import (
"github.com/mattermost/mattermost/server/v8/channels/audit"
)
const maxUpdatePreferences = 100
func (api *API) InitPreference() {
api.BaseRoutes.Preferences.Handle("", api.APISessionRequired(getPreferences)).Methods("GET")
api.BaseRoutes.Preferences.Handle("", api.APISessionRequired(updatePreferences)).Methods("PUT")
@@ -101,8 +103,12 @@ func updatePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
}
var preferences model.Preferences
if jsonErr := json.NewDecoder(r.Body).Decode(&preferences); jsonErr != nil {
c.SetInvalidParamWithErr("preferences", jsonErr)
err := model.StructFromJSONLimited(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes, &preferences)
if err != nil {
c.SetInvalidParamWithErr("preferences", err)
return
} else if len(preferences) == 0 || len(preferences) > maxUpdatePreferences {
c.SetInvalidParam("preferences")
return
}
@@ -149,8 +155,12 @@ func deletePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
}
var preferences model.Preferences
if jsonErr := json.NewDecoder(r.Body).Decode(&preferences); jsonErr != nil {
c.SetInvalidParamWithErr("preferences", jsonErr)
err := model.StructFromJSONLimited(r.Body, *c.App.Config().ServiceSettings.MaximumPayloadSizeBytes, &preferences)
if err != nil {
c.SetInvalidParamWithErr("preferences", err)
return
} else if len(preferences) == 0 || len(preferences) > maxUpdatePreferences {
c.SetInvalidParam("preferences")
return
}