Add drift tolerance to one time password checks - #9437
Open
gbp wants to merge 1 commit into
Open
Conversation
Codes were verified with no drift, so one was rejected the instant its 30 seconds window ended. RFC 6238 recommends comparing against past timestamps within the transmission delay, allowing at most one step. Default the drift to 15 seconds - half a step. so we accepts the code that has just expired for the first 15 seconds of the next step. Nothing becomes replayable: authenticate_totp still passes otp_last_used_at as `after`, so ROTP drops any step at or before the one already used, and OtpRateLimit still caps repeat attempts. This fixes flaky tests where codes rolled over into the next window.
chrismytton
approved these changes
Aug 6, 2026
chrismytton
left a comment
Member
There was a problem hiding this comment.
Looks good to me! Just one suggestion for a potential code simplification, but no blockers.
Comment on lines
+1122
to
+1126
| def expiring_code | ||
| code = nil | ||
| travel_to(period_end - 1.second) { code = user.otp_code } | ||
| code | ||
| end |
Member
There was a problem hiding this comment.
Looks like #travel_to returns its block's value, so this can be simplified a bit.
Suggested change
| def expiring_code | |
| code = nil | |
| travel_to(period_end - 1.second) { code = user.otp_code } | |
| code | |
| end | |
| def expiring_code | |
| travel_to(period_end - 1.second) { user.otp_code } | |
| end |
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.
Codes were verified with no drift, so one was rejected the instant its 30 seconds window ended.
RFC 6238 recommends comparing against past timestamps within the transmission delay, allowing at most one step.
Default the drift to 15 seconds - half a step. so we accepts the code that has just expired for the first 15 seconds of the next step.
Nothing becomes replayable: authenticate_totp still passes otp_last_used_at as
after, so ROTP drops any step at or before the one already used, and OtpRateLimit still caps repeat attempts.This fixes flaky tests where codes rolled over into the next window.