fix(service): validate image data with bounded decoding
This commit is contained in:
@@ -36,10 +36,11 @@ type attachmentQueries interface {
|
||||
}
|
||||
|
||||
type AttachmentService struct {
|
||||
q attachmentQueries
|
||||
s3 *s3store.Client
|
||||
maxBytes int
|
||||
maxPixels int
|
||||
q attachmentQueries
|
||||
s3 *s3store.Client
|
||||
maxBytes int
|
||||
maxPixels int
|
||||
decodeSlot chan struct{}
|
||||
}
|
||||
|
||||
func NewAttachmentService(
|
||||
@@ -48,10 +49,11 @@ func NewAttachmentService(
|
||||
maxBytes, maxPixels int,
|
||||
) *AttachmentService {
|
||||
return &AttachmentService{
|
||||
q: q,
|
||||
s3: s3,
|
||||
maxBytes: maxBytes,
|
||||
maxPixels: maxPixels,
|
||||
q: q,
|
||||
s3: s3,
|
||||
maxBytes: maxBytes,
|
||||
maxPixels: maxPixels,
|
||||
decodeSlot: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +97,7 @@ func (s *AttachmentService) Upload(
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
detectedType, err := s.checkImage(tmp, contentType)
|
||||
detectedType, err := s.checkImage(ctx, tmp, contentType)
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
@@ -215,30 +217,52 @@ func (s *AttachmentService) GetContent(
|
||||
return data, model.ContentType(row.ContentType), nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) checkImage(r io.Reader, contentType string) (model.ContentType, error) {
|
||||
func (s *AttachmentService) checkImage(ctx context.Context, r io.ReadSeeker, contentType string) (model.ContentType, error) {
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil {
|
||||
return "", ErrInvalidImage
|
||||
}
|
||||
|
||||
if cfg.Width*cfg.Height > s.maxPixels {
|
||||
if cfg.Width <= 0 || cfg.Height <= 0 {
|
||||
return "", ErrInvalidImage
|
||||
}
|
||||
if cfg.Width > s.maxPixels/cfg.Height {
|
||||
return "", ErrInvalidSize
|
||||
}
|
||||
|
||||
var detectedType model.ContentType
|
||||
switch format {
|
||||
case "png":
|
||||
if contentType != string(model.ContentTypePNG) {
|
||||
return "", ErrInvalidImage
|
||||
}
|
||||
return model.ContentTypePNG, nil
|
||||
detectedType = model.ContentTypePNG
|
||||
case "jpeg":
|
||||
if contentType != string(model.ContentTypeJPEG) {
|
||||
return "", ErrInvalidImage
|
||||
}
|
||||
return model.ContentTypeJPEG, nil
|
||||
detectedType = model.ContentTypeJPEG
|
||||
default:
|
||||
return "", ErrInvalidImage
|
||||
}
|
||||
|
||||
select {
|
||||
case s.decodeSlot <- struct{}{}:
|
||||
defer func() { <-s.decodeSlot }()
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, _, err := image.Decode(r); err != nil {
|
||||
return "", ErrInvalidImage
|
||||
}
|
||||
|
||||
return detectedType, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID uuid.UUID) error {
|
||||
|
||||
Reference in New Issue
Block a user