TOTP: Enforce single-use of TOTP one-time passwords. #517
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.
Per the TOTP spec, section 5.2:
Once a TOTP token is entered, it should not be able to be re-used, it's one-time.
This is a hardening ticket, making the TOTP provider comply with the spec.
For example:
Per spec, both windows logging in successfully shouldn't be allowed, window 2 should fail the TOTP code and be forced to wait for the next generated one.
For a an example of why this protection exists, If someone were to be looking over your shoulder as you type in your password and authcode, they should not be able to repeat the same on their own device and succeed at logging in, because you've already used your one-time code.
This PR takes it a step further, mostly out of convenience, by making any TOTP codes for previous-timestamp-ticks be invalid once you use the future code.
In this next scenario, both windows are logging in with unique TOTP codes, but with this change window 2 will be rejected as window 1 has already logged in with a newer one-time password.
That means if someone were standing beside you, saw the first TOTP code (with 5s remaining on it), and you waited
and entered the next code generated, they should still not be able to login during the grace period with that now "old" code.
In this next scenario, both logins will proceed and login:
There is no minimum wait between successful logins, only that the next login must be using a later-generated code.
There are edge-cases which this does break, but I don't believe is a concern. For example, if there is a shared user account, with multiple TOTP generators, and their clocks are out of sync, and multiple people try to login at the same time, some will fail.
Note: The login nonce prevents you having two windows open on the 2FA screen, you must start each test of the flow from the user/pass login screen.
See WordPress/wporg-two-factor#3