问题描述: - JWT_SECRET使用硬编码默认值,存在严重安全风险 - 生产环境token可被轻易伪造 修复内容: 1. 在auth.js中添加JWT密钥安全检查 - 检测默认密钥并发出警告 - 生产环境强制要求设置JWT_SECRET 2. 更新.env.example添加JWT_SECRET配置说明 - 提供密钥生成方法 - 添加其他安全配置项 3. 优化deploy.sh部署脚本 - 自动生成随机JWT密钥 - 检测并替换默认密钥 影响范围: 安全认证模块 测试建议: - 启动服务验证JWT_SECRET警告正常显示 - 使用deploy.sh部署验证自动生成密钥 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
141 lines
4.3 KiB
JavaScript
141 lines
4.3 KiB
JavaScript
const jwt = require('jsonwebtoken');
|
||
const { UserDB } = require('./database');
|
||
|
||
// JWT密钥(必须在环境变量中设置)
|
||
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production';
|
||
|
||
// 安全检查:验证JWT密钥配置
|
||
const DEFAULT_SECRETS = [
|
||
'your-secret-key-change-in-production',
|
||
'your-secret-key-change-in-production-PLEASE-CHANGE-THIS'
|
||
];
|
||
|
||
if (DEFAULT_SECRETS.includes(JWT_SECRET)) {
|
||
const errorMsg = `
|
||
╔═══════════════════════════════════════════════════════════════╗
|
||
║ ⚠️ 安全警告 ⚠️ ║
|
||
╠═══════════════════════════════════════════════════════════════╣
|
||
║ JWT_SECRET 使用默认值,存在严重安全风险! ║
|
||
║ ║
|
||
║ 请立即设置环境变量 JWT_SECRET ║
|
||
║ 生成随机密钥: ║
|
||
║ node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
|
||
║ ║
|
||
║ 在 backend/.env 文件中设置: ║
|
||
║ JWT_SECRET=你生成的随机密钥 ║
|
||
╚═══════════════════════════════════════════════════════════════╝
|
||
`;
|
||
|
||
if (process.env.NODE_ENV === 'production') {
|
||
console.error(errorMsg);
|
||
throw new Error('生产环境必须设置 JWT_SECRET!');
|
||
} else {
|
||
console.warn(errorMsg);
|
||
}
|
||
}
|
||
|
||
console.log('[安全] JWT密钥已配置');
|
||
|
||
// 生成JWT Token
|
||
function generateToken(user) {
|
||
return jwt.sign(
|
||
{
|
||
id: user.id,
|
||
username: user.username,
|
||
is_admin: user.is_admin
|
||
},
|
||
JWT_SECRET,
|
||
{ expiresIn: '7d' }
|
||
);
|
||
}
|
||
|
||
// 验证Token中间件
|
||
function authMiddleware(req, res, next) {
|
||
// 从请求头、cookie或URL参数中获取token
|
||
const token = req.headers.authorization?.replace('Bearer ', '') || req.cookies?.token || req.query?.token;
|
||
|
||
if (!token) {
|
||
return res.status(401).json({
|
||
success: false,
|
||
message: '未提供认证令牌'
|
||
});
|
||
}
|
||
|
||
try {
|
||
const decoded = jwt.verify(token, JWT_SECRET);
|
||
const user = UserDB.findById(decoded.id);
|
||
|
||
if (!user) {
|
||
return res.status(401).json({
|
||
success: false,
|
||
message: '用户不存在'
|
||
});
|
||
}
|
||
|
||
if (user.is_banned) {
|
||
return res.status(403).json({
|
||
success: false,
|
||
message: '账号已被封禁'
|
||
});
|
||
}
|
||
|
||
if (!user.is_active) {
|
||
return res.status(403).json({
|
||
success: false,
|
||
message: '账号未激活'
|
||
});
|
||
}
|
||
|
||
// 将用户信息附加到请求对象(包含所有存储相关字段)
|
||
req.user = {
|
||
id: user.id,
|
||
username: user.username,
|
||
email: user.email,
|
||
is_admin: user.is_admin,
|
||
has_ftp_config: user.has_ftp_config,
|
||
ftp_host: user.ftp_host,
|
||
ftp_port: user.ftp_port,
|
||
ftp_user: user.ftp_user,
|
||
ftp_password: user.ftp_password,
|
||
http_download_base_url: user.http_download_base_url,
|
||
// 存储相关字段(v2.0新增)
|
||
storage_permission: user.storage_permission || 'sftp_only',
|
||
current_storage_type: user.current_storage_type || 'sftp',
|
||
local_storage_quota: user.local_storage_quota || 1073741824,
|
||
local_storage_used: user.local_storage_used || 0
|
||
};
|
||
|
||
next();
|
||
} catch (error) {
|
||
if (error.name === 'TokenExpiredError') {
|
||
return res.status(401).json({
|
||
success: false,
|
||
message: '令牌已过期'
|
||
});
|
||
}
|
||
|
||
return res.status(401).json({
|
||
success: false,
|
||
message: '无效的令牌'
|
||
});
|
||
}
|
||
}
|
||
|
||
// 管理员权限中间件
|
||
function adminMiddleware(req, res, next) {
|
||
if (!req.user || !req.user.is_admin) {
|
||
return res.status(403).json({
|
||
success: false,
|
||
message: '需要管理员权限'
|
||
});
|
||
}
|
||
next();
|
||
}
|
||
|
||
module.exports = {
|
||
JWT_SECRET,
|
||
generateToken,
|
||
authMiddleware,
|
||
adminMiddleware
|
||
};
|