#!/usr/bin/env python3 """Level 4: PostgreSQL rules — username race, transaction rollback, keyset paging, injection.""" import threading import uuid from urllib.parse import quote from common import arguments, call, chat_with, credentials, expect, member, run RACE_CLIENTS = 12 def duplicate_username_race_creates_one_user(base): body = credentials("race") results = [] lock = threading.Lock() ready = threading.Barrier(RACE_CLIENTS) def attempt(): ready.wait() result = call(base, "POST", "/api/v1/users", body=body) with lock: results.append(result["status"]) threads = [threading.Thread(target=attempt) for _ in range(RACE_CLIENTS)] for thread in threads: thread.start() for thread in threads: thread.join() created = results.count(201) conflicts = results.count(409) expect(created == 1, "создано %d пользователей с одним username (ожидался 1): %s" % (created, results)) expect(created + conflicts == RACE_CLIENTS, "неожиданные ответы в гонке: %s" % results) def unknown_member_rolls_chat_back(base): alice = member(base, "rollback") title = "Откат " + uuid.uuid4().hex[:8] body = {"title": title, "member_ids": [alice["id"], str(uuid.uuid4())]} result = call(base, "POST", "/api/v1/chats", body=body, token=alice["token"]) expect(result["status"] == 400, "неизвестный участник дал %s" % result["status"]) listed = call(base, "GET", "/api/v1/chats?limit=100", token=alice["token"])["json"] expect(all(chat["title"] != title for chat in listed["items"]), "чат остался в базе после отката") def keyset_pagination_is_complete(base): alice = member(base, "pages") bob = member(base, "pages2") chat = chat_with(base, alice, bob) total = 25 for index in range(total): result = call(base, "POST", "/api/v1/chats/%s/messages" % chat["id"], body={"text": "сообщение %d" % index}, token=alice["token"]) expect(result["status"] == 201, "отправка %d вернула %s" % (index, result["status"])) seen = [] cursor = None for _ in range(total): path = "/api/v1/chats/%s/messages?limit=7" % chat["id"] if cursor: path += "&cursor=" + quote(cursor, safe="") page = call(base, "GET", path, token=bob["token"])["json"] seen.extend(page["items"]) cursor = page["next_cursor"] if cursor is None: break expect(cursor is None, "обход истории не завершился") ids = [item["id"] for item in seen] expect(len(ids) == total, "прочитано %d сообщений из %d" % (len(ids), total)) expect(len(set(ids)) == total, "в обходе есть повторы") keys = [(item["created_at"], uuid.UUID(item["id"]).int) for item in seen] expect(keys == sorted(keys), "порядок (created_at, id) нарушен") def cursor_is_bound_to_its_chat(base): alice = member(base, "cursor") bob = member(base, "cursor2") first = chat_with(base, alice, bob) second = chat_with(base, alice, bob) for chat in (first, second): for index in range(3): call(base, "POST", "/api/v1/chats/%s/messages" % chat["id"], body={"text": "текст %d" % index}, token=alice["token"]) page = call(base, "GET", "/api/v1/chats/%s/messages?limit=1" % first["id"], token=alice["token"])["json"] expect(page["next_cursor"], "нет курсора для проверки") foreign = call(base, "GET", "/api/v1/chats/%s/messages?limit=1&cursor=%s" % (second["id"], quote(page["next_cursor"], safe="")), token=alice["token"]) expect(foreign["status"] == 400, "курсор другого чата принят: %s" % foreign["status"]) tampered = page["next_cursor"][:-2] + ("aa" if not page["next_cursor"].endswith("aa") else "bb") altered = call(base, "GET", "/api/v1/chats/%s/messages?cursor=%s" % (first["id"], quote(tampered, safe="")), token=alice["token"]) expect(altered["status"] == 400, "изменённый курсор принят: %s" % altered["status"]) def limit_bounds_are_enforced(base): alice = member(base, "limit") bob = member(base, "limit2") chat = chat_with(base, alice, bob) for value in ("0", "101", "abc", "-1", "1.5", ""): result = call(base, "GET", "/api/v1/chats/%s/messages?limit=%s" % (chat["id"], quote(value, safe="")), token=alice["token"]) expect(result["status"] == 400, "limit=%r дал %s" % (value, result["status"])) valid = call(base, "GET", "/api/v1/chats/%s/messages?limit=100" % chat["id"], token=alice["token"]) expect(valid["status"] == 200, "limit=100 дал %s" % valid["status"]) def sql_injection_is_not_executed(base): probes = ["ad'; DROP TABLE users; --", "' OR '1'='1", "admin\"--", "ad\\'; SELECT 1"] for probe in probes: body = credentials("inj") body["username"] = probe result = call(base, "POST", "/api/v1/users", body=body) expect(result["status"] == 400, "username %r дал %s" % (probe, result["status"])) alice = member(base, "after") bob = member(base, "after2") chat = chat_with(base, alice, bob) text = "текст с кавычкой ' и ; DROP TABLE messages; --" created = call(base, "POST", "/api/v1/chats/%s/messages" % chat["id"], body={"text": text}, token=alice["token"]) expect(created["status"] == 201 and created["json"]["text"] == text, "текст с кавычками испорчен или отклонён") history = call(base, "GET", "/api/v1/chats/%s/messages" % chat["id"], token=bob["token"])["json"] expect(any(item["text"] == text for item in history["items"]), "сообщение не читается после вставки") def malformed_requests_are_rejected(base): alice = member(base, "shape") bob = member(base, "shape2") chat = chat_with(base, alice, bob) path = "/api/v1/chats/%s/messages" % chat["id"] expect(call(base, "POST", path, body={"text": "ок", "sender_id": alice["id"]}, token=alice["token"])["status"] == 400, "лишнее поле принято") expect(call(base, "POST", path, body={}, token=alice["token"])["status"] == 400, "пустой объект принят") expect(call(base, "POST", path, body={"text": " "}, token=alice["token"])["status"] == 400, "пробельный текст принят") expect(call(base, "POST", path, body={"text": "x" * 4001}, token=alice["token"])["status"] == 400, "текст длиннее 4000 принят") expect(call(base, "GET", "/api/v1/chats/not-a-uuid", token=alice["token"])["status"] == 400, "неверный UUID дал не 400") expect(call(base, "GET", "/api/v1/chats/%s" % uuid.uuid4(), token=alice["token"])["status"] == 404, "чужой чат дал не 404") def nul_and_unicode_text_round_trips(base): alice = member(base, "unicode") bob = member(base, "unicode2") chat = chat_with(base, alice, bob) path = "/api/v1/chats/%s/messages" % chat["id"] samples = [ "a\u0000b", "\u0000", "emoji \U0001f44b и ZWJ \U0001f469\u200d\U0001f4bb", "комбинация e\u0301 и RTL \u05d0\u05d1", "край \u0007\u001b\u007f", ] created = [] for sample in samples: result = call(base, "POST", path, body={"text": sample}, token=alice["token"]) expect(result["status"] == 201, "текст %r дал %s" % (sample, result["status"])) expect(result["json"]["text"] == sample, "текст изменён при записи: %r -> %r" % (sample, result["json"]["text"])) created.append(result["json"]) history = call(base, "GET", path + "?limit=100", token=bob["token"])["json"]["items"] by_id = {item["id"]: item["text"] for item in history} for item in created: expect(by_id.get(item["id"]) == item["text"], "текст изменён при чтении: %r" % item["text"]) def text_length_limit_counts_code_points(base): alice = member(base, "length") bob = member(base, "length2") chat = chat_with(base, alice, bob) path = "/api/v1/chats/%s/messages" % chat["id"] exact = "\U0001f600" * 4000 result = call(base, "POST", path, body={"text": exact}, token=alice["token"]) expect(result["status"] == 201, "4000 code points дали %s" % result["status"]) expect(result["json"]["text"] == exact, "текст на границе изменён") over = call(base, "POST", path, body={"text": "\U0001f600" * 4001}, token=alice["token"]) expect(over["status"] == 400, "4001 code point дал %s" % over["status"]) def unicode_blanks_are_rejected(base): alice = member(base, "blank") bob = member(base, "blank2") chat = chat_with(base, alice, bob) path = "/api/v1/chats/%s/messages" % chat["id"] blanks = [" ", "\t\n", "\u00a0", "\u2007", "\u3000", "\u200b", "\u200b \u200b", "\ufeff"] for blank in blanks: result = call(base, "POST", path, body={"text": blank}, token=alice["token"]) expect(result["status"] == 400, "пробельный текст %r дал %s" % (blank, result["status"])) for field in ("display_name",): body = credentials("blank") body[field] = "\u200b" expect(call(base, "POST", "/api/v1/users", body=body)["status"] == 400, "пустой %s принят" % field) titles = call(base, "POST", "/api/v1/chats", body={"title": "\u00a0", "member_ids": [alice["id"]]}, token=alice["token"]) expect(titles["status"] == 400, "пробельный title принят: %s" % titles["status"]) def broken_json_does_not_break_the_api(base): alice = member(base, "json") bob = member(base, "json2") chat = chat_with(base, alice, bob) path = "/api/v1/chats/%s/messages" % chat["id"] payloads = [ b'{"text": 1e999}', b'{"text": NaN}', b'{"text": Infinity}', b'{"text": 1e+}', b'{"text": ' + b"9" * 5000 + b"}", b'{"text": ' + b"[" * 2000 + b"]" * 2000 + b"}", b"[" * 2000, b'{"text": {"text": "nested object"}}', b'{"text": "\xff\xfe"}', b"", b"{", ] for payload in payloads: result = call(base, "POST", path, raw=payload, token=alice["token"], content_type="application/json") expect(result["status"] == 400, "тело %r дало %s" % (payload[:24], result["status"])) alive = call(base, "POST", path, body={"text": "после битого JSON"}, token=alice["token"]) expect(alive["status"] == 201, "API не отвечает после битых тел: %s" % alive["status"]) def content_type_is_required(base): alice = member(base, "ctype") bob = member(base, "ctype2") chat = chat_with(base, alice, bob) path = "/api/v1/chats/%s/messages" % chat["id"] for content_type in ("text/plain", "application/x-www-form-urlencoded", "", "application/jsonx"): result = call(base, "POST", path, raw=b'{"text":"x"}', token=alice["token"], content_type=content_type) expect(result["status"] == 415, "Content-Type %r дал %s" % (content_type, result["status"])) body = credentials("ctype") import json as jsonlib plain = call(base, "POST", "/api/v1/users", raw=jsonlib.dumps(body).encode(), content_type="text/plain") expect(plain["status"] == 415, "регистрация с text/plain дала %s" % plain["status"]) charset = call(base, "POST", path, raw=b'{"text":"charset"}', token=alice["token"], content_type="application/json; charset=utf-8") expect(charset["status"] == 201, "application/json с charset дал %s" % charset["status"]) TESTS = [ duplicate_username_race_creates_one_user, unknown_member_rolls_chat_back, keyset_pagination_is_complete, cursor_is_bound_to_its_chat, limit_bounds_are_enforced, sql_injection_is_not_executed, malformed_requests_are_rejected, nul_and_unicode_text_round_trips, text_length_limit_counts_code_points, unicode_blanks_are_rejected, broken_json_does_not_break_the_api, content_type_is_required, ] if __name__ == "__main__": run(TESTS, arguments(__doc__).base_url)