fix: enforce pipeline lifecycle limits
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"image"
|
||||
@@ -9,11 +10,14 @@ import (
|
||||
|
||||
var errInvalidEXIF = errors.New("invalid EXIF metadata")
|
||||
|
||||
func jpegOrientation(data []byte) (int, error) {
|
||||
func jpegOrientation(ctx context.Context, data []byte) (int, error) {
|
||||
if len(data) < 2 || data[0] != 0xff || data[1] != 0xd8 {
|
||||
return 0, errInvalidEXIF
|
||||
}
|
||||
for offset := 2; offset < len(data); {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if data[offset] != 0xff {
|
||||
return 0, errInvalidEXIF
|
||||
}
|
||||
@@ -43,12 +47,12 @@ func jpegOrientation(data []byte) (int, error) {
|
||||
if marker != 0xe1 || len(payload) < 6 || string(payload[:6]) != "Exif\x00\x00" {
|
||||
continue
|
||||
}
|
||||
return tiffOrientation(payload[6:])
|
||||
return tiffOrientation(ctx, payload[6:])
|
||||
}
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func tiffOrientation(data []byte) (int, error) {
|
||||
func tiffOrientation(ctx context.Context, data []byte) (int, error) {
|
||||
if len(data) < 8 {
|
||||
return 0, errInvalidEXIF
|
||||
}
|
||||
@@ -74,6 +78,9 @@ func tiffOrientation(data []byte) (int, error) {
|
||||
return 0, errInvalidEXIF
|
||||
}
|
||||
for index := uint64(0); index < count; index++ {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
entry := data[entriesStart+index*12 : entriesStart+(index+1)*12]
|
||||
if order.Uint16(entry[:2]) != 0x0112 {
|
||||
continue
|
||||
@@ -90,7 +97,7 @@ func tiffOrientation(data []byte) (int, error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
func applyOrientation(source image.Image, orientation int) *image.NRGBA {
|
||||
func applyOrientation(ctx context.Context, source image.Image, orientation int) (*image.NRGBA, error) {
|
||||
bounds := source.Bounds()
|
||||
width, height := bounds.Dx(), bounds.Dy()
|
||||
outputWidth, outputHeight := width, height
|
||||
@@ -99,12 +106,20 @@ func applyOrientation(source image.Image, orientation int) *image.NRGBA {
|
||||
}
|
||||
output := image.NewNRGBA(image.Rect(0, 0, outputWidth, outputHeight))
|
||||
for y := 0; y < outputHeight; y++ {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for x := 0; x < outputWidth; x++ {
|
||||
if x&255 == 0 {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
sourceX, sourceY := orientedSourcePoint(x, y, width, height, orientation)
|
||||
output.Set(x, y, source.At(bounds.Min.X+sourceX, bounds.Min.Y+sourceY))
|
||||
}
|
||||
}
|
||||
return output
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func orientedSourcePoint(x, y, width, height, orientation int) (int, int) {
|
||||
@@ -128,11 +143,19 @@ func orientedSourcePoint(x, y, width, height, orientation int) (int, int) {
|
||||
}
|
||||
}
|
||||
|
||||
func flattenWhite(source image.Image) *image.NRGBA {
|
||||
func flattenWhite(ctx context.Context, source image.Image) (*image.NRGBA, error) {
|
||||
bounds := source.Bounds()
|
||||
output := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
|
||||
for y := 0; y < bounds.Dy(); y++ {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for x := 0; x < bounds.Dx(); x++ {
|
||||
if x&255 == 0 {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
r, g, b, a := source.At(bounds.Min.X+x, bounds.Min.Y+y).RGBA()
|
||||
output.SetNRGBA(x, y, color.NRGBA{
|
||||
R: uint8((r + 0xffff - a) / 0x101),
|
||||
@@ -142,5 +165,5 @@ func flattenWhite(source image.Image) *image.NRGBA {
|
||||
})
|
||||
}
|
||||
}
|
||||
return output
|
||||
return output, nil
|
||||
}
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -64,7 +64,10 @@ func TestFlattenWhiteCompositesAlpha(t *testing.T) {
|
||||
source.SetNRGBA(0, 0, color.NRGBA{R: 10, G: 20, B: 30, A: 0})
|
||||
source.SetNRGBA(1, 0, color.NRGBA{R: 100, G: 50, B: 0, A: 128})
|
||||
|
||||
result := flattenWhite(source)
|
||||
result, err := flattenWhite(context.Background(), source)
|
||||
if err != nil {
|
||||
t.Fatalf("flatten image: %v", err)
|
||||
}
|
||||
if got := result.NRGBAAt(0, 0); got != (color.NRGBA{R: 255, G: 255, B: 255, A: 255}) {
|
||||
t.Fatalf("transparent pixel: got %#v", got)
|
||||
}
|
||||
@@ -154,6 +157,28 @@ func TestProcessHonorsCanceledContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResizeStopsAfterCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
source := cancelingImage{
|
||||
Image: image.NewNRGBA(image.Rect(0, 0, 256, 256)),
|
||||
cancel: cancel,
|
||||
}
|
||||
_, err := resizeContext(ctx, source, 128, 128)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("error: got %v, want %v", err, context.Canceled)
|
||||
}
|
||||
}
|
||||
|
||||
type cancelingImage struct {
|
||||
image.Image
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (i cancelingImage) At(x, y int) color.Color {
|
||||
i.cancel()
|
||||
return i.Image.At(x, y)
|
||||
}
|
||||
|
||||
func TestEstimateMemoryWeight(t *testing.T) {
|
||||
weight, err := EstimateMemoryWeight(1000, 320, 200)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
package transform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
func fit(source image.Image, maxWidth, maxHeight int, upscale bool) *image.NRGBA {
|
||||
func fitContext(
|
||||
ctx context.Context,
|
||||
source image.Image,
|
||||
maxWidth int,
|
||||
maxHeight int,
|
||||
upscale bool,
|
||||
) (*image.NRGBA, error) {
|
||||
bounds := source.Bounds()
|
||||
width, height := fitDimensions(bounds.Dx(), bounds.Dy(), maxWidth, maxHeight, upscale)
|
||||
return resize(source, width, height)
|
||||
return resizeContext(ctx, source, width, height)
|
||||
}
|
||||
|
||||
func fitDimensions(width, height, maxWidth, maxHeight int, upscale bool) (int, int) {
|
||||
@@ -43,15 +50,23 @@ func (i croppedImage) At(x, y int) color.Color {
|
||||
return i.source.At(i.offset.X+x, i.offset.Y+y)
|
||||
}
|
||||
|
||||
func resize(source image.Image, width, height int) *image.NRGBA {
|
||||
func resizeContext(ctx context.Context, source image.Image, width, height int) (*image.NRGBA, error) {
|
||||
bounds := source.Bounds()
|
||||
if bounds.Dx() == width && bounds.Dy() == height {
|
||||
return copyNRGBA(source)
|
||||
return copyNRGBAContext(ctx, source)
|
||||
}
|
||||
output := image.NewNRGBA(image.Rect(0, 0, width, height))
|
||||
for y := 0; y < height; y++ {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
y0, y1, fy := sampleCoordinate(y, height, bounds.Dy())
|
||||
for x := 0; x < width; x++ {
|
||||
if x&255 == 0 {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
x0, x1, fx := sampleCoordinate(x, width, bounds.Dx())
|
||||
c00 := color.NRGBAModel.Convert(source.At(bounds.Min.X+x0, bounds.Min.Y+y0)).(color.NRGBA)
|
||||
c10 := color.NRGBAModel.Convert(source.At(bounds.Min.X+x1, bounds.Min.Y+y0)).(color.NRGBA)
|
||||
@@ -60,7 +75,7 @@ func resize(source image.Image, width, height int) *image.NRGBA {
|
||||
output.SetNRGBA(x, y, interpolate(c00, c10, c01, c11, fx, fy))
|
||||
}
|
||||
}
|
||||
return output
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func sampleCoordinate(destination, destinationSize, sourceSize int) (int, int, uint32) {
|
||||
@@ -93,13 +108,21 @@ func interpolateChannel(c00, c10, c01, c11 uint8, fx, fy uint32) uint8 {
|
||||
return uint8((value + 1<<31) >> 32)
|
||||
}
|
||||
|
||||
func copyNRGBA(source image.Image) *image.NRGBA {
|
||||
func copyNRGBAContext(ctx context.Context, source image.Image) (*image.NRGBA, error) {
|
||||
bounds := source.Bounds()
|
||||
output := image.NewNRGBA(image.Rect(0, 0, bounds.Dx(), bounds.Dy()))
|
||||
for y := 0; y < bounds.Dy(); y++ {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for x := 0; x < bounds.Dx(); x++ {
|
||||
if x&255 == 0 {
|
||||
if err := contextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
output.Set(x, y, source.At(bounds.Min.X+x, bounds.Min.Y+y))
|
||||
}
|
||||
}
|
||||
return output
|
||||
return output, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user