Skip to content

Limit the wrong two-factor codes a customer may try - #1100

Merged
KrzysztofPajak merged 3 commits into
developfrom
fix/two-factor-attempt-limit
Nov 23, 2026
Merged

KrzysztofPajak merged 3 commits into
developfrom
fix/two-factor-attempt-limit

Conversation

@KrzysztofPajak

@KrzysztofPajak KrzysztofPajak commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

After the password step, the second step accepted any number of codes. A six-digit code could be guessed by trying codes in turn, in the storefront and in the admin, store and vendor panels. A wrong code raised CustomerLoginFailedEvent, but lockout is off by default and the second step did not look at it.

Changes

  • New ITwoFactorSignInService (Grand.Business.Core) with TwoFactorSignInService (Grand.Business.Authentication): CheckCode runs the code check of one customer at a time under IResourceLock (key TwoFactorSignIn:{customerId}), reads the customer again, refuses a locked-out customer (CannotLoginUntilDateUtc), and counts wrong codes on the customer in the user field TwoFactorWrongCodes.
  • The fifth wrong code (MaxWrongCodes) locks the customer out for CustomerSettings.FailedPasswordLockoutMinutes, or 30 minutes (the installer's value) when the store set none. A code that passes clears the count; a new password step does not.
  • CustomerLoginFailedEvent is published from the service with the customer read under the lock, so its handler writes the lockout fields back from current data.
  • AccountController and BaseLoginController: the POST of the second step uses the service. On lockout the waiting sign-in is dropped and the form shows Account.Login.WrongCredentials.LockedOut; the password step already refuses a locked-out customer.

Notes

  • No new resources.
  • A customer who knows the password but not the code can lock the account for the lockout length. That needs the password, and it is the same trade the password lockout makes.

Tests

  • TwoFactorSignInServiceTests: right code passes; a wrong code is counted on the customer and reported; the fifth locks out; a locked-out customer is refused even with the right code; the count survives a new password step and a passing code clears it; lockout without a configured length; the check runs under the customer's lock.
  • LoginControllerTwoFactorTests, AccountControllerTwoFactorReturnUrlTests moved to the service.
  • Grand.Business.Authentication.Tests (410), Grand.Web.Admin.Tests (2110), Grand.Web.Tests (655), Grand.Web.Store.Tests (245), Grand.Web.Vendor.Tests (53) pass.

After the password step, the second step accepted any number of codes in the same session, so a six-digit code could be guessed by trying them in turn, in the storefront and in the panels.

After five wrong codes the waiting sign-in is dropped and the customer signs in with the password again. The count starts again on each password step and after a code passes.
Copilot AI balanced review requested due to automatic review settings November 23, 2026 05:04

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.

… session

The limit lived in the session: parallel requests of one sign-in each read the same count, and each password step started it again, so with an authenticator app the guessing went on in rounds of five. The second step also never looked at the login lockout.

A new ITwoFactorSignInService checks the code under a lock on the customer, reads the customer again, refuses a locked-out one and counts wrong codes on the customer, where a new password step does not clear them. The fifth wrong code locks the customer out for the store's lockout length (30 minutes when none is set) and drops the waiting sign-in; the password step already refuses a locked-out customer. A code that passes clears the count. The storefront and the panels use it, and the session counter and its resource are gone.
@KrzysztofPajak KrzysztofPajak changed the title Limit the wrong two-factor codes one sign-in may try Limit the wrong two-factor codes a customer may try Nov 23, 2026
@KrzysztofPajak
KrzysztofPajak merged commit e89d234 into develop Nov 23, 2026
9 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the fix/two-factor-attempt-limit branch November 25, 2026 09:08
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