Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 10, 2026

✅ Completed: 2FA Recovery Codes Security Enhancement

Changes Implemented

  • Add database migration to add two_factor_recovery_codes_viewed_at column to users table
  • Update confirmTwoFactorAuthentication() to set the viewed timestamp when showing codes for the first time
  • Update showRecoveryCodes() method to prevent showing codes if they have been viewed
  • Update the view to hide "Show Recovery Codes" button if codes have been viewed
  • Update regenerateRecoveryCodes() to reset the viewed timestamp so new codes can be shown once
  • Add comprehensive tests to verify recovery codes cannot be viewed after initial display
  • Fix code style issues (trailing whitespace)
  • Run security scan with CodeQL (no vulnerabilities found)
  • Fix test failure by adding hasViewedRecoveryCodes() helper method

Recent Fix

Added hasViewedRecoveryCodes() helper method to User model to safely check if recovery codes have been viewed, preventing attribute access errors in blade views during testing.

Security Improvement

Recovery codes are now only displayed once when first generated:

  1. When 2FA is confirmed, codes are shown and marked as viewed
  2. The "Show Recovery Codes" button is hidden after codes are viewed
  3. Attempting to call showRecoveryCodes() after viewing shows an error message
  4. When codes are regenerated, they can be viewed once again (timestamp is reset)
  5. When 2FA is disabled, the viewed timestamp is properly cleared

Testing

Added 3 new test cases:

  • Verify codes can be viewed initially
  • Verify codes cannot be viewed after being viewed once
  • Verify codes can be viewed after regeneration

All existing tests updated to account for the new timestamp field.

Original prompt

This section details on the original issue you should resolve

<issue_title>FIX: 2FA recover codes should only be allowed to be displayed the first time, and never again.</issue_title>
<issue_description>FIX: 2FA recover codes should only be allowed to be displayed the first time, and never again.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix 2FA recovery codes display issue Enforce one-time viewing of 2FA recovery codes Jan 10, 2026
Copilot AI requested a review from mortenebak January 10, 2026 20:11
@mortenebak
Copy link
Owner

@copilot FAILED Tests\Feature\Settings\TwoFactorTest > recovery co… ViewException
The attribute [two_factor_recovery_codes_viewed_at] either does not exist or was not retrieved for model [App\Models\User]. (View: /home/runner/work/tallstarter/tallstarter/resources/views/livewire/settings/two-factor.blade.php) (View: /home/runner/work/tallstarter/tallstarter/resources/views/livewire/settings/two-factor.blade.php)

Copy link
Contributor Author

Copilot AI commented Jan 10, 2026

@copilot FAILED Tests\Feature\Settings\TwoFactorTest > recovery co… ViewException
The attribute [two_factor_recovery_codes_viewed_at] either does not exist or was not retrieved for model [App...

Fixed in e10d083. Added hasViewedRecoveryCodes() helper method to the User model to safely check if recovery codes have been viewed, preventing direct attribute access in the blade view that was causing the test failure.

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.

FIX: 2FA recover codes should only be allowed to be displayed the first time, and never again.

2 participants