Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
755d48a2f8 | ||
|
|
284880a170 | ||
|
|
950b516081 | ||
|
|
0088a7b447 | ||
|
|
19f646c318 | ||
|
|
115ac5caf7 | ||
|
|
798082934e | ||
|
|
a017b0fa93 | ||
|
|
877dbb6a4e |
No files matched your search
@@ -31,27 +31,28 @@ async def upload(
|
||||
filename = img.filename
|
||||
if not filename:
|
||||
return ResponseUtils.error(message="No filename provided", code=400)
|
||||
# sanitize filename to prevent path traversal
|
||||
filename = os.path.basename(filename)
|
||||
|
||||
# enforce content type and size limits
|
||||
allowed_types = {"image/jpeg", "image/png", "image/webp"}
|
||||
if img.content_type not in allowed_types:
|
||||
return ResponseUtils.error(message="Unsupported file type", code=400)
|
||||
|
||||
# 2. Calculate SHA256 of uploaded content
|
||||
contents = await img.read()
|
||||
max_size = 5 * 1024 * 1024
|
||||
if len(contents) > max_size:
|
||||
return ResponseUtils.error(message="File too large (max 5MB)", code=413)
|
||||
file_hash = hashlib.sha256(contents).hexdigest()
|
||||
|
||||
# Reset file pointer for subsequent save
|
||||
await img.seek(0)
|
||||
|
||||
# Verify provided SHA256 matches computed hash
|
||||
if sha256 and file_hash != sha256:
|
||||
return ResponseUtils.error(message="File SHA256 checksum mismatch", code=400)
|
||||
|
||||
# 3. Create directory based on current date
|
||||
today = datetime.now()
|
||||
date_dir = today.strftime("%Y%m%d")
|
||||
|
||||
# Create upload directory
|
||||
upload_dir = os.path.join("uploads","images")
|
||||
upload_dir = os.path.join(upload_dir, date_dir)
|
||||
upload_dir = os.path.join("uploads","images", date_dir)
|
||||
os.makedirs(upload_dir, exist_ok=True)
|
||||
# Save file
|
||||
file_path = os.path.join(upload_dir, filename)
|
||||
|
||||
with open(file_path, "wb") as f:
|
||||
|
||||
@@ -33,21 +33,36 @@ class AuthService:
|
||||
|
||||
def send_email_login_captcha(self, email: str) -> bool:
|
||||
"""
|
||||
Sends a login captcha code to the specified email address.
|
||||
The function generates a random 6-digit captcha code, stores it in Redis cache for 10 minutes,
|
||||
and sends the code to the user's email using HTML template.
|
||||
Args:
|
||||
email (str): The recipient's email address.
|
||||
Returns:
|
||||
bool: True if email was sent successfully, otherwise False.
|
||||
Sends a login captcha code to the specified email address with simple rate limiting.
|
||||
Limit: 5 sends per 10 minutes per email.
|
||||
"""
|
||||
# Rate limit
|
||||
try:
|
||||
from services.common.redis import redis_client
|
||||
rate_key = f"email_login_rate:{email}"
|
||||
current = redis_client.get(rate_key)
|
||||
if current is None:
|
||||
redis_client.set(rate_key, "1", ex=600)
|
||||
count = 1
|
||||
else:
|
||||
try:
|
||||
count = int(current) + 1
|
||||
except Exception:
|
||||
count = 1
|
||||
ttl = redis_client.ttl(rate_key)
|
||||
ttl = int(ttl) if isinstance(ttl, (int, str)) else 600
|
||||
if ttl < 1:
|
||||
ttl = 600
|
||||
redis_client.set(rate_key, str(count), ex=ttl)
|
||||
if count > 5:
|
||||
logger.warning(f"Email send rate limited for: {email}")
|
||||
return False
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Generate random 6-digit verification code
|
||||
captcha = str(random.randint(100000, 999999))
|
||||
|
||||
# Cache to redis
|
||||
CacheUtils.set_cache(RedisKeys.email_login_captcha(email), captcha, 10 * 60)
|
||||
|
||||
# Send verification code using HTML email template
|
||||
return EmailUtils.send_register_code_email(self.db, email, captcha)
|
||||
|
||||
def email_login_by_captcha(self, email: str, captcha: str) -> Optional[str]:
|
||||
|
||||
@@ -2,14 +2,6 @@ import os
|
||||
import ssl
|
||||
import urllib3
|
||||
|
||||
# Disable SSL certificate verification warnings
|
||||
try:
|
||||
_create_unverified_https_context = ssl._create_unverified_context
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
ssl._create_default_https_context = _create_unverified_https_context
|
||||
|
||||
from alipay.aop.api.AlipayClientConfig import AlipayClientConfig
|
||||
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
|
||||
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
|
||||
|
||||
@@ -9,6 +9,8 @@ from starlette.requests import Request
|
||||
from starlette.responses import Response
|
||||
import asyncio
|
||||
import time
|
||||
import hashlib
|
||||
import re
|
||||
from mcp.server.sse import SseServerTransport
|
||||
from mcp.server.streamable_http import StreamableHTTPServerTransport
|
||||
from mcp.server.lowlevel import Server
|
||||
@@ -43,7 +45,8 @@ class McpController:
|
||||
async def handle_sse_connection(self, request: Request):
|
||||
"""Handle MCP Streamable HTTP SSE connection with resume capability."""
|
||||
# Get connection identification info
|
||||
client_ip = request.client.host if request.client else "unknown"
|
||||
x_real_ip = request.headers.get("x-real-ip") or request.headers.get("x-forwarded-for")
|
||||
client_ip = (x_real_ip.split(",")[0].strip() if x_real_ip else (request.client.host if request.client else "unknown"))
|
||||
user_agent = request.headers.get("user-agent", "unknown")
|
||||
service_id = None
|
||||
|
||||
@@ -135,7 +138,8 @@ class McpController:
|
||||
|
||||
async def asgi(scope, receive, send):
|
||||
req = Request(scope)
|
||||
client_ip = req.client.host if req.client else "unknown"
|
||||
x_real_ip = req.headers.get("x-real-ip") or req.headers.get("x-forwarded-for")
|
||||
client_ip = (x_real_ip.split(",")[0].strip() if x_real_ip else (req.client.host if req.client else "unknown"))
|
||||
user_agent = req.headers.get("user-agent", "unknown")
|
||||
service_id = None
|
||||
|
||||
@@ -242,7 +246,8 @@ class McpController:
|
||||
|
||||
async def asgi(scope, receive, send):
|
||||
req = Request(scope)
|
||||
client_ip = req.client.host if req.client else "unknown"
|
||||
x_real_ip = req.headers.get("x-real-ip") or req.headers.get("x-forwarded-for")
|
||||
client_ip = (x_real_ip.split(",")[0].strip() if x_real_ip else (req.client.host if req.client else "unknown"))
|
||||
user_agent = req.headers.get("user-agent", "unknown")
|
||||
service_id = None
|
||||
session_key = None
|
||||
@@ -344,7 +349,12 @@ class McpController:
|
||||
return
|
||||
|
||||
user_id, apikey_id = user_info
|
||||
session_key = f"{service_id}:{user_id}"
|
||||
raw_client_id = req.headers.get('x-client-instance-id') or req.headers.get('x-client-id')
|
||||
client_instance_id = (re.sub(r'[^a-zA-Z0-9._-]', '', raw_client_id)[:32]) if raw_client_id else None
|
||||
ua = user_agent or ""
|
||||
ua_hash = hashlib.sha1(ua.encode('utf-8')).hexdigest()[:8]
|
||||
client_fingerprint = client_instance_id or f"{client_ip}:{ua_hash}"
|
||||
session_key = f"{service_id}:{user_id}:{client_fingerprint}"
|
||||
|
||||
# Build connection key for lifecycle management
|
||||
connection_key = f"{service_id}:{user_id}:{client_ip}"
|
||||
@@ -352,7 +362,7 @@ class McpController:
|
||||
try:
|
||||
# Get or create a persistent StreamableHTTP transport and MCP server
|
||||
transport = await self._ensure_http_session(session_key, service_id, user_id, apikey_id)
|
||||
logger.debug(f'method: {req.method},request body: {req.body}')
|
||||
logger.debug(f"method: {req.method}, content_type: {content_type}")
|
||||
# Note session activity
|
||||
self._note_session_activity(session_key)
|
||||
|
||||
|
||||
@@ -100,11 +100,11 @@ app.add_middleware(ExceptionHandlingMiddleware)
|
||||
# Add CORS middleware for MCP client cross-origin and reconnection support
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # MCP clients may come from different domains
|
||||
allow_credentials=True,
|
||||
allow_methods=["GET", "POST", "DELETE", "OPTIONS"], # 添加 DELETE 支持 StreamableHTTP 终止
|
||||
allow_origins=Config.ALLOWED_ORIGINS,
|
||||
allow_credentials=False,
|
||||
allow_methods=["GET", "POST", "DELETE", "OPTIONS"],
|
||||
allow_headers=["*"],
|
||||
expose_headers=["*"], # Expose all response headers for SSE reconnection
|
||||
expose_headers=["*"]
|
||||
)
|
||||
|
||||
# Create MCP controller instance
|
||||
|
||||
@@ -84,11 +84,7 @@ class McpToolService:
|
||||
raise ValueError(f"Unsupported HTTP method: {method}")
|
||||
|
||||
logger.info(f"HTTP response status code: {response.status_code}")
|
||||
if response.text != "":
|
||||
logger.debug(f"Response content: {response.text}")
|
||||
response.raise_for_status()
|
||||
|
||||
response_text = response.text
|
||||
logger.debug(f"Response content length: {len(response_text)} characters")
|
||||
|
||||
logger.debug(f"Response length: {len(response_text)}")
|
||||
return response_text
|
||||
@@ -75,3 +75,4 @@ class Config:
|
||||
]
|
||||
MCP_SESSION_IDLE_TTL_SECONDS = int(os.getenv("MCP_SESSION_IDLE_TTL_SECONDS", 900))
|
||||
MCP_SESSION_CLEANUP_INTERVAL_SECONDS = int(os.getenv("MCP_SESSION_CLEANUP_INTERVAL_SECONDS", 60))
|
||||
ALLOWED_ORIGINS = [s.strip() for s in os.getenv("ALLOWED_ORIGINS", "*").split(",") if s.strip()]
|
||||
@@ -35,10 +35,15 @@ def verify_token(token: str, db: Session) -> Optional[User]:
|
||||
logging.info(f"query token info")
|
||||
|
||||
# Check if token is expired
|
||||
expire_datetime = datetime.strptime(str(user_access_token.expire_at), "%Y-%m-%d %H:%M:%S")
|
||||
expire_at = user_access_token.expire_at
|
||||
try:
|
||||
expire_datetime = expire_at if isinstance(expire_at, datetime) else datetime.strptime(str(expire_at), "%Y-%m-%d %H:%M:%S")
|
||||
except Exception:
|
||||
logger.warning(f"Invalid token expiry format: {expire_at}")
|
||||
delete_cache(token_cache_key)
|
||||
return None
|
||||
if expire_datetime <= datetime.now():
|
||||
logger.warning(f"Token expired: {token}, expire_at: {user_access_token.expire_at}")
|
||||
# Delete expired cache
|
||||
delete_cache(token_cache_key)
|
||||
return None
|
||||
|
||||
@@ -47,7 +52,7 @@ def verify_token(token: str, db: Session) -> Optional[User]:
|
||||
user = get_model_cache(user_cache_key, User)
|
||||
if not user:
|
||||
user = (
|
||||
db.query(User).filter(User.id == user_access_token.user_id and User.is_deleted == False and User.is_active == True).first()
|
||||
db.query(User).filter(User.id == user_access_token.user_id, User.is_deleted == False, User.is_active == True).first()
|
||||
)
|
||||
if not user:
|
||||
logger.warning(f"User not found for token: {token}, user_id: {user_access_token.user_id}")
|
||||
|
||||
@@ -114,16 +114,17 @@ class CacheUtils:
|
||||
try:
|
||||
cache_value = redis_client.get(cache_key)
|
||||
if cache_value:
|
||||
# Check if cache_value is already deserialized (dict) or needs deserialization (str)
|
||||
if isinstance(cache_value, dict):
|
||||
if isinstance(cache_value, str):
|
||||
try:
|
||||
model_data = json.loads(cache_value)
|
||||
except Exception:
|
||||
logger.warning(f"Failed to parse JSON cache for key {cache_key}")
|
||||
return None
|
||||
elif isinstance(cache_value, dict):
|
||||
model_data = cache_value
|
||||
elif isinstance(cache_value, str):
|
||||
# Deserialize from pickled string data
|
||||
model_data = pickle.loads(cache_value.encode('latin1'))
|
||||
else:
|
||||
logger.warning(f"Unexpected cache value type for key {cache_key}: {type(cache_value)}")
|
||||
return None
|
||||
|
||||
return SqlalchemyUtils.dict_to_model(model_class, model_data)
|
||||
return None
|
||||
except Exception as e:
|
||||
@@ -145,8 +146,7 @@ class CacheUtils:
|
||||
"""
|
||||
try:
|
||||
model_dict = SqlalchemyUtils.model_to_dict(model)
|
||||
# Serialize using pickle for better type preservation
|
||||
serialized_data = pickle.dumps(model_dict).decode('latin1')
|
||||
serialized_data = json.dumps(model_dict, ensure_ascii=False)
|
||||
redis_client.set(cache_key, serialized_data, ex=expire_time)
|
||||
return True
|
||||
except Exception as e:
|
||||
@@ -168,16 +168,17 @@ class CacheUtils:
|
||||
try:
|
||||
cache_value = redis_client.get(cache_key)
|
||||
if cache_value:
|
||||
# Check if cache_value is already deserialized (list) or needs deserialization (str)
|
||||
if isinstance(cache_value, list):
|
||||
if isinstance(cache_value, str):
|
||||
try:
|
||||
model_list_data = json.loads(cache_value)
|
||||
except Exception:
|
||||
logger.warning(f"Failed to parse JSON cache for key {cache_key}")
|
||||
return None
|
||||
elif isinstance(cache_value, list):
|
||||
model_list_data = cache_value
|
||||
elif isinstance(cache_value, str):
|
||||
# Deserialize from pickled string data
|
||||
model_list_data = pickle.loads(cache_value.encode('latin1'))
|
||||
else:
|
||||
logger.warning(f"Unexpected cache value type for key {cache_key}: {type(cache_value)}")
|
||||
return None
|
||||
|
||||
if isinstance(model_list_data, list):
|
||||
return [SqlalchemyUtils.dict_to_model(model_class, item) for item in model_list_data]
|
||||
return None
|
||||
@@ -204,8 +205,7 @@ class CacheUtils:
|
||||
return False
|
||||
|
||||
model_dict_list = [SqlalchemyUtils.model_to_dict(model) for model in model_list]
|
||||
# Serialize using pickle for better type preservation
|
||||
serialized_data = pickle.dumps(model_dict_list).decode('latin1')
|
||||
serialized_data = json.dumps(model_dict_list, ensure_ascii=False)
|
||||
redis_client.set(cache_key, serialized_data, ex=expire_time)
|
||||
return True
|
||||
except Exception as e:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import aiohttp
|
||||
import asyncio
|
||||
from urllib.parse import urlparse
|
||||
from typing import Optional, Dict, Any
|
||||
from services.common.exceptions import (
|
||||
ValidationException,
|
||||
@@ -60,6 +61,15 @@ class HttpUtils:
|
||||
if headers:
|
||||
request_headers.update(headers)
|
||||
|
||||
# Basic SSRF protections: only allow http/https and block localhost/private IPs
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValidationException("URL scheme must be http or https")
|
||||
host = (parsed.hostname or "").lower()
|
||||
private_prefixes = ("127.", "10.", "192.168.")
|
||||
if host in ("localhost", "127.0.0.1") or host.startswith(private_prefixes) or host.startswith("172."):
|
||||
raise ValidationException("Access to local/private addresses is not allowed")
|
||||
|
||||
async with aiohttp.ClientSession(
|
||||
timeout=aiohttp.ClientTimeout(total=timeout),
|
||||
headers=request_headers
|
||||
@@ -178,6 +188,15 @@ class HttpUtils:
|
||||
if headers:
|
||||
request_headers.update(headers)
|
||||
|
||||
# Basic SSRF protections: only allow http/https and block localhost/private IPs
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ("http", "https"):
|
||||
raise ValidationException("URL scheme must be http or https")
|
||||
host = (parsed.hostname or "").lower()
|
||||
private_prefixes = ("127.", "10.", "192.168.")
|
||||
if host in ("localhost", "127.0.0.1") or host.startswith(private_prefixes) or host.startswith("172."):
|
||||
raise ValidationException("Access to local/private addresses is not allowed")
|
||||
|
||||
async with aiohttp.ClientSession(
|
||||
timeout=aiohttp.ClientTimeout(total=timeout),
|
||||
headers=request_headers
|
||||
|
||||
Reference in new issue
Block a user