lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -0,0 +1,668 @@
|
||||
// Package imagecheck validates PNG and JPEG structure without decoding the
|
||||
// raster into an image. A full decode of a 40-megapixel image would allocate
|
||||
// hundreds of megabytes; attachment uploads must stay bounded. For PNG the
|
||||
// parser verifies chunk CRCs and streams the zlib image data to check exact
|
||||
// scanline sizes and filter types without keeping the raster in memory. The
|
||||
// JPEG parser verifies marker/scan structure. Both catch truncation and
|
||||
// corruption while using bounded memory.
|
||||
package imagecheck
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/zlib"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnsupported = errors.New("unsupported image format")
|
||||
ErrCorrupt = errors.New("corrupt image")
|
||||
ErrTooLarge = errors.New("image has too many pixels")
|
||||
)
|
||||
|
||||
const (
|
||||
pngSignature = "\x89PNG\r\n\x1a\n"
|
||||
maxChunkSize = 1 << 30
|
||||
)
|
||||
|
||||
// Info is the validated header. ContentType is one of image/png or image/jpeg.
|
||||
type Info struct {
|
||||
ContentType string
|
||||
Width int64
|
||||
Height int64
|
||||
}
|
||||
|
||||
func ValidateFile(path string, maxPixels int64) (Info, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return Info{}, fmt.Errorf("open upload: %w", err)
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
|
||||
return Validate(file, maxPixels)
|
||||
}
|
||||
|
||||
// Validate consumes the reader to EOF (so a caller hashing the stream gets all
|
||||
// bytes) and returns the detected format and dimensions.
|
||||
func Validate(r io.Reader, maxPixels int64) (Info, error) {
|
||||
reader := bufio.NewReaderSize(r, 64<<10)
|
||||
|
||||
header, err := reader.Peek(8)
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
return Info{}, fmt.Errorf("%w: image is empty or truncated", ErrUnsupported)
|
||||
}
|
||||
|
||||
return Info{}, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case string(header[:8]) == pngSignature:
|
||||
return validatePNG(reader, maxPixels)
|
||||
case header[0] == 0xFF && header[1] == 0xD8 && header[2] == 0xFF:
|
||||
return validateJPEG(reader, maxPixels)
|
||||
default:
|
||||
return Info{}, fmt.Errorf("%w: unrecognised signature", ErrUnsupported)
|
||||
}
|
||||
}
|
||||
|
||||
func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
|
||||
if _, err := reader.Discard(len(pngSignature)); err != nil {
|
||||
return Info{}, corrupt("png signature", err)
|
||||
}
|
||||
|
||||
var (
|
||||
info Info
|
||||
seen = make(map[string]bool, 4)
|
||||
first = true
|
||||
hasIDAT bool
|
||||
paletteReady bool
|
||||
trnsSeen bool
|
||||
colorType byte
|
||||
bitDepth byte
|
||||
paletteEntries int
|
||||
idat bytes.Buffer
|
||||
raster []pngPass
|
||||
rasterBytes int64
|
||||
maxIDAT int64 = -1
|
||||
lastChunk string
|
||||
)
|
||||
|
||||
info.ContentType = "image/png"
|
||||
|
||||
for {
|
||||
var lengthBytes [4]byte
|
||||
if _, err := io.ReadFull(reader, lengthBytes[:]); err != nil {
|
||||
return Info{}, corrupt("png chunk length", err)
|
||||
}
|
||||
|
||||
length := int64(binary.BigEndian.Uint32(lengthBytes[:]))
|
||||
if length < 0 || length > maxChunkSize {
|
||||
return Info{}, corruptf("png chunk length %d", length)
|
||||
}
|
||||
|
||||
var typeBytes [4]byte
|
||||
if _, err := io.ReadFull(reader, typeBytes[:]); err != nil {
|
||||
return Info{}, corrupt("png chunk type", err)
|
||||
}
|
||||
|
||||
chunkType := string(typeBytes[:])
|
||||
checksum := crc32.NewIEEE()
|
||||
_, _ = checksum.Write(typeBytes[:])
|
||||
|
||||
if first && chunkType != "IHDR" {
|
||||
return Info{}, corruptf("png first chunk is %q, want IHDR", chunkType)
|
||||
}
|
||||
|
||||
first = false
|
||||
|
||||
if seen[chunkType] && (chunkType == "IHDR" || chunkType == "PLTE" || chunkType == "IEND") {
|
||||
return Info{}, corruptf("png duplicate %s chunk", chunkType)
|
||||
}
|
||||
|
||||
if chunkType == "PLTE" && hasIDAT {
|
||||
return Info{}, corruptf("png PLTE chunk after IDAT")
|
||||
}
|
||||
|
||||
if chunkType == "IDAT" && seen["IDAT"] && lastChunk != "IDAT" {
|
||||
return Info{}, corruptf("png IDAT chunks must be consecutive")
|
||||
}
|
||||
|
||||
if err := checkCriticalChunk(chunkType, seen); err != nil {
|
||||
return Info{}, err
|
||||
}
|
||||
|
||||
var chunkSink io.Writer
|
||||
if chunkType == "IDAT" {
|
||||
if maxIDAT >= 0 && int64(idat.Len())+length > maxIDAT {
|
||||
return Info{}, corruptf("png idat compressed data exceeds %d bytes", maxIDAT)
|
||||
}
|
||||
|
||||
chunkSink = &idat
|
||||
}
|
||||
|
||||
data, err := readChunkData(reader, checksum, length, chunkSink)
|
||||
if err != nil {
|
||||
return Info{}, corrupt("png chunk data", err)
|
||||
}
|
||||
|
||||
var crcBytes [4]byte
|
||||
if _, err := io.ReadFull(reader, crcBytes[:]); err != nil {
|
||||
return Info{}, corrupt("png chunk crc", err)
|
||||
}
|
||||
|
||||
if binary.BigEndian.Uint32(crcBytes[:]) != checksum.Sum32() {
|
||||
return Info{}, corruptf("png chunk %q crc mismatch", chunkType)
|
||||
}
|
||||
|
||||
switch chunkType {
|
||||
case "IHDR":
|
||||
if length != 13 {
|
||||
return Info{}, corruptf("png IHDR length %d", length)
|
||||
}
|
||||
|
||||
info.Width = int64(binary.BigEndian.Uint32(data[0:4]))
|
||||
info.Height = int64(binary.BigEndian.Uint32(data[4:8]))
|
||||
colorType = data[9]
|
||||
bitDepth = data[8]
|
||||
|
||||
if info.Width <= 0 || info.Height <= 0 {
|
||||
return Info{}, corruptf("png dimensions %dx%d", info.Width, info.Height)
|
||||
}
|
||||
|
||||
// Short-circuit before multiplying: a malicious IHDR can contain
|
||||
// 32-bit dimensions whose product overflows int64.
|
||||
if maxPixels > 0 && (info.Width > maxPixels || info.Height > maxPixels || info.Width*info.Height > maxPixels) {
|
||||
return Info{}, fmt.Errorf("%w: %dx%d", ErrTooLarge, info.Width, info.Height)
|
||||
}
|
||||
|
||||
bound, err := pngRasterLayout(info, data[8], data[9], data[10], data[11], data[12])
|
||||
if err != nil {
|
||||
return Info{}, err
|
||||
}
|
||||
|
||||
raster = bound
|
||||
rasterBytes = pngRasterBytes(bound)
|
||||
if rasterBytes <= 1<<40 {
|
||||
// zlib overhead: ~5 bytes per 16 KiB block, 2-byte header and
|
||||
// a 4-byte Adler trailer. The 1 MiB constant covers tiny
|
||||
// images where the relative term vanishes.
|
||||
maxIDAT = rasterBytes + rasterBytes/256 + 1<<20
|
||||
}
|
||||
case "IDAT":
|
||||
hasIDAT = true
|
||||
case "PLTE":
|
||||
if colorType == 0 || colorType == 4 {
|
||||
return Info{}, corruptf("png PLTE is invalid for color type %d", colorType)
|
||||
}
|
||||
|
||||
if length == 0 || length%3 != 0 || length > 3*256 {
|
||||
return Info{}, corruptf("png PLTE length %d", length)
|
||||
}
|
||||
|
||||
if length/3 > 1<<bitDepth {
|
||||
return Info{}, corruptf(
|
||||
"png PLTE has %d entries for bit depth %d", length/3, bitDepth,
|
||||
)
|
||||
}
|
||||
|
||||
if trnsSeen {
|
||||
return Info{}, corruptf("png PLTE chunk after tRNS")
|
||||
}
|
||||
|
||||
paletteEntries = int(length / 3)
|
||||
|
||||
if !hasIDAT {
|
||||
paletteReady = true
|
||||
}
|
||||
case "tRNS":
|
||||
if trnsSeen {
|
||||
return Info{}, corruptf("png duplicate tRNS chunk")
|
||||
}
|
||||
|
||||
if hasIDAT {
|
||||
return Info{}, corruptf("png tRNS chunk after IDAT")
|
||||
}
|
||||
|
||||
switch colorType {
|
||||
case 0:
|
||||
if length != 2 {
|
||||
return Info{}, corruptf("png tRNS length %d for color type 0", length)
|
||||
}
|
||||
case 2:
|
||||
if length != 6 {
|
||||
return Info{}, corruptf("png tRNS length %d for color type 2", length)
|
||||
}
|
||||
case 3:
|
||||
if !paletteReady {
|
||||
return Info{}, corruptf("png tRNS before PLTE")
|
||||
}
|
||||
|
||||
if length > 256 {
|
||||
return Info{}, corruptf("png tRNS length %d", length)
|
||||
}
|
||||
|
||||
if int(length) > paletteEntries {
|
||||
return Info{}, corruptf(
|
||||
"png tRNS length %d exceeds %d palette entries",
|
||||
length, paletteEntries,
|
||||
)
|
||||
}
|
||||
default:
|
||||
return Info{}, corruptf("png tRNS invalid for color type %d", colorType)
|
||||
}
|
||||
|
||||
trnsSeen = true
|
||||
case "IEND":
|
||||
if length != 0 {
|
||||
return Info{}, corruptf("png IEND length %d", length)
|
||||
}
|
||||
|
||||
if !hasIDAT {
|
||||
return Info{}, corruptf("png has no IDAT chunk")
|
||||
}
|
||||
|
||||
if colorType == 3 && !paletteReady {
|
||||
return Info{}, corruptf("png palette image has no PLTE before IDAT")
|
||||
}
|
||||
|
||||
// The chunk walker proves structure, but a stream whose bytes
|
||||
// merely look like a chunk layout (valid length/type/CRC) may
|
||||
// still not be a PNG raster: the zlib payload must inflate to
|
||||
// exactly the IHDR-declared scanlines with valid filter bytes.
|
||||
if err := validateRaster(idat.Bytes(), raster); err != nil {
|
||||
return Info{}, err
|
||||
}
|
||||
|
||||
if _, err := io.Copy(io.Discard, reader); err != nil {
|
||||
return Info{}, corrupt("png trailing data", err)
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
lastChunk = chunkType
|
||||
}
|
||||
}
|
||||
|
||||
// pngPass is one consecutive block of scanlines in the inflated image stream.
|
||||
// Non-interlaced PNGs have a single pass; Adam7 has up to seven.
|
||||
type pngPass struct {
|
||||
rowBytes int64
|
||||
rows int64
|
||||
}
|
||||
|
||||
type pngPassSpec struct {
|
||||
x0, y0 int64
|
||||
dx, dy int64
|
||||
}
|
||||
|
||||
var adam7Passes = []pngPassSpec{
|
||||
{x0: 0, y0: 0, dx: 8, dy: 8},
|
||||
{x0: 4, y0: 0, dx: 8, dy: 8},
|
||||
{x0: 0, y0: 4, dx: 4, dy: 8},
|
||||
{x0: 2, y0: 0, dx: 4, dy: 4},
|
||||
{x0: 0, y0: 2, dx: 2, dy: 4},
|
||||
{x0: 1, y0: 0, dx: 2, dy: 2},
|
||||
{x0: 0, y0: 1, dx: 1, dy: 2},
|
||||
}
|
||||
|
||||
// pngRasterLayout returns the exact inflated size and row layout for the
|
||||
// IHDR. The Adam7 layout matters: a single non-interlaced formula either
|
||||
// rejects valid interlaced images or accepts oversized streams.
|
||||
func pngRasterLayout(
|
||||
info Info,
|
||||
bitDepth, colorType, compression, filter, interlace byte,
|
||||
) ([]pngPass, error) {
|
||||
if compression != 0 || filter != 0 {
|
||||
return nil, corruptf(
|
||||
"png compression/filter method %d/%d", compression, filter,
|
||||
)
|
||||
}
|
||||
|
||||
var bitsPerPixel int64
|
||||
|
||||
switch colorType {
|
||||
case 0: // grayscale
|
||||
if bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 && bitDepth != 16 {
|
||||
return nil, corruptf("png invalid bit depth %d for color type 0", bitDepth)
|
||||
}
|
||||
|
||||
bitsPerPixel = int64(bitDepth)
|
||||
case 2: // truecolor
|
||||
if bitDepth != 8 && bitDepth != 16 {
|
||||
return nil, corruptf("png invalid bit depth %d for color type 2", bitDepth)
|
||||
}
|
||||
|
||||
bitsPerPixel = 3 * int64(bitDepth)
|
||||
case 3: // palette
|
||||
if bitDepth != 1 && bitDepth != 2 && bitDepth != 4 && bitDepth != 8 {
|
||||
return nil, corruptf("png invalid bit depth %d for color type 3", bitDepth)
|
||||
}
|
||||
|
||||
bitsPerPixel = int64(bitDepth)
|
||||
case 4: // grayscale + alpha
|
||||
if bitDepth != 8 && bitDepth != 16 {
|
||||
return nil, corruptf("png invalid bit depth %d for color type 4", bitDepth)
|
||||
}
|
||||
|
||||
bitsPerPixel = 2 * int64(bitDepth)
|
||||
case 6: // RGBA
|
||||
if bitDepth != 8 && bitDepth != 16 {
|
||||
return nil, corruptf("png invalid bit depth %d for color type 6", bitDepth)
|
||||
}
|
||||
|
||||
bitsPerPixel = 4 * int64(bitDepth)
|
||||
default:
|
||||
return nil, corruptf("png invalid color type %d", colorType)
|
||||
}
|
||||
|
||||
specs := []pngPassSpec{{x0: 0, y0: 0, dx: 1, dy: 1}}
|
||||
switch interlace {
|
||||
case 0:
|
||||
case 1:
|
||||
specs = adam7Passes
|
||||
default:
|
||||
return nil, corruptf("png invalid interlace method %d", interlace)
|
||||
}
|
||||
|
||||
passes := make([]pngPass, 0, len(specs))
|
||||
|
||||
for _, spec := range specs {
|
||||
if info.Width <= spec.x0 || info.Height <= spec.y0 {
|
||||
continue
|
||||
}
|
||||
|
||||
width := (info.Width - spec.x0 + spec.dx - 1) / spec.dx
|
||||
rows := (info.Height - spec.y0 + spec.dy - 1) / spec.dy
|
||||
rowBytes := (width*bitsPerPixel + 7) / 8
|
||||
|
||||
// Keep the total overflow-safe. Valid PNGs are additionally bounded
|
||||
// by the pixel limit checked by the caller.
|
||||
if rowBytes+1 > (1<<62)/rows {
|
||||
return nil, fmt.Errorf("%w: png dimensions overflow", ErrTooLarge)
|
||||
}
|
||||
|
||||
passes = append(passes, pngPass{rowBytes: rowBytes, rows: rows})
|
||||
}
|
||||
|
||||
return passes, nil
|
||||
}
|
||||
|
||||
func pngRasterBytes(passes []pngPass) int64 {
|
||||
var total int64
|
||||
|
||||
for _, pass := range passes {
|
||||
row := (pass.rowBytes + 1) * pass.rows
|
||||
if total > (1<<62)-row {
|
||||
return 1<<62 - 1
|
||||
}
|
||||
|
||||
total += row
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
// validateRaster streams the inflated image data, checking every scanline
|
||||
// filter byte and requiring exactly the IHDR-declared number of rows. It
|
||||
// catches zlib streams that decompress cleanly but do not describe a valid
|
||||
// raster (empty payloads, bogus filter bytes) and rejects extra data as a
|
||||
// decompression bomb.
|
||||
func validateRaster(compressed []byte, passes []pngPass) error {
|
||||
source := bytes.NewReader(compressed)
|
||||
|
||||
reader, err := zlib.NewReader(source)
|
||||
if err != nil {
|
||||
return corrupt("png idat zlib header", err)
|
||||
}
|
||||
|
||||
for _, pass := range passes {
|
||||
for range pass.rows {
|
||||
var filter [1]byte
|
||||
if _, err := io.ReadFull(reader, filter[:]); err != nil {
|
||||
return corrupt("png scanline filter", err)
|
||||
}
|
||||
|
||||
if filter[0] > 4 {
|
||||
return corruptf("png invalid scanline filter %d", filter[0])
|
||||
}
|
||||
|
||||
if _, err := io.CopyN(io.Discard, reader, pass.rowBytes); err != nil {
|
||||
return corrupt("png scanline data", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var extra [1]byte
|
||||
switch _, err := io.ReadFull(reader, extra[:]); {
|
||||
case errors.Is(err, io.EOF):
|
||||
// A clean zlib EOF also verifies the Adler-32 trailer. bytes.Reader
|
||||
// satisfies io.ByteReader, so the inflater consumes exactly the zlib
|
||||
// stream; anything left is data after the stream, which PNG forbids.
|
||||
if source.Len() != 0 {
|
||||
return corruptf("png idat has %d trailing bytes", source.Len())
|
||||
}
|
||||
|
||||
return nil
|
||||
case err == nil:
|
||||
return fmt.Errorf("%w: png raster exceeds declared dimensions", ErrTooLarge)
|
||||
default:
|
||||
return corrupt("png idat zlib trailer", err)
|
||||
}
|
||||
}
|
||||
|
||||
func checkCriticalChunk(chunkType string, seen map[string]bool) error {
|
||||
if chunkType == "IHDR" || chunkType == "PLTE" || chunkType == "IDAT" || chunkType == "IEND" {
|
||||
seen[chunkType] = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if chunkType[0] >= 'A' && chunkType[0] <= 'Z' {
|
||||
return corruptf("png unknown critical chunk %q", chunkType)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readChunkData(reader io.Reader, checksum io.Writer, length int64, sink io.Writer) ([]byte, error) {
|
||||
if length <= 64<<10 {
|
||||
data := make([]byte, length)
|
||||
if _, err := io.ReadFull(reader, data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, _ = checksum.Write(data)
|
||||
if sink != nil {
|
||||
_, _ = sink.Write(data)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
destination := checksum
|
||||
if sink != nil {
|
||||
destination = io.MultiWriter(checksum, sink)
|
||||
}
|
||||
|
||||
if _, err := io.CopyN(destination, reader, length); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func validateJPEG(reader *bufio.Reader, maxPixels int64) (Info, error) {
|
||||
if _, err := reader.Discard(2); err != nil {
|
||||
return Info{}, corrupt("jpeg SOI", err)
|
||||
}
|
||||
|
||||
var (
|
||||
info Info
|
||||
hasSOF bool
|
||||
hasSOS bool
|
||||
pending byte
|
||||
markerErr error
|
||||
)
|
||||
|
||||
info.ContentType = "image/jpeg"
|
||||
|
||||
for {
|
||||
marker := pending
|
||||
pending = 0
|
||||
|
||||
if marker == 0 {
|
||||
marker, markerErr = nextMarker(reader)
|
||||
if markerErr != nil {
|
||||
return Info{}, corrupt("jpeg marker", markerErr)
|
||||
}
|
||||
}
|
||||
|
||||
switch {
|
||||
case marker == 0xD9: // EOI
|
||||
if !hasSOF || !hasSOS {
|
||||
return Info{}, corruptf("jpeg missing SOF or SOS")
|
||||
}
|
||||
|
||||
if _, err := io.Copy(io.Discard, reader); err != nil {
|
||||
return Info{}, corrupt("jpeg trailing data", err)
|
||||
}
|
||||
|
||||
return info, nil
|
||||
case isStandalone(marker):
|
||||
continue
|
||||
}
|
||||
|
||||
length, err := readUint16(reader)
|
||||
if err != nil {
|
||||
return Info{}, corrupt("jpeg segment length", err)
|
||||
}
|
||||
|
||||
if length < 2 {
|
||||
return Info{}, corruptf("jpeg segment %02x length %d", marker, length)
|
||||
}
|
||||
|
||||
data := make([]byte, int(length)-2)
|
||||
if _, err := io.ReadFull(reader, data); err != nil {
|
||||
return Info{}, corrupt("jpeg segment data", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case isSOF(marker):
|
||||
if len(data) < 5 {
|
||||
return Info{}, corruptf("jpeg SOF %02x is too short", marker)
|
||||
}
|
||||
|
||||
info.Height = int64(binary.BigEndian.Uint16(data[1:3]))
|
||||
info.Width = int64(binary.BigEndian.Uint16(data[3:5]))
|
||||
|
||||
if info.Width <= 0 || info.Height <= 0 {
|
||||
return Info{}, corruptf("jpeg dimensions %dx%d", info.Width, info.Height)
|
||||
}
|
||||
|
||||
hasSOF = true
|
||||
|
||||
if maxPixels > 0 && info.Width*info.Height > maxPixels {
|
||||
return Info{}, fmt.Errorf("%w: %dx%d", ErrTooLarge, info.Width, info.Height)
|
||||
}
|
||||
case marker == 0xDA: // SOS
|
||||
hasSOS = true
|
||||
|
||||
next, err := scanEntropy(reader)
|
||||
if err != nil {
|
||||
return Info{}, corrupt("jpeg scan", err)
|
||||
}
|
||||
|
||||
pending = next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func nextMarker(reader *bufio.Reader) (byte, error) {
|
||||
for {
|
||||
value, err := reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if value != 0xFF {
|
||||
return 0, fmt.Errorf("unexpected byte %02x before marker", value)
|
||||
}
|
||||
|
||||
value, err = reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if value == 0xFF {
|
||||
continue
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
|
||||
// scanEntropy consumes entropy-coded data after SOS and returns the next
|
||||
// non-RST marker (0xFF prefix consumed).
|
||||
func scanEntropy(reader *bufio.Reader) (byte, error) {
|
||||
for {
|
||||
value, err := reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if value != 0xFF {
|
||||
continue
|
||||
}
|
||||
|
||||
value, err = reader.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case value == 0x00:
|
||||
continue
|
||||
case value >= 0xD0 && value <= 0xD7:
|
||||
continue
|
||||
default:
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readUint16(reader io.Reader) (uint16, error) {
|
||||
var raw [2]byte
|
||||
if _, err := io.ReadFull(reader, raw[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return binary.BigEndian.Uint16(raw[:]), nil
|
||||
}
|
||||
|
||||
func isSOF(marker byte) bool {
|
||||
switch marker {
|
||||
case 0xC0, 0xC1, 0xC2, 0xC3, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isStandalone(marker byte) bool {
|
||||
return marker == 0x01 || (marker >= 0xD0 && marker <= 0xD7)
|
||||
}
|
||||
|
||||
func corrupt(context string, err error) error {
|
||||
return fmt.Errorf("%w: %s: %w", ErrCorrupt, context, err)
|
||||
}
|
||||
|
||||
func corruptf(format string, args ...any) error {
|
||||
return fmt.Errorf("%w: %s", ErrCorrupt, fmt.Sprintf(format, args...))
|
||||
}
|
||||
@@ -0,0 +1,483 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user