lab5: закрытие второго ревью
Validate the PNG raster itself: exact Adam7/non-interlaced row layout, scanline filter bytes, palette/PLTE rules, bounded streaming without keeping the raster. Settle an attachment as failed when a newer pipeline fails before any version was published, while keeping a ready serving version intact. Reject unsupported pipeline versions in jobctl before changing state.
This commit is contained in:
@@ -26,6 +26,22 @@ def upload(client, actor, chat, content, filename='sample.png', content_type='im
|
||||
return client.call('POST', path, expected, actor=actor, raw=content, headers={'Content-Type': content_type})
|
||||
|
||||
|
||||
def png_chunk(ctype, payload):
|
||||
crc = zlib.crc32(ctype + payload) & 0xffffffff
|
||||
return struct.pack('>I', len(payload)) + ctype + payload + struct.pack('>I', crc)
|
||||
|
||||
|
||||
def crafted_png(width, height, interlace, raster):
|
||||
"""PNG with correct chunk lengths and CRCs around arbitrary IDAT bytes."""
|
||||
ihdr = struct.pack('>IIBBBBB', width, height, 8, 6, 0, 0, interlace)
|
||||
return (
|
||||
b'\x89PNG\r\n\x1a\n'
|
||||
+ png_chunk(b'IHDR', ihdr)
|
||||
+ png_chunk(b'IDAT', zlib.compress(raster))
|
||||
+ png_chunk(b'IEND', b'')
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Attachment negative scenarios (lab4)')
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
@@ -56,23 +72,21 @@ def main():
|
||||
# not accept it: the stream has to inflate. Regression for a review
|
||||
# finding that used to produce 202 queued and then failed/invalid_image
|
||||
# in the worker.
|
||||
def craft_not_a_zlib_png():
|
||||
def chunk(ctype, payload):
|
||||
crc = zlib.crc32(ctype + payload) & 0xffffffff
|
||||
return struct.pack('>I', len(payload)) + ctype + payload + struct.pack('>I', crc)
|
||||
|
||||
ihdr = struct.pack('>IIBBBBB', 64, 32, 8, 6, 0, 0, 0) # 64x32, RGBA
|
||||
return (
|
||||
b'\x89PNG\r\n\x1a\n'
|
||||
+ chunk(b'IHDR', ihdr)
|
||||
+ chunk(b'IDAT', b'this is not a zlib stream, but the CRC is right')
|
||||
+ chunk(b'IEND', b'')
|
||||
)
|
||||
|
||||
|
||||
upload(client, alice, chat, craft_not_a_zlib_png(), expected=415)
|
||||
upload(
|
||||
client, alice, chat,
|
||||
crafted_png(64, 32, 0, b'this is not a zlib stream'),
|
||||
expected=415,
|
||||
)
|
||||
print('structurally valid PNG with non-zlib IDAT: 415')
|
||||
|
||||
# Correct zlib stream that is not a valid PNG raster: an empty payload
|
||||
# and a scanline with an undefined filter byte must both be 415, not 202.
|
||||
upload(client, alice, chat, crafted_png(2, 2, 0, b''), expected=415)
|
||||
print('valid zlib, empty PNG raster: 415')
|
||||
invalid_filter = b'\x05' + b'\x00' * 8 + b'\x00' + b'\x00' * 8
|
||||
upload(client, alice, chat, crafted_png(2, 2, 0, invalid_filter), expected=415)
|
||||
print('valid zlib, invalid PNG scanline filter: 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)
|
||||
|
||||
Reference in New Issue
Block a user