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
+130 -14
View File
@@ -141,24 +141,41 @@ func craftedPNG(t *testing.T, width, height int, interlace byte, raster []byte)
func craftedPNGType(t *testing.T, width, height int, colorType, interlace byte, raster []byte) []byte {
t.Helper()
var idat bytes.Buffer
writer := zlib.NewWriter(&idat)
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())
ihdr := make([]byte, 13)
binary.BigEndian.PutUint32(ihdr[0:4], uint32(width))
binary.BigEndian.PutUint32(ihdr[4:8], uint32(height))
ihdr[8] = 8 // bit depth
ihdr[9] = colorType
ihdr[12] = interlace
return pngFile(
pngChunk("IHDR", ihdr),
pngChunk("IDAT", idat.Bytes()),
pngChunk("IEND", nil),
)
return compressed.Bytes()
}
func TestValidateRejectsPNGEmptyRaster(t *testing.T) {
@@ -202,6 +219,105 @@ func TestValidateAcceptsPalettePNG(t *testing.T) {
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)
}
}
// 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) {