Skip to content

Commit

Permalink
Prevent pixel flooding attacks by blocking images larger than 10,000x…
Browse files Browse the repository at this point in the history
…10,000 that need to be transformed
  • Loading branch information
blakestoddard committed Dec 8, 2020
1 parent c08b3c5 commit b5cd1fe
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package imageproxy

import (
"bytes"
"errors"
"fmt"
"image"
_ "image/gif" // register gif format
Expand Down Expand Up @@ -54,6 +55,18 @@ func Transform(img []byte, opt Options) ([]byte, error) {
return img, nil
}

// decode image metadata
imageMeta, _, err := image.DecodeConfig(bytes.NewReader(img))
if err != nil {
return nil, err
}

// prevent pixel flooding attacks
// accept no larger than a 10,000 x 10,000 image
if imageMeta.Width*imageMeta.Height > 100000000 {
return nil, errors.New("image too large")
}

// decode image
m, format, err := image.Decode(bytes.NewReader(img))
if err != nil {
Expand Down

0 comments on commit b5cd1fe

Please sign in to comment.