Files
kolaente 27ec08bb03 fix(images): limit the side length of decoded images
Images longer than MaxSide (65535) pixels on either side are now rejected
before decoding, like images over the pixel limit.

Attachments with such images are still stored; only their preview is not
generated. Project backgrounds and avatars reject them with the existing
image too large error.
2026-11-05 15:42:45 +01:00

72 lines
2.2 KiB
Go

// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package imageutils
import (
"errors"
"fmt"
"net/http"
"code.vikunja.io/api/pkg/web"
)
// ErrImageTooLarge rejects hostile dimensions before decoding (GHSA-4vh2-39rq-rq8j).
type ErrImageTooLarge struct {
Width int
Height int
}
func (err *ErrImageTooLarge) Error() string {
return fmt.Sprintf("image dimensions %dx%d are invalid or exceed the maximum of %d pixels or %d pixels per side", err.Width, err.Height, MaxPixels, MaxSide)
}
const ErrCodeImageTooLarge = 4032
func (err *ErrImageTooLarge) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusBadRequest,
Code: ErrCodeImageTooLarge,
Message: fmt.Sprintf("Image dimensions %dx%d are invalid or exceed the maximum of %d pixels or %d pixels per side.", err.Width, err.Height, MaxPixels, MaxSide),
}
}
func IsErrImageTooLarge(err error) bool {
var e *ErrImageTooLarge
return errors.As(err, &e)
}
type ErrDecodeBusy struct{}
func (err ErrDecodeBusy) Error() string {
return "Timed out waiting for capacity to decode an image"
}
const ErrCodeDecodeBusy = 4037
func (err ErrDecodeBusy) HTTPError() web.HTTPError {
return web.HTTPError{
HTTPCode: http.StatusServiceUnavailable,
Code: ErrCodeDecodeBusy,
Message: "The server is busy processing other images, please try again later.",
}
}
func IsErrDecodeBusy(err error) bool {
var busy ErrDecodeBusy
return errors.As(err, &busy)
}