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
+27 -2
View File
@@ -31,9 +31,9 @@ def png_chunk(ctype, payload):
return struct.pack('>I', len(payload)) + ctype + payload + struct.pack('>I', crc)
def crafted_png(width, height, interlace, raster):
def crafted_png(width, height, interlace, raster, bit_depth=8, color_type=6):
"""PNG with correct chunk lengths and CRCs around arbitrary IDAT bytes."""
ihdr = struct.pack('>IIBBBBB', width, height, 8, 6, 0, 0, interlace)
ihdr = struct.pack('>IIBBBBB', width, height, bit_depth, color_type, 0, 0, interlace)
return (
b'\x89PNG\r\n\x1a\n'
+ png_chunk(b'IHDR', ihdr)
@@ -87,6 +87,31 @@ def main():
upload(client, alice, chat, crafted_png(2, 2, 0, invalid_filter), expected=415)
print('valid zlib, invalid PNG scanline filter: 415')
# RGBA forbids bit depth 1; every chunk CRC is correct.
upload(client, alice, chat, crafted_png(1, 1, 0, b'\x00', bit_depth=1), expected=415)
print('invalid PNG color type/bit depth pair: 415')
# Duplicate IHDR is forbidden even with correct CRCs.
ihdr = png_chunk(b'IHDR', struct.pack('>IIBBBBB', 1, 1, 8, 6, 0, 0, 0))
duplicate_header = (
b'\x89PNG\r\n\x1a\n'
+ ihdr + ihdr
+ png_chunk(b'IDAT', zlib.compress(b'\x00' + b'\x00' * 4))
+ png_chunk(b'IEND', b'')
)
upload(client, alice, chat, duplicate_header, expected=415)
print('duplicate PNG IHDR: 415')
# Bytes after the zlib stream inside IDAT are not part of a valid raster.
trailing = (
b'\x89PNG\r\n\x1a\n'
+ png_chunk(b'IHDR', struct.pack('>IIBBBBB', 1, 1, 8, 6, 0, 0, 0))
+ png_chunk(b'IDAT', zlib.compress(b'\x00' + b'\x00' * 4) + b'junk')
+ png_chunk(b'IEND', b'')
)
upload(client, alice, chat, trailing, expected=415)
print('trailing bytes after PNG zlib stream: 415')
# Dangerous display names.
for name in ('../x.png', 'a/b.png', 'a\\b.png', 'a\x00.png', 'a\n.png'):
upload(client, alice, chat, fixture, filename=name, expected=400)