fix: harden proxy auth and deployment security

This commit is contained in:
nyan404 committed 2026-10-25 00:40:52 +03:00
1 parent f5e8dde608
commit ebf5d45491
14 files changed
+172 -44

No files matched your search

+50 -1
View File
@@ -4,13 +4,15 @@ import tempfile
import threading
import unittest
import zipfile
from ipaddress import ip_network
from datetime import datetime, timezone
from io import BytesIO
from pathlib import Path
from unittest.mock import patch
from app.belki.admin_filters import WEB_TIMEZONE, matches_period, period_bounds
from app.belki.xlsx_export import build_xlsx
from app.main import BelkiHTTPServer, Handler
from app.main import BelkiHTTPServer, Handler, _ADMIN_AUTH_ATTEMPTS
class AdminFilterAndExportTests(unittest.TestCase):
@@ -61,3 +63,50 @@ class AdminFilterAndExportTests(unittest.TestCase):
del os.environ["BELKI_ADMIN_PASSWORD"]
else:
os.environ["BELKI_ADMIN_PASSWORD"] = prior
def test_admin_login_proxy_peers_do_not_share_app_lockout(self) -> None:
prior = os.environ.get("BELKI_ADMIN_PASSWORD")
os.environ["BELKI_ADMIN_PASSWORD"] = "test-password"
_ADMIN_AUTH_ATTEMPTS.clear()
server = BelkiHTTPServer(("127.0.0.1", 0), Handler)
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
try:
with patch("app.main.ADMIN_AUTH_RATE_LIMIT_EXEMPT_PROXY_NETWORKS", (ip_network("127.0.0.0/8"),)):
for index in range(7):
connection = http.client.HTTPConnection("127.0.0.1", server.server_port)
connection.request("POST", "/api/admin/auth", body='{"password":"wrong"}', headers={"Content-Type": "application/json", "X-Real-IP": f"198.51.100.{10 + index}"})
response = connection.getresponse()
self.assertEqual(response.status, 401)
response.read()
connection.close()
connection = http.client.HTTPConnection("127.0.0.1", server.server_port)
connection.request("POST", "/api/admin/auth", body='{"password":"test-password"}', headers={"Content-Type": "application/json"})
response = connection.getresponse()
self.assertEqual(response.status, 200)
response.read()
connection.close()
with patch("app.main.ADMIN_AUTH_RATE_LIMIT_EXEMPT_PROXY_NETWORKS", (ip_network("10.42.3.0/24"),)):
for _ in range(5):
connection = http.client.HTTPConnection("127.0.0.1", server.server_port)
connection.request("POST", "/api/admin/auth", body='{"password":"wrong"}', headers={"Content-Type": "application/json", "X-Real-IP": "198.51.100.10"})
response = connection.getresponse()
self.assertEqual(response.status, 401)
response.read()
connection.close()
connection = http.client.HTTPConnection("127.0.0.1", server.server_port)
connection.request("POST", "/api/admin/auth", body='{"password":"wrong"}', headers={"Content-Type": "application/json", "X-Real-IP": "198.51.100.11"})
response = connection.getresponse()
self.assertEqual(response.status, 429)
response.read()
connection.close()
finally:
server.shutdown()
server.server_close()
worker.join(timeout=2)
_ADMIN_AUTH_ATTEMPTS.clear()
if prior is None:
del os.environ["BELKI_ADMIN_PASSWORD"]
else:
os.environ["BELKI_ADMIN_PASSWORD"] = prior
+18 -1
View File
@@ -4,7 +4,7 @@ from unittest.mock import MagicMock, patch
from app.belki.image_host import MAX_MIRRORED_IMAGE_BYTES, ImageHostClient, ImageHostError, ImageStoreClient, detect_raster_image_type as detect_mirrored_image_type
from app.main import (ADMIN_SESSION_TTL_SECONDS, MAX_ADMIN_AUTH_ATTEMPTS, MAX_ADMIN_SESSIONS, MAX_CONCURRENT_ADMIN_UPDATES, MAX_CONCURRENT_REQUESTS, MAX_ISSUED_WEB_UPLOAD_URLS, MAX_JSON_BODY_BYTES,
REQUEST_READ_TIMEOUT_SECONDS, ADMIN_AUTH_WINDOW_SECONDS, BelkiHTTPServer, Handler,
_ADMIN_AUTH_ATTEMPTS, _ADMIN_SESSIONS, _ISSUED_WEB_UPLOAD_URLS, admin_session_authorized, allow_admin_auth_attempt,
_ADMIN_AUTH_ATTEMPTS, _ADMIN_SESSIONS, _ISSUED_WEB_UPLOAD_URLS, admin_auth_rate_limit_key, admin_session_authorized, allow_admin_auth_attempt,
acquire_admin_update_slot, clear_admin_auth_attempts, detect_raster_image_type,
forget_admin_session, remember_admin_session, remember_issued_web_upload, release_admin_update_slot, safe_upload_name,
validate_web_attachments, validate_web_report_attachments, WEB_UPLOAD_URL_TTL_SECONDS)
@@ -67,6 +67,23 @@ class ImageHostClientTests(unittest.TestCase):
clear_admin_auth_attempts("192.0.2.1")
self.assertNotIn("192.0.2.1", _ADMIN_AUTH_ATTEMPTS)
def test_admin_auth_rate_limit_skips_only_configured_proxy_peers(self) -> None:
from ipaddress import ip_network
trusted = (ip_network("10.42.3.0/24"),)
self.assertEqual(
admin_auth_rate_limit_key("10.42.3.58", exempt_proxy_networks=trusted),
None,
)
self.assertEqual(
admin_auth_rate_limit_key("10.42.4.58", exempt_proxy_networks=trusted),
"10.42.4.58",
)
self.assertEqual(
admin_auth_rate_limit_key("192.0.2.44", exempt_proxy_networks=trusted),
"192.0.2.44",
)
def test_admin_sessions_expire_and_have_a_fixed_capacity(self) -> None:
_ADMIN_SESSIONS.clear()
remember_admin_session("active", now=100)