package imagecheck_test import ( "bytes" "compress/zlib" "encoding/binary" "hash/crc32" "image" "image/color" "image/jpeg" "image/png" "testing" "github.com/stretchr/testify/require" "mtusi-messenger/internal/imagecheck" ) func encodePNG(t *testing.T, width, height int) []byte { t.Helper() source := image.NewRGBA(image.Rect(0, 0, width, height)) for x := range width { for y := range height { source.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 128, A: 255}) } } var buffer bytes.Buffer require.NoError(t, png.Encode(&buffer, source)) return buffer.Bytes() } func encodeJPEG(t *testing.T, width, height int) []byte { t.Helper() source := image.NewRGBA(image.Rect(0, 0, width, height)) for x := range width { for y := range height { source.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 64, A: 255}) } } var buffer bytes.Buffer require.NoError(t, jpeg.Encode(&buffer, source, &jpeg.Options{Quality: 90})) return buffer.Bytes() } func pngChunk(ctype string, payload []byte) []byte { out := make([]byte, 12+len(payload)) binary.BigEndian.PutUint32(out[0:4], uint32(len(payload))) copy(out[4:8], ctype) copy(out[8:], payload) checksum := crc32.NewIEEE() _, _ = checksum.Write(out[4 : 8+len(payload)]) binary.BigEndian.PutUint32(out[8+len(payload):], checksum.Sum32()) return out } func pngFile(chunks ...[]byte) []byte { data := []byte(pngSignatureForTest) for _, chunk := range chunks { data = append(data, chunk...) } return data } const pngSignatureForTest = "\x89PNG\r\n\x1a\n" func TestValidatePNG(t *testing.T) { info, err := imagecheck.Validate(bytes.NewReader(encodePNG(t, 32, 20)), 40_000_000) require.NoError(t, err) require.Equal(t, "image/png", info.ContentType) require.Equal(t, int64(32), info.Width) require.Equal(t, int64(20), info.Height) } func TestValidateJPEG(t *testing.T) { info, err := imagecheck.Validate(bytes.NewReader(encodeJPEG(t, 24, 16)), 40_000_000) require.NoError(t, err) require.Equal(t, "image/jpeg", info.ContentType) require.Equal(t, int64(24), info.Width) require.Equal(t, int64(16), info.Height) } func TestValidateRejectsUnsupported(t *testing.T) { _, err := imagecheck.Validate(bytes.NewReader([]byte("not an image at all")), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrUnsupported) _, err = imagecheck.Validate(bytes.NewReader(nil), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrUnsupported) } func TestValidateRejectsTruncatedPNG(t *testing.T) { data := encodePNG(t, 64, 64) _, err := imagecheck.Validate(bytes.NewReader(data[:len(data)/2]), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsPNGCRCMismatch(t *testing.T) { data := encodePNG(t, 64, 64) index := bytes.Index(data, []byte("IDAT")) require.Positive(t, index) data[index+8] ^= 0xFF // first IDAT payload byte; CRC no longer matches _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } // Builds a structurally valid PNG (signature, well-formed IHDR/IDAT/IEND, // correct CRCs) whose IDAT payload is not a zlib stream at all. Merely a // stream-like structure must not make the upload pass: it has to inflate. func TestValidateRejectsPNGIDATNotAZlibStream(t *testing.T) { ihdr := make([]byte, 13) binary.BigEndian.PutUint32(ihdr[0:4], 64) // width binary.BigEndian.PutUint32(ihdr[4:8], 32) // height ihdr[8] = 8 // bit depth ihdr[9] = 6 // color type RGBA data := pngFile( pngChunk("IHDR", ihdr), pngChunk("IDAT", []byte("this is not a zlib stream, but the CRC is right")), pngChunk("IEND", nil), ) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func craftedPNG(t *testing.T, width, height int, interlace byte, raster []byte) []byte { t.Helper() return craftedPNGType(t, width, height, 6, interlace, raster) } func craftedPNGType(t *testing.T, width, height int, colorType, interlace byte, raster []byte) []byte { t.Helper() return craftedPNGDepth(t, width, height, 8, colorType, interlace, raster) } func craftedPNGDepth(t *testing.T, width, height int, bitDepth, colorType, interlace byte, raster []byte) []byte { t.Helper() return pngFile( pngChunk("IHDR", pngIHDR(width, height, bitDepth, colorType, interlace)), pngChunk("IDAT", zlibBytes(t, raster)), pngChunk("IEND", nil), ) } func pngIHDR(width, height int, bitDepth, colorType, interlace byte) []byte { ihdr := make([]byte, 13) binary.BigEndian.PutUint32(ihdr[0:4], uint32(width)) binary.BigEndian.PutUint32(ihdr[4:8], uint32(height)) ihdr[8] = bitDepth ihdr[9] = colorType ihdr[12] = interlace return ihdr } func zlibBytes(t *testing.T, raster []byte) []byte { t.Helper() var compressed bytes.Buffer writer := zlib.NewWriter(&compressed) _, err := writer.Write(raster) require.NoError(t, err) require.NoError(t, writer.Close()) return compressed.Bytes() } func TestValidateRejectsPNGEmptyRaster(t *testing.T) { data := craftedPNG(t, 2, 2, 0, nil) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsPNGInvalidScanlineFilter(t *testing.T) { raster := []byte{5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} data := craftedPNG(t, 2, 2, 0, raster) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsPNGPaletteWithoutPLTE(t *testing.T) { raster := []byte{0, 0, 0, 0, 0, 0} // two rows of two 8-bit palette indexes data := craftedPNGType(t, 2, 2, 3, 0, raster) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateAcceptsPalettePNG(t *testing.T) { palette := color.Palette{color.RGBA{R: 255, A: 255}, color.RGBA{G: 255, A: 255}, color.RGBA{B: 255, A: 255}} source := image.NewPaletted(image.Rect(0, 0, 13, 5), palette) for x := range 13 { for y := range 5 { source.SetColorIndex(x, y, uint8((x+y)%len(palette))) } } var buffer bytes.Buffer require.NoError(t, png.Encode(&buffer, source)) info, err := imagecheck.Validate(bytes.NewReader(buffer.Bytes()), 40_000_000) require.NoError(t, err) require.Equal(t, int64(13), info.Width) require.Equal(t, int64(5), info.Height) } func TestValidateRejectsPNGInvalidColorDepthCombination(t *testing.T) { data := craftedPNGDepth(t, 1, 1, 1, 6, 0, []byte{0, 0, 0}) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsPNGDuplicateIHDR(t *testing.T) { ihdr := pngChunk("IHDR", pngIHDR(1, 1, 8, 6, 0)) data := pngFile( ihdr, ihdr, pngChunk("IDAT", zlibBytes(t, []byte{0, 0, 0, 0, 0})), pngChunk("IEND", nil), ) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsPNGDuplicatePLTE(t *testing.T) { plte := pngChunk("PLTE", []byte{255, 0, 0}) data := pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 3, 0)), plte, plte, pngChunk("IDAT", zlibBytes(t, []byte{0, 0})), pngChunk("IEND", nil), ) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsPNGNonConsecutiveIDAT(t *testing.T) { compressed := zlibBytes(t, []byte{0, 0, 0, 0, 0}) middle := len(compressed) / 2 data := pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 6, 0)), pngChunk("IDAT", compressed[:middle]), pngChunk("tEXt", []byte("k\x00v")), pngChunk("IDAT", compressed[middle:]), pngChunk("IEND", nil), ) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsPNGTrailingIDATData(t *testing.T) { idat := append(zlibBytes(t, []byte{0, 0, 0, 0, 0}), []byte("junk")...) data := pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 6, 0)), pngChunk("IDAT", idat), pngChunk("IEND", nil), ) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateAcceptsSplitIDAT(t *testing.T) { compressed := zlibBytes(t, []byte{0, 0, 0, 0, 0}) middle := len(compressed) / 2 data := pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 6, 0)), pngChunk("IDAT", compressed[:middle]), pngChunk("IDAT", compressed[middle:]), pngChunk("IEND", nil), ) info, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.NoError(t, err) require.Equal(t, int64(1), info.Width) } func TestValidateAcceptsStandardEncodings(t *testing.T) { bounds := image.Rect(0, 0, 7, 5) images := []image.Image{ image.NewGray(bounds), image.NewGray16(bounds), image.NewRGBA(bounds), image.NewNRGBA(bounds), image.NewNRGBA64(bounds), } for _, source := range images { var buffer bytes.Buffer require.NoError(t, png.Encode(&buffer, source)) info, err := imagecheck.Validate(bytes.NewReader(buffer.Bytes()), 40_000_000) require.NoError(t, err) require.Equal(t, int64(7), info.Width) require.Equal(t, int64(5), info.Height) } } func TestValidateRejectsPNGPLTEEntriesExceedBitDepth(t *testing.T) { data := pngFile( pngChunk("IHDR", pngIHDR(1, 1, 1, 3, 0)), pngChunk("PLTE", []byte{255, 0, 0, 0, 255, 0, 0, 0, 255}), pngChunk("IDAT", zlibBytes(t, []byte{0, 0})), pngChunk("IEND", nil), ) _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsInvalidTRNS(t *testing.T) { rgb := pngChunk("IDAT", zlibBytes(t, []byte{0, 10, 20, 30})) gray := pngChunk("IDAT", zlibBytes(t, []byte{0, 10})) rgba := pngChunk("IDAT", zlibBytes(t, []byte{0, 10, 20, 30, 255})) paletted := pngChunk("IDAT", zlibBytes(t, []byte{0, 0})) cases := map[string][]byte{ "tRNS on RGBA": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 6, 0)), pngChunk("tRNS", []byte{0}), rgba, pngChunk("IEND", nil), ), "RGB length": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 2, 0)), pngChunk("tRNS", []byte{0}), rgb, pngChunk("IEND", nil), ), "gray length": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 0, 0)), pngChunk("tRNS", []byte{0}), gray, pngChunk("IEND", nil), ), "palette before PLTE": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 3, 0)), pngChunk("tRNS", []byte{0}), pngChunk("PLTE", []byte{255, 0, 0, 0, 255, 0}), paletted, pngChunk("IEND", nil), ), "palette tRNS longer than PLTE": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 3, 0)), pngChunk("PLTE", []byte{255, 0, 0}), pngChunk("tRNS", []byte{0, 0}), paletted, pngChunk("IEND", nil), ), "duplicate tRNS": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 2, 0)), pngChunk("tRNS", make([]byte, 6)), pngChunk("tRNS", make([]byte, 6)), rgb, pngChunk("IEND", nil), ), "tRNS after IDAT": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 2, 0)), rgb, pngChunk("tRNS", make([]byte, 6)), pngChunk("IEND", nil), ), } for name, data := range cases { _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt, name) } } func TestValidateAcceptsValidTRNS(t *testing.T) { cases := map[string][]byte{ "RGB": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 2, 0)), pngChunk("tRNS", make([]byte, 6)), pngChunk("IDAT", zlibBytes(t, []byte{0, 10, 20, 30})), pngChunk("IEND", nil), ), "gray": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 8, 0, 0)), pngChunk("tRNS", make([]byte, 2)), pngChunk("IDAT", zlibBytes(t, []byte{0, 10})), pngChunk("IEND", nil), ), "palette": pngFile( pngChunk("IHDR", pngIHDR(1, 1, 1, 3, 0)), pngChunk("PLTE", []byte{255, 0, 0, 0, 255, 0}), pngChunk("tRNS", []byte{128}), pngChunk("IDAT", zlibBytes(t, []byte{0, 0})), pngChunk("IEND", nil), ), } for name, data := range cases { _, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.NoError(t, err, name) } } // A valid Adam7 stream for a tall, narrow image exceeds the old // (rowBytes+1)*(height+8) guess; the exact pass layout must accept it. func TestValidateAcceptsAdam7PNG(t *testing.T) { const width, height = 2, 256 var raster bytes.Buffer passes := [][4]int{ {0, 0, 8, 8}, {4, 0, 8, 8}, {0, 4, 4, 8}, {2, 0, 4, 4}, {0, 2, 2, 4}, {1, 0, 2, 2}, {0, 1, 1, 2}, } for _, pass := range passes { x0, y0, dx, dy := pass[0], pass[1], pass[2], pass[3] if width <= x0 || height <= y0 { continue } for y := y0; y < height; y += dy { raster.WriteByte(0) // filter: none for x := x0; x < width; x += dx { raster.Write([]byte{byte(x), byte(y), 128, 255}) } } } data := craftedPNG(t, width, height, 1, raster.Bytes()) info, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000) require.NoError(t, err) require.Equal(t, int64(width), info.Width) require.Equal(t, int64(height), info.Height) } func TestValidateRejectsTruncatedJPEG(t *testing.T) { data := encodeJPEG(t, 64, 64) _, err := imagecheck.Validate(bytes.NewReader(data[:len(data)-32]), 40_000_000) require.ErrorIs(t, err, imagecheck.ErrCorrupt) } func TestValidateRejectsTooManyPixels(t *testing.T) { _, err := imagecheck.Validate(bytes.NewReader(encodePNG(t, 100, 100)), 9_999) require.ErrorIs(t, err, imagecheck.ErrTooLarge) _, err = imagecheck.Validate(bytes.NewReader(encodeJPEG(t, 100, 100)), 9_999) require.ErrorIs(t, err, imagecheck.ErrTooLarge) } func TestValidateAllowsTrailingBytes(t *testing.T) { pngData := append(encodePNG(t, 8, 8), []byte("trailing")...) _, err := imagecheck.Validate(bytes.NewReader(pngData), 40_000_000) require.NoError(t, err) jpegData := append(encodeJPEG(t, 8, 8), []byte("trailing")...) _, err = imagecheck.Validate(bytes.NewReader(jpegData), 40_000_000) require.NoError(t, err) }