Skip to content

Two-factor e-mail code: draw it from a secure source and accept it once - #1099

Merged
KrzysztofPajak merged 2 commits into
developfrom
fix/two-factor-email-code
Nov 23, 2026
Merged

KrzysztofPajak merged 2 commits into
developfrom
fix/two-factor-email-code

Conversation

@KrzysztofPajak

@KrzysztofPajak KrzysztofPajak commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

The e-mail sign-in code was drawn with System.Random (Next(0, 999999), so 999999 never came up), compared with !=, and stayed valid for its whole 30 minutes after it had signed in.

Changes

  • TwoFactorAuthenticationService:
    • the code is drawn with RandomNumberGenerator.GetInt32(1_000_000)
    • it is compared with CryptographicOperations.FixedTimeEquals; an empty stored code never matches
    • after a successful check the stored code is cleared, so the same code cannot sign in again

Notes

  • The number of wrong codes a sign-in may try is not limited here; that is a separate change in the login controllers.

Tests

  • TwoFactorAuthenticationServiceTests: correct code signs in and is cleared; wrong, empty and missing codes, a used-up code and an expired code are refused.
  • Grand.Business.Authentication.Tests (409) pass.

Review follow-up

  • The e-mail code is now checked and cleared under IResourceLock (key TwoFactorCode:{customerId}) after reading the customer again, so two requests with the same code at once cannot both sign in.
  • TwoFactorAuthenticationServiceTests: a code used up by another request is refused; the check runs under the customer's lock. Grand.Business.Authentication.Tests (411) pass.

The e-mail sign-in code came from System.Random, was compared with string equality and stayed valid for its whole 30 minutes after it had been used.

The code is now drawn with RandomNumberGenerator, compared in constant time, and cleared once it signs in, so the same code cannot sign in again.
Copilot AI balanced review requested due to automatic review settings November 23, 2026 04:54

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

The code was checked against the customer the request had loaded and cleared afterwards, so two requests with the same code that arrived together could both pass before either cleared it. The check now takes a lock on the customer's code, reads the code again from the database and clears it before letting go, so only one of them signs in.
@KrzysztofPajak
KrzysztofPajak merged commit 9abe193 into develop Nov 23, 2026
9 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the fix/two-factor-email-code branch November 25, 2026 09:09
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