feat: 修复CORS安全漏洞 + 升级主页设计
🔒 安全修复: - 修复分享链接HTTP/HTTPS协议识别问题 - 自动配置CORS安全策略(根据部署模式) - 自动配置Cookie安全设置(HTTPS环境) - 移除不安全的默认CORS配置 ✨ 功能改进: - install.sh: 升级create_env_file()函数,智能配置CORS * 域名+HTTPS模式: ALLOWED_ORIGINS=https://domain * 域名+HTTP模式: ALLOWED_ORIGINS=http://domain * IP模式: 留空并显示安全警告 - backend/server.js: 添加getProtocol()函数,正确识别HTTPS - backend/.env.example: 完全重写,添加详细的CORS配置说明 🎨 主页升级: - frontend/index.html: 全新现代化设计 * 渐变背景+动画效果 * 9大功能特性展示 * 8项技术栈展示 * 完美响应式支持 📝 修改文件: - backend/server.js (第63-83行, 1255行, 1282行) - install.sh (第2108-2195行) - backend/.env.example (完全重写) - frontend/index.html (完全重写) 🔗 相关问题: - 修复CORS允许任意域名访问的安全漏洞 - 修复分享链接使用HTTP的问题 - 解决Cookie在HTTP环境下的安全隐患 💡 向后兼容: - 已部署项目可选择性升级 - 手动添加ALLOWED_ORIGINS配置即可生效 🎉 Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -59,6 +59,28 @@ app.use((req, res, next) => {
|
||||
next();
|
||||
});
|
||||
|
||||
// 获取正确的协议(考虑反向代理)
|
||||
function getProtocol(req) {
|
||||
// 1. 检查 X-Forwarded-Proto 头(nginx 代理传递的协议)
|
||||
const forwardedProto = req.get('X-Forwarded-Proto');
|
||||
if (forwardedProto) {
|
||||
return forwardedProto.split(',')[0].trim();
|
||||
}
|
||||
|
||||
// 2. 检查 req.protocol
|
||||
if (req.protocol) {
|
||||
return req.protocol;
|
||||
}
|
||||
|
||||
// 3. 如果配置了SSL,默认使用https
|
||||
if (req.secure) {
|
||||
return 'https';
|
||||
}
|
||||
|
||||
// 4. 默认使用https(因为生产环境应该都配置了SSL)
|
||||
return 'https';
|
||||
}
|
||||
|
||||
// 文件上传配置(临时存储)
|
||||
const upload = multer({
|
||||
dest: path.join(__dirname, 'uploads'),
|
||||
@@ -1230,7 +1252,7 @@ app.post('/api/share/create', authMiddleware, (req, res) => {
|
||||
db.prepare('UPDATE shares SET storage_type = ? WHERE id = ?')
|
||||
.run(req.user.current_storage_type || 'sftp', result.id);
|
||||
|
||||
const shareUrl = `${req.protocol}://${req.get('host')}/s/${result.share_code}`;
|
||||
const shareUrl = `${getProtocol(req)}://${req.get('host')}/s/${result.share_code}`;
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
@@ -1257,7 +1279,7 @@ app.get('/api/share/my', authMiddleware, (req, res) => {
|
||||
success: true,
|
||||
shares: shares.map(share => ({
|
||||
...share,
|
||||
share_url: `${req.protocol}://${req.get('host')}/s/${share.share_code}`
|
||||
share_url: `${getProtocol(req)}://${req.get('host')}/s/${share.share_code}`
|
||||
}))
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user