Handle PNG EXIF and distinguish transient encoder crashes from invalid images
This commit is contained in:
@@ -3,6 +3,7 @@ package messenger
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
@@ -70,3 +71,48 @@ func TestTransformAlphaAndEXIF(t *testing.T) {
|
||||
t.Fatalf("EXIF ignored: %v", img.Bounds())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPNGEXIFAndMalformedIFD(t *testing.T) {
|
||||
tiff := []byte{'I', 'I', 42, 0, 8, 0, 0, 0, 1, 0, 0x12, 1, 3, 0, 1, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0}
|
||||
if tiffOrientation(tiff) != 6 {
|
||||
t.Fatal("TIFF orientation")
|
||||
}
|
||||
for i := 0; i < len(tiff); i++ {
|
||||
_ = tiffOrientation(tiff[:i])
|
||||
}
|
||||
bad := append([]byte{}, tiff...)
|
||||
for i := 4; i < 8; i++ {
|
||||
bad[i] = 255
|
||||
}
|
||||
if tiffOrientation(bad) != 1 {
|
||||
t.Fatal("bad IFD offset")
|
||||
}
|
||||
var raw bytes.Buffer
|
||||
png.Encode(&raw, image.NewNRGBA(image.Rect(0, 0, 320, 200)))
|
||||
var chunk bytes.Buffer
|
||||
binary.Write(&chunk, binary.BigEndian, uint32(len(tiff)))
|
||||
chunk.WriteString("eXIf")
|
||||
chunk.Write(tiff)
|
||||
binary.Write(&chunk, binary.BigEndian, crc32.ChecksumIEEE(chunk.Bytes()[4:]))
|
||||
data := append(append(append([]byte{}, raw.Bytes()[:33]...), chunk.Bytes()...), raw.Bytes()[33:]...)
|
||||
dir := t.TempDir()
|
||||
input := filepath.Join(dir, "input.png")
|
||||
os.WriteFile(input, data, 0600)
|
||||
if pngOrientation(input) != 6 {
|
||||
t.Fatal("PNG eXIf orientation")
|
||||
}
|
||||
if _, e := exec.LookPath("cwebp"); e != nil {
|
||||
return
|
||||
}
|
||||
if e := Transform(input, dir); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
b, _ := os.ReadFile(filepath.Join(dir, "thumbnail"))
|
||||
img, e := jpeg.Decode(bytes.NewReader(b))
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if img.Bounds().Dx() != 160 || img.Bounds().Dy() != 256 {
|
||||
t.Fatal("PNG orientation not applied", img.Bounds())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user