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:
Vladislav Kunetskiy
2026-10-31 01:06:43 +03:00
parent b965350cc9
commit 8c0a455aa5
14 changed files with 478 additions and 112 deletions
+29 -15
View File
@@ -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)
+21
View File
@@ -120,6 +120,27 @@ def main():
'old job not superseded or new job not ready')
print('worker up: reprocessed upload reached ready at pipeline 2, v1 superseded')
# 5. A pipeline version the worker cannot execute must be refused by the
# operator CLI before any state change; the accepted v1 upload keeps its
# job and finishes normally instead of being superseded into queued.
compose('stop', 'worker')
try:
preserved, _ = client.attachment(alice, chat)
check(preserved['status'] == 'queued', 'unsupported-pipeline fixture must be queued')
refused = jobctl('reprocess', '-attachment', preserved['id'], '-pipeline', '3', check=False)
check(refused.returncode != 0 and 'unsupported pipeline version 3' in refused.stderr,
'unsupported pipeline version was not rejected')
check(psql("select status from attachment_jobs where attachment_id='%s' and pipeline_version=1" % preserved['id']) == 'queued',
'v1 job changed by rejected reprocess')
print('worker down: reprocess pipeline 3 refused, v1 job untouched')
finally:
compose('up', '-d', '--no-deps', 'worker', check=False)
client.wait_ready(alice, preserved, wait_seconds=90)
check(psql("select pipeline_version from attachments where id='%s'" % preserved['id']) == '1',
'rejected reprocess changed serving version')
print('worker up: preserved upload reached ready at pipeline 1')
print('PASS: lab5 async worker/broker durability')