Регистрация: throttle по IP от спама аккаунтов
throttle_register ограничивает частоту POST /api/auth/register с одного IP через тот же LoginThrottle (считаются все попытки). Enumeration ников через 409 не закрываем — ники и так публичны в топе (отмечено в отчёте). #62 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XfTsytzT6TojfmprRDKiV6
This commit is contained in:
@@ -65,6 +65,27 @@ def test_register_rejects_bad_nickname(client: TestClient):
|
||||
assert _register(client, nickname="x").status_code == 422
|
||||
|
||||
|
||||
def test_register_is_throttled_per_ip(client: TestClient, monkeypatch):
|
||||
"""Спам регистраций с одного IP упирается в лимит (#62)."""
|
||||
import app.core.ratelimit as ratelimit
|
||||
from app.auth.password import _REGISTER_IP_LIMIT
|
||||
|
||||
now = [4000.0]
|
||||
monkeypatch.setattr(ratelimit.time, "monotonic", lambda: now[0])
|
||||
|
||||
for i in range(_REGISTER_IP_LIMIT):
|
||||
client.cookies.clear()
|
||||
assert _register(client, nickname=f"Ник{i}").status_code == 200
|
||||
client.cookies.clear()
|
||||
blocked = _register(client, nickname="Лишний")
|
||||
assert blocked.status_code == 429
|
||||
assert blocked.json()["error"]["code"] == "TOO_MANY_ATTEMPTS"
|
||||
|
||||
now[0] += 15 * 60 # окно истекло
|
||||
client.cookies.clear()
|
||||
assert _register(client, nickname="ПослеОкна").status_code == 200
|
||||
|
||||
|
||||
# ─── Вход ────────────────────────────────────────────────────────────────────
|
||||
|
||||
def test_login_after_logout(client: TestClient):
|
||||
|
||||
Reference in New Issue
Block a user