Add MaxImageResolution config setting (#17941)

Этот коммит содержится в:
Claudio Costa
2021-08-12 17:43:10 +02:00
коммит произвёл GitHub
родитель e1b0644b0d
Коммит 96593580ae
11 изменённых файлов: 35 добавлений и 14 удалений

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

@@ -28,7 +28,7 @@ func (a *App) SaveBrandImage(imageData *multipart.FileHeader) *model.AppError {
}
defer file.Close()
if err = checkImageLimits(file); err != nil {
if err = checkImageLimits(file, *a.Config().FileSettings.MaxImageResolution); err != nil {
return model.NewAppError("SaveBrandImage", "brand.save_brand_image.check_image_limits.app_error", nil, err.Error(), http.StatusBadRequest)
}

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

@@ -38,7 +38,6 @@ import (
)
const (
maxImageRes = int64(6048 * 4032) // 24 megapixels, up to ~196MB as a raw image
imageThumbnailWidth = 120
imageThumbnailHeight = 100
imagePreviewWidth = 1920
@@ -642,6 +641,7 @@ type UploadFileTask struct {
teeInput io.Reader
fileinfo *model.FileInfo
maxFileSize int64
maxImageRes int64
// Cached image data that (may) get initialized in preprocessImage and
// is used in postprocessImage
@@ -702,6 +702,7 @@ func (a *App) UploadFileX(c *request.Context, channelID, name string, input io.R
Name: filepath.Base(name),
Input: input,
maxFileSize: *a.Config().FileSettings.MaxFileSize,
maxImageRes: *a.Config().FileSettings.MaxImageResolution,
imgDecoder: a.srv.imgDecoder,
imgEncoder: a.srv.imgEncoder,
}
@@ -806,7 +807,7 @@ func (t *UploadFileTask) preprocessImage() *model.AppError {
t.fileinfo.Width = w
t.fileinfo.Height = h
if err = checkImageResolutionLimit(w, h); err != nil {
if err = checkImageResolutionLimit(w, h, t.maxImageRes); err != nil {
return t.newAppError("api.file.upload_file.large_image_detailed.app_error", http.StatusBadRequest)
}
@@ -973,7 +974,7 @@ func (a *App) DoUploadFileExpectModification(c *request.Context, now time.Time,
info.Path = pathPrefix + filename
if info.IsImage() {
if limitErr := checkImageResolutionLimit(info.Width, info.Height); limitErr != nil {
if limitErr := checkImageResolutionLimit(info.Width, info.Height, *a.Config().FileSettings.MaxImageResolution); limitErr != nil {
err := model.NewAppError("uploadFile", "api.file.upload_file.large_image.app_error", map[string]interface{}{"Filename": filename}, limitErr.Error(), http.StatusBadRequest)
return nil, data, err
}

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

@@ -10,23 +10,23 @@ import (
"github.com/mattermost/mattermost-server/v6/app/imaging"
)
func checkImageResolutionLimit(w, h int) error {
func checkImageResolutionLimit(w, h int, maxRes int64) error {
// This casting is done to prevent overflow on 32 bit systems (not needed
// in 64 bits systems because images can't have more than 32 bits height or
// width)
imageRes := int64(w) * int64(h)
if imageRes > maxImageRes {
return fmt.Errorf("image resolution is too high: %d, max allowed is %d", imageRes, maxImageRes)
if imageRes > maxRes {
return fmt.Errorf("image resolution is too high: %d, max allowed is %d", imageRes, maxRes)
}
return nil
}
func checkImageLimits(imageData io.Reader) error {
func checkImageLimits(imageData io.Reader, maxRes int64) error {
w, h, err := imaging.GetDimensions(imageData)
if err != nil {
return fmt.Errorf("failed to get image dimensions: %w", err)
}
return checkImageResolutionLimit(w, h)
return checkImageResolutionLimit(w, h, maxRes)
}

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

@@ -1938,7 +1938,7 @@ func (a *App) SetTeamIconFromMultiPartFile(teamID string, file multipart.File) *
return model.NewAppError("setTeamIcon", "api.team.set_team_icon.storage.app_error", nil, "", http.StatusNotImplemented)
}
if limitErr := checkImageLimits(file); limitErr != nil {
if limitErr := checkImageLimits(file, *a.Config().FileSettings.MaxImageResolution); limitErr != nil {
return model.NewAppError("SetTeamIcon", "api.team.set_team_icon.check_image_limits.app_error",
nil, limitErr.Error(), http.StatusBadRequest)
}

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

@@ -265,7 +265,7 @@ func (a *App) UploadData(c *request.Context, us *model.UploadSession, rd io.Read
// image post-processing
if info.IsImage() {
if limitErr := checkImageResolutionLimit(info.Width, info.Height); limitErr != nil {
if limitErr := checkImageResolutionLimit(info.Width, info.Height, *a.Config().FileSettings.MaxImageResolution); limitErr != nil {
return nil, model.NewAppError("uploadData", "app.upload.upload_data.large_image.app_error",
map[string]interface{}{"Filename": us.Filename, "Width": info.Width, "Height": info.Height}, "", http.StatusBadRequest)
}

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

@@ -753,7 +753,7 @@ func (a *App) SetProfileImage(userID string, imageData *multipart.FileHeader) *m
}
func (a *App) SetProfileImageFromMultiPartFile(userID string, file multipart.File) *model.AppError {
if limitErr := checkImageLimits(file); limitErr != nil {
if limitErr := checkImageLimits(file, *a.Config().FileSettings.MaxImageResolution); limitErr != nil {
return model.NewAppError("SetProfileImage", "api.user.upload_profile_user.check_image_limits.app_error", nil, "", http.StatusBadRequest)
}