fix: harden attachment cleanup

This commit is contained in:
2026-10-09 13:27:58 +03:00
parent f8c31de7c0
commit 3fde1c324c
8 changed files with 134 additions and 24 deletions
+4 -3
View File
@@ -11,6 +11,7 @@ import (
_ "image/png"
"io"
"log/slog"
"math"
"mime"
"net/http"
"time"
@@ -68,7 +69,7 @@ type Service struct {
}
func NewService(logger *slog.Logger, repository Repository, objects ObjectStore, config Config) (*Service, error) {
if config.MaxBytes < 1 || config.MaxPixels < 1 || config.CleanupTimeout <= 0 {
if config.MaxBytes < 1 || config.MaxPixels < 1 || config.MaxPixels > math.MaxInt64/8 || config.CleanupTimeout <= 0 {
return nil, errors.New("invalid attachment service configuration")
}
if config.BodyMemoryBytes < config.MaxBytes {
@@ -207,11 +208,11 @@ func (s *Service) readImage(input Upload) ([]byte, string, func(), error) {
releaseBody()
return nil, "", nil, model.ErrUnsupportedMedia
}
pixels := int64(imageConfig.Width) * int64(imageConfig.Height)
if imageConfig.Width < 1 || imageConfig.Height < 1 || pixels > s.config.MaxPixels {
if imageConfig.Width < 1 || imageConfig.Height < 1 || int64(imageConfig.Width) > s.config.MaxPixels/int64(imageConfig.Height) {
releaseBody()
return nil, "", nil, model.ErrPayloadTooLarge
}
pixels := int64(imageConfig.Width) * int64(imageConfig.Height)
decodeBytes := pixels * 8
if !s.decodeMemory.TryAcquire(decodeBytes) {
releaseBody()