fix(service):lab4 full decode catches truncated images under semaphore
This commit is contained in:
@@ -25,6 +25,7 @@ type AttachmentService struct {
|
|||||||
storage domain.ObjectStorage
|
storage domain.ObjectStorage
|
||||||
maxBytes int64
|
maxBytes int64
|
||||||
maxPixels int64
|
maxPixels int64
|
||||||
|
decodeSem chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAttachmentService(
|
func NewAttachmentService(
|
||||||
@@ -39,6 +40,9 @@ func NewAttachmentService(
|
|||||||
storage: storage,
|
storage: storage,
|
||||||
maxBytes: maxBytes,
|
maxBytes: maxBytes,
|
||||||
maxPixels: maxPixels,
|
maxPixels: maxPixels,
|
||||||
|
// 40M pixels × 4 bytes (RGBA8) = 160 MB per full decode.
|
||||||
|
// API_MEMORY=256M → только один decode безопасен.
|
||||||
|
decodeSem: make(chan struct{}, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +85,7 @@ func (s *AttachmentService) Upload(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, &domain.UnsupportedMediaTypeError{
|
return nil, &domain.UnsupportedMediaTypeError{
|
||||||
MediaType: contentType,
|
MediaType: contentType,
|
||||||
Expected: "image/png или image/jpeg",
|
Expected: "целостный image/png или image/jpeg",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if int64(cfg.Width)*int64(cfg.Height) > s.maxPixels {
|
if int64(cfg.Width)*int64(cfg.Height) > s.maxPixels {
|
||||||
@@ -91,6 +95,10 @@ func (s *AttachmentService) Upload(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := s.verifyIntegrity(ctx, buf, contentType); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
sha := sha256.Sum256(buf)
|
sha := sha256.Sum256(buf)
|
||||||
id := uuid.NewString()
|
id := uuid.NewString()
|
||||||
objectKey := "chats/" + chatID + "/" + id
|
objectKey := "chats/" + chatID + "/" + id
|
||||||
@@ -127,6 +135,23 @@ func (s *AttachmentService) Upload(
|
|||||||
return a, nil
|
return a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *AttachmentService) verifyIntegrity(ctx context.Context, buf []byte, contentType string) error {
|
||||||
|
select {
|
||||||
|
case s.decodeSem <- struct{}{}:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return &domain.UnavailableError{Reason: "ожидание декодирования"}
|
||||||
|
}
|
||||||
|
defer func() { <-s.decodeSem }()
|
||||||
|
|
||||||
|
if _, _, err := image.Decode(bytes.NewReader(buf)); err != nil {
|
||||||
|
return &domain.UnsupportedMediaTypeError{
|
||||||
|
MediaType: contentType,
|
||||||
|
Expected: "целостный " + contentType + " (без обрезки/повреждений)",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string) (*domain.Attachment, error) {
|
func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string) (*domain.Attachment, error) {
|
||||||
a, err := s.attachments.GetByID(ctx, attachmentID)
|
a, err := s.attachments.GetByID(ctx, attachmentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -166,8 +191,8 @@ func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateFilename(name string) error {
|
func validateFilename(name string) error {
|
||||||
if name == "" {
|
if strings.TrimSpace(name) == "" {
|
||||||
return &domain.ValidationError{Field: "filename", Message: "не может быть пустым"}
|
return &domain.ValidationError{Field: "filename", Message: "не может быть пустым или состоять только из пробелов"}
|
||||||
}
|
}
|
||||||
if utf8.RuneCountInString(name) > 255 {
|
if utf8.RuneCountInString(name) > 255 {
|
||||||
return &domain.ValidationError{Field: "filename", Message: "не более 255 символов"}
|
return &domain.ValidationError{Field: "filename", Message: "не более 255 символов"}
|
||||||
|
|||||||
Reference in New Issue
Block a user