安全: 修复密码修改无需验证旧密码的安全漏洞
问题描述: - 用户修改密码时不需要验证当前密码 - 攻击者获取session后可直接修改密码 - 违反基本的安全最佳实践 修复内容: 1. 添加current_password必填验证 2. 在更新密码前验证当前密码正确性 3. 验证失败返回401错误 4. 更新API文档注释 API变更: POST /api/user/change-password 请求参数: - current_password (新增,必填) - new_password (已有,必填) 影响范围: 用户密码修改功能 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -353,10 +353,11 @@ app.post('/api/admin/update-profile',
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// 修改当前用户密码(管理员直接修改,不需要验证当前密码)
|
// 修改当前用户密码(需要验证当前密码)
|
||||||
app.post('/api/user/change-password',
|
app.post('/api/user/change-password',
|
||||||
authMiddleware,
|
authMiddleware,
|
||||||
[
|
[
|
||||||
|
body('current_password').notEmpty().withMessage('当前密码不能为空'),
|
||||||
body('new_password').isLength({ min: 6 }).withMessage('新密码至少6个字符')
|
body('new_password').isLength({ min: 6 }).withMessage('新密码至少6个字符')
|
||||||
],
|
],
|
||||||
(req, res) => {
|
(req, res) => {
|
||||||
@@ -369,9 +370,26 @@ app.post('/api/user/change-password',
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { new_password } = req.body;
|
const { current_password, new_password } = req.body;
|
||||||
|
|
||||||
// 直接更新密码,不需要验证当前密码
|
// 获取当前用户信息
|
||||||
|
const user = UserDB.findById(req.user.id);
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
message: '用户不存在'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证当前密码
|
||||||
|
if (!UserDB.verifyPassword(current_password, user.password)) {
|
||||||
|
return res.status(401).json({
|
||||||
|
success: false,
|
||||||
|
message: '当前密码错误'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新密码
|
||||||
UserDB.update(req.user.id, { password: new_password });
|
UserDB.update(req.user.id, { password: new_password });
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
|
|||||||
Reference in New Issue
Block a user