Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:auth): prevent 2FA password attempt counter bypass
johaven5f53f7f2685b
diff --git a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.ts b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.tsindex ae8201df..6585d5b5 100644--- a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.ts+++ b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.spec.ts@@ -115,6 +115,7 @@ describe(AuthProvider2FA.name, () => {it('should throw error if verification fails', async () => {cache.get.mockResolvedValue('encrypted-secret')usersManager.fromUserId.mockResolvedValue(mockUser as UserModel)+ usersManager.compareUserPassword.mockResolvedValue(true)vi.spyOn(service, 'validateTwoFactorCode').mockReturnValue({ success: false, message: 'Invalid code' })await expect(service.enableTwoFactor(enableDto, mockRequest as FastifyAuthenticatedRequest)).rejects.toThrow(@@ -125,12 +126,15 @@ describe(AuthProvider2FA.name, () => {it('should throw error if password is incorrect', async () => {cache.get.mockResolvedValue('encrypted-secret')usersManager.fromUserId.mockResolvedValue(mockUser as UserModel)- vi.spyOn(service, 'validateTwoFactorCode').mockReturnValue({ success: true, message: '' })+ const validateTwoFactorCodeSpy = vi.spyOn(service, 'validateTwoFactorCode')usersManager.compareUserPassword.mockResolvedValue(false)await expect(service.enableTwoFactor(enableDto, mockRequest as FastifyAuthenticatedRequest)).rejects.toThrow(new HttpException('Incorrect code or password', HttpStatus.BAD_REQUEST))+ expect(validateTwoFactorCodeSpy).not.toHaveBeenCalled()+ expect(usersManager.updateAccesses).toHaveBeenCalledWith(mockUser, mockRequest.ip, false, true)+ expect(usersManager.updateAccesses).not.toHaveBeenCalledWith(mockUser, mockRequest.ip, true, true)})it('should enable 2FA and return recovery codes on success', async () => {@@ -165,6 +169,7 @@ describe(AuthProvider2FA.name, () => {it('should throw error if verification fails', async () => {usersManager.fromUserId.mockResolvedValue(mockUser as UserModel)+ usersManager.compareUserPassword.mockResolvedValue(true)vi.spyOn(service, 'validateTwoFactorCode').mockReturnValue({ success: false, message: 'Invalid code' })await expect(service.disableTwoFactor(disableDto, mockRequest as FastifyAuthenticatedRequest)).rejects.toThrow(@@ -174,12 +179,15 @@ describe(AuthProvider2FA.name, () => {it('should throw error if password is incorrect', async () => {usersManager.fromUserId.mockResolvedValue(mockUser as UserModel)- vi.spyOn(service, 'validateTwoFactorCode').mockReturnValue({ success: true, message: '' })+ const validateTwoFactorCodeSpy = vi.spyOn(service, 'validateTwoFactorCode')usersManager.compareUserPassword.mockResolvedValue(false)await expect(service.disableTwoFactor(disableDto, mockRequest as FastifyAuthenticatedRequest)).rejects.toThrow(new HttpException('Incorrect code or password', HttpStatus.BAD_REQUEST))+ expect(validateTwoFactorCodeSpy).not.toHaveBeenCalled()+ expect(usersManager.updateAccesses).toHaveBeenCalledWith(mockUser, mockRequest.ip, false, true)+ expect(usersManager.updateAccesses).not.toHaveBeenCalledWith(mockUser, mockRequest.ip, true, true)})it('should disable 2FA on success', async () => {diff --git a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.tsindex 0052079e..2b1f3747 100644--- a/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts+++ b/backend/src/authentication/providers/two-fa/auth-provider-two-fa.service.ts@@ -41,12 +41,14 @@ export class AuthProvider2FA {throw new HttpException('The secret has expired', HttpStatus.BAD_REQUEST)}// load user- const [auth, user] = await this.verify(body, req, true, secret)+ const user = await this.loadUser(req.user.id, req.ip)+ // verify user password+ await this.verifyUserPassword(user, body.password, req.ip)+ const auth = await this.validateVerificationCode(body, user, secret)+ await this.usersManager.updateAccesses(user, req.ip, auth.success, true)if (!auth.success) {throw new HttpException(auth.message, HttpStatus.FORBIDDEN)}- // verify user password- await this.verifyUserPassword(user, body.password, req.ip)// generate recovery codesconst recoveryCodes = this.generateRecoveryCodes()// store and enable TwoFA & recovery codes@@ -60,12 +62,14 @@ export class AuthProvider2FA {async disableTwoFactor(body: TwoFaVerifyWithPasswordDto, req: FastifyAuthenticatedRequest): Promise<TwoFaVerifyResult> {// load user- const [auth, user] = await this.verify(body, req, true)+ const user = await this.loadUser(req.user.id, req.ip)+ // verify user password+ await this.verifyUserPassword(user, body.password, req.ip)+ const auth = await this.validateVerificationCode(body, user)+ await this.usersManager.updateAccesses(user, req.ip, auth.success, true)if (!auth.success) {throw new HttpException(auth.message, HttpStatus.FORBIDDEN)}- // verify user password- await this.verifyUserPassword(user, body.password, req.ip)// store and disable TwoFA & recovery codesawait this.usersManager.updateSecrets(user.id, { twoFaSecret: undefined, recoveryCodes: undefined })this.sendEmailNotification(req, ACTION.DELETE)@@ -81,10 +85,7 @@ export class AuthProvider2FA {secret?: string): Promise<TwoFaVerifyResult | [TwoFaVerifyResult, UserModel]> {const user = await this.loadUser(req.user.id, req.ip)- secret = secret || user.secrets.twoFaSecret- const auth = verifyDto.isRecoveryCode- ? await this.validateRecoveryCode(req.user.id, verifyDto.code, user.secrets.recoveryCodes)- : this.validateTwoFactorCode(verifyDto.code, secret)+ const auth = await this.validateVerificationCode(verifyDto, user, secret)await this.usersManager.updateAccesses(user, req.ip, auth.success, true)return fromLogin ? [auth, user] : auth}@@ -121,6 +122,13 @@ export class AuthProvider2FA {}}+ private async validateVerificationCode(verifyDto: TwoFaVerifyDto, user: UserModel, secret?: string): Promise<TwoFaVerifyResult> {+ secret = secret || user.secrets.twoFaSecret+ return verifyDto.isRecoveryCode+ ? await this.validateRecoveryCode(user.id, verifyDto.code, user.secrets.recoveryCodes)+ : this.validateTwoFactorCode(verifyDto.code, secret)+ }+validateTwoFactorCode(code: string, encryptedSecret: string): TwoFaVerifyResult {const auth: TwoFaVerifyResult = { success: false, message: '' }if (!encryptedSecret) {