forked from mtusi2026/mtusi2026lab7
feat: harden authentication (Argon2id by measurement, login limits, body limit)
Apply auth-hardening up to the bench_login label: Argon2id parameters from [auth] chosen by driver.py argon2-params (m=64 MiB, t=2, p=1), rehash on login, per-process KDF budget with 429, shared login failure limit in Valkey with a local fallback, X-Forwarded-For from HAProxy, 64 KiB body limit (400) in LB and middleware, bench login profile, tests, threat model and trust matrix rows. Hypotheses H1-H4 are committed here before the baseline label. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -481,3 +481,109 @@ def test_attachment_ids_in_upper_case(
|
||||
assert_content(api.attachment_content(alice, upper), data, "image/png")
|
||||
message = api.send_message(alice, chat["id"], "x", attachment_ids=[upper])
|
||||
assert message["attachment_ids"] == [attachment["id"]]
|
||||
|
||||
|
||||
# предел размера тела запроса (change auth-hardening, design, решение 8):
|
||||
# 64 KiB вне загрузки вложения, отказ — 400 invalid_request
|
||||
BODY_MAX_BYTES = 65536
|
||||
|
||||
|
||||
def _chunks(total: int, size: int = 8192):
|
||||
"""Тело частями без Content-Length (httpx шлёт chunked)."""
|
||||
sent = 0
|
||||
while sent < total:
|
||||
part = min(size, total - sent)
|
||||
sent += part
|
||||
yield b" " * part
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Объявленное тело больше предела")
|
||||
def test_declared_body_over_limit(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
body = b'{"text": "x"' + b" " * (BODY_MAX_BYTES + 1 - 13) + b"}"
|
||||
assert len(body) == BODY_MAX_BYTES + 1
|
||||
headers = {"Content-Type": "application/json"}
|
||||
with_auth = api.post_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
content=body,
|
||||
headers=headers,
|
||||
)
|
||||
without_auth = api.post_message(
|
||||
None,
|
||||
chat["id"],
|
||||
content=body,
|
||||
headers=headers,
|
||||
)
|
||||
# через LB отказывает LB: X-Instance-Id у его ответа нет
|
||||
for resp in (with_auth, without_auth):
|
||||
error = assert_error(resp, 400, instance_id=False)
|
||||
assert error["message"] == "Request body is too large"
|
||||
assert api.all_messages(alice, chat["id"]) == []
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Тело частями больше предела")
|
||||
def test_chunked_body_over_limit(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
resp = api.post_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
content=_chunks(BODY_MAX_BYTES + 8192),
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
# Content-Length нет — LB тело не меряет, отказывает реплика
|
||||
error = assert_error(resp, 400)
|
||||
assert error["message"] == "Request body is too large"
|
||||
assert_instance_id(resp)
|
||||
assert api.all_messages(alice, chat["id"]) == []
|
||||
|
||||
|
||||
@pytest.mark.spec("api-conventions", "Наибольшее валидное тело принимается")
|
||||
def test_largest_valid_body_accepted(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
attachment_ids = [
|
||||
api.upload(alice, chat["id"], images.sample_png())["id"]
|
||||
for _ in range(10)
|
||||
]
|
||||
# 4000 символов вне BMP, каждый — экранированная суррогатная пара
|
||||
text = "\\ud83d\\ude00" * 4000
|
||||
body = (
|
||||
'{"text": "'
|
||||
+ text
|
||||
+ '", "attachment_ids": '
|
||||
+ json.dumps(attachment_ids)
|
||||
+ "}"
|
||||
).encode("ascii")
|
||||
assert 48000 < len(body) < BODY_MAX_BYTES
|
||||
resp = api.post_message(
|
||||
alice,
|
||||
chat["id"],
|
||||
content=body,
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
message = assert_ok(resp, 201, "Message")
|
||||
assert message["text"] == "\U0001f600" * 4000
|
||||
assert message["attachment_ids"] == attachment_ids
|
||||
|
||||
|
||||
@pytest.mark.spec(
|
||||
"api-conventions",
|
||||
"Загрузка вложения не ограничена пределом JSON",
|
||||
)
|
||||
def test_upload_not_limited_by_json_limit(
|
||||
api: Api,
|
||||
alice: Actor,
|
||||
chat: dict[str, Any],
|
||||
) -> None:
|
||||
data = images.png_noise(seed=7)
|
||||
assert len(data) > BODY_MAX_BYTES
|
||||
api.upload(alice, chat["id"], data)
|
||||
|
||||
Reference in New Issue
Block a user