mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-05 11:39:24 +08:00
merge: 修复禁用账号无密码枚举与生产环境 NextAuth debug
This commit is contained in:
@@ -19,6 +19,8 @@ vi.mock('@/lib/logger', () => ({
|
||||
|
||||
import { authOptions } from '@/lib/auth';
|
||||
import { isDefaultAdminPassword } from '@/lib/default-admin';
|
||||
import { TOO_MANY_ATTEMPTS } from '@/lib/auth-errors';
|
||||
import { loginAccountLimiter } from '@/lib/rate-limit';
|
||||
|
||||
type AuthorizeFn = (credentials: Record<string, string>) => Promise<(User & { mustChangePassword?: boolean }) | null>;
|
||||
const credentialsProvider = authOptions.providers[0] as unknown as { options: { authorize: AuthorizeFn } };
|
||||
@@ -104,3 +106,50 @@ describe('jwt 回调', () => {
|
||||
expect(token.mustChangePassword).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('authorize 禁用账号', () => {
|
||||
const disabledRow = { ...adminRow, email: 'disabled@example.com', role: 'user', isActive: false };
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
loginAccountLimiter.reset('disabled@example.com');
|
||||
loginAccountLimiter.reset('locked@example.com');
|
||||
});
|
||||
|
||||
it('错误密码访问禁用账号时与普通密码错误一致,且计入失败限流', async () => {
|
||||
mocks.findUnique.mockResolvedValue(disabledRow);
|
||||
mocks.compare.mockResolvedValue(false);
|
||||
|
||||
expect(await authorize('disabled@example.com', 'wrong-pass')).toBeNull();
|
||||
expect(mocks.compare).toHaveBeenCalledWith('wrong-pass', 'hashed');
|
||||
expect(loginAccountLimiter.retryAfter('disabled@example.com')).toBe(0);
|
||||
expect(loginAccountLimiter.retryAfter('disabled@example.com')).toBeGreaterThanOrEqual(0);
|
||||
// 已记一次失败:再错 4 次即达到锁定阈值
|
||||
for (let i = 0; i < 4; i += 1) {
|
||||
await authorize('disabled@example.com', 'wrong-pass');
|
||||
}
|
||||
expect(loginAccountLimiter.retryAfter('disabled@example.com')).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('正确密码访问禁用账号时提示已停用', async () => {
|
||||
mocks.findUnique.mockResolvedValue(disabledRow);
|
||||
mocks.compare.mockResolvedValue(true);
|
||||
|
||||
await expect(authorize('disabled@example.com', 'right-pass')).rejects.toThrow('Account is disabled');
|
||||
});
|
||||
|
||||
it('连续错误密码达到上限后锁定,不再暴露账号状态', async () => {
|
||||
mocks.findUnique.mockResolvedValue(disabledRow);
|
||||
mocks.compare.mockResolvedValue(false);
|
||||
|
||||
for (let i = 0; i < 5; i += 1) {
|
||||
await authorize('locked@example.com', 'wrong-pass');
|
||||
}
|
||||
await expect(authorize('locked@example.com', 'wrong-pass')).rejects.toThrow(TOO_MANY_ATTEMPTS);
|
||||
expect(mocks.compare).toHaveBeenCalledTimes(5);
|
||||
});
|
||||
|
||||
it('生产环境关闭 NextAuth debug', () => {
|
||||
expect(authOptions.debug).toBe(process.env.NODE_ENV !== 'production');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -75,12 +75,8 @@ export const authOptions: NextAuthOptions = {
|
||||
return null
|
||||
}
|
||||
|
||||
// Check if user is active
|
||||
if (!user.isActive) {
|
||||
logger.warn('User is disabled');
|
||||
throw new Error("Account is disabled")
|
||||
}
|
||||
|
||||
// 先校验密码:密码错误时与普通账号一致返回 null 并计入失败限流,
|
||||
// 不提前暴露「账号存在但已停用」,避免无密码枚举禁用账号
|
||||
const isPasswordValid = await compare(credentials.password, user.password)
|
||||
|
||||
if (!isPasswordValid) {
|
||||
@@ -89,6 +85,13 @@ export const authOptions: NextAuthOptions = {
|
||||
return null
|
||||
}
|
||||
|
||||
// 密码正确但账号已停用:明确提示,并计入失败限流防止反复探测
|
||||
if (!user.isActive) {
|
||||
logger.warn('User is disabled');
|
||||
recordFailure();
|
||||
throw new Error("Account is disabled")
|
||||
}
|
||||
|
||||
loginAccountLimiter.reset(accountKey);
|
||||
|
||||
logger.info({ email: user.email }, 'Login successful');
|
||||
@@ -103,8 +106,8 @@ export const authOptions: NextAuthOptions = {
|
||||
}
|
||||
})
|
||||
],
|
||||
// Enable debug messages in the console
|
||||
debug: true,
|
||||
// 仅非生产环境输出 NextAuth 调试日志
|
||||
debug: process.env.NODE_ENV !== "production",
|
||||
logger: {
|
||||
error(code, metadata) {
|
||||
logger.error({ code, metadata }, 'NextAuth error');
|
||||
|
||||
Reference in New Issue
Block a user