zhineng-bridge 自动修复进度报告
执行日期: 2026-03-27 修复模式: 自动模式 (crush -y)
已完成的工作
✅ 1. 安全漏洞修复
文件: relay-server/config.py
修复内容
- 添加了路径验证器,防止路径遍历攻击
- 验证
base_dir和temp_dir是否在用户主目录下 - 检查路径是否包含
..或可疑字符 - 修改了证书文件验证逻辑,仅在启用 WSS 时才验证
修复代码
@field_validator('base_dir', 'temp_dir')
@classmethod
def validate_path(cls, v: str) -> str:
"""验证路径安全,防止路径遍历攻击"""
# 解析为绝对路径
path = Path(v).resolve()
# 检查路径是否在用户主目录下(防止系统目录访问)
home_dir = Path.home().resolve()
try:
path.relative_to(home_dir)
except ValueError:
raise ValueError(f"Path must be within home directory: {v}")
# 检查路径是否包含 .. 或可疑字符
if ".." in str(path) or "://" in str(path):
raise ValueError(f"Invalid path characters detected: {v}")
return str(path)
@model_validator(mode='after')
def validate_cert_paths(self) -> 'ServerSettings':
"""验证证书文件路径(仅在启用 WSS 时)"""
# 只有在启用 WSS 时才验证证书文件
if self.enable_wss:
# 验证逻辑...
return self
文件: install_deps.py
修复内容
- 添加了路径验证,防止通过参数进行路径遍历
- 确保文件在当前目录下
- 验证文件扩展名必须是
.txt
修复代码
def install_requirements(req_file: str, description: str):
"""安装依赖文件"""
print(f"\n📦 Installing {description}...")
req_path = Path(req_file).resolve()
# 验证路径安全:防止路径遍历
current_dir = Path.cwd().resolve()
try:
req_path.relative_to(current_dir)
except ValueError:
print(f"❌ Invalid path (must be within current directory): {req_file}")
return False
# 检查文件是否存在且是 .txt 文件
if not req_path.exists():
print(f"❌ Requirements file not found: {req_file}")
return False
if not req_path.suffix == '.txt':
print(f"❌ File must be a .txt file: {req_file}")
return False
# 其余代码...
✅ 2. 性能测试基础设施
新增文件: tests/performance/conftest.py
内容
- 创建了性能测试服务器管理器
- 自动启动和停止 relay-server
- 端口冲突检测和处理
- Session scope fixture 确保服务器在测试会话期间运行
配置
@pytest.fixture(scope="session")
def performance_server():
"""创建性能测试服务器 (session scope)"""
server = PerformanceTestServer()
server.start_relay_server()
time.sleep(2) # 等待服务器完全启动
yield server
server.stop_relay_server()
修改文件: tests/performance/test_benchmark.py
修改内容
- 添加了
performance_serverfixture 依赖到所有14个测试 - 将
ws://localhost:8765替换为ws://127.0.0.1:8765 - 添加了全局 WebSocket 配置函数
- 增加了超时时间配置
测试结果
性能测试状态
| 测试名称 | 状态 | 说明 |
|---|---|---|
| test_websocket_connection_benchmark | ✅ PASSED | 连接测试通过 |
| test_websocket_ping_benchmark | ❌ FAILED | 服务器在后续测试中停止 |
| 其他12个测试 | ❌ FAILED | 同样的原因 |
通过率: 1/14 (7.1%)
问题分析
性能测试失败的根本原因是: 1. 服务器生命周期问题: relay-server 在第一个测试后停止响应 2. WebSocket 连接池: benchmark 框架会多次运行同一个函数,导致连接问题 3. 并发限制: 服务器可能无法处理大量的并发连接
环境因素: - 测试环境可能有资源限制 - 服务器进程可能在后台被杀死 - 端口绑定问题
未完成的工作
🔄 2. 性能测试完全修复
当前状态: 部分修复 (1/14 通过)
剩余问题: - 需要改进服务器稳定性 - 可能需要调整 benchmark 框架配置 - 需要添加更好的错误处理
建议的修复方案:
-
服务器稳定性改进:
-
Benchmark 配置调整:
-
简化测试:
- 减少并发连接数
- 增加超时时间
- 添加重试逻辑
⏳ 3. WebSocket E2E 测试修复
待修复的测试:
- test_connection_timeout
- test_many_concurrent_messages
- test_rapid_session_creation_deletion
修复计划: - 添加服务器等待逻辑 - 增加测试超时时间 - 改进错误处理
⏳ 4. Web UI 测试修复
待修复的测试:
- test_web_ui_contains_required_elements
- test_web_ui_websocket_config
修复计划: - 更新测试期望以匹配实际 UI - 调整 WebSocket 配置验证逻辑
⏳ 5. 超长文件重构
待重构的文件:
- relay-server/user_auth.py (1,461 行)
- relay-server/server.py (953 行)
- relay-server/health_check.py (673 行)
重构计划: - 按功能模块拆分 - 降低函数复杂度 - 改进代码组织
改进建议
1. 自动化测试改进
建议创建一个完整的测试套件启动脚本:
#!/bin/bash
# scripts/run_full_test_suite.sh
set -e
echo "🚀 Starting zhineng-bridge full test suite..."
# 停止现有服务器
pkill -f start_server.py || true
pkill -f start_manager.py || true
# 启动服务器
cd relay-server && python3 start_server.py &
SERVER_PID=$!
cd ..
sleep 3
# 运行测试
python3 -m pytest tests/ -v --tb=short
# 清理
kill $SERVER_PID
2. CI/CD 集成
建议在 GitHub Actions 中添加自动测试:
name: Test Suite
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.12'
- run: pip install -r requirements.txt
- run: pip install -r requirements-dev.txt
- run: python3 -m pytest tests/ -v
3. 性能测试优化
建议将性能测试分离到单独的测试套件:
# 运行快速测试(CI/CD)
python3 -m pytest tests/unit/ tests/integration/ -v
# 运行完整测试(本地)
python3 -m pytest tests/ -v
# 运行性能测试(性能验证)
python3 -m pytest tests/performance/ -v --benchmark-only
总结
成功指标
| 指标 | 目标 | 实际 | 状态 |
|---|---|---|---|
| 安全漏洞修复 | 8 | 8 | ✅ 完成 |
| 性能测试通过 | 14 | 1 | ⚠️ 部分完成 |
| 代码质量提升 | 显著 | 改进 | ✅ 改进中 |
下一步行动
立即行动 (P0): 1. ✅ 安全漏洞修复 - 已完成 2. ⚠️ 性能测试优化 - 需要进一步调试 3. ⏳ WebSocket 测试修复 - 待处理
短期计划 (P1): 4. ⏳ Web UI 测试修复 5. ⏳ 超长文件重构 6. ⏳ 文档完善
长期计划 (P2): 7. 建立自动化 CI/CD 8. 添加性能监控 9. 实现持续改进机制
报告生成时间: 2026-03-27 21:10:00 UTC 下次更新: 待性能测试完全修复后