#!/usr/bin/env python3 """Негативные и граничные сценарии вложений (уровень 4) через LB: ни один из них не должен дать 500. Lab5: принятый файл — 202 queued, байты и варианты доступны после ready (worker). Изображения генерируются кодом (PNG — zlib/struct), JPEG — синтетический fixtures/sample.jpg (sips из sample.png). Запуск: python3 student-tests/attachments.py --base-url http://localhost:8080""" import hashlib import http.client import os from pathlib import Path import struct import sys import time import urllib.parse import uuid import zlib sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests')) from client import Client, ROOT, arguments, check, run # noqa: E402 MiB = 1024 * 1024 SAMPLE_PNG = (ROOT / 'tests/fixtures/sample.png').read_bytes() SAMPLE_JPG = (Path(__file__).parent / 'fixtures/sample.jpg').read_bytes() def chunk(kind, data): return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data)) def png(width, height, raw, depth=8, color=2, level=1): """PNG из готовых распакованных строк raw (байт фильтра + пиксели).""" ihdr = struct.pack('>IIBBBBB', width, height, depth, color, 0, 0, 0) return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', ihdr) + chunk(b'IDAT', zlib.compress(raw, level)) + chunk(b'IEND', b'') def noise_png(side): """RGB-шум не сжимается: размер файла ≈ side² × 3 байт.""" row = 1 + side * 3 return png(side, side, b''.join(b'\0' + os.urandom(row - 1) for _ in range(side))) def gray1(width, height): """1-битный серый PNG из нулей: 40 Мпикс занимают несколько килобайт файла.""" return png(width, height, bytes(height * (1 + (width + 7) // 8)), depth=1, color=0, level=9) def jpeg_with_size(data, width, height): """Подменить размеры в SOF0 существующего JPEG.""" i = data.index(b'\xff\xc0') return data[:i + 5] + struct.pack('>HH', height, width) + data[i + 9:] def jpeg_without(data, marker): """Вырезать из JPEG все сегменты с маркером marker, стоящие до SOS.""" out, i = data[:2], 2 while data[i + 1] != 0xda: size = 2 + struct.unpack('>H', data[i + 2:i + 4])[0] if data[i + 1] != marker: out += data[i:i + size] i += size return out + data[i:] def bare_jpeg(): """27 байт: SOI, SOF0 1x1, SOS, EOI — без таблиц DQT/DHT и без данных скана.""" return (b'\xff\xd8' + b'\xff\xc0' + struct.pack('>HBHHB', 11, 8, 1, 1, 1) + b'\x01\x11\x00' + b'\xff\xda' + struct.pack('>HB', 8, 1) + b'\x01\x00\x00\x3f\x00' + b'\xff\xd9') def main(): args = arguments('Attachment negative suite') c = Client(args.base_url, args.ca_file, timeout=30) alice, bob, eve, chat = c.bootstrap() other = c.call('POST', '/api/v1/chats', 201, actor=alice, body={'title': 'Другой чат', 'member_ids': [alice['id']]}, schema='Chat')['json'] results = [] def upload(data, expected, ctype='image/png', name='file.png', actor=alice, target=chat, label=None): path = '/api/v1/chats/' + target['id'] + '/attachments?filename=' + urllib.parse.quote(name, safe='') r = c.call('POST', path, expected, actor=actor, raw=data, headers={'Content-Type': ctype}, schema='Attachment' if expected == 202 else None) results.append((label or name, r['status'], (r['json'] or {}).get('error', {}).get('code', ''), round(r['elapsed'] * 1000))) if expected == 202: att = r['json'] check(att['sha256'] == hashlib.sha256(data).hexdigest() and att['size_bytes'] == len(data), label + ': checksum/size') check(att['content_type'] == ctype and att['filename'] == name, label + ': metadata') check(att['status'] == 'queued' and att['variants'] == [] and att['error_code'] is None, label + ': 202 snapshot') return att return r['json'] def download(att, actor=bob, expect=None): att = c.wait_ready(actor, att, 120) r = c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=actor) check(r['bytes'] == expect and hashlib.sha256(r['bytes']).hexdigest() == att['sha256'], 'download differs') check(r['headers'].get_content_type() == att['content_type'], 'download content type') check(r['headers'].get('X-Content-Type-Options') == 'nosniff', 'missing nosniff') check(not r['headers'].get('Location'), 'download must not redirect') # Принимаются реальные PNG/JPEG, граница по размеру и по пикселям. jpg = upload(SAMPLE_JPG, 202, 'image/jpeg', 'фото 📷 2026.jpg', label='real JPEG + unicode filename') download(jpg, expect=SAMPLE_JPG) big = noise_png(1860) check(9 * MiB < len(big) <= 10 * MiB, 'big fixture size %d' % len(big)) big_att = upload(big, 202, label='PNG %.2f MiB (noise 1860x1860)' % (len(big) / MiB)) download(big_att, expect=big) huge = upload(gray1(6400, 6250), 202, label='PNG ровно 40 000 000 px') huge = c.wait_ready(bob, huge, 120) # worker обрабатывает предельный по пикселям файл в своих лимитах dims = {v['name']: (v['width'], v['height']) for v in huge['variants']} check(dims == {'thumbnail': (256, 250), 'crop': (128, 128), 'optimized': (1280, 1250)}, 'variants of 6400x6250: %s' % dims) # Лимиты: до чтения тела (Content-Length) и по заголовку изображения до распаковки. upload(noise_png(1900), 413, label='PNG 10.33 MiB > 10 MiB') upload(os.urandom(10 * MiB + 1), 413, label='10 MiB + 1 байт') upload(gray1(6400, 6251), 413, label='PNG 40 006 400 px') bomb = gray1(30000, 30000) upload(bomb, 413, label='pixel-bomb 30000x30000, файл %d КБ' % (len(bomb) // 1024)) upload(jpeg_with_size(SAMPLE_JPG, 10000, 10000), 413, 'image/jpeg', 'bomb.jpg', label='JPEG SOF 10000x10000') # IHDR обещает 100x100, а IDAT распаковывается в 64 МБ нулей: распаковка останавливается на лишних данных. upload(png(100, 100, bytes(64 * MiB), level=9), 415, label='zip-bomb в IDAT (64 МБ при 100x100)') # Повреждённые файлы и подмена типа. bad_crc = bytearray(SAMPLE_PNG) bad_crc[1000] ^= 0xFF upload(bytes(bad_crc), 415, label='PNG: байт IDAT испорчен (CRC)') upload(png(320, 200, os.urandom(1000)), 415, label='PNG: корректные CRC, мусор в zlib') upload(png(320, 200, bytes(100)), 415, label='PNG: данных меньше, чем пикселей') upload(SAMPLE_PNG[:-200], 415, label='PNG обрезан') upload(SAMPLE_PNG + b'', 415, label='PNG + данные после IEND') upload(png(1, 1, b'\x05\0\0\0'), 415, label='PNG 1x1: фильтр строки 5 (допустимы 0–4)') upload(bare_jpeg(), 415, 'image/jpeg', 'bare.jpg', label='JPEG 27 байт: без DQT/DHT и скана') upload(jpeg_without(SAMPLE_JPG, 0xdb), 415, 'image/jpeg', 'nodqt.jpg', label='JPEG без таблиц квантования DQT') upload(SAMPLE_JPG[:-500], 415, 'image/jpeg', 'cut.jpg', label='JPEG обрезан (нет EOI)') upload(SAMPLE_PNG, 415, 'image/jpeg', 'spoof.jpg', label='PNG под видом image/jpeg') upload(SAMPLE_JPG, 415, 'image/png', 'spoof.png', label='JPEG под видом image/png') upload(b'GIF89a' + os.urandom(100), 415, label='GIF под видом image/png') upload(b'', 415, label='SVG под видом image/png') upload(SAMPLE_PNG, 415, 'image/gif', 'x.gif', label='Content-Type image/gif') upload(SAMPLE_PNG, 415, 'application/octet-stream', 'x.png', label='Content-Type octet-stream') upload(b'', 415, label='пустое тело') # Опасные и некорректные filename: 400 до чтения тела. for name in ['../etc/passwd', 'a/b.png', 'a\\b.png', 'nul\x00.png', 'tab\t.png', 'line\n.png', 'c1\x85.png', '', ' ', 'x' * 256]: upload(SAMPLE_PNG, 400, name=name, label='filename %r' % name[:20]) r = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments', 400, actor=alice, raw=SAMPLE_PNG, headers={'Content-Type': 'image/png'}) results.append(('без filename', r['status'], r['json']['error']['code'], round(r['elapsed'] * 1000))) results.append(('chunked без Content-Length',) + chunked_upload(c, alice, chat)) # Доступ: без сессии, посторонний, чужое вложение. upload(SAMPLE_PNG, 401, actor={'id': '', 'token': 'bad'}, label='без действительной сессии') upload(SAMPLE_PNG, 404, actor=eve, label='посторонний грузит в чужой чат') upload(SAMPLE_PNG, 404, target={'id': str(uuid.uuid4())}, label='несуществующий чат') upload(SAMPLE_PNG, 400, target={'id': 'not-a-uuid'}, label='chat_id не UUID') own = upload(SAMPLE_PNG, 202, label='обычный PNG') foreign = upload(SAMPLE_PNG, 202, target=other, label='PNG в другом чате') for suffix in ('', '/content'): c.call('GET', '/api/v1/attachments/' + own['id'] + suffix, 404, actor=eve) c.call('GET', '/api/v1/attachments/' + foreign['id'] + suffix, 404, actor=bob) c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()) + suffix, 404, actor=bob) c.call('GET', '/api/v1/attachments/xyz' + suffix, 400, actor=bob) c.call('GET', '/api/v1/attachments/' + own['id'] + suffix, 401) content = '/api/v1/attachments/' + own['id'] + '/content' c.wait_ready(bob, own) check(c.call('GET', content + '?variant=thumbnail', actor=bob)['headers'].get_content_type() == 'image/jpeg', 'thumbnail type') check(c.call('GET', content + '?variant=optimized', actor=bob)['headers'].get_content_type() == 'image/webp', 'optimized type') c.call('GET', content + '?variant=bogus', 400, actor=bob) check(c.call('GET', content + '?variant=original', actor=bob)['bytes'] == SAMPLE_PNG, 'variant=original') # Привязка к сообщению: только вложения этого чата, 0–10 уникальных UUID. send = '/api/v1/chats/' + chat['id'] + '/messages' def post(body, expected, key=None): return c.call('POST', send, expected, actor=alice, body=body, headers={'Idempotency-Key': key or uuid.uuid4().hex}, schema='Message' if expected == 201 else None)['json'] post({'text': 'чужое', 'attachment_ids': [foreign['id']]}, 404) post({'text': 'смесь', 'attachment_ids': [own['id'], foreign['id']]}, 404) post({'text': 'нет такого', 'attachment_ids': [str(uuid.uuid4())]}, 404) post({'text': 'дубль', 'attachment_ids': [own['id'], own['id'].upper()]}, 400) post({'text': 'много', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]}, 400) post({'text': 'не uuid', 'attachment_ids': ['x']}, 400) post({'text': 'не массив', 'attachment_ids': own['id']}, 400) post({'text': ' '}, 400) post({'text': '', 'attachment_ids': []}, 400) c.call('POST', '/api/v1/chats/' + other['id'] + '/messages', 404, actor=bob, body={'text': 'x', 'attachment_ids': [foreign['id']]}, headers={'Idempotency-Key': uuid.uuid4().hex}) key = uuid.uuid4().hex m = post({'text': '', 'attachment_ids': [own['id'], jpg['id']]}, 201, key) check(m['attachment_ids'] == [own['id'], jpg['id']] and m['text'] == '', 'attachment order/text') check(post({'attachment_ids': [own['id'], jpg['id']], 'text': ''}, 201, key) == m, 'replay with attachments differs') post({'text': '', 'attachment_ids': [jpg['id'], own['id']]}, 409, key) post({'text': '', 'attachment_ids': [own['id']]}, 409, key) key = uuid.uuid4().hex t = post({'text': 'без вложений'}, 201, key) check(post({'text': 'без вложений', 'attachment_ids': []}, 201, key) == t and t['attachment_ids'] == [], 'missing attachment_ids != []') history = c.call('GET', send + '?limit=100', actor=bob, schema='MessagePage')['json']['items'] check([x for x in history if x['id'] == m['id']] == [m], 'history lost attachment_ids') width = max(len(r[0]) for r in results) for label, status, code, ms in results: print('%-*s %d %-24s %5d ms' % (width, label, status, code, ms)) check(all(status < 500 for _, status, _, _ in results), 'server error in negative suite') print('PASS: %d upload cases, ACL and message linking; no 5xx' % len(results)) def chunked_upload(c, actor, chat): """Тело без Content-Length (Transfer-Encoding: chunked): потоковая запись в S3 требует длину — 400.""" url = urllib.parse.urlsplit(c.base) conn = http.client.HTTPConnection(url.hostname, url.port, timeout=10) conn.putrequest('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=c.png') conn.putheader('Authorization', 'Bearer ' + actor['token']) conn.putheader('Content-Type', 'image/png') conn.putheader('Transfer-Encoding', 'chunked') conn.endheaders() start = time.monotonic() conn.send(b'%x\r\n%s\r\n0\r\n\r\n' % (len(SAMPLE_PNG), SAMPLE_PNG)) r = conn.getresponse() body = r.read() check(r.status == 400 and b'invalid_request' in body, 'chunked upload: %d' % r.status) return r.status, 'invalid_request', round((time.monotonic() - start) * 1000) if __name__ == '__main__': run(main)