security: fix critical vulnerabilities and code quality issues

Critical fixes:
- init_admin.py: replace hardcoded admin123 password with ADMIN_PASSWORD env var
- proxmox_client.py, models.py: change verify_ssl default from False to True
- api/proxmox.py: use server.verify_ssl instead of hardcoded verify=False in websocket handler
- ssh_client.py: replace AutoAddPolicy (MITM-vulnerable) with WarningPolicy + load_system_host_keys

Security hardening:
- main.py: add security headers middleware (CSP, X-Frame-Options, HSTS, X-XSS-Protection, Referrer-Policy)
- main.py: fix CORS — allow_credentials=True is incompatible with allow_origins=["*"]; read origins from CORS_ORIGINS env var
- auth.py, logging_middleware.py: validate X-Forwarded-For/X-Real-IP to prevent IP spoofing

Performance:
- monitoring_worker.py: wire up cleanup_expired_connections() as hourly scheduled job (was defined but never called)

Code quality:
- logging_service.py: move `import json` from inside function to module level
- ssh_client.py, proxmox_client.py, api/proxmox.py, rbac/migration.py, task_queue_service.py: replace all bare `except:` with `except Exception:`
- .env.example, config.py: document ADMIN_PASSWORD and CORS_ORIGINS settings

https://claude.ai/code/session_019rK2wvaWg5zXFwmAPPEB7R
This commit is contained in:
Claude
2026-03-06 15:57:20 +00:00
parent e6facf43f4
commit 91ff467f2f
14 changed files with 122 additions and 53 deletions

View File

@@ -26,8 +26,11 @@ DEFAULT_SSH_USER=root
DEFAULT_SSH_PORT=22
# Security
# In production set to your actual domain(s), e.g.: https://panel.example.com
CORS_ORIGINS=*
ALLOWED_HOSTS=localhost,127.0.0.1
# IMPORTANT: Change this before first deployment!
ADMIN_PASSWORD=admin123
# Update Check Configuration
# Note: DISABLE_UPDATE_CHECK and GITHUB_TOKEN are managed via root .env

View File

