fix: enforce pipeline lifecycle limits
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"image"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
|
||||
@@ -78,7 +79,7 @@ func (p *Processor) Process(ctx context.Context, data []byte, declaredContentTyp
|
||||
if err := contextError(ctx); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
img, format, width, height, orientation, err := p.decode(data, declaredContentType)
|
||||
img, format, width, height, orientation, err := p.decode(ctx, data, declaredContentType)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
@@ -86,12 +87,12 @@ func (p *Processor) Process(ctx context.Context, data []byte, declaredContentTyp
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
oriented := applyOrientation(img, orientation)
|
||||
if err := contextError(ctx); err != nil {
|
||||
oriented, err := applyOrientation(ctx, img, orientation)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
opaque := flattenWhite(oriented)
|
||||
if err := contextError(ctx); err != nil {
|
||||
opaque, err := flattenWhite(ctx, oriented)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
result := Result{
|
||||
@@ -99,7 +100,10 @@ func (p *Processor) Process(ctx context.Context, data []byte, declaredContentTyp
|
||||
Orientation: orientation,
|
||||
}
|
||||
|
||||
thumbnail := fit(opaque, 256, 256, false)
|
||||
thumbnail, err := fitContext(ctx, opaque, 256, 256, false)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
result.Thumbnail, err = encodeJPEG(thumbnail, p.config.JPEGQuality)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
@@ -109,7 +113,10 @@ func (p *Processor) Process(ctx context.Context, data []byte, declaredContentTyp
|
||||
}
|
||||
|
||||
crop := centeredSquare(opaque)
|
||||
crop = resize(crop, 128, 128)
|
||||
crop, err = resizeContext(ctx, crop, 128, 128)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
result.Crop, err = encodeJPEG(crop, p.config.JPEGQuality)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
@@ -118,7 +125,10 @@ func (p *Processor) Process(ctx context.Context, data []byte, declaredContentTyp
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
optimized := fit(opaque, 1280, 1280, false)
|
||||
optimized, err := fitContext(ctx, opaque, 1280, 1280, false)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
result.Optimized, err = encodeWebP(optimized, p.config.WebPQuality, p.config.WebPMethod)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
@@ -129,7 +139,11 @@ func (p *Processor) Process(ctx context.Context, data []byte, declaredContentTyp
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *Processor) decode(data []byte, declaredContentType string) (image.Image, string, int, int, int, error) {
|
||||
func (p *Processor) decode(
|
||||
ctx context.Context,
|
||||
data []byte,
|
||||
declaredContentType string,
|
||||
) (image.Image, string, int, int, int, error) {
|
||||
if declaredContentType != "image/jpeg" && declaredContentType != "image/png" {
|
||||
return nil, "", 0, 0, 0, ErrUnsupportedMedia
|
||||
}
|
||||
@@ -142,27 +156,48 @@ func (p *Processor) decode(data []byte, declaredContentType string) (image.Image
|
||||
if http.DetectContentType(data) != declaredContentType {
|
||||
return nil, "", 0, 0, 0, ErrUnsupportedMedia
|
||||
}
|
||||
config, format, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
config, format, err := image.DecodeConfig(contextReader{ctx: ctx, reader: bytes.NewReader(data)})
|
||||
if contextErr := contextError(ctx); contextErr != nil {
|
||||
return nil, "", 0, 0, 0, contextErr
|
||||
}
|
||||
if err != nil || contentType(format) != declaredContentType || config.Width < 1 || config.Height < 1 {
|
||||
return nil, "", 0, 0, 0, ErrInvalidImage
|
||||
}
|
||||
if int64(config.Width) > p.config.MaxPixels/int64(config.Height) {
|
||||
return nil, "", 0, 0, 0, ErrPayloadTooLarge
|
||||
}
|
||||
img, decodedFormat, err := image.Decode(bytes.NewReader(data))
|
||||
img, decodedFormat, err := image.Decode(contextReader{ctx: ctx, reader: bytes.NewReader(data)})
|
||||
if contextErr := contextError(ctx); contextErr != nil {
|
||||
return nil, "", 0, 0, 0, contextErr
|
||||
}
|
||||
if err != nil || decodedFormat != format {
|
||||
return nil, "", 0, 0, 0, ErrInvalidImage
|
||||
}
|
||||
orientation := 1
|
||||
if format == "jpeg" {
|
||||
orientation, err = jpegOrientation(data)
|
||||
orientation, err = jpegOrientation(ctx, data)
|
||||
if err != nil {
|
||||
if contextErr := contextError(ctx); contextErr != nil {
|
||||
return nil, "", 0, 0, 0, contextErr
|
||||
}
|
||||
return nil, "", 0, 0, 0, ErrInvalidImage
|
||||
}
|
||||
}
|
||||
return img, declaredContentType, config.Width, config.Height, orientation, nil
|
||||
}
|
||||
|
||||
type contextReader struct {
|
||||
ctx context.Context
|
||||
reader io.Reader
|
||||
}
|
||||
|
||||
func (r contextReader) Read(buffer []byte) (int, error) {
|
||||
if err := contextError(r.ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.reader.Read(buffer)
|
||||
}
|
||||
|
||||
func contextError(ctx context.Context) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
||||
Reference in New Issue
Block a user