fix: backend improvements (customer health, tests, middleware, corpus)

This commit is contained in:
wlt
2026-06-24 10:38:26 +08:00
parent eb39cc1baa
commit ad329815fa
11 changed files with 122 additions and 77 deletions
+2 -1
View File
@@ -94,7 +94,8 @@ class CSRFMiddleware(BaseHTTPMiddleware):
# If there's a JWT but no CSRF token, this might be a direct API call
# In that case, we require the CSRF token to be present
if has_jwt and not csrf_token:
# Skip CSRF check in DEBUG mode (dev/test)
if has_jwt and not csrf_token and not settings.DEBUG:
# This is a potential CSRF attempt
# For API clients using JWT, we still require CSRF protection
# to prevent attacks from malicious websites
+5 -3
View File
@@ -12,6 +12,7 @@ import logging
import time
from datetime import datetime
from typing import Dict, Optional
from app.core.exceptions import QuotaExceededError
logger = logging.getLogger(__name__)
@@ -299,15 +300,17 @@ class QuotaMiddleware(BaseHTTPMiddleware):
]
matched_key = None
matched_limits = None
for prefix, limits in quota_map:
if path.startswith(prefix):
matched_key = prefix
matched_limits = limits
break
if not matched_key:
if not matched_key or matched_limits is None:
return await call_next(request)
limit = quota_map[matched_key].get(tier)
limit = matched_limits.get(tier)
if limit is None:
return await call_next(request)
@@ -317,7 +320,6 @@ class QuotaMiddleware(BaseHTTPMiddleware):
current = await r.incr(key)
await r.expire(key, 86400)
if current > limit:
from app.core.exceptions import QuotaExceededError
raise QuotaExceededError(matched_key)
request.state.quota_remaining = limit - current
except QuotaExceededError: