forked from mtusi2026/mtusi2026lab4
188 lines
12 KiB
Python
188 lines
12 KiB
Python
"""Негативные HTTP/Redis проверки для production-аудита; только локальный Compose."""
|
||
import base64
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
import hashlib
|
||
import http.client
|
||
import json
|
||
from pathlib import Path
|
||
import subprocess
|
||
import threading
|
||
import uuid
|
||
from urllib.parse import urlsplit, quote
|
||
|
||
from client import Client, arguments, check, run
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
|
||
|
||
def redis(*args, raw=None):
|
||
command = ['docker', 'compose', 'exec', '-T', 'redis', 'redis-cli', '--raw']
|
||
if raw is not None:
|
||
command += ['-x']
|
||
return subprocess.check_output(command + list(args), cwd=ROOT, input=raw, timeout=10).rstrip(b'\n')
|
||
|
||
|
||
def main():
|
||
args = arguments('Production-аудит входных данных, Redis и изоляции сессий')
|
||
c = Client(args.base_url, args.ca_file)
|
||
alice, bob, _, chat = c.bootstrap()
|
||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||
failures = []
|
||
def expect(label, method, path, status, **kwargs):
|
||
response = c.call(method, path, [200, 201, 400, 401, 413, 415, 500, 503], **kwargs)
|
||
if response['status'] != status:
|
||
failures.append(f'{label}: ожидался {status}, получен {response["status"]}')
|
||
return response
|
||
|
||
payload = {'kind': 'messages', 'owner_id': chat['id'],
|
||
'created_at': '2026-01-01T00:00:00+00:00', 'item_id': str(uuid.uuid4())}
|
||
for field, value in [('owner_id', 123), ('item_id', 123), ('owner_id', []),
|
||
('created_at', '9999-12-31T23:59:59-23:59')]:
|
||
cursor = base64.urlsafe_b64encode(json.dumps({**payload, field: value}).encode()).decode()
|
||
expect('Типы/диапазон курсора ' + field, 'GET', path + '?cursor=' + cursor, 400, actor=alice)
|
||
expect('Длина курсора', 'GET', path + '?cursor=' + 'a' * 5000, 400, actor=alice)
|
||
for text in ('\ud800', '\udfff'):
|
||
expect('Непредставимый текст PostgreSQL', 'POST', path, 400, actor=alice,
|
||
raw=json.dumps({'text': 'valid' + text}).encode(), headers={'Content-Type': 'application/json'})
|
||
registration = {'username': 'unicode_' + uuid.uuid4().hex[:16],
|
||
'password': 'valid-password-123', 'display_name': 'Valid'}
|
||
for field, value in [('display_name', '\ud800'),
|
||
('password', 'secret-audit-\ud800')]:
|
||
expect('Непредставимая строка ' + field, 'POST', '/api/v1/users', 400,
|
||
raw=json.dumps({**registration, field: value}).encode(), headers={'Content-Type': 'application/json'})
|
||
expect('Большое тело с допустимым коротким текстом', 'POST', path, 413, actor=alice,
|
||
raw=b'{"text":"valid"}' + b' ' * 70000, headers={'Content-Type': 'application/json'})
|
||
expect('Повторяющиеся ключи JSON', 'POST', path, 400, actor=alice,
|
||
raw=b'{"text":"first","text":"second"}', headers={'Content-Type': 'application/json'})
|
||
expect('JSON не в UTF-8', 'POST', path, 400, actor=alice,
|
||
raw='{"text":"valid"}'.encode('utf-16'), headers={'Content-Type': 'application/json'})
|
||
expect('Нестандартный NaN', 'POST', path, 400, actor=alice,
|
||
raw=b'{"text":"valid","extra":NaN}', headers={'Content-Type': 'application/json'})
|
||
expect('Глубокий JSON', 'POST', path, 400, actor=alice,
|
||
raw=b'[' * 1200 + b'0' + b']' * 1200, headers={'Content-Type': 'application/json'})
|
||
|
||
# Максимально допустимый текст остаётся допустимым и в escaped JSON.
|
||
emoji = '😀' * 4000
|
||
maximum = c.call('POST', path, 201, actor=alice, schema='Message',
|
||
raw=json.dumps({'text': emoji}, ensure_ascii=True).encode(),
|
||
headers={'Content-Type': 'application/json', 'Idempotency-Key':uuid.uuid4().hex})
|
||
check(maximum['json']['text'] == emoji, 'Лимит тела отверг/исказил 4000 Unicode-символов')
|
||
check(maximum['headers'].get('Cache-Control') == 'no-store', 'Ответ API может попасть в кэш')
|
||
check(maximum['headers'].get('X-Content-Type-Options') == 'nosniff', 'Нет nosniff')
|
||
|
||
parsed = urlsplit(c.base)
|
||
check(parsed.scheme == 'http', 'Сетевые проверки этого Compose-сценария рассчитаны на localhost HTTP')
|
||
def wire(duplicate=False, chunked=False, slow=False, started=None):
|
||
connection = http.client.HTTPConnection(parsed.hostname, parsed.port, timeout=10)
|
||
try:
|
||
if chunked:
|
||
connection.request('POST', path, body=iter([b'{"text":"valid"}', b' ' * 40000, b' ' * 40000]),
|
||
headers={'Authorization': 'Bearer ' + alice['token'],
|
||
'Content-Type': 'application/json'}, encode_chunked=True)
|
||
elif slow:
|
||
connection.putrequest('POST', path)
|
||
connection.putheader('Authorization', 'Bearer ' + alice['token'])
|
||
connection.putheader('Content-Type', 'application/json')
|
||
connection.putheader('Content-Length', '100')
|
||
connection.endheaders(b'{\"text\":')
|
||
started.set()
|
||
else:
|
||
connection.putrequest('GET', '/api/v1/users/me')
|
||
connection.putheader('Authorization', 'Bearer ' + alice['token'])
|
||
if duplicate:
|
||
connection.putheader('Authorization', 'Bearer ' + bob['token'])
|
||
connection.endheaders()
|
||
response = connection.getresponse()
|
||
data = json.loads(response.read())
|
||
check(data['error']['request_id'] == response.getheader('X-Request-Id'), 'Сетевой запрос потерял request_id')
|
||
return response.status
|
||
finally:
|
||
connection.close()
|
||
check(wire(chunked=True) == 413, 'Chunked обходит лимит размера тела')
|
||
check(wire(duplicate=True) == 401, 'Два Authorization принимаются неоднозначно')
|
||
print('PASS: реальный chunked=413, два Authorization=401, no-store; 4000 emoji в escaped JSON допустимы')
|
||
started = threading.Event()
|
||
with ThreadPoolExecutor(max_workers=1) as pool:
|
||
pending = pool.submit(wire, slow=True, started=started)
|
||
check(started.wait(2), 'Не началась медленная передача')
|
||
health = c.call('GET', '/health/live', schema='Health')
|
||
check(health['elapsed'] < 2, 'Медленная передача заблокировала event loop')
|
||
check(pending.result(timeout=10) == 408, 'У медленной передачи нет deadline')
|
||
print('PASS: реальное незавершённое тело получает 408; health работает во время ожидания')
|
||
|
||
|
||
created = [chat]
|
||
for index in range(12):
|
||
created.append(c.call('POST', '/api/v1/chats', 201, actor=alice, schema='Chat',
|
||
body={'title': 'SQL-page-' + str(index), 'member_ids': [alice['id']]})['json'])
|
||
chats, cursor = [], None
|
||
while True:
|
||
url = '/api/v1/chats?limit=3' + ('&cursor=' + quote(cursor, safe='') if cursor else '')
|
||
page = c.call('GET', url, actor=alice, schema='ChatPage')['json']
|
||
chats.extend(item['id'] for item in page['items'])
|
||
cursor = page['next_cursor']
|
||
if cursor is None:
|
||
break
|
||
c.call('GET', '/api/v1/chats?cursor=' + quote(cursor, safe=''), 400, actor=bob)
|
||
check(len(chats) <= len(created), 'Зациклилась пагинация чатов')
|
||
check(chats == [item['id'] for item in sorted(created, key=lambda x: (x['created_at'], uuid.UUID(x['id']).int))],
|
||
'SQL-пагинация чатов потеряла/повторила объекты')
|
||
bob_chats = c.call('GET', '/api/v1/chats', actor=bob, schema='ChatPage')['json']
|
||
check([item['id'] for item in bob_chats['items']] == [chat['id']], 'В список попали чужие чаты')
|
||
print('PASS: SQL-пагинация 13 чатов по 3; курсор принадлежит пользователю, чужие чаты скрыты')
|
||
|
||
profile_key = 'user:v1:' + alice['id']
|
||
for raw in (b'\xff\xfe', b'null', b'{"id":123}'):
|
||
redis('SET', profile_key, raw=raw)
|
||
expect('Повреждённый профиль Redis', 'GET', '/api/v1/users/me', 200, actor=alice)
|
||
session_key = 'session:v1:' + hashlib.sha256(alice['token'].encode()).hexdigest()
|
||
saved = redis('GET', session_key)
|
||
ttl = int(redis('PTTL', session_key))
|
||
for raw in (b'\xff', json.dumps({'user_id': 123, 'expires_at': '2099-01-01T00:00:00+00:00'}).encode(),
|
||
b'[]', b'null', json.dumps({'user_id': alice['id'], 'expires_at': '2020-01-01T00:00:00+00:00'}).encode()):
|
||
try:
|
||
redis('SET', session_key, raw=raw)
|
||
redis('PEXPIRE', session_key, str(ttl))
|
||
expect('Повреждённая/истёкшая сессия Redis', 'GET', '/api/v1/users/me', 401, actor=alice)
|
||
finally:
|
||
redis('SET', session_key, raw=saved)
|
||
redis('PEXPIRE', session_key, str(ttl))
|
||
c.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
||
|
||
# NUL недопустим в PostgreSQL text, но пароль хранится как Argon2-хеш.
|
||
special = {**registration, 'username': 'nulpass_' + uuid.uuid4().hex[:16],
|
||
'password': ' пароль:\x00с пробелами '}
|
||
c.call('POST', '/api/v1/users', 201, body=special, schema='User')
|
||
special_basic = 'Basic ' + base64.b64encode((special['username'] + ':' + special['password']).encode()).decode()
|
||
special_session = c.call('POST', '/api/v1/auth/sessions', 201,
|
||
headers={'Authorization': special_basic}, schema='Session')['json']
|
||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor={'token': special_session['token']})
|
||
print('PASS: пароль с Unicode, пробелами, двоеточием и NUL не усекается')
|
||
|
||
basic = 'Basic ' + base64.b64encode((alice['registration']['username'] + ':' + alice['registration']['password']).encode()).decode()
|
||
another = c.call('POST', '/api/v1/auth/sessions', 201, headers={'Authorization': basic}, schema='Session')['json']
|
||
check(another['token'] != alice['token'], 'Повторный вход вернул старый токен')
|
||
def get_profile(_):
|
||
return Client(args.base_url, args.ca_file).call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||
profiles = list(pool.map(get_profile, range(24)))
|
||
check(all(p['json']['id'] == alice['id'] for p in profiles), 'Смешаны пользователи конкурентных запросов')
|
||
check(len({p['headers']['X-Request-Id'] for p in profiles}) == 24, 'Повторяется request_id')
|
||
with ThreadPoolExecutor(max_workers=4) as pool:
|
||
list(pool.map(lambda _: Client(args.base_url, args.ca_file).call(
|
||
'DELETE', '/api/v1/auth/sessions/current', [204, 401], actor=alice), range(4)))
|
||
c.call('GET', '/api/v1/users/me', 401, actor=alice)
|
||
c.call('GET', '/api/v1/users/me', actor={'token': another['token']}, schema='User')
|
||
c.call('GET', '/api/v1/users/me', actor=bob, schema='User')
|
||
c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor={'token': another['token']})
|
||
print('PASS: независимые токены, конкурентные logout/GET, уникальные request_id')
|
||
if failures:
|
||
for failure in failures:
|
||
print('FAIL:', failure)
|
||
check(False, 'Production-аудит нашёл ошибки: ' + str(len(failures)))
|
||
print('PASS: типы/диапазон курсора, Unicode, размер/формат JSON и повреждённый Redis')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
run(main)
|