lab5: исправления второго ревью

Reprocess of a queued/processing/failed upload now sources from verified staging instead of the not-yet-written final original, which used to supersede v1 and strand the attachment in queued. PNG uploads now inflate the concatenated IDAT stream (bounded by IHDR dimensions) before acceptance, so a structurally valid PNG whose IDAT is not a zlib stream is rejected with 415 instead of 202.
This commit is contained in:
Vladislav Kunetskiy
2026-10-31 00:41:38 +03:00
parent ecdd91a15b
commit b965350cc9
13 changed files with 311 additions and 44 deletions
+33
View File
@@ -2,6 +2,8 @@ package imagecheck_test
import (
"bytes"
"encoding/binary"
"hash/crc32"
"image"
"image/color"
"image/jpeg"
@@ -85,6 +87,37 @@ func TestValidateRejectsPNGCRCMismatch(t *testing.T) {
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) {
chunk := func(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
}
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 := append([]byte("\x89PNG\r\n\x1a\n"), chunk("IHDR", ihdr)...)
data = append(data, chunk("IDAT", []byte("this is not a zlib stream, but the CRC is right"))...)
data = append(data, chunk("IEND", nil)...)
_, err := imagecheck.Validate(bytes.NewReader(data), 40_000_000)
require.ErrorIs(t, err, imagecheck.ErrCorrupt)
}
func TestValidateRejectsTruncatedJPEG(t *testing.T) {
data := encodeJPEG(t, 64, 64)
_, err := imagecheck.Validate(bytes.NewReader(data[:len(data)-32]), 40_000_000)