fix: harden proxy auth and deployment security
This commit is contained in:
1 parent
f5e8dde608
commit
ebf5d45491
14 files changed
+172
-44
No files matched your search
+28
-3
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import json
|
||||
import base64
|
||||
import hashlib
|
||||
from ipaddress import ip_address, ip_network
|
||||
import mimetypes
|
||||
import os
|
||||
import re
|
||||
@@ -47,6 +48,11 @@ MAX_CONCURRENT_REQUESTS = 32
|
||||
MAX_CONCURRENT_ADMIN_UPDATES = 8
|
||||
ADMIN_AUTH_WINDOW_SECONDS = 15 * 60
|
||||
MAX_ADMIN_AUTH_ATTEMPTS = 5
|
||||
ADMIN_AUTH_RATE_LIMIT_EXEMPT_PROXY_NETWORKS = tuple(
|
||||
ip_network(value.strip(), strict=False)
|
||||
for value in os.getenv("BELKI_ADMIN_RATE_LIMIT_EXEMPT_PROXY_CIDRS", "").split(",")
|
||||
if value.strip()
|
||||
)
|
||||
_ADMIN_UPDATE_SLOTS = threading.BoundedSemaphore(MAX_CONCURRENT_ADMIN_UPDATES)
|
||||
WEB_UPLOAD_URL_TTL_SECONDS = 30 * 60
|
||||
MAX_ISSUED_WEB_UPLOAD_URLS = 1_000
|
||||
@@ -130,6 +136,21 @@ def allow_admin_auth_attempt(client_ip: str, *, now: float | None = None) -> boo
|
||||
return True
|
||||
|
||||
|
||||
def admin_auth_rate_limit_key(
|
||||
remote_ip: str,
|
||||
*,
|
||||
exempt_proxy_networks: tuple = ADMIN_AUTH_RATE_LIMIT_EXEMPT_PROXY_NETWORKS,
|
||||
) -> str | None:
|
||||
"""Skip the shared app-side bucket for known proxy peers; they hide client IPs."""
|
||||
try:
|
||||
peer = ip_address(remote_ip)
|
||||
except ValueError:
|
||||
return remote_ip
|
||||
if any(peer in network for network in exempt_proxy_networks):
|
||||
return None
|
||||
return str(peer)
|
||||
|
||||
|
||||
def clear_admin_auth_attempts(client_ip: str) -> None:
|
||||
"""Удаляет временные неудачные попытки входа администратора для IP."""
|
||||
with _ADMIN_AUTH_LOCK:
|
||||
@@ -588,15 +609,19 @@ class Handler(BaseHTTPRequestHandler):
|
||||
sudoku_store().get(game_id, payload.get("nickname"))
|
||||
self._json(201, {"game": public_sudoku_game(sudoku_store().create(payload.get("nickname"), payload.get("difficulty")))})
|
||||
elif self.path == "/api/admin/auth":
|
||||
client_ip = self.client_address[0]
|
||||
if not allow_admin_auth_attempt(client_ip):
|
||||
client_ip = admin_auth_rate_limit_key(
|
||||
self.client_address[0],
|
||||
exempt_proxy_networks=ADMIN_AUTH_RATE_LIMIT_EXEMPT_PROXY_NETWORKS,
|
||||
)
|
||||
if client_ip is not None and not allow_admin_auth_attempt(client_ip):
|
||||
self._json(429, {"error": "Слишком много попыток. Повторите через 15 минут."})
|
||||
return
|
||||
expected = os.getenv("BELKI_ADMIN_PASSWORD", "")
|
||||
if not expected or not secrets.compare_digest(str(payload.get("password", "")), expected):
|
||||
self._json(401, {"error": "Неверный пароль"})
|
||||
return
|
||||
clear_admin_auth_attempts(client_ip)
|
||||
if client_ip is not None:
|
||||
clear_admin_auth_attempts(client_ip)
|
||||
token = secrets.token_urlsafe(32)
|
||||
remember_admin_session(token)
|
||||
self._json_with_cookie(200, {"authenticated": True}, self._admin_cookie(token))
|
||||
|
||||
Reference in new issue
Block a user