@@ -190,7 +190,7 @@ async def auto_setup_proxmox_server(
port = data.get('port', 8006)
api_user = data.get('api_user', 'root@pam')
password = data.get('password')
verify_ssl = data.get('verify_ssl', False)
verify_ssl = data.get('verify_ssl', True)
description = data.get('description')
if not all([name, ip_address, password]):
@@ -2065,7 +2065,7 @@ def control_vm(
try:
vm_status = client.get_vm_status(node, vmid)
vm_name = vm_status.get('name') if isinstance(vm_status, dict) else None
except:
except Exception:
pass
if action == 'start':
@@ -3733,7 +3733,7 @@ async def vnc_websocket_proxy(
await proxmox_ws.close()
try:
await websocket.close()
except:
except Exception:
pass
logger.info(f"VNC WebSocket connection closed for {vmtype}/{vmid}")
@@ -5202,7 +5202,7 @@ async def terminal_websocket_fixed(
try:
# Шаг 1: Получаем auth ticket через Proxmox API
async with httpx.AsyncClient(verify=False, timeout=10) as client:
async with httpx.AsyncClient(verify=server.verify_ssl, timeout=10) as client:
auth_url = f"https://{server.ip_address}:8006/api2/json/access/ticket"
# Используем пароль если есть, иначе API token
@@ -5327,7 +5327,7 @@ async def terminal_websocket_fixed(
logger.error(traceback.format_exc())
try:
await websocket.close(code=1011, reason=str(e))
except:
except Exception:
pass
finally:
logger.info(f"Terminal session closed for container {vmid}")

View File

@@ -179,19 +179,32 @@ def require_permission(user: User, permission: str) -> None:
PermissionEngine.check_permission(user, permission)
def _validate_ip(ip: str) -> bool:
"""Return True if ip is a valid IPv4 or IPv6 address."""
import ipaddress
try:
ipaddress.ip_address(ip)
return True
except ValueError:
return False
def get_client_ip(request: Request) -> str:
"""Get client IP from request, handling proxies"""
# Try X-Forwarded-For first (from nginx/proxy)
"""Get client IP from request, handling proxies.
Only X-Forwarded-For / X-Real-IP values that parse as valid IP addresses
are trusted, preventing header-injection / IP-spoofing attacks.
"""
forwarded = request.headers.get("x-forwarded-for")
if forwarded:
return forwarded.split(",")[0].strip()
candidate = forwarded.split(",")[0].strip()
if _validate_ip(candidate):
return candidate
# Try X-Real-IP
real_ip = request.headers.get("x-real-ip")
if real_ip:
real_ip = request.headers.get("x-real-ip", "").strip()
if real_ip and _validate_ip(real_ip):
return real_ip
# Fall back to direct client
if request.client:
return request.client.host

View File

@@ -47,6 +47,12 @@ class Settings(BaseSettings):
DEFAULT_SSH_USER: str = Field(default="root", env="DEFAULT_SSH_USER")
DEFAULT_SSH_PORT: int = Field(default=22, env="DEFAULT_SSH_PORT")
# CORS — comma-separated list of allowed origins; "*" is insecure in production
CORS_ORIGINS: str = Field(default="*", env="CORS_ORIGINS")
# Default admin password — MUST be changed via env var in production
ADMIN_PASSWORD: str = Field(default="admin123", env="ADMIN_PASSWORD")
# Timezone
TZ: str = Field(default="UTC", env="TZ")

View File

@@ -76,11 +76,10 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
auth_header = request.headers.get("authorization", "")
if auth_header.startswith("Bearer "):
token = auth_header[7:]
# Extract session ID from token (first 16 chars of hash)
import hashlib
session_id = hashlib.sha256(token.encode()).hexdigest()[:16]
except:
pass
except Exception as e:
logger.debug(f"Could not extract session ID from token: {e}")
# Capture request body for important operations
request_body = None
@@ -91,10 +90,10 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
if len(body_bytes) <= self.MAX_BODY_SIZE:
try:
request_body = json.loads(body_bytes)
except:
except Exception:
request_body = {"_raw": body_bytes.decode('utf-8', errors='replace')[:1000]}
except:
pass
except Exception as e:
logger.debug(f"Could not read request body: {e}")
# Get query params
query_params = str(request.query_params) if request.query_params else None
@@ -174,19 +173,27 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
return response
@staticmethod
def _is_valid_ip(ip: str) -> bool:
import ipaddress
try:
ipaddress.ip_address(ip)
return True
except ValueError:
return False
def _get_client_ip(self, request: Request) -> str:
"""Extract client IP from request, considering proxies"""
# Check X-Forwarded-For header (from reverse proxy)
"""Extract client IP from request, validating proxy headers to prevent spoofing."""
forwarded_for = request.headers.get("x-forwarded-for")
if forwarded_for:
return forwarded_for.split(",")[0].strip()
candidate = forwarded_for.split(",")[0].strip()
if self._is_valid_ip(candidate):
return candidate
# Check X-Real-IP header
real_ip = request.headers.get("x-real-ip")
if real_ip:
real_ip = request.headers.get("x-real-ip", "").strip()
if real_ip and self._is_valid_ip(real_ip):
return real_ip
# Fall back to direct client IP
if request.client:
return request.client.host

View File

@@ -3,6 +3,7 @@ Logging Service for PVEmanager
Provides centralized logging functionality for audit, system, and API events
"""
import json
import uuid
import traceback
from datetime import datetime, timedelta, timezone
@@ -74,7 +75,6 @@ class LoggingService:
return data[:max_size] + f"... (truncated, {len(data)} total chars)"
return data
if isinstance(data, dict):
import json
json_str = json.dumps(data)
if len(json_str) > max_size:
return {"_truncated": True, "_size": len(json_str), "_preview": json_str[:500]}

View File

@@ -163,10 +163,14 @@ def create_app() -> FastAPI:
)
# Add CORS middleware
cors_origins = [o.strip() for o in settings.CORS_ORIGINS.split(",") if o.strip()]
# allow_credentials=True is incompatible with allow_origins=["*"] per the CORS spec.
# Disable credentials when wildcard origin is configured.
allow_credentials = cors_origins != ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, specify exact origins
allow_credentials=True,
allow_origins=cors_origins,
allow_credentials=allow_credentials,
allow_methods=["*"],
allow_headers=["*"],
)
@@ -180,7 +184,26 @@ def create_app() -> FastAPI:
# Templates
templates = Jinja2Templates(directory="app/templates")
# Security headers middleware
@app.middleware("http")
async def add_security_headers(request: Request, call_next):
response = await call_next(request)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["X-Frame-Options"] = "SAMEORIGIN"
response.headers["X-XSS-Protection"] = "1; mode=block"
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
response.headers["Content-Security-Policy"] = (
"default-src 'self'; "
"script-src 'self' 'unsafe-inline' 'unsafe-eval'; "
"style-src 'self' 'unsafe-inline'; "
"img-src 'self' data: blob:; "
"connect-src 'self' wss: ws:; "
"font-src 'self' data:; "
"frame-ancestors 'self';"
)
if not settings.DEBUG:
response.headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains"
return response
# Custom exception handler
@app.exception_handler(Exception)

View File

@@ -382,7 +382,7 @@ class ProxmoxServer(Base):
password = Column(String(255), nullable=True) # Encrypted in production
# SSL/TLS verification
verify_ssl = Column(Boolean, nullable=False, default=False)
verify_ssl = Column(Boolean, nullable=False, default=True)
# Metadata
description = Column(Text, nullable=True)

View File

@@ -19,7 +19,7 @@ class ProxmoxClient:
"""Клиент для работы с Proxmox VE API с кешированием соединений"""
def __init__(self, host: str, user: str = "root@pam", password: str = None,
token_name: str = None, token_value: str = None, verify_ssl: bool = False,
token_name: str = None, token_value: str = None, verify_ssl: bool = True,
timeout: int = 30):
"""
Инициализация Proxmox клиента
@@ -994,7 +994,7 @@ class ProxmoxClient:
'is_template': is_template,
'status': status
}
except:
except Exception:
continue
return None

View File

@@ -328,7 +328,7 @@ def migrate_all_roles_to_new_format(conn) -> int:
if isinstance(permissions, str):
try:
permissions = json.loads(permissions)
except:
except Exception:
continue
# Check if already migrated (has : in keys)
@@ -382,7 +382,7 @@ def ensure_default_roles_new_format(conn) -> None:
if isinstance(current_perms, str):
try:
current_perms = json.loads(current_perms)
except:
except Exception:
current_perms = {}
# Check if needs update

View File

@@ -291,7 +291,7 @@ class TaskQueueProcessor:
task.error_message = str(e)
task.completed_at = utcnow()
db.commit()
except:
except Exception:
pass
finally:
db.close()
@@ -325,7 +325,7 @@ class TaskQueueProcessor:
finally:
try:
db.close()
except:
except Exception:
pass

View File

@@ -46,7 +46,7 @@ class SSHClient:
transport = client.get_transport()
if transport and transport.is_active():
return client
except:
except Exception:
pass
# Remove invalid connection
@@ -64,7 +64,7 @@ class SSHClient:
if self.connection_key in self._connections:
try:
self._connections[self.connection_key].close()
except:
except Exception:
pass
del self._connections[self.connection_key]
del self._connection_times[self.connection_key]
@@ -82,7 +82,8 @@ class SSHClient:
for attempt in range(self.max_retries):
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
connect_kwargs = {
'hostname': self.hostname,
@@ -169,7 +170,7 @@ class SSHClient:
try:
transport = self.client.get_transport()
return transport and transport.is_active()
except:
except Exception:
return False
def close(self):
@@ -235,7 +236,7 @@ class SSHClient:
for key in list(cls._connections.keys()):
try:
cls._connections[key].close()
except:
except Exception:
pass
cls._connections.clear()
cls._connection_times.clear()
@@ -409,7 +410,7 @@ async def cleanup_ssh_connections():
if key in SSHClient._connections:
try:
SSHClient._connections[key].close()
except:
except Exception:
pass
del SSHClient._connections[key]
del SSHClient._connection_times[key]

View File

@@ -972,6 +972,20 @@ def start_monitoring_worker():
replace_existing=True
)
# Cleanup expired Proxmox connection cache - every hour
try:
from app.proxmox_client import cleanup_expired_connections
scheduler.add_job(
cleanup_expired_connections,
trigger=IntervalTrigger(hours=1),
id='proxmox_cache_cleanup',
name='Cleanup expired Proxmox connections',
replace_existing=True
)
logger.info("Proxmox cache cleanup task registered")
except ImportError as e:
logger.warning(f"Proxmox cache cleanup not available: {e}")
# Run initial VM cache sync on startup
try:
logger.info("Running initial VM cache sync...")

View File

@@ -17,7 +17,9 @@ def create_default_admin():
"""Create admin user with default credentials and full RBAC permissions"""
username = "admin"
email = "admin@example.com"
password = "admin123"
password = os.environ.get("ADMIN_PASSWORD", "admin123")
if password == "admin123":
print("⚠️ WARNING: Using default password 'admin123'. Set ADMIN_PASSWORD env var in production!")
db = next(get_db())