Files
mtusi2026lab3/scripts/edge-audit-lab3.py
T
2026-11-06 18:54:28 +03:00

345 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Дополнительный adversarial-аудит: неоднозначность, Unicode, гонки, rollback.
Только синтетические данные. Временный DB-trigger действует на один случайный
ключ и удаляется в finally. Повреждённый кэш восстанавливается из PostgreSQL.
"""
import base64
import http.client
import json
import os
import subprocess
import sys
import threading
import uuid
from concurrent.futures import ThreadPoolExecutor
from urllib.parse import urlencode, urlsplit
from replicas import ROOT
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
def sql(query):
return subprocess.check_output(
[
"docker",
"compose",
"exec",
"-T",
"postgres",
"sh",
"-c",
'exec psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" -At',
],
cwd=ROOT,
input=query,
text=True,
timeout=15,
).strip()
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 duplicate_headers(path, actor, pairs):
body = b'{"text":"ambiguous headers"}'
conn = http.client.HTTPConnection(
urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).hostname,
urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).port or 80,
timeout=5,
)
try:
conn.putrequest("POST", path)
for name, value in [
("Authorization", "Bearer " + actor["token"]),
("Content-Length", str(len(body))),
*pairs,
]:
conn.putheader(name, value)
conn.endheaders(body)
response = conn.getresponse()
response.read()
assert response.status == 400, response.status
assert response.getheader("X-Instance-Id") and response.getheader(
"X-Request-Id"
)
finally:
conn.close()
def main():
client = Client()
alice, bob, eve, chat = client.bootstrap()
path = "/api/v1/chats/" + chat["id"] + "/messages"
duplicate_headers(
path,
alice,
[
("Content-Type", "application/json"),
("Idempotency-Key", uuid.uuid4().hex),
("Idempotency-Key", uuid.uuid4().hex),
],
)
duplicate_headers(
path,
alice,
[
("Content-Type", "application/json"),
("Content-Type", "text/plain"),
("Idempotency-Key", uuid.uuid4().hex),
],
)
for endpoint in ["/api/v1/chats", path]:
for val in ["1.0", "1e0", "+1", " 1 ", "١", "true", "-1", "101"]:
client.call(
"GET", endpoint + "?" + urlencode({"limit": val}), 400, actor=alice
)
for query in ["limit=1&limit=100", "cursor=W10&cursor=bnVsbA", "extra=1"]:
client.call("GET", endpoint + "?" + query, 400, actor=alice)
# Every Unicode scalar permitted by the contract is stored unchanged.
special = "Имя\u0000😀"
registration = {
"username": "edge_" + uuid.uuid4().hex[:16],
"display_name": special,
"password": " пароль:\u0000😀с пробелами ",
}
registered = client.call(
"POST", "/api/v1/users", 201, body=registration, schema="User"
)["json"]
assert registered["display_name"] == special
basic = (
"Basic "
+ base64.b64encode(
(registration["username"] + ":" + registration["password"]).encode()
).decode()
)
session = client.call(
"POST",
"/api/v1/auth/sessions",
201,
headers={"Authorization": basic},
schema="Session",
)["json"]
assert session["user"] == registered
special_actor = {"id": registered["id"], "token": session["token"]}
assert (
client.call("GET", "/api/v1/users/me", actor=special_actor, schema="User")[
"json"
]
== registered
)
title = "Чат\u0000😀"
special_chat = client.call(
"POST",
"/api/v1/chats",
201,
actor=special_actor,
body={"title": title, "member_ids": [registered["id"]]},
schema="Chat",
)["json"]
assert special_chat["title"] == title
assert (
client.call(
"GET",
"/api/v1/chats/" + special_chat["id"],
actor=special_actor,
schema="Chat",
)["json"]
== special_chat
)
maximum_name = {
**registration,
"username": "max_" + uuid.uuid4().hex[:16],
"display_name": "😀" * 100,
}
assert (
client.call("POST", "/api/v1/users", 201, body=maximum_name, schema="User")[
"json"
]["display_name"]
== "😀" * 100
)
max_chat = client.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "😀" * 100, "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
assert max_chat["title"] == "😀" * 100
for value in ["\u0000", "😀" * 4000, "e\u0301", "\u200b", " Пробелы\nсохраняются "]:
assert client.message(alice, chat, value)["json"]["text"] == value
for value in ["😀" * 4001, "\u2003" * 4, None, 123, True, [], {}]:
client.call(
"POST",
path,
400,
actor=alice,
body={"text": value},
headers={"Idempotency-Key": uuid.uuid4().hex},
)
same_key = uuid.uuid4().hex
client.message(alice, chat, "é", key=same_key)
client.call(
"POST",
path,
409,
actor=alice,
body={"text": "e\u0301"},
headers={"Idempotency-Key": same_key},
)
# Conflicting simultaneous content must commit exactly one of the bodies.
race_chat = client.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Conflicting race", "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
race_path = "/api/v1/chats/" + race_chat["id"] + "/messages"
race_key = uuid.uuid4().hex
barrier = threading.Barrier(16)
locals_ = [Client() for _ in range(16)]
def contender(i):
barrier.wait()
return locals_[i].call(
"POST",
race_path,
[201, 409],
actor=alice,
body={"text": "A" if i % 2 else "B"},
headers={"Idempotency-Key": race_key},
schema="Message",
)
with ThreadPoolExecutor(max_workers=16) as pool:
results = list(pool.map(contender, range(16)))
winners = [r["json"] for r in results if r["status"] == 201]
assert len(winners) == 8 and all(m == winners[0] for m in winners)
assert (
int(sql(f"SELECT count(*) FROM messages WHERE chat_id='{race_chat['id']}'"))
== 1
)
# Inject a failure exactly between the INSERT message and saving its result.
fail_key = uuid.uuid4().hex
trigger = "audit_fail_" + uuid.uuid4().hex[:10]
fault_chat = client.call(
"POST",
"/api/v1/chats",
201,
actor=alice,
body={"title": "Atomicity fault", "member_ids": [alice["id"]]},
schema="Chat",
)["json"]
fault_path = "/api/v1/chats/" + fault_chat["id"] + "/messages"
try:
sql(
f"CREATE FUNCTION {trigger}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected audit failure'; END $$; CREATE TRIGGER {trigger} BEFORE UPDATE ON message_requests FOR EACH ROW WHEN (NEW.key='{fail_key}' AND NEW.message_id IS NOT NULL) EXECUTE FUNCTION {trigger}();"
)
client.call(
"POST",
fault_path,
503,
actor=alice,
body={"text": "Retry after rollback"},
headers={"Idempotency-Key": fail_key},
)
assert (
int(
sql(f"SELECT count(*) FROM messages WHERE chat_id='{fault_chat['id']}'")
)
== 0
)
assert (
int(
sql(
f"SELECT count(*) FROM message_requests WHERE chat_id='{fault_chat['id']}'"
)
)
== 0
)
finally:
sql(
f"DROP TRIGGER IF EXISTS {trigger} ON message_requests; DROP FUNCTION IF EXISTS {trigger}();"
)
client.message(alice, fault_chat, "Retry after rollback", key=fail_key)
assert (
int(sql(f"SELECT count(*) FROM messages WHERE chat_id='{fault_chat['id']}'"))
== 1
)
# Corrupt-but-parseable cached values must not break the public API.
profile_key = "user:v1:" + alice["id"]
for field, value in [
("display_name", "bad\ud800"),
("created_at", "0001-01-01T00:00:00+23:59"),
("created_at", "2026-11-06T12:00:00"),
]:
try:
redis(
"SET",
profile_key,
raw=json.dumps({**alice["user"], field: value}).encode(),
)
assert (
client.call("GET", "/api/v1/users/me", actor=alice, schema="User")[
"json"
]
== alice["user"]
)
assert 0 < int(redis("TTL", profile_key)) <= 60
finally:
redis("DEL", profile_key)
# Reuse one TCP connection: routing must not pin this user to one API.
conn = http.client.HTTPConnection(
urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).hostname,
urlsplit(os.environ.get("BASE_URL", "http://localhost:8080")).port or 80,
timeout=5,
)
instances = set()
try:
for _ in range(8):
conn.request(
"GET",
"/api/v1/users/me",
headers={"Authorization": "Bearer " + alice["token"]},
)
r = conn.getresponse()
assert r.status == 200
r.read()
instances.add(r.getheader("X-Instance-Id"))
finally:
conn.close()
assert len(instances) >= 2
proof = {
"result": "PASS",
"checks": [
"duplicate Idempotency-Key and Content-Type rejected",
"strict integer and repeated query parameters",
"NUL/emoji user and chat Unicode roundtrip",
"100 code point names and 4000 code point messages",
"no text trim or silent Unicode normalization",
"16 conflicting simultaneous requests: 8 replayed winners, 8 conflicts, one DB row",
"DB failure after message INSERT rolls back message and key; same key succeeds after recovery",
"invalid cached Unicode/timezone falls back to DB with TTL",
"one TCP connection reaches both API",
],
"keepalive_instances": sorted(instances),
}
(ROOT / "evidence/lab3-edge-audit.json").write_text(
json.dumps(proof, ensure_ascii=False, indent=2) + "\n"
)
print(json.dumps(proof, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()