[PR #6546] [MERGED] fix(panel): accept 2FA codes from adjacent TOTP windows #6344

Closed
opened 2026-11-02 04:08:05 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6546
Author: @sdhfsl
Created: 9/15/2026
Status: ✅ Merged
Merged: 9/15/2026
Merged by: @BlindMaster24

Base: main ← Head: main


📝 Commits (3)

  • 959e6fd fix(panel): accept 2FA codes from adjacent TOTP windows
  • f3096bb fix(panel): share TOTP skew tolerance with VerifyTwoFactorCode
  • 198ac75 Merge branch 'main' into main

📊 Changes

5 files changed (+65 additions, -4 deletions)

View changed files

➕ internal/util/totp/totp.go (+22 -0)
➕ internal/util/totp/totp_test.go (+34 -0)
📝 internal/web/service/panel/user.go (+3 -2)
📝 internal/web/service/setting.go (+2 -2)
📝 internal/web/service/setting_security_test.go (+4 -0)

📄 Description

Problem

Fixes #6535.

Every 2FA login fails once with invalid 2FA code and then succeeds on an immediate retry (see timestamps in the issue: fail at 00:54:59, success at 00:55:01).

Root cause

UserService.CheckUser (internal/web/service/panel/user.go) compared only gotp.NewDefaultTOTP(token).Now() — the single current 30s window. A code submitted near the end of its window (or with slight client/server clock drift) arrives after the server has rolled over, so the first attempt fails and the retry in the next window succeeds. Classic missing-skew bug.

Fix

Accept the current window ±1 step (verifyTOTPWithSkew), the standard TOTP clock-drift tolerance. Wrong codes and codes two windows away are still rejected.

Tests

New TestVerifyTOTPWithSkew: current/previous/next window accept, ±2 windows and wrong code reject — passes. Full panel package has one pre-existing failure (TestUpdateProxyEnvVars, fails on clean tree too, unrelated env-casing issue).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6546 **Author:** [@sdhfsl](https://github.com/sdhfsl) **Created:** 9/15/2026 **Status:** ✅ Merged **Merged:** 9/15/2026 **Merged by:** [@BlindMaster24](https://github.com/BlindMaster24) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (3) - [`959e6fd`](https://github.com/MHSanaei/3x-ui/commit/959e6fd62b07efd1038d70ecc18c3bc59ce2e3c2) fix(panel): accept 2FA codes from adjacent TOTP windows - [`f3096bb`](https://github.com/MHSanaei/3x-ui/commit/f3096bb3c1e3091647101d80939da52ab054b21d) fix(panel): share TOTP skew tolerance with VerifyTwoFactorCode - [`198ac75`](https://github.com/MHSanaei/3x-ui/commit/198ac751d71b3b063a61b59efed80d50444febbf) Merge branch 'main' into main ### 📊 Changes **5 files changed** (+65 additions, -4 deletions) <details> <summary>View changed files</summary> ➕ `internal/util/totp/totp.go` (+22 -0) ➕ `internal/util/totp/totp_test.go` (+34 -0) 📝 `internal/web/service/panel/user.go` (+3 -2) 📝 `internal/web/service/setting.go` (+2 -2) 📝 `internal/web/service/setting_security_test.go` (+4 -0) </details> ### 📄 Description ## Problem Fixes #6535. Every 2FA login fails once with `invalid 2FA code` and then succeeds on an immediate retry (see timestamps in the issue: fail at 00:54:59, success at 00:55:01). ## Root cause `UserService.CheckUser` (`internal/web/service/panel/user.go`) compared only `gotp.NewDefaultTOTP(token).Now()` — the single current 30s window. A code submitted near the end of its window (or with slight client/server clock drift) arrives after the server has rolled over, so the first attempt fails and the retry in the next window succeeds. Classic missing-skew bug. ## Fix Accept the current window ±1 step (`verifyTOTPWithSkew`), the standard TOTP clock-drift tolerance. Wrong codes and codes two windows away are still rejected. ## Tests New `TestVerifyTOTPWithSkew`: current/previous/next window accept, ±2 windows and wrong code reject — passes. Full `panel` package has one pre-existing failure (`TestUpdateProxyEnvVars`, fails on clean tree too, unrelated env-casing issue). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:08:05 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6344
No description provided.