mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[GH-ISSUE #6535] [Bug]: 2FA login fails with "invalid 2FA code" and then succeeds on an immediate retry #4384
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#4384
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @JinerJia on GitHub (Sep 15, 2026).
Original GitHub issue: https://github.com/MHSanaei/3x-ui/issues/6535
Describe the bug
Every time I log in with two-factor authentication (2FA), an error occurs.
Logs:
2026/09/15 00:55:01 INFO - X-UI: logged in successfully: username="KBupor36pq", IP="60.209.181.226"
2026/09/15 00:54:59 WARNING - X-UI: failed login: username="KBupor36pq", IP="60.209.181.226", reason="invalid 2FA code"
How to reproduce the problem
1.Pannel Settings--Authentication--Two-factor authentication
2.Enable 2FA
3.Login
Expected behavior
No response
Actual behavior
No response
Relevant logs
Screenshots
No response
3x-ui version
3.8.0
Xray-core version
No response
How did you install 3x-ui?
install.sh script
Operating system
Debian 13
Which parts of the panel are affected?
No response
Browser (only if it is a UI bug)
No response
Is the panel behind a reverse proxy or CDN?
No response
Before submitting
@github-actions[bot] commented on GitHub (Sep 15, 2026):
Confirmed bug: the panel accepts only the TOTP code for the exact 30-second step the server is in at the moment the request is evaluated — there is no tolerance for the previous/next step, so a code that was valid on your phone a second or two earlier is rejected. Your two log lines straddle a step boundary (
00:54:59failed,00:55:01succeeded — the boundary is00:55:00), which is the signature of exactly that.What the code does
The login check is a plain equality against a single generated value,
internal/web/service/panel/user.go:100:.Now()produces the code for the current instant only. There is no ±1 time-step window, which RFC 6238 §5.2 recommends and which virtually every TOTP verifier implements. The period really is 30 s: the QR/URI the panel hands your authenticator app is built withperiod: 30, digits: 6(frontend/src/pages/settings/TwoFactorModal.tsx:41-45).Two things follow from your log that are worth stating plainly:
internal/web/service/panel/user.go:54-93— and the log reason is produced solely by that comparison (internal/web/controller/index.go:132-137). So the only thing that failed was the code.internal/web/controller/index.go:111), which is why the failure looks like a credentials error in the UI.A second, smaller asymmetry in the same area: the setup-time check trims whitespace,
internal/web/service/setting.go:658:…but the login-time check at
user.go:100compares the raw submitted string, and the login form only requires a non-empty value (TwoFactorCodeSchema = z.string().min(1, ...),frontend/src/schemas/login.ts:9). A pasted or autofilled code carrying a leading/trailing space is therefore rejected at login while the identical input would be accepted in the settings dialog.What you can do right now
loginLimitMaxFailures = 5,loginLimitWindow = 5 * time.Minute,loginLimitCooldown = 15 * time.Minute,internal/web/controller/login_limiter.go:9-17). Repeatedly retrying a stale code can lock you out.(also available as the
x-uimenu's Reset Username & Password step; see First login → Two-factor authentication).Verdict: Confirmed bug
Severity: Medium
Confidence: Medium
Root cause:
internal/web/service/panel/user.go:100,UserService.CheckUser—gotp.NewDefaultTOTP(twoFactorToken).Now() != twoFactorCodeaccepts only the current 30-second step, with no ±1-step skew tolerance and noTrimSpaceon the submitted code. Triggered whenever the step rolls over between the authenticator generating the code and the server evaluating it, or whenever the server clock is offset from the client's by a few seconds. Introduced infe3b1c9b"chore: implement 2fa auth (#2968)" (the line is unchanged since, only relocated by41645255).Already fixed: Still present on the default branch (
a810f497);git log -S "NewDefaultTOTP" -- internal/shows only the introducing commit and theinternal/relocation.Duplicate of: None. Related: #3880 (login API returns the same response for a wrong password and a wrong 2FA code — same generic-message behaviour, different root cause).
Evidence:
internal/web/service/panel/user.go:100—if gotp.NewDefaultTOTP(twoFactorToken).Now() != twoFactorCode {;internal/web/controller/index.go:133—if err != nil && err.Error() == "invalid 2fa code"(the reason string in the reporter's log comes only from this path, i.e. the password already verified);internal/web/service/setting.go:658— the setup path trims and the login path does not;frontend/src/pages/settings/TwoFactorModal.tsx:41-45—period: 30, digits: 6;internal/web/controller/login_limiter.go:9-17— 5 failures / 5 min / 15 min lockout. No test asserts that an adjacent step must be rejected:TestVerifyTwoFactorCode(internal/web/service/setting_security_test.go:219-236) only checks a code generated at the same instant plus the literal"000000", so the no-window behaviour is not pinned as intended anywhere.Not determined: The actual offset between this server's clock and the reporter's authenticator —
timedatectl statuson the server, compared against the phone, would show whether this is routine step-rollover or a standing clock skew. I could not reproduce or run anything here; all statements above come from reading the source at the tip.Suggested fix: Verify the submitted code against the adjacent time steps as well as the current one, instead of a single
.Now()equality, and trim the submitted code the waySettingService.VerifyTwoFactorCodealready does. A regression test that generates a code one step in the past and asserts the login still accepts it would fail today.This reply was generated automatically; a maintainer may follow up.