Skip to content

🔑 fix: Require OTP Verification for 2FA Re-Enrollment and Backup Code Regeneration#12223

Merged
danny-avila merged 8 commits into
devfrom
fix/sec-2fa-otp
Mar 14, 2026
Merged

🔑 fix: Require OTP Verification for 2FA Re-Enrollment and Backup Code Regeneration#12223
danny-avila merged 8 commits into
devfrom
fix/sec-2fa-otp

Conversation

@danny-avila

Copy link
Copy Markdown
Owner

No description provided.

…ler and UserController

- Reformatted code in TwoFactorController and UserController for better readability by aligning parameters and breaking long lines.
- Updated test cases in deleteUser.spec.js and TwoFactorController.spec.js to enhance clarity by formatting object parameters consistently.
Copilot AI review requested due to automatic review settings March 14, 2026 01:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the 2FA “enable” flow to accept OTP/backup-code input (and switches the route from GET to POST), strengthens 2FA enforcement for sensitive actions (account deletion, backup-code regeneration, re-enrollment), and adds Jest coverage for the updated controller behavior.

Changes:

  • Change /api/auth/2fa/enable from GET → POST and plumb an optional { token, backupCode } request payload through the data-provider and client mutation.
  • Enforce 2FA verification for account deletion when 2FA is enabled on the account.
  • Tighten controller behavior around 2FA re-enrollment / backup-code regeneration and add unit tests for these flows.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
packages/data-provider/src/types.ts Adds TEnable2FARequest; removes backupCodesHash from TRegenerateBackupCodesResponse.
packages/data-provider/src/data-service.ts Switches enableTwoFactor() to POST and forwards an optional payload.
client/src/data-provider/Auth/mutations.ts Updates useEnableTwoFactorMutation to accept an optional 2FA payload.
api/server/routes/auth.js Switches /2fa/enable route from GET to POST.
api/server/controllers/UserController.js Requires OTP/backup-code verification to delete an account when 2FA is enabled.
api/server/controllers/TwoFactorController.js Adds verification requirements for re-enrollment and backup-code regeneration; adjusts responses.
api/server/controllers/tests/deleteUser.spec.js Adds tests covering delete-user behavior with/without 2FA and with valid/invalid credentials.
api/server/controllers/tests/TwoFactorController.spec.js Adds tests for enable2FA re-enrollment rules and backup-code regeneration response shape.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread api/server/controllers/TwoFactorController.js Outdated
Comment thread api/server/controllers/TwoFactorController.js
Comment thread api/server/controllers/TwoFactorController.js Outdated
Comment thread api/server/controllers/UserController.js Outdated
Comment thread api/server/controllers/UserController.js Outdated
Comment thread packages/data-provider/src/types.ts Outdated
Comment thread api/server/controllers/TwoFactorController.js Outdated
…ctorController and UserController

- Introduced a new `verifyOTPOrBackupCode` function to streamline the verification process for TOTP tokens and backup codes across multiple controllers.
- Updated the `enable2FA`, `disable2FA`, and `deleteUserController` methods to utilize the new verification function, enhancing code reusability and readability.
- Adjusted related tests to reflect the changes in verification logic, ensuring consistent behavior across different scenarios.
- Improved error handling and response messages for verification failures, providing clearer feedback to users.
…n logic

- Consolidated OTP input handling by moving the 2FA verification UI logic to a more consistent location within the component.
- Improved the state management for OTP readiness, ensuring the regenerate button is only enabled when the OTP is ready.
- Cleaned up imports by removing redundant type imports, enhancing code clarity and maintainability.
@danny-avila danny-avila changed the title fix/sec 2fa otp 🔑 fix: Require OTP Verification for 2FA Re-Enrollment and Backup Code Regeneration Mar 14, 2026
… window

enable2FA now writes to pendingTotpSecret/pendingBackupCodes instead of
overwriting the live fields. confirm2FA performs the atomic swap only after
the new TOTP code is verified. If the user abandons mid-flow, their
existing 2FA remains active and intact.
@danny-avila danny-avila merged commit 71a3b48 into dev Mar 14, 2026
13 checks passed
@danny-avila danny-avila deleted the fix/sec-2fa-otp branch March 14, 2026 05:51
jcbartle pushed a commit to jcbartle/LibreChat that referenced this pull request May 11, 2026
… Regeneration (danny-avila#12223)

* fix: require OTP verification for 2FA re-enrollment and backup code regeneration

* fix: require OTP verification for account deletion when 2FA is enabled

* refactor: Improve code formatting and readability in TwoFactorController and UserController

- Reformatted code in TwoFactorController and UserController for better readability by aligning parameters and breaking long lines.
- Updated test cases in deleteUser.spec.js and TwoFactorController.spec.js to enhance clarity by formatting object parameters consistently.

* refactor: Consolidate OTP and backup code verification logic in TwoFactorController and UserController

- Introduced a new `verifyOTPOrBackupCode` function to streamline the verification process for TOTP tokens and backup codes across multiple controllers.
- Updated the `enable2FA`, `disable2FA`, and `deleteUserController` methods to utilize the new verification function, enhancing code reusability and readability.
- Adjusted related tests to reflect the changes in verification logic, ensuring consistent behavior across different scenarios.
- Improved error handling and response messages for verification failures, providing clearer feedback to users.

* chore: linting

* refactor: Update BackupCodesItem component to enhance OTP verification logic

- Consolidated OTP input handling by moving the 2FA verification UI logic to a more consistent location within the component.
- Improved the state management for OTP readiness, ensuring the regenerate button is only enabled when the OTP is ready.
- Cleaned up imports by removing redundant type imports, enhancing code clarity and maintainability.

* chore: lint

* fix: stage 2FA re-enrollment in pending fields to prevent disarmament window

enable2FA now writes to pendingTotpSecret/pendingBackupCodes instead of
overwriting the live fields. confirm2FA performs the atomic swap only after
the new TOTP code is verified. If the user abandons mid-flow, their
existing 2FA remains active and intact.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants