Canalul web trece de la 100% deschis (hardcodat cont 1) la autentificat si multi-tenant. Un service nou se inregistreaza din browser, primeste o cheie API (o singura data) si o sesiune; contul se creeaza "in asteptare" (active=0) si nu trimite la RAR pana la activarea de catre admin (tools/account.py activate). - users + app/users.py: parole scrypt (salt per-user, eticheta parametri onorata la verify pentru migrare cost), email unic case-insensitive - sesiune: SessionMiddleware (same_site=strict, https_only config) + app/web/session.py (current_account/web_account/require_login->LoginRequired, set_session clear-inainte) - CSRF (app/web/csrf.py) enforce in prod inclusiv pe login/signup + rate-limit in-proces (app/web/ratelimit.py) pe signup si login - signup/login/logout (app/web/auth_routes.py): signup tranzactie atomica, cheie-o-data, log SIGNUP pentru descoperire admin - dashboard + import scoped pe contul sesiunii (regula NULL->cont 1); toate rutele web care ating date sensibile sub require_login; nomenclator ramane global - banner "cont in asteptare" pentru conturi active=0 - gate worker: claim_one LEFT JOIN accounts COALESCE(active,1)=1 (account_id NULL=activ) VERIFY context curat (2 runde): leak cross-account /_fragments/mapari prins+reparat. /code-review high: csrf_token lipsa pe re-randari de eroare, scrypt_params ignorat, login fara rate-limit -- toate reparate. 361 teste pass (de la 313). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
"""Rate-limit in-proces cu fereastra glisanta. US-009 PRD 3.3 C5.
|
|
|
|
Fara dependinta externa. Folosit de POST /signup (US-003) cu cheia = IP client.
|
|
Configurabil prin AUTOPASS_signup_rate_max / AUTOPASS_signup_rate_window_s (config.py).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import defaultdict
|
|
|
|
# ip/key -> lista de timestamps (time.monotonic) ale cererilor din fereastra activa
|
|
_hits: dict[str, list[float]] = defaultdict(list)
|
|
|
|
|
|
def check_rate_limit(key: str, max_hits: int, window_s: int) -> bool:
|
|
"""Fereastra glisanta: returneaza True daca cererea e permisa, False la depasire.
|
|
|
|
Curata timestamp-urile expirate la fiecare apel (O(n) per cheie, acceptabil
|
|
pentru trafic de signup). Thread-safety: GIL Python protejeaza list ops simple;
|
|
suficient pentru un singur proces uvicorn.
|
|
"""
|
|
now = time.monotonic()
|
|
cutoff = now - window_s
|
|
timestamps = _hits[key]
|
|
# Sterge intrari expirate
|
|
_hits[key] = [t for t in timestamps if t > cutoff]
|
|
if len(_hits[key]) >= max_hits:
|
|
return False
|
|
_hits[key].append(now)
|
|
return True
|