Skip to content

Accept TOTP codes from one time step either side of the current one. #9783 - #10899

Merged
dpage merged 2 commits into
pgadmin-org:masterfrom
dpage:fix/issue-9783-totp-valid-window
Nov 30, 2026
Merged

dpage merged 2 commits into
pgadmin-org:masterfrom
dpage:fix/issue-9783-totp-valid-window

Conversation

@dpage

@dpage dpage commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • TOTP registration and login only accepted the code for the current 30-second step, so even a few seconds' difference between the server's clock and the authenticator's could make a correct code fail with "Failed to validate the code".
  • Both checks in web/pgadmin/authenticate/mfa/authenticator.py now pass valid_window=1 to pyotp's verify(), accepting one step either side of the current one, which is common practice for TOTP. Nothing else in the MFA flow changes.

The reporter says both clocks are synchronised, so this may not resolve their case on its own (a lost session during registration would also produce this error, and I've asked about that on the issue); hence this PR is related to the issue rather than closing it.

Test plan

  • New test_totp_window.py scenarios, with the clock frozen: codes for the previous, current and next step are accepted at login and registration, and codes two steps away are rejected. Both fail without the fix.
  • runtests.py --pkg authenticate.mfa (22 tests) passes.
  • pycodestyle on the changed files.

Related to #9783

Summary by CodeRabbit

  • Bug Fixes
    • Multi-factor authentication codes are accepted for the current 30-second time step or one step before or after during sign-in and authenticator registration. Codes from two or more steps away are rejected, keeping the accepted time window limited while allowing for minor differences in device and service clocks.

@dpage dpage self-assigned this Nov 30, 2026
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

TOTP login and registration accept codes from the current 30-second step or either adjacent step. Tests verify that codes two steps away are rejected.

Changes

TOTP validation window

Layer / File(s) Summary
Apply the shared TOTP window
web/pgadmin/authenticate/mfa/authenticator.py
Login and registration code verification use a one-step window. Codes outside the window retain the existing failure handling.
Test login and registration offsets
web/pgadmin/authenticate/mfa/tests/test_totp_window.py, web/pgadmin/authenticate/mfa/tests/test_mfa.py
Frozen-time scenarios verify that offsets -1, 0, and 1 are accepted, while offsets -2 and 2 are rejected. Registration tests also check authenticator addition and view state.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: asheshv

Merge Risk: Minimal · up to 15e6b

TOTP login and registration now tolerate one 30-second step of clock drift in either direction. Tests cover the accepted and rejected offsets. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Accept TOTP codes from one time step either side of the current one." matches the main change: TOTP registration and login now accept codes from the previous or next 30-second step via vali…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Accept TOTP codes from one time step either side of the current one." matches the main change: TOTP registration and login now accept codes from the previous or next 30-second step via valid_window=1, and the new tests cover this. It also names the change specifically. The issue reference #9783 is extra but does not make the title unclear.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between aa27447 and 15e6b4f.

Files selected for processing: 1

  • web/pgadmin/authenticate/mfa/tests/test_mfa.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

…gadmin-org#9783

Both registration and login previously accepted only the code for the
current 30-second step, so a small difference between the server and
authenticator clocks caused "Failed to validate the code". Pass
valid_window=1 to pyotp's verify() in both places.
pyotp 2.12 requires secrets of at least 112 bits, makes counter_offset
keyword-only and reads the clock from time.time(), so use a 160-bit
secret, generate codes from timestamps and freeze both clock sources.
@dpage
dpage force-pushed the fix/issue-9783-totp-valid-window branch from aa27447 to 15e6b4f Compare November 30, 2026 14:37
@dpage
dpage merged commit 43d359a into pgadmin-org:master Nov 30, 2026
30 of 32 checks passed
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.

1 participant