lab5: закрытие остаточных замечаний
Validate tRNS semantics and bind PLTE entry count to the IHDR bit depth so malformed PNGs are rejected with 415 instead of failing in the worker. Refuse replay of an older failed version while a newer pipeline job exists, preventing loss of the shared staging source, and retire version-stale queued jobs as superseded instead of republishing them forever.
This commit is contained in:
@@ -82,7 +82,9 @@ func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
|
||||
first = true
|
||||
hasIDAT bool
|
||||
paletteReady bool
|
||||
trnsSeen bool
|
||||
colorType byte
|
||||
bitDepth byte
|
||||
idat bytes.Buffer
|
||||
raster []pngPass
|
||||
rasterBytes int64
|
||||
@@ -166,6 +168,7 @@ func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
|
||||
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)
|
||||
@@ -201,9 +204,50 @@ func validatePNG(reader *bufio.Reader, maxPixels int64) (Info, error) {
|
||||
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")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
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)
|
||||
|
||||
@@ -318,6 +318,100 @@ func TestValidateAcceptsStandardEncodings(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
),
|
||||
"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) {
|
||||
|
||||
Reference in New Issue
Block a user