Compare commits

...
14 Commits
Author SHA1 Message Date
Dot.L 755d48a2f8 Merge pull request #61 from xpack-ai/feature/mcp
Update client IP acquisition logic
2025-11-26 18:02:54 +08:00
Liujian 284880a170 Update client IP acquisition logic 2025-11-26 18:02:10 +08:00
Dot.L 950b516081 Merge pull request #60 from xpack-ai/feature/mcp
Email captcha rate limiting fix && CORS tightening via configuration
2025-11-26 17:08:41 +08:00
Liujian 0088a7b447 Merge remote-tracking branch 'origin/main' into feature/mcp 2025-11-26 17:00:29 +08:00
Liujian 19f646c318 Email captcha rate limiting fix && CORS tightening via configuration 2025-11-26 16:58:54 +08:00
Dot.L 115ac5caf7 Merge pull request #59 from xpack-ai/feature/mcp
repairing potential risks
2025-11-26 16:39:29 +08:00
Liujian 798082934e Merge remote-tracking branch 'origin/main' into feature/mcp 2025-11-26 16:21:40 +08:00
Liujian a017b0fa93 repairing potential risks 2025-11-26 16:16:23 +08:00
Liujian 877dbb6a4e MCP session keys use client fingerprints to distinguish requests from different clients. 2025-11-26 15:09:35 +08:00
Dot.L 523265e550 Merge pull request #58 from xpack-ai/feature/mcp
Payment information encryption and  wait for the initial connection to initialize successfully before processing the request
2025-11-14 18:11:01 +08:00
Liujian 5d2db84d75 update env example 2025-11-14 17:47:31 +08:00
Liujian 4826859152 Payment information encryption 2025-11-14 16:58:08 +08:00
Liujian 92f8b68f0e A new connection recovery mechanism has been added in the event of a network outage. 2025-11-14 15:50:39 +08:00
Liujian 87a4b6e2de Wait for the initial connection to initialize successfully before processing the request. 2025-11-14 15:50:00 +08:00
20 changed files with 585 additions and 104 deletions

No files matched your search

