fix: 修复防爆破保护不生效的问题

问题原因:
- 登录接口缺少recordFailure调用
- 分享密码接口缺少recordFailure和recordSuccess调用

修复内容:
- 在用户不存在时记录失败尝试
- 在密码错误时记录失败尝试
- 在分享密码错误时记录失败尝试
- 在分享密码验证成功时清除失败记录

测试方法:
- 连续5次错误登录后应被封锁30分钟
- 连续10次错误分享密码后应被封锁20分钟
This commit is contained in:
2025-11-13 23:20:25 +08:00
parent b7ecfa4dff
commit f6e88d85e7

View File

@@ -574,6 +574,13 @@ app.post('/api/login',
const user = UserDB.findByUsername(username); const user = UserDB.findByUsername(username);
if (!user) { if (!user) {
// 记录失败尝试
if (req.rateLimitKeys) {
loginLimiter.recordFailure(req.rateLimitKeys.ipKey);
if (req.rateLimitKeys.usernameKey) {
loginLimiter.recordFailure(req.rateLimitKeys.usernameKey);
}
}
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: '用户名或密码错误' message: '用户名或密码错误'
@@ -588,6 +595,13 @@ app.post('/api/login',
} }
if (!UserDB.verifyPassword(password, user.password)) { if (!UserDB.verifyPassword(password, user.password)) {
// 记录失败尝试
if (req.rateLimitKeys) {
loginLimiter.recordFailure(req.rateLimitKeys.ipKey);
if (req.rateLimitKeys.usernameKey) {
loginLimiter.recordFailure(req.rateLimitKeys.usernameKey);
}
}
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: '用户名或密码错误' message: '用户名或密码错误'
@@ -1587,6 +1601,10 @@ app.post('/api/share/:code/verify', shareRateLimitMiddleware, async (req, res) =
} }
if (!ShareDB.verifyPassword(password, share.share_password)) { if (!ShareDB.verifyPassword(password, share.share_password)) {
// 记录密码错误
if (req.shareRateLimitKey) {
shareLimiter.recordFailure(req.shareRateLimitKey);
}
return res.status(401).json({ return res.status(401).json({
success: false, success: false,
message: '密码错误' message: '密码错误'
@@ -1594,6 +1612,11 @@ app.post('/api/share/:code/verify', shareRateLimitMiddleware, async (req, res) =
} }
} }
// 清除失败记录(密码验证成功)
if (req.shareRateLimitKey) {
shareLimiter.recordSuccess(req.shareRateLimitKey);
}
// 增加查看次数 // 增加查看次数
ShareDB.incrementViewCount(code); ShareDB.incrementViewCount(code);