Allow disabling 2FA with a backup/recovery code (#5451)#5700
Open
TowyTowy wants to merge 1 commit into
Open
Conversation
The 2FA disable endpoint passed the submitted code straight to otplib's
verify(), which only accepts 6-digit numeric TOTP tokens. A backup/recovery
code (8 hex characters) makes verify() throw ("Token must be 6 digits" /
"Token must contain only digits"), which is returned to the client as a
generic 500 "Internal error".
As a result, a user who lost their authenticator and logged in with a
recovery code could not disable 2FA at all, since the recovery code is the
only credential they have.
Reuse the existing verifyForLogin() path, which already accepts both a TOTP
code and a backup code (and relaxes the secret-length guardrails for legacy
secrets), so disabling 2FA works with either, matching login behaviour.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Docker Image for build 1 is available on DockerHub: Note Ensure you backup your NPM instance before testing this image! Especially if there are database changes. Warning Changes and additions to DNS Providers require verification by at least 2 members of the community! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Fixes #5451.
The 2FA disable endpoint (
DELETE /api/users/:id/2fa) passed the submitted code straight to otplib'sverify(), which only accepts 6-digit numeric TOTP tokens. A backup/recovery code (8 hex characters, e.g.A1B2C3D4) makesverify()throw (Token must be 6 digits/Token must contain only digits), which is returned to the client as a generic 500 "Internal error".disable()also never fell back to matching the code against the stored backup codes.The practical impact: a user who lost their authenticator and logged in with a recovery code cannot disable 2FA at all — the recovery code is the only credential they have, and it is rejected. Multiple users reported this on #5451.
What
Reuse the existing
verifyForLogin()path indisable(). It already accepts both a TOTP code and a backup code, guards the TOTP check by length so non-numeric codes don't throw, and relaxes the secret-length guardrails for legacy secrets. Disabling 2FA now works with either a TOTP code or a recovery code, matching login behaviour. No API shape change.Verified with a standalone reproduction against the bundled otplib v13 + bcryptjs: the old path throws
TokenLengthErroron an 8-char recovery code (→ 500), the new path accepts a recovery code and a valid TOTP and rejects wrong codes without throwing.biome lintclean. (No backend unit-test harness exists — thetest/suite is Cypress integration needing a full Docker stack; reviewer steps below.)To verify manually: enable 2FA, log out, log back in using a recovery code, then Account → Two-Factor Auth → Disable and enter a recovery code — it should now succeed instead of "Internal error".
Type of Change
AI Usage
This PR was authored with AI assistance (Claude); every line was reviewed and the fix reproduced/verified before submission.