lab5: исправления второго ревью

Reprocess of a queued/processing/failed upload now sources from verified staging instead of the not-yet-written final original, which used to supersede v1 and strand the attachment in queued. PNG uploads now inflate the concatenated IDAT stream (bounded by IHDR dimensions) before acceptance, so a structurally valid PNG whose IDAT is not a zlib stream is rejected with 415 instead of 202.
This commit is contained in:
Vladislav Kunetskiy
2026-10-31 00:41:38 +03:00
parent ecdd91a15b
commit b965350cc9
13 changed files with 311 additions and 44 deletions
+24
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
@@ -49,6 +51,28 @@ 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.
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)
print('structurally valid PNG with non-zlib IDAT: 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)
+24 -1
View File
@@ -12,7 +12,7 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
sys.path.insert(0, str(Path(__file__).resolve().parent))
from client import Client, Failure, arguments, check, run # noqa: E402
from tests_helpers import ROOT, compose, psql # noqa: E402
from tests_helpers import ROOT, compose, jobctl, psql # noqa: E402
FIXTURE = Path(__file__).resolve().parents[1] / 'tests/fixtures/sample.png'
@@ -97,6 +97,29 @@ def main():
check(body['bytes'] == content, 'original lost across restart')
print('stack restart: ready attachment and original preserved')
# 4. Reprocess of a queued upload while the worker is down. The final
# original has never been written, so pipeline 2 must source from staging;
# pointing at the not-yet-existing final original superseded v1 and left
# the attachment queued forever with v2 failed (source_missing).
compose('stop', 'worker')
try:
pinned, _ = client.attachment(alice, chat)
check(pinned['status'] == 'queued', 'reprocess fixture must be queued')
result = jobctl('reprocess', '-attachment', pinned['id'], '-pipeline', '2')
check('SourceKey' in result.stdout, 'reprocess did not return the created job')
queued2 = client.call('GET', '/api/v1/attachments/' + pinned['id'], actor=alice, schema='Attachment')['json']
check(queued2['status'] == 'queued', 'reprocessed upload is not queued')
print('worker down: reprocess pipeline 2 accepted, upload still queued')
finally:
compose('up', '-d', '--no-deps', 'worker', check=False)
ready2 = client.wait_ready(alice, pinned, wait_seconds=90)
check(psql("select pipeline_version from attachments where id='%s'" % pinned['id']) == '2',
'serving pipeline version is not 2 after reprocess')
check(psql("select status from attachment_jobs where attachment_id='%s' order by pipeline_version" % pinned['id']) == 'superseded\nready',
'old job not superseded or new job not ready')
print('worker up: reprocessed upload reached ready at pipeline 2, v1 superseded')
print('PASS: lab5 async worker/broker durability')