lab5: завершение второго ревью

PNG: validate color-type/bit-depth pairs, reject duplicate IHDR/PLTE, non-consecutive IDAT, PLTE after IDAT, and trailing bytes after the zlib stream; keep accepting valid palette, split-IDAT, 16-bit, and Adam7 uploads. Replay: a newer failed unpublished version returns the attachment to queued, while a published ready version stays serving.
This commit is contained in:
Vladislav Kunetskiy
2026-10-31 01:22:22 +03:00
parent 8c0a455aa5
commit 624f76b7d0
15 changed files with 406 additions and 61 deletions
+45 -8
View File
@@ -87,6 +87,7 @@ func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
raster []pngPass
rasterBytes int64
maxIDAT int64 = -1
lastChunk string
)
info.ContentType = "image/png"
@@ -117,6 +118,18 @@ func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
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
}
@@ -218,6 +231,8 @@ func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
return info, nil
}
lastChunk = chunkType
}
}
@@ -260,25 +275,39 @@ func pngRasterLayout(
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)
}
switch bitDepth {
case 1, 2, 4, 8, 16:
default:
return nil, corruptf("png invalid bit depth %d", bitDepth)
}
specs := []pngPassSpec{{x0: 0, y0: 0, dx: 1, dy: 1}}
switch interlace {
case 0:
@@ -332,7 +361,9 @@ func pngRasterBytes(passes []pngPass) int64 {
// raster (empty payloads, bogus filter bytes) and rejects extra data as a
// decompression bomb.
func validateRaster(compressed []byte, passes []pngPass) error {
reader, err := zlib.NewReader(bytes.NewReader(compressed))
source := bytes.NewReader(compressed)
reader, err := zlib.NewReader(source)
if err != nil {
return corrupt("png idat zlib header", err)
}
@@ -357,7 +388,13 @@ func validateRaster(compressed []byte, passes []pngPass) error {
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.
// 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)