+8 -2
View File
@@ -6,7 +6,7 @@ MYSQL_PASSWORD=mysql_ZTdhRB
MYSQL_DB=xpack MYSQL_DB=xpack
# 服务端口配置 # 服务端口配置
API_PORT=8000 API_PORT=8002
ADMIN_PORT=8001 ADMIN_PORT=8001
# 调试配置 # 调试配置
@@ -24,4 +24,10 @@ RABBITMQ_HOST=xpack-rabbitmq
RABBITMQ_PORT=5672 RABBITMQ_PORT=5672
RABBITMQ_USER=rabbitmq RABBITMQ_USER=rabbitmq
RABBITMQ_PASSWORD=rabbitmq_Gs123dA RABBITMQ_PASSWORD=rabbitmq_Gs123dA
RABBITMQ_VHOST=/ RABBITMQ_VHOST=/
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
# Optional: key ID used to support key rotation identification
PAYMENT_CONFIG_KEY_ID=default
+2 -1
View File
@@ -15,4 +15,5 @@ google-auth>=2.0.0
google-auth-oauthlib>=1.0.0 google-auth-oauthlib>=1.0.0
fastapi_mcp fastapi_mcp
alipay-sdk-python>=3.0.0 alipay-sdk-python>=3.0.0
wechatpayv3[async] wechatpayv3[async]
cryptography>=41.0.0
+8 -2
View File
@@ -11,7 +11,7 @@ ADMIN_PORT=8001
# 调试配置 # 调试配置
DEBUG=false DEBUG=false
LOG_LEVEL=INFO LOG_LEVEL=DEBUG
# Redis 配置 # Redis 配置
REDIS_HOST=xpack-redis REDIS_HOST=xpack-redis
@@ -24,4 +24,10 @@ RABBITMQ_HOST=xpack-rabbitmq
RABBITMQ_PORT=5672 RABBITMQ_PORT=5672
RABBITMQ_USER=rabbitmq RABBITMQ_USER=rabbitmq
RABBITMQ_PASSWORD=rabbitmq_Gs123dA RABBITMQ_PASSWORD=rabbitmq_Gs123dA
RABBITMQ_VHOST=/ RABBITMQ_VHOST=/
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
# Optional: key ID used to support key rotation identification
PAYMENT_CONFIG_KEY_ID=default
@@ -0,0 +1,81 @@
"""
One-time migration: encrypt plaintext payment channel configs at rest.
Usage:
python scripts/resource/migrate_encrypt_payment_configs.py [--dry-run] [--limit N] [--ids alipay,wechat,stripe]
Requirements:
- Environment variable PAYMENT_CONFIG_SECRET_KEY must be set to a base64-encoded 32-byte key.
- cryptography library installed.
"""
import argparse
import sys
from typing import List
from datetime import datetime, timezone
from services.common.database import SessionLocal
from services.common.models.payment_channel import PaymentChannel
from services.common.utils.secure_config import is_encrypted, decrypt_config, encrypt_config
def migrate_configs(dry_run: bool = False, limit: int = 0, only_ids: List[str] | None = None) -> int:
db = SessionLocal()
migrated = 0
try:
query = db.query(PaymentChannel)
if only_ids:
query = query.filter(PaymentChannel.id.in_(only_ids))
rows = query.all()
if limit and len(rows) > limit:
rows = rows[:limit]
for row in rows:
raw = row.config
if not raw:
continue
if is_encrypted(raw):
# already encrypted
continue
cfg = decrypt_config(raw)
if cfg is None:
# cannot parse plaintext json; skip but report
print(f"[WARN] Skip {row.id}: config not JSON or decrypt failed")
continue
try:
enc = encrypt_config(cfg)
except Exception as e:
print(f"[ERROR] Encrypt {row.id} failed: {e}")
continue
if dry_run:
print(f"[DRY-RUN] Would encrypt {row.id}")
else:
row.config = enc
row.updated_at = datetime.now(timezone.utc)
db.add(row)
migrated += 1
if not dry_run and migrated:
db.commit()
print(f"Done. Migrated {migrated} rows. Dry-run={dry_run}")
return migrated
finally:
db.close()
def main():
parser = argparse.ArgumentParser(description="Encrypt legacy payment channel configs")
parser.add_argument("--dry-run", action="store_true", help="Do not write changes, only report")
parser.add_argument("--limit", type=int, default=0, help="Limit number of rows to process")
parser.add_argument("--ids", type=str, default="", help="Comma-separated channel ids to process")
args = parser.parse_args()
only_ids = [x.strip() for x in args.ids.split(",") if x.strip()] if args.ids else None
try:
migrate_configs(dry_run=args.dry_run, limit=args.limit, only_ids=only_ids)
except Exception as e:
print(f"Migration failed: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
@@ -1,10 +1,11 @@
from fastapi import APIRouter, Depends, Query, Body from fastapi import APIRouter, Depends, Query, Body
from pydantic import config
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from services.common.database import get_db from services.common.database import get_db
from services.common.utils.response_utils import ResponseUtils from services.common.utils.response_utils import ResponseUtils
from services.admin_service.services.payment_channel_service import PaymentChannelService from services.admin_service.services.payment_channel_service import PaymentChannelService
from services.common.utils.secure_config import decrypt_config, encrypt_config
from services.admin_service.tasks.order_monitor_task import OrderMonitorTask
import json import json
router = APIRouter() router = APIRouter()
@@ -27,10 +28,9 @@ async def payment_channel_list(
result = [] result = []
for item in list: for item in list:
enable = item.status == 1 enable = item.status == 1
try: cfg = {}
cfg = json.loads(item.config) if item.config:
except: cfg = decrypt_config(item.config) or {}
cfg = {}
result.append( result.append(
{ {
"id": item.id, "id": item.id,
@@ -56,14 +56,16 @@ async def payment_channel_enable(
if data: if data:
if id == "alipay": if id == "alipay":
from services.admin_service.tasks.alipay_order_monitor_task import AlipayOrderMonitorTask monitor = OrderMonitorTask.get_instance()
alipay_monitor_task = AlipayOrderMonitorTask.get_instance() # Ensure monitor is running and enable Alipay client
alipay_monitor_task.start_monitor() monitor.start_monitor()
monitor.enable_client("alipay")
cfg = decrypt_config(data.config) if data.config else {}
return ResponseUtils.success( return ResponseUtils.success(
{ {
"id": id, "id": id,
"name": data.name, "name": data.name,
"config": data.config, "config": cfg or {},
"is_enabled": data.status == 1, "is_enabled": data.status == 1,
"updated_time": data.updated_at, "updated_time": data.updated_at,
} }
@@ -83,14 +85,15 @@ async def payment_channel_disable(
data = payment_channel_service.update_status(id, 0) data = payment_channel_service.update_status(id, 0)
if data: if data:
if id == "alipay": if id == "alipay":
from services.admin_service.tasks.alipay_order_monitor_task import AlipayOrderMonitorTask monitor = OrderMonitorTask.get_instance()
alipay_monitor_task = AlipayOrderMonitorTask.get_instance() # Disable Alipay client without stopping global monitor
alipay_monitor_task.stop_monitor() monitor.disable_client("alipay")
cfg = decrypt_config(data.config) if data.config else {}
return ResponseUtils.success( return ResponseUtils.success(
{ {
"id": id, "id": id,
"name": data.name, "name": data.name,
"config": data.config, "config": cfg or {},
"is_enabled": data.status == 1, "is_enabled": data.status == 1,
"updated_time": data.updated_at, "updated_time": data.updated_at,
} }
@@ -109,8 +112,12 @@ async def payment_channel_config(
return ResponseUtils.error("Payment channel id cannot be empty") return ResponseUtils.error("Payment channel id cannot be empty")
config = body.get("config") config = body.get("config")
if config: if config:
configStr = json.dumps(config) # Encrypt config before persisting to DB
data = payment_channel_service.update_config(id=id, config=configStr) try:
encrypted = encrypt_config(config)
except Exception as e:
return ResponseUtils.error(f"Failed to encrypt config: {str(e)}")
data = payment_channel_service.update_config(id=id, config=encrypted)
if data: if data:
# Convert str to object # Convert str to object
return ResponseUtils.success( return ResponseUtils.success(
+11 -10
View File
@@ -31,27 +31,28 @@ async def upload(
filename = img.filename filename = img.filename
if not filename: if not filename:
return ResponseUtils.error(message="No filename provided", code=400) 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() 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() file_hash = hashlib.sha256(contents).hexdigest()
# Reset file pointer for subsequent save
await img.seek(0) await img.seek(0)
# Verify provided SHA256 matches computed hash
if sha256 and file_hash != sha256: if sha256 and file_hash != sha256:
return ResponseUtils.error(message="File SHA256 checksum mismatch", code=400) return ResponseUtils.error(message="File SHA256 checksum mismatch", code=400)
# 3. Create directory based on current date
today = datetime.now() today = datetime.now()
date_dir = today.strftime("%Y%m%d") date_dir = today.strftime("%Y%m%d")
upload_dir = os.path.join("uploads","images", date_dir)
# Create upload directory
upload_dir = os.path.join("uploads","images")
upload_dir = os.path.join(upload_dir, date_dir)
os.makedirs(upload_dir, exist_ok=True) os.makedirs(upload_dir, exist_ok=True)
# Save file
file_path = os.path.join(upload_dir, filename) file_path = os.path.join(upload_dir, filename)
with open(file_path, "wb") as f: with open(file_path, "wb") as f:
+26 -11
View File
@@ -33,21 +33,36 @@ class AuthService:
def send_email_login_captcha(self, email: str) -> bool: def send_email_login_captcha(self, email: str) -> bool:
""" """
Sends a login captcha code to the specified email address. Sends a login captcha code to the specified email address with simple rate limiting.
The function generates a random 6-digit captcha code, stores it in Redis cache for 10 minutes, Limit: 5 sends per 10 minutes per email.
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.
""" """
# 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 # Generate random 6-digit verification code
captcha = str(random.randint(100000, 999999)) captcha = str(random.randint(100000, 999999))
# Cache to redis
CacheUtils.set_cache(RedisKeys.email_login_captcha(email), captcha, 10 * 60) 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) return EmailUtils.send_register_code_email(self.db, email, captcha)
def email_login_by_captcha(self, email: str, captcha: str) -> Optional[str]: def email_login_by_captcha(self, email: str, captcha: str) -> Optional[str]:
@@ -6,6 +6,7 @@ from services.common.database import SessionLocal
from datetime import datetime from datetime import datetime
from services.admin_service.repositories.payment_channel_repository import PaymentChannelRepository from services.admin_service.repositories.payment_channel_repository import PaymentChannelRepository
from services.common.models.payment_channel import PaymentChannel from services.common.models.payment_channel import PaymentChannel
from services.common.utils.secure_config import decrypt_config
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -35,13 +36,14 @@ class PaymentChannelService:
channel_config = self.payment_channel_repository.payment_channel_get(id) channel_config = self.payment_channel_repository.payment_channel_get(id)
if channel_config is None: if channel_config is None:
return None return None
# Support encrypted-at-rest config
config = {} config = {}
if channel_config.config is not None: if channel_config.config:
try: cfg = decrypt_config(channel_config.config)
config = json.loads(channel_config.config) if cfg is None:
except json.JSONDecodeError as e: logger.error("Failed to decrypt or parse payment channel config")
logger.error(f"Failed to parse payment channel config JSON: {str(e)}")
return None return None
config = cfg
config["enable"] = channel_config.status == 1 config["enable"] = channel_config.status == 1
config["id"] = channel_config.id config["id"] = channel_config.id
return config return config
@@ -64,19 +66,17 @@ class PaymentChannelService:
logger.warning("Stripe payment channel config is empty") logger.warning("Stripe payment channel config is empty")
return None return None
# Parse config JSON # Decrypt/parse config JSON
try: config = decrypt_config(stripe_channel.config)
config = json.loads(stripe_channel.config) if config is None:
# Validate required fields logger.error("Failed to decrypt Stripe config")
if not config.get("secret") or not config.get("webhook_secret"):
logger.warning("Stripe config missing required fields: secret or webhook_secret")
return None
logger.info("Successfully retrieved Stripe config")
return config
except json.JSONDecodeError as e:
logger.error(f"Failed to parse Stripe config JSON: {str(e)}")
return None return None
# Validate required fields
if not config.get("secret") or not config.get("webhook_secret"):
logger.warning("Stripe config missing required fields: secret or webhook_secret")
return None
logger.info("Successfully retrieved Stripe config")
return config
except Exception as e: except Exception as e:
logger.error(f"Failed to get Stripe config: {str(e)}") logger.error(f"Failed to get Stripe config: {str(e)}")
@@ -2,14 +2,6 @@ import os
import ssl import ssl
import urllib3 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.AlipayClientConfig import AlipayClientConfig
from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient from alipay.aop.api.DefaultAlipayClient import DefaultAlipayClient
from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel from alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModel
+164 -8
View File
@@ -9,6 +9,8 @@ from starlette.requests import Request
from starlette.responses import Response from starlette.responses import Response
import asyncio import asyncio
import time import time
import hashlib
import re
from mcp.server.sse import SseServerTransport from mcp.server.sse import SseServerTransport
from mcp.server.streamable_http import StreamableHTTPServerTransport from mcp.server.streamable_http import StreamableHTTPServerTransport
from mcp.server.lowlevel import Server from mcp.server.lowlevel import Server
@@ -18,6 +20,7 @@ from services.api_service.repositories.user_apikey_repository import UserApiKeyR
from services.api_service.utils.connection_manager import connection_manager from services.api_service.utils.connection_manager import connection_manager
from services.common.database import get_db from services.common.database import get_db
from services.common.config import Config from services.common.config import Config
from services.api_service.utils.mcp_event_store import RedisEventStore
logger = get_logger(__name__) logger = get_logger(__name__)
@@ -42,7 +45,8 @@ class McpController:
async def handle_sse_connection(self, request: Request): async def handle_sse_connection(self, request: Request):
"""Handle MCP Streamable HTTP SSE connection with resume capability.""" """Handle MCP Streamable HTTP SSE connection with resume capability."""
# Get connection identification info # 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") user_agent = request.headers.get("user-agent", "unknown")
service_id = None service_id = None
@@ -134,7 +138,8 @@ class McpController:
async def asgi(scope, receive, send): async def asgi(scope, receive, send):
req = Request(scope) 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") user_agent = req.headers.get("user-agent", "unknown")
service_id = None service_id = None
@@ -241,7 +246,8 @@ class McpController:
async def asgi(scope, receive, send): async def asgi(scope, receive, send):
req = Request(scope) 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") user_agent = req.headers.get("user-agent", "unknown")
service_id = None service_id = None
session_key = None session_key = None
@@ -264,6 +270,69 @@ class McpController:
logger.info(f"Received StreamableHTTP request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...") logger.info(f"Received StreamableHTTP request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...")
# Security: validate Origin to prevent DNS rebinding (MCP spec requirement)
origin = req.headers.get('origin')
if origin and not self._is_origin_allowed(origin, req):
logger.warning(f"Rejected by Origin validation - Origin: {origin}, Host: {req.url.scheme}://{req.url.netloc}")
response_body = b"Forbidden: invalid Origin"
await send({
'type': 'http.response.start',
'status': 403,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
# Validate Accept & Content-Type headers according to MCP Streamable HTTP rules
accept = (req.headers.get('accept') or '').lower()
content_type = (req.headers.get('content-type') or '').lower()
if req.method == 'GET':
if 'text/event-stream' not in accept:
logger.warning("Missing Accept: text/event-stream for GET SSE")
response_body = b"Not Acceptable: require text/event-stream"
await send({
'type': 'http.response.start',
'status': 406,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
elif req.method == 'POST':
# Client should list both application/json and text/event-stream
if not ('application/json' in accept and 'text/event-stream' in accept):
logger.warning("POST Accept must include application/json and text/event-stream")
response_body = b"Not Acceptable: require application/json and text/event-stream"
await send({
'type': 'http.response.start',
'status': 406,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
# Body must be JSON-RPC payload
if 'application/json' not in content_type:
logger.warning("Unsupported Media Type for POST; require application/json")
response_body = b"Unsupported Media Type: require application/json"
await send({
'type': 'http.response.start',
'status': 415,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
# Extract user ID (for billing) - must provide valid apikey # Extract user ID (for billing) - must provide valid apikey
user_info = self._extract_user_info(req) user_info = self._extract_user_info(req)
if not user_info: if not user_info:
@@ -280,7 +349,12 @@ class McpController:
return return
user_id, apikey_id = user_info 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 # Build connection key for lifecycle management
connection_key = f"{service_id}:{user_id}:{client_ip}" connection_key = f"{service_id}:{user_id}:{client_ip}"
@@ -288,13 +362,31 @@ class McpController:
try: try:
# Get or create a persistent StreamableHTTP transport and MCP server # Get or create a persistent StreamableHTTP transport and MCP server
transport = await self._ensure_http_session(session_key, service_id, user_id, apikey_id) transport = await self._ensure_http_session(session_key, service_id, user_id, apikey_id)
logger.debug(f"method: {req.method}, content_type: {content_type}")
# Note session activity # Note session activity
self._note_session_activity(session_key) self._note_session_activity(session_key)
# Ensure transport is connected before delegating (avoid race on first request)
try:
await self._wait_transport_ready(transport, timeout_seconds=2.0)
except TimeoutError:
logger.error("Transport not ready within timeout; refusing request")
response_body = b"Service Unavailable: transport not ready"
await send({
'type': 'http.response.start',
'status': 503,
'headers': [
[b'content-type', b'text/plain'],
[b'content-length', str(len(response_body)).encode()],
],
})
await send({'type': 'http.response.body', 'body': response_body})
return
# Register only on GET (SSE handshake); POST just updates activity # Register only on GET (SSE handshake); POST just updates activity
if req.method == "GET": if req.method == "GET":
connection_manager.register_connection(service_id, user_id, client_ip) connection_manager.register_connection(service_id, user_id, client_ip)
logger.debug(f'register connection successful.connection_key is {connection_key}')
elif req.method == "POST": elif req.method == "POST":
connection_manager.update_activity(connection_key) connection_manager.update_activity(connection_key)
@@ -304,8 +396,16 @@ class McpController:
# Update activity after handling (avoid cleaning long-running processing) # Update activity after handling (avoid cleaning long-running processing)
self._note_session_activity(session_key) self._note_session_activity(session_key)
# If the session has terminated or DELETE called, clean up and unregister # If the session has terminated, clean up and unregister
if transport.is_terminated or req.method == "DELETE": if transport.is_terminated:
await self._cleanup_http_session(session_key)
connection_manager.unregister_connection(connection_key)
# For explicit DELETE, ensure termination cleanup
if req.method == "DELETE" and not transport.is_terminated:
try:
await transport.terminate()
except Exception:
logger.exception("Error terminating transport on DELETE")
await self._cleanup_http_session(session_key) await self._cleanup_http_session(session_key)
connection_manager.unregister_connection(connection_key) connection_manager.unregister_connection(connection_key)
finally: finally:
@@ -340,6 +440,57 @@ class McpController:
return asgi return asgi
async def _wait_transport_ready(self, transport: StreamableHTTPServerTransport, timeout_seconds: float = 2.0) -> None:
"""Wait until StreamableHTTP transport is connected and streams are initialized.
The underlying transport sets internal streams in connect(); POST/GET handling
requires these to exist. We poll briefly to avoid race on first request.
"""
deadline = time.time() + timeout_seconds
# Access internal attributes defensively; break once read writer is present
while True:
# If terminated, break early
if transport.is_terminated:
raise TimeoutError("Transport terminated during wait")
# Heuristic: writer/reader set once connect() started
has_read_writer = getattr(transport, "_read_stream_writer", None) is not None
has_write_reader = getattr(transport, "_write_stream_reader", None) is not None
if has_read_writer and has_write_reader:
return
if time.time() > deadline:
raise TimeoutError("Transport not ready")
await asyncio.sleep(0.01)
def _is_origin_allowed(self, origin: str, req: Request) -> bool:
"""Validate Origin header against request host to prevent DNS rebinding.
Policy:
- If Origin is present, it must match the scheme+host of the request URL.
- Allow common localhost variants (127.0.0.1, localhost) across http/https.
- If Origin is missing, treat as allowed (native clients may omit it).
"""
try:
if not origin:
return True
origin = origin.strip()
request_host = f"{req.url.scheme}://{req.url.netloc}"
if origin == request_host:
return True
allowed_local_origins = {
"http://127.0.0.1",
f"http://127.0.0.1:{req.url.port}" if req.url.port else "http://127.0.0.1",
"http://localhost",
f"http://localhost:{req.url.port}" if req.url.port else "http://localhost",
"https://127.0.0.1",
f"https://127.0.0.1:{req.url.port}" if req.url.port else "https://127.0.0.1",
"https://localhost",
f"https://localhost:{req.url.port}" if req.url.port else "https://localhost",
}
return origin in allowed_local_origins
except Exception:
logger.exception("Origin validation error")
return False
async def _ensure_http_session(self, session_key: str, service_id: str, user_id: str, apikey_id: str) -> StreamableHTTPServerTransport: async def _ensure_http_session(self, session_key: str, service_id: str, user_id: str, apikey_id: str) -> StreamableHTTPServerTransport:
"""Ensure a persistent StreamableHTTP transport and MCP server exist for the session. """Ensure a persistent StreamableHTTP transport and MCP server exist for the session.
@@ -378,7 +529,12 @@ class McpController:
return transport return transport
# Create transport and server # Create transport and server
transport = StreamableHTTPServerTransport(mcp_session_id=None, is_json_response_enabled=False) event_store = RedisEventStore(namespace=session_key, ttl_seconds=Config.MCP_SESSION_IDLE_TTL_SECONDS)
transport = StreamableHTTPServerTransport(
mcp_session_id=None,
is_json_response_enabled=False,
event_store=event_store,
)
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id) mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
self._http_transports[session_key] = transport self._http_transports[session_key] = transport
self._http_servers[session_key] = mcp_server self._http_servers[session_key] = mcp_server
+4 -4
View File
@@ -100,11 +100,11 @@ app.add_middleware(ExceptionHandlingMiddleware)
# Add CORS middleware for MCP client cross-origin and reconnection support # Add CORS middleware for MCP client cross-origin and reconnection support
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=["*"], # MCP clients may come from different domains allow_origins=Config.ALLOWED_ORIGINS,
allow_credentials=True, allow_credentials=False,
allow_methods=["GET", "POST", "DELETE", "OPTIONS"], # 添加 DELETE 支持 StreamableHTTP 终止 allow_methods=["GET", "POST", "DELETE", "OPTIONS"],
allow_headers=["*"], allow_headers=["*"],
expose_headers=["*"], # Expose all response headers for SSE reconnection expose_headers=["*"]
) )
# Create MCP controller instance # Create MCP controller instance
@@ -84,11 +84,7 @@ class McpToolService:
raise ValueError(f"Unsupported HTTP method: {method}") raise ValueError(f"Unsupported HTTP method: {method}")
logger.info(f"HTTP response status code: {response.status_code}") 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.raise_for_status()
response_text = response.text 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 return response_text
@@ -0,0 +1,91 @@
import json
import asyncio
from typing import List, cast
from mcp.server.streamable_http import (
EventStore,
EventId,
StreamId,
EventMessage,
EventCallback,
)
from mcp.types import JSONRPCMessage
from services.common.redis import redis_client
class RedisEventStore(EventStore):
def __init__(self, namespace: str, ttl_seconds: int = 900):
self.namespace = namespace
self.ttl_seconds = ttl_seconds
# Synchronous helpers; wrap at call sites to avoid blocking
def _rpush(self, key: str, value: str) -> int:
return cast(int, redis_client.client.rpush(key, value))
def _expire(self, key: str, seconds: int) -> None:
try:
redis_client.client.expire(key, int(seconds))
except Exception:
# Ignore expiration errors
pass
def _llen(self, key: str) -> int:
return cast(int, redis_client.client.llen(key))
def _lrange(self, key: str, start: int, end: int) -> List[str]:
raw = redis_client.client.lrange(key, int(start), int(end))
items: List[str] = cast(List[str], raw or [])
return [str(i) for i in items]
def _key(self, stream_id: StreamId) -> str:
return f"xpack:mcp:event:{self.namespace}:{stream_id}"
def _event_id(self, stream_id: StreamId, seq: int) -> EventId:
return f"{stream_id}:{seq}"
async def store_event(self, stream_id: StreamId, message: JSONRPCMessage) -> EventId:
key = self._key(stream_id)
payload = json.dumps(message.model_dump(by_alias=True, exclude_none=True))
# Append to Redis list via thread wrapper to avoid blocking
seq: int = await asyncio.to_thread(self._rpush, key, payload)
# rpush returns new length; sequence index is length-1
await asyncio.to_thread(self._expire, key, self.ttl_seconds)
return self._event_id(stream_id, seq - 1)
async def replay_events_after(
self,
last_event_id: EventId,
send_callback: EventCallback,
) -> StreamId | None:
# Parse last_event_id of format "<stream_id>:<seq>"
try:
stream_id_str, seq_str = last_event_id.rsplit(":", 1)
stream_id: StreamId = stream_id_str
start_idx = int(seq_str) + 1
except Exception:
return None
key = self._key(stream_id)
try:
length: int = await asyncio.to_thread(self._llen, key)
except Exception:
# Redis unavailable or key type mismatch; cannot replay
return stream_id
if length <= 0 or start_idx >= length:
return stream_id
# Fetch range [start_idx, length-1]
try:
items: list[str] = await asyncio.to_thread(self._lrange, key, start_idx, length - 1)
except Exception:
return stream_id
for item in items:
try:
data = json.loads(item)
msg = JSONRPCMessage.model_validate(data)
# send_callback is async and expects EventMessage
await send_callback(EventMessage(message=msg, event_id=None))
except Exception:
# Skip malformed entries
continue
return stream_id
+1
View File
@@ -75,3 +75,4 @@ class Config:
] ]
MCP_SESSION_IDLE_TTL_SECONDS = int(os.getenv("MCP_SESSION_IDLE_TTL_SECONDS", 900)) 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)) 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()]
+2 -1
View File
@@ -14,5 +14,6 @@ class PaymentChannel(Base):
id: Mapped[str] = mapped_column(String(36), primary_key=True, autoincrement=False, comment="Primary key") id: Mapped[str] = mapped_column(String(36), primary_key=True, autoincrement=False, comment="Primary key")
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Channel name") name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Channel name")
status: Mapped[int] = mapped_column(Integer, nullable=False, comment="Channel status, 0: disabled, 1: enabled") status: Mapped[int] = mapped_column(Integer, nullable=False, comment="Channel status, 0: disabled, 1: enabled")
config: Mapped[str] = mapped_column(Text, nullable=True, comment="Channel config") # Store encrypted JSON envelope for channel configuration (AES-GCM). See secure_config.py
config: Mapped[str] = mapped_column(Text, nullable=True, comment="Channel config (encrypted envelope JSON)")
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="Update time") updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="Update time")
+2 -1
View File
@@ -1,4 +1,5 @@
import redis import redis
from redis import Redis
from typing import Optional, Any from typing import Optional, Any
from .config import Config from .config import Config
@@ -9,7 +10,7 @@ class RedisClient:
def __init__(self): def __init__(self):
"""Initialize Redis connection""" """Initialize Redis connection"""
try: try:
self.client = redis.Redis( self.client: Redis = redis.Redis(
host=Config.REDIS_HOST, host=Config.REDIS_HOST,
port=Config.REDIS_PORT, port=Config.REDIS_PORT,
password=Config.REDIS_PASSWORD, password=Config.REDIS_PASSWORD,
+8 -3
View File
@@ -35,10 +35,15 @@ def verify_token(token: str, db: Session) -> Optional[User]:
logging.info(f"query token info") logging.info(f"query token info")
# Check if token is expired # 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(): if expire_datetime <= datetime.now():
logger.warning(f"Token expired: {token}, expire_at: {user_access_token.expire_at}") logger.warning(f"Token expired: {token}, expire_at: {user_access_token.expire_at}")
# Delete expired cache
delete_cache(token_cache_key) delete_cache(token_cache_key)
return None return None
@@ -47,7 +52,7 @@ def verify_token(token: str, db: Session) -> Optional[User]:
user = get_model_cache(user_cache_key, User) user = get_model_cache(user_cache_key, User)
if not user: if not user:
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: if not user:
logger.warning(f"User not found for token: {token}, user_id: {user_access_token.user_id}") logger.warning(f"User not found for token: {token}, user_id: {user_access_token.user_id}")
+16 -16
View File
@@ -114,16 +114,17 @@ class CacheUtils:
try: try:
cache_value = redis_client.get(cache_key) cache_value = redis_client.get(cache_key)
if cache_value: if cache_value:
# Check if cache_value is already deserialized (dict) or needs deserialization (str) if isinstance(cache_value, str):
if isinstance(cache_value, dict): 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 model_data = cache_value
elif isinstance(cache_value, str):
# Deserialize from pickled string data
model_data = pickle.loads(cache_value.encode('latin1'))
else: else:
logger.warning(f"Unexpected cache value type for key {cache_key}: {type(cache_value)}") logger.warning(f"Unexpected cache value type for key {cache_key}: {type(cache_value)}")
return None return None
return SqlalchemyUtils.dict_to_model(model_class, model_data) return SqlalchemyUtils.dict_to_model(model_class, model_data)
return None return None
except Exception as e: except Exception as e:
@@ -145,8 +146,7 @@ class CacheUtils:
""" """
try: try:
model_dict = SqlalchemyUtils.model_to_dict(model) model_dict = SqlalchemyUtils.model_to_dict(model)
# Serialize using pickle for better type preservation serialized_data = json.dumps(model_dict, ensure_ascii=False)
serialized_data = pickle.dumps(model_dict).decode('latin1')
redis_client.set(cache_key, serialized_data, ex=expire_time) redis_client.set(cache_key, serialized_data, ex=expire_time)
return True return True
except Exception as e: except Exception as e:
@@ -168,16 +168,17 @@ class CacheUtils:
try: try:
cache_value = redis_client.get(cache_key) cache_value = redis_client.get(cache_key)
if cache_value: if cache_value:
# Check if cache_value is already deserialized (list) or needs deserialization (str) if isinstance(cache_value, str):
if isinstance(cache_value, list): 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 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: else:
logger.warning(f"Unexpected cache value type for key {cache_key}: {type(cache_value)}") logger.warning(f"Unexpected cache value type for key {cache_key}: {type(cache_value)}")
return None return None
if isinstance(model_list_data, list): if isinstance(model_list_data, list):
return [SqlalchemyUtils.dict_to_model(model_class, item) for item in model_list_data] return [SqlalchemyUtils.dict_to_model(model_class, item) for item in model_list_data]
return None return None
@@ -204,8 +205,7 @@ class CacheUtils:
return False return False
model_dict_list = [SqlalchemyUtils.model_to_dict(model) for model in model_list] model_dict_list = [SqlalchemyUtils.model_to_dict(model) for model in model_list]
# Serialize using pickle for better type preservation serialized_data = json.dumps(model_dict_list, ensure_ascii=False)
serialized_data = pickle.dumps(model_dict_list).decode('latin1')
redis_client.set(cache_key, serialized_data, ex=expire_time) redis_client.set(cache_key, serialized_data, ex=expire_time)
return True return True
except Exception as e: except Exception as e:
+19
View File
@@ -1,5 +1,6 @@
import aiohttp import aiohttp
import asyncio import asyncio
from urllib.parse import urlparse
from typing import Optional, Dict, Any from typing import Optional, Dict, Any
from services.common.exceptions import ( from services.common.exceptions import (
ValidationException, ValidationException,
@@ -60,6 +61,15 @@ class HttpUtils:
if headers: if headers:
request_headers.update(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( async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=timeout), timeout=aiohttp.ClientTimeout(total=timeout),
headers=request_headers headers=request_headers
@@ -178,6 +188,15 @@ class HttpUtils:
if headers: if headers:
request_headers.update(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( async with aiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=timeout), timeout=aiohttp.ClientTimeout(total=timeout),
headers=request_headers headers=request_headers
+102
View File
@@ -0,0 +1,102 @@
import os
import json
import base64
import secrets
from typing import Optional, Any, Dict
try:
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
except Exception:
AESGCM = None
ENC_MARKER = "__enc__"
def _get_key() -> bytes:
"""Load symmetric encryption key from environment.
Expects `PAYMENT_CONFIG_SECRET_KEY` to be a base64-encoded 32-byte key.
Optionally supports `PAYMENT_CONFIG_KEY_ID` for key rotation tracking.
"""
key_b64 = os.getenv("PAYMENT_CONFIG_SECRET_KEY", "")
if not key_b64:
raise RuntimeError("Missing PAYMENT_CONFIG_SECRET_KEY environment variable")
try:
key = base64.b64decode(key_b64)
except Exception as e:
raise RuntimeError("Invalid base64 in PAYMENT_CONFIG_SECRET_KEY") from e
if len(key) != 32:
raise RuntimeError("PAYMENT_CONFIG_SECRET_KEY must decode to 32 bytes (AES-256)")
return key
def is_encrypted(value: Optional[str]) -> bool:
"""Heuristically determine if the stored config string is an encrypted envelope."""
if not value:
return False
try:
obj = json.loads(value)
return isinstance(obj, dict) and obj.get(ENC_MARKER) is True and "ct" in obj and "nonce" in obj
except Exception:
return False
def encrypt_config(config_obj: Dict[str, Any]) -> str:
"""Encrypt config object using AES-GCM and return envelope JSON string.
Envelope format:
{
"__enc__": true,
"v": 1,
"alg": "AESGCM",
"kid": "default",
"nonce": base64,
"ct": base64
}
"""
if AESGCM is None:
raise RuntimeError("cryptography library is required for encryption")
key = _get_key()
aesgcm = AESGCM(key)
nonce = secrets.token_bytes(12) # AES-GCM standard 96-bit nonce
plaintext = json.dumps(config_obj, ensure_ascii=False).encode("utf-8")
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
envelope = {
ENC_MARKER: True,
"v": 1,
"alg": "AESGCM",
"kid": os.getenv("PAYMENT_CONFIG_KEY_ID", "default"),
"nonce": base64.b64encode(nonce).decode("ascii"),
"ct": base64.b64encode(ciphertext).decode("ascii"),
}
return json.dumps(envelope, ensure_ascii=False)
def decrypt_config(value: Optional[str]) -> Optional[Dict[str, Any]]:
"""Decrypt envelope if present; otherwise parse plaintext JSON.
Returns dict on success, or None on failure.
"""
if not value:
return None
# If looks like encrypted envelope, decrypt.
if is_encrypted(value):
if AESGCM is None:
# Cannot decrypt without cryptography
return None
try:
obj = json.loads(value)
key = _get_key()
aesgcm = AESGCM(key)
nonce = base64.b64decode(obj["nonce"])
ct = base64.b64decode(obj["ct"])
plaintext = aesgcm.decrypt(nonce, ct, None)
return json.loads(plaintext.decode("utf-8"))
except Exception:
return None
# Fallback: treat as plaintext JSON
try:
return json.loads(value)
except Exception:
return None