Files
mtusi2026lab7/student-tests/lab7_api_security.py
T
zemiles 9cc80d2529
Application / app (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР 7 — БПИ2303 — Лазарев — на 3
Перенесена реализация lab6; бизнес-API без изменений, транспорт — HTTPS.

- Внешний HTTPS (public-ca) на 127.0.0.1:8443; HSTS, TLS >= 1.2.
- Обязательное mTLS LB -> каждая API-реплика: API проверяет цепочку
  internal-ca и SAN клиента (allowlist: lb); health только на 127.0.0.1.
- TLS с проверкой сервера ко всем зависимостям: PostgreSQL verify-full
  (pg_hba только hostssl), HTTPS S3, AMQPS, OTLP/HTTPS с client cert,
  mTLS Collector -> Prometheus/Loki/Tempo, Grafana -> datasources, scrape.
- OpenBao 2.7.2 не в dev-mode: Raft на volume, init/unseal оператором,
  auth/cert с ролью и policy на сервис, TTL 15 мин + renew/re-login.
- Отдельные роли PostgreSQL с грантами по таблицам; разные ключи S3 и
  пользователи брокера выдаются из OpenBao.
- Argon2id (m=19 MiB, t=2, p=1, замер на стенде) и rehash при входе.
- Случайный X-Instance-Id; hardening контейнеров (non-root, cap_drop ALL,
  no-new-privileges, read-only, лимиты); TCP_NODELAY на сокете API.
- Модель угроз, trust matrix, отрицательные тесты TLS/mTLS, API, OpenBao,
  hardening; evidence, в т.ч. прогон из чистого clone.
2026-11-18 23:59:35 +03:00

257 lines
15 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.
#!/usr/bin/env python3
"""Lab7: отрицательные проверки внешнего API через HTTPS-ingress (с хоста).
python3 student-tests/lab7_api_security.py --ca-file secrets/pki/public-ca.crt \
--output evidence/lab7-api-security.json
Покрывает: внешний TLS (CA, hostname, внутренний CA не подходит, TLS<1.2), посторонний пользователь
(metadata/content/история/прикрепление чужого файла), поддельный X-User-Id, Basic на бизнес-endpoint,
logout, expiry сессии, одинаковый 401 без раскрытия существования логина, rate limit входа
(429 + Retry-After), лимиты размера (JSON 1 MiB, upload 10 MiB), заголовки ответа (alias реплики,
нет Server/hostname), Argon2id в БД (параметры, соль) и rehash старого scrypt-хеша.
Для expiry/rehash/формата хеша тест обращается к PostgreSQL как ОПЕРАТОР
(`docker compose exec db psql` через локальный сокет, peer) — только к строкам своих тестовых
пользователей. Токены и пароли не печатаются и не сохраняются.
"""
import argparse
import base64
import hashlib
import json
import os
import socket
import ssl
import subprocess
import sys
import time
import uuid
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tests"))
from client import Client, Failure # noqa: E402
RESULTS = []
def ok(name, cond, detail=""):
RESULTS.append({"check": name, "pass": bool(cond), "detail": detail})
print("%s %s %s" % ("PASS" if cond else "FAIL", name, detail))
def psql(sql):
out = subprocess.run(["docker", "compose", "exec", "-T", "db", "su-exec", "postgres", "psql", "-qAt",
"-d", "messenger", "-v", "ON_ERROR_STOP=1", "-c", sql],
cwd=ROOT, capture_output=True, text=True, timeout=30)
if out.returncode != 0:
raise RuntimeError("psql failed: %s" % out.stderr.strip()[:200])
return out.stdout.strip()
def tls_probe(host, port, cafile, server_hostname, max_version=None):
c = ssl.create_default_context(cafile=cafile)
if max_version:
c.minimum_version = ssl.TLSVersion.MINIMUM_SUPPORTED
import warnings
warnings.simplefilter("ignore", DeprecationWarning)
c.maximum_version = max_version
c.set_ciphers("ALL:@SECLEVEL=0")
try:
with socket.create_connection((host, port), timeout=5) as raw, \
c.wrap_socket(raw, server_hostname=server_hostname) as tls:
return "ok", tls.version()
except ssl.SSLCertVerificationError as exc:
return "cert_error", exc.verify_message
except ssl.SSLError as exc:
return "tls_error", exc.reason
except (socket.timeout, TimeoutError):
return "timeout", ""
def basic(username, password):
return "Basic " + base64.b64encode(("%s:%s" % (username, password)).encode()).decode()
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--base-url", default=os.environ.get("BASE_URL", "https://localhost:8443"))
ap.add_argument("--ca-file", default=os.environ.get("CA_FILE", os.path.join(ROOT, "secrets/pki/public-ca.crt")))
ap.add_argument("--output")
ap.add_argument("--skip-rate-limit", action="store_true")
args = ap.parse_args()
ca_internal = os.path.join(ROOT, "secrets/pki/internal-ca.crt")
c = Client(args.base_url, args.ca_file)
_call = c.call
def call(*a, **kw): # публичный клиент разбирает JSON только для ошибок/схем
r = _call(*a, **kw)
if r["json"] is None and r["bytes"] and r["headers"].get_content_type() == "application/json":
r["json"] = json.loads(r["bytes"])
return r
c.call = call
host = "localhost"
port = int(args.base_url.rsplit(":", 1)[1].split("/")[0])
# ---------------- внешний TLS
st = tls_probe(host, port, args.ca_file, "localhost")
ok("tls.trusted_ca_and_hostname", st[0] == "ok", st[1])
st = tls_probe("127.0.0.1", port, args.ca_file, "evil.example")
ok("tls.wrong_hostname_rejected", st[0] == "cert_error" and "Hostname mismatch" in (st[1] or ""), st[1])
st = tls_probe(host, port, ca_internal, "localhost")
ok("tls.internal_ca_does_not_validate_ingress", st[0] == "cert_error", st[1])
st = tls_probe(host, port, args.ca_file, "localhost", max_version=ssl.TLSVersion.TLSv1_1)
ok("tls.tls11_refused", st[0] in ("tls_error",), "%s %s" % st)
# ---------------- пользователи, чат, вложение
# Rate limit входа общий на LB по IP: если перед тестом шли входы (smoke и т. п.), ждём окно 10 с.
time.sleep(11)
alice, bob, eve = c.user("alice"), c.user("bob"), c.user("eve")
chat = c.call("POST", "/api/v1/chats", 201, actor=alice,
body={"title": "lab7 security", "member_ids": [alice["id"], bob["id"]]})["json"]
png = open(os.path.join(ROOT, "tests/fixtures/sample.png"), "rb").read()
att = c.call("POST", "/api/v1/chats/%s/attachments?filename=a.png" % chat["id"], 202, actor=alice,
raw=png, headers={"Content-Type": "image/png"})["json"]
msg = c.call("POST", "/api/v1/chats/%s/messages" % chat["id"], 201, actor=alice,
body={"text": "секрет чата"}, headers={"Idempotency-Key": uuid.uuid4().hex})["json"]
deadline = time.time() + 60
while time.time() < deadline:
meta = c.call("GET", "/api/v1/attachments/%s" % att["id"], 200, actor=alice)["json"]
if meta["status"] in ("ready", "failed"):
break
time.sleep(0.5)
ok("setup.attachment_ready", meta["status"] == "ready", meta["status"])
# ---------------- посторонний
for name, path in (("chat", "/api/v1/chats/%s" % chat["id"]),
("history", "/api/v1/chats/%s/messages" % chat["id"]),
("attachment_metadata", "/api/v1/attachments/%s" % att["id"]),
("attachment_content", "/api/v1/attachments/%s/content" % att["id"]),
("attachment_thumbnail", "/api/v1/attachments/%s/content?variant=thumbnail" % att["id"])):
r = c.call("GET", path, [404, 200, 403, 401], actor=eve)
ok("outsider.%s_404" % name, r["status"] == 404, str(r["status"]))
eve_chat = c.call("POST", "/api/v1/chats", 201, actor=eve, body={"title": "eve", "member_ids": [eve["id"]]})["json"]
r = c.call("POST", "/api/v1/chats/%s/messages" % eve_chat["id"], [404, 201, 400, 409], actor=eve,
body={"text": "", "attachment_ids": [att["id"]]}, headers={"Idempotency-Key": uuid.uuid4().hex})
ok("outsider.attach_foreign_file_404", r["status"] == 404, str(r["status"]))
r = c.call("POST", "/api/v1/chats/%s/messages" % chat["id"], [404, 201], actor=eve,
body={"text": "x"}, headers={"Idempotency-Key": uuid.uuid4().hex})
ok("outsider.post_into_foreign_chat_404", r["status"] == 404, str(r["status"]))
r = c.call("GET", "/api/v1/chats", 200, actor=eve)["json"]
ok("outsider.chat_list_hides_foreign", all(x["id"] != chat["id"] for x in r["items"]), "")
# ---------------- подмена identity
r = c.call("GET", "/api/v1/users/me", 200, actor=eve, headers={"X-User-Id": alice["id"]})["json"]
ok("spoof.x_user_id_ignored", r["id"] == eve["id"], "me=%s" % ("eve" if r["id"] == eve["id"] else "OTHER"))
r = c.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]})
ok("spoof.x_user_id_alone_401", r["status"] == 401, "")
reg = alice["registration"]
r = c.call("GET", "/api/v1/chats", 401, headers={"Authorization": basic(reg["username"], reg["password"])})
ok("auth.basic_on_business_endpoint_401", r["status"] == 401, "")
r = c.call("GET", "/api/v1/users/me", 401, headers={"Authorization": "Bearer " + "A" * 43})
ok("auth.unknown_token_401", r["status"] == 401, r["headers"].get("WWW-Authenticate", ""))
# ---------------- одинаковый отказ: неизвестный логин vs неверный пароль
r1 = c.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": basic(reg["username"], "wrong-password-123")})
r2 = c.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": basic("no_such_" + uuid.uuid4().hex[:8], "wrong-password-123")})
same = (r1["json"]["error"]["code"], r1["json"]["error"]["message"], r1["headers"].get("WWW-Authenticate")) == \
(r2["json"]["error"]["code"], r2["json"]["error"]["message"], r2["headers"].get("WWW-Authenticate"))
ok("auth.same_401_unknown_user_and_wrong_password", same, "")
body = r1["bytes"].decode("utf-8", "replace")
ok("auth.error_has_no_credentials_or_internals",
not any(x in body.lower() for x in ("traceback", "argon", "select ", "password_hash", reg["password"].lower())), "")
# ---------------- logout
bob_token = bob["token"]
r = c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=bob)
ok("logout.204", r["status"] == 204, "")
r = c.call("GET", "/api/v1/users/me", 401, actor={"token": bob_token})
ok("logout.token_revoked_401", r["status"] == 401, "")
r = c.call("DELETE", "/api/v1/auth/sessions/current", 401, actor={"token": bob_token})
ok("logout.repeat_401", r["status"] == 401, "")
# ---------------- expiry (оператор сдвигает expires_at ТОЛЬКО своей тестовой сессии)
carol = c.user("carol")
th = hashlib.sha256(carol["token"].encode()).hexdigest()
c.call("GET", "/api/v1/users/me", 200, actor=carol)
n = psql("UPDATE sessions SET expires_at = now() - interval '1 second' WHERE token_hash = decode('%s','hex') RETURNING 1" % th)
r = c.call("GET", "/api/v1/users/me", 401, actor=carol)
ok("expiry.expired_session_401", r["status"] == 401 and n == "1", "rows=%s" % n)
# ---------------- Argon2id в БД
h = psql("SELECT password_hash FROM users WHERE id = '%s'" % alice["id"])
parts = h.split("$")
ok("argon2.stored_format", h.startswith("$argon2id$v=19$m=19456,t=2,p=1$") and len(parts) == 6,
"$".join(parts[:4]) + "$<salt>$<hash>")
h2 = psql("SELECT password_hash FROM users WHERE id = '%s'" % eve["id"])
ok("argon2.unique_salt", h.split("$")[4] != h2.split("$")[4], "")
# ---------------- rehash: старый scrypt (lab2–6) -> Argon2id при успешном входе
dave = c.user("dave")
pw = dave["registration"]["password"]
salt = os.urandom(16)
dg = hashlib.scrypt(pw.encode(), salt=salt, n=2 ** 15, r=8, p=1, maxmem=128 * 1024 * 1024, dklen=32)
legacy = "scrypt$15$8$1$%s$%s" % (base64.b64encode(salt).decode(), base64.b64encode(dg).decode())
psql("UPDATE users SET password_hash = '%s' WHERE id = '%s'" % (legacy, dave["id"]))
r = c.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": basic(dave["registration"]["username"], pw)})
after = psql("SELECT password_hash FROM users WHERE id = '%s'" % dave["id"])
ok("argon2.rehash_legacy_scrypt_on_login", r["status"] == 201 and after.startswith("$argon2id$"), after[:30])
# ---------------- лимиты размера
big = {"username": "x" * 10, "display_name": "y" * (1024 * 1024 + 10), "password": "z" * 20}
r = c.call("POST", "/api/v1/users", 413, body=big)
ok("limits.json_body_over_1mib_413", r["status"] == 413, r["json"]["error"]["code"])
r = c.call("POST", "/api/v1/chats/%s/attachments?filename=b.png" % chat["id"], 413, actor=alice,
raw=b"\x89PNG" + b"\0" * (10 * 1024 * 1024 + 1), headers={"Content-Type": "image/png"})
ok("limits.upload_over_10mib_413", r["status"] == 413, r["json"]["error"]["code"])
# ---------------- заголовки ответа
r = c.call("GET", "/health/ready", 200)
iid = r["headers"].get("X-Instance-Id", "")
ok("headers.instance_alias_random", iid.startswith("r-") and len(iid) == 12, iid)
ok("headers.no_server_banner", r["headers"].get("Server") is None, str(r["headers"].get("Server")))
ok("headers.hsts", "max-age" in (r["headers"].get("Strict-Transport-Security") or ""), "")
# ---------------- rate limit входа (общий на LB для всех реплик)
if not args.skip_rate_limit:
time.sleep(11) # окно http_req_rate(10s) от предыдущих входов теста
import urllib.error
import urllib.request
ctx = ssl.create_default_context(cafile=args.ca_file)
statuses, retry_after, err, rid_ok = [], None, None, False
for i in range(30):
# 429 формирует сам LB (stick-table): X-Instance-Id у такого ответа нет — это допустимо
# контрактом, поэтому здесь сырые запросы, а не публичный Client.
req = urllib.request.Request(args.base_url + "/api/v1/auth/sessions", method="POST", headers={
"Authorization": basic("rl_" + uuid.uuid4().hex[:8], "wrong-password-123")})
try:
with urllib.request.urlopen(req, timeout=10, context=ctx) as resp:
code, hdrs, data = resp.status, resp.headers, resp.read()
except urllib.error.HTTPError as exc:
code, hdrs, data = exc.code, exc.headers, exc.read()
statuses.append(code)
if code == 429 and retry_after is None:
retry_after = hdrs.get("Retry-After")
err = json.loads(data)["error"]["code"]
rid_ok = json.loads(data)["error"]["request_id"] == hdrs.get("X-Request-Id")
first = statuses.index(429) + 1 if 429 in statuses else None
ok("ratelimit.login_429_with_retry_after", first is not None and retry_after and err == "rate_limited" and rid_ok,
"first 429 at attempt %s, Retry-After=%s, code=%s, request_id matches header=%s" % (first, retry_after, err, rid_ok))
time.sleep(11)
r = c.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": basic(reg["username"], reg["password"])})
ok("ratelimit.recovers_after_window", r["status"] == 201, "")
failed = [x for x in RESULTS if not x["pass"]]
if args.output:
with open(args.output, "w") as f:
json.dump({"checked_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "base_url": args.base_url,
"total": len(RESULTS), "failed": len(failed), "results": RESULTS}, f, ensure_ascii=False, indent=1)
print("%s: %d checks, %d failed" % ("PASS" if not failed else "FAIL", len(RESULTS), len(failed)))
return 1 if failed else 0
if __name__ == "__main__":
try:
sys.exit(main())
except Failure as exc:
print("FAIL:", exc)
sys.exit(1)