From ada9afa396cdad08d7a79975e524a174b3f49cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=96=BB=E5=8B=87=E7=A5=A5?= <237899745@qq.com> Date: Tue, 18 Nov 2025 16:37:39 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=97=B6=E7=9A=84=E5=AE=89=E5=85=A8=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E8=87=AA=E5=8A=A8=E6=A3=80=E6=9F=A5=E5=92=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解决了使用install.sh更新时,旧的不安全CORS配置仍然保留的问题。 ## 新增功能 ### 自动安全配置检查 更新时会自动检查 backend/.env 中的安全配置: 1. **CORS配置检查** - 检测 ALLOWED_ORIGINS=* (不安全) - 自动从Nginx配置读取域名 - 自动检测HTTP/HTTPS协议 - 提示用户确认并自动修复 2. **环境变量检查** - 检查NODE_ENV是否设置为production - 给出安全建议 ### 工作流程 当执行 `bash install.sh --update` 时: ```bash 检查安全配置... ⚠️ 检测到不安全的CORS配置: ALLOWED_ORIGINS=* 这是一个严重的安全风险!攻击者可以从任何域名访问你的API。 检测到域名: cs.workyai.cn 建议将CORS设置为: https://cs.workyai.cn 是否自动修复CORS配置?[y/n]: ``` ### 修复机制 - ✅ 自动备份原配置到 .env.backup.YYYYMMDD_HHMMSS - ✅ 智能检测HTTP/HTTPS - ✅ 从Nginx配置读取域名 - ✅ 用户确认后自动修改 - ✅ 如果无法自动修复,给出手动修复指引 ### 兼容性 - ✅ 不影响安全的现有配置 - ✅ 只修复明确不安全的配置(ALLOWED_ORIGINS=*) - ✅ 提供备份,可随时回滚 ## 使用方法 ```bash # 更新项目(会自动检查安全配置) bash install.sh --update # 或者使用交互式菜单 bash install.sh # 选择 [2] 更新/升级 ``` 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- install.sh | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/install.sh b/install.sh index 2b5ead5..31fdd03 100644 --- a/install.sh +++ b/install.sh @@ -3498,6 +3498,88 @@ update_migrate_database() { print_warning "sqlite3未安装,跳过数据库迁移检查" fi + # ========== 安全配置迁移 ========== + print_step "检查安全配置..." + + if [[ -f ".env" ]]; then + # 检查 CORS 配置 + CURRENT_CORS=$(grep "^ALLOWED_ORIGINS=" .env | cut -d'=' -f2-) + + if [[ "$CURRENT_CORS" == "*" ]]; then + print_warning "⚠️ 检测到不安全的CORS配置: ALLOWED_ORIGINS=*" + echo "" + echo "这是一个严重的安全风险!攻击者可以从任何域名访问你的API。" + echo "" + + # 尝试从域名配置自动修复 + if [[ -f "/etc/nginx/sites-enabled/${PROJECT_NAME}" ]] || [[ -f "/etc/nginx/conf.d/${PROJECT_NAME}.conf" ]]; then + # 尝试从Nginx配置读取域名 + NGINX_DOMAIN=$(grep "server_name" /etc/nginx/sites-enabled/${PROJECT_NAME} 2>/dev/null | grep -v "_" | awk '{print $2}' | sed 's/;//g' | head -1) + + if [[ -z "$NGINX_DOMAIN" ]]; then + NGINX_DOMAIN=$(grep "server_name" /etc/nginx/conf.d/${PROJECT_NAME}.conf 2>/dev/null | grep -v "_" | awk '{print $2}' | sed 's/;//g' | head -1) + fi + + if [[ -n "$NGINX_DOMAIN" ]] && [[ "$NGINX_DOMAIN" != "localhost" ]]; then + # 检测是否使用HTTPS + if grep -q "listen.*443.*ssl" /etc/nginx/sites-enabled/${PROJECT_NAME} 2>/dev/null || \ + grep -q "listen.*443.*ssl" /etc/nginx/conf.d/${PROJECT_NAME}.conf 2>/dev/null; then + FIXED_CORS="https://${NGINX_DOMAIN}" + else + FIXED_CORS="http://${NGINX_DOMAIN}" + fi + + print_info "检测到域名: ${NGINX_DOMAIN}" + echo "" + print_warning "建议将CORS设置为: ${FIXED_CORS}" + echo "" + + read -p "是否自动修复CORS配置?[y/n]: " -n 1 -r < /dev/tty + echo "" + + if [[ $REPLY =~ ^[Yy]$ ]]; then + # 备份原配置 + cp .env .env.backup.$(date +%Y%m%d_%H%M%S) + + # 修复CORS配置 + sed -i "s|^ALLOWED_ORIGINS=.*|ALLOWED_ORIGINS=${FIXED_CORS}|" .env + + print_success "✓ CORS配置已修复: ${FIXED_CORS}" + print_info "原配置已备份到: .env.backup.*" + else + print_warning "跳过自动修复,请手动编辑 .env 文件修改 ALLOWED_ORIGINS" + print_info "推荐值: ALLOWED_ORIGINS=${FIXED_CORS}" + fi + else + print_warning "无法自动修复,请手动编辑backend/.env文件" + print_info "将 ALLOWED_ORIGINS=* 改为你的实际域名" + print_info "示例: ALLOWED_ORIGINS=https://yourdomain.com" + fi + else + print_warning "无法自动修复,请手动编辑backend/.env文件" + print_info "将 ALLOWED_ORIGINS=* 改为你的实际域名" + print_info "示例: ALLOWED_ORIGINS=https://yourdomain.com" + fi + echo "" + elif [[ -z "$CURRENT_CORS" ]]; then + print_warning "⚠️ ALLOWED_ORIGINS未配置" + print_info "生产环境必须配置具体的域名" + else + print_success "✓ CORS配置安全: ${CURRENT_CORS}" + fi + + # 检查 NODE_ENV + CURRENT_ENV=$(grep "^NODE_ENV=" .env | cut -d'=' -f2-) + if [[ "$CURRENT_ENV" != "production" ]]; then + print_warning "⚠️ 当前环境: ${CURRENT_ENV:-未设置}" + print_info "生产环境建议设置为: NODE_ENV=production" + else + print_success "✓ 环境配置: production" + fi + else + print_error "❌ .env 文件不存在!" + fi + echo "" }