Files
vue-driven-cloud-storage/nginx/nginx.conf
喻勇祥 32429334ab 🔒 重大安全更新:修复CORS、XSS、敏感文件暴露等漏洞
本次更新修复了安全测试中发现的所有严重问题,大幅提升系统安全性。

## 修复的安全问题

### 1. CORS跨域配置漏洞 ⚠️ 严重
**问题**: 默认允许所有域名访问(ALLOWED_ORIGINS=*)
**修复**:
- 默认值改为空数组,生产环境必须明确配置域名白名单
- 未配置时拒绝所有跨域请求(生产环境)
- 开发环境仅允许localhost访问

### 2. XSS跨站脚本攻击 ⚠️ 严重
**问题**: 用户输入未过滤,可注入恶意脚本
**修复**:
- 添加XSS过滤中间件,自动转义所有POST/PUT请求的用户输入
- 过滤 <, >, ', " 等危险字符
- 递归处理嵌套对象和数组

### 3. 缺少安全响应头 ⚠️ 重要
**问题**: 缺少X-Frame-Options等安全响应头
**修复**:
- X-Frame-Options: SAMEORIGIN (防止点击劫持)
- X-Content-Type-Options: nosniff (防止MIME嗅探)
- X-XSS-Protection: 1; mode=block
- Strict-Transport-Security (HTTPS环境)
- Content-Security-Policy (内容安全策略)
- 隐藏X-Powered-By和Server版本信息

### 4. 敏感文件暴露风险 ⚠️ 严重
**问题**: .env、.git等敏感文件可能被访问
**修复**:
- Nginx配置禁止访问以.开头的隐藏文件
- 禁止访问.env、.git、.config、.key、.pem等敏感文件
- 更新.gitignore,防止敏感文件提交到代码仓库
- 添加证书、密钥等文件类型到忽略列表

## 代码改动

### backend/server.js
- 修改CORS默认配置,移除危险的 * 通配符
- 添加安全响应头中间件
- 添加XSS过滤中间件(sanitizeInput函数)
- 生产环境强制检查ALLOWED_ORIGINS配置

### nginx/nginx.conf
- 添加安全响应头配置
- 禁止访问隐藏文件和敏感文件的location规则
- 隐藏Nginx版本号(server_tokens off)

### .gitignore
- 添加敏感配置文件保护(.env.local, config.json等)
- 添加证书和密钥文件类型(.key, .pem, .crt等)

### deploy.sh
- 修改默认配置,移除ALLOWED_ORIGINS=*
- 添加安全警告提示

## 部署说明

⚠️ **重要**: 更新后必须配置ALLOWED_ORIGINS环境变量!

### 手动部署
编辑 `backend/.env` 文件:
```bash
ALLOWED_ORIGINS=https://cs.workyai.cn
NODE_ENV=production
```

### 使用install.sh部署
脚本会自动根据域名配置ALLOWED_ORIGINS

## 测试结果

修复前安全评分: 57.6% (14个安全问题)
修复后预期评分: 90%+ (预计解决12+个问题)

## 兼容性

-  向后兼容,不影响现有功能
-  开发环境自动允许localhost访问
- ⚠️ 生产环境必须配置ALLOWED_ORIGINS(否则无法访问)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 16:29:44 +08:00

64 lines
1.8 KiB
Nginx Configuration File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
server {
listen 80;
server_name localhost;
# 设置最大上传文件大小为10GB
client_max_body_size 10G;
# 安全响应头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
# 隐藏Nginx版本
server_tokens off;
# 禁止访问隐藏文件和敏感文件
location ~ /\. {
deny all;
return 404;
}
location ~ \.(env|git|config|key|pem|crt)$ {
deny all;
return 404;
}
# 前端静态文件
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ =404;
}
# 后端API反向代理
location /api/ {
proxy_pass http://backend:40001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 修复使用当前请求协议http或https适用于直接IP访问
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
# 增加超时时间支持大文件上传
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
}
# 分享链接重定向
location /s/ {
proxy_pass http://backend:40001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 修复使用当前请求协议http或https适用于直接IP访问
proxy_set_header X-Forwarded-Proto $scheme;
}
}