Repository navigation
Two-factor e-mail code: draw it from a secure source and accept it once - #1099
Merged
Merged
Conversation
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.
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.
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.
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:RandomNumberGenerator.GetInt32(1_000_000)CryptographicOperations.FixedTimeEquals; an empty stored code never matchesNotes
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
IResourceLock(keyTwoFactorCode:{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.