lab5: очереди и асинхронная обработка изображений

Durable staging handoff, transactional outbox, at-least-once worker with lease fencing, idempotent versioned pipeline, three variants, operator tooling, contract tests and evidence. Includes the review fixes: reprocess source selection and failure settlement, replay guards, stale-job retirement, PNG structural/raster validation, migration rollback safety and audit events.
This commit is contained in:
Vladislav Kunetskiy
2026-10-31 14:34:24 +03:00
parent 6c5ba026f1
commit 01b885e846
99 changed files with 10164 additions and 1452 deletions
+114 -4
View File
@@ -5,10 +5,12 @@ Run against a live stand:
python3 student-tests/attachments_negative.py --base-url http://localhost:8080
"""
import hashlib
import struct
import sys
import urllib.error
import urllib.request
import uuid
import zlib
from pathlib import Path
from urllib.parse import quote
@@ -19,11 +21,36 @@ from client import Client, Failure, arguments, check, run # noqa: E402
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
def upload(client, actor, chat, content, filename='sample.png', content_type='image/png', expected=201):
def upload(client, actor, chat, content, filename='sample.png', content_type='image/png', expected=202):
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + quote(filename, safe='')
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, bit_depth=8, color_type=6):
"""PNG with correct chunk lengths and CRCs around arbitrary IDAT bytes."""
return crafted_png_extra(
width, height, raster, bit_depth=bit_depth, color_type=color_type,
interlace=interlace,
)
def crafted_png_extra(width, height, raster, pre_idat=(), bit_depth=8, color_type=6, interlace=0):
"""PNG whose IDAT is preceded by caller-supplied chunks (PLTE/tRNS/...)."""
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)
+ b''.join(pre_idat)
+ 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)
@@ -49,6 +76,87 @@ def main():
upload(client, alice, chat, fixture, filename='photo.jpg', content_type='image/jpeg', expected=415)
print('MIME spoof (PNG declared as JPEG): 415')
# Structurally valid PNG (chunk layout, lengths, CRCs all correct) whose
# IDAT payload is not a zlib stream at all. Structure-checking alone must
# 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.
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')
# 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')
# tRNS is forbidden for alpha color types and has fixed lengths otherwise.
rgba_trns = crafted_png_extra(
1, 1, b'\x00' + b'\x00' * 4,
pre_idat=(png_chunk(b'tRNS', b'\x00'),), color_type=6,
)
upload(client, alice, chat, rgba_trns, expected=415)
print('tRNS on RGBA PNG: 415')
rgb_trns = crafted_png_extra(
1, 1, b'\x00' + b'\x00' * 3,
pre_idat=(png_chunk(b'tRNS', b'\x00'),), color_type=2,
)
upload(client, alice, chat, rgb_trns, expected=415)
print('short tRNS on RGB PNG: 415')
# A 1-bit palette cannot address three entries.
palette_overflow = crafted_png_extra(
1, 1, b'\x00\x00',
pre_idat=(png_chunk(b'PLTE', bytes((255, 0, 0, 0, 255, 0, 0, 0, 255))),),
bit_depth=1, color_type=3,
)
upload(client, alice, chat, palette_overflow, expected=415)
print('PLTE entries exceed palette bit depth: 415')
# Palette transparency cannot describe more entries than PLTE provides.
palette_trns = crafted_png_extra(
1, 1, b'\x00\x00',
pre_idat=(
png_chunk(b'PLTE', bytes((255, 0, 0))),
png_chunk(b'tRNS', b'\x00\x00'),
),
bit_depth=8, color_type=3,
)
upload(client, alice, chat, palette_trns, expected=415)
print('palette tRNS exceeds PLTE entries: 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)
@@ -92,16 +200,18 @@ def main():
)
print('duplicate attachment ids: 400')
# The only variant in the lab4 contract is original; other values are 400.
# Lab5 exposes the derived variants after ready; queued content is a conflict.
own, content = client.attachment(alice, chat)
client.call('GET', '/api/v1/attachments/' + own['id'] + '/content?variant=thumbnail', 400, actor=alice)
client.call('GET', '/api/v1/attachments/' + own['id'] + '/content', 409, actor=alice)
own = client.wait_ready(alice, own)
client.call('GET', '/api/v1/attachments/' + own['id'] + '/content?variant=thumbnail', 200, actor=alice)
# Attachment-only message is valid.
client.message(alice, chat, '', attachments=[own['id']])
print('attachment-only message: 201')
# Object keys are server-generated; anonymous direct access must be denied.
key = 'attachments/' + chat['id'] + '/' + own['id']
key = 'attachments/' + chat['id'] + '/' + own['id'] + '/original'
request = urllib.request.Request('http://127.0.0.1:8333/messenger-attachments/' + key, method='GET')
try:
urllib.request.urlopen(request, timeout=5)