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)