Gitea: TOTP TOCTOU race on web 2FA paths + missing replay check on Basic-Auth X-Gitea-OTP surface
I'm reporting two related TOTP one-time-use defects in Gitea that survive the CVE-2021-45331 fix. The 2018 fix (PR #3878) introduced the TwoFactor.LastUsedPasscode field and added an in-memory inequality check on the web 2FA login path. That check works correctly in the single-request case, but it leaves two follow-up gaps:
1. A TOCTOU race on the web surfaces (Defect 1). The read-validate-check-save sequence against the two_factor row is not atomic. Two parallel submissions of the same passcode each load their own in-memory copy where LastUsedPasscode still holds the prior value; both pass the inequality check, both authenticate, and both then write the same new value back. Net effect: the same OTP redeems for two independent logged-in sessions.
2. No LastUsedPasscode check at all on the Basic-Auth API surface (Defect 2). services/auth/basic.go calls twofa.ValidateTOTP(...) for X-Gitea-OTP without ever reading or writing LastUsedPasscode. The same six-digit code is replayable for the full totp.Validate acceptance window (~60–90 s with the default Skew=1). This is a clean [RFC 6238 §5.2](https://datatracker.ietf.org/doc/html/rfc6238#section-5.2) violation independent of timing, shaped identically to the pre-CVE-2021-45331 behaviour but scoped to the API / Git-over-HTTPS basic-auth path instead of the web form.
Both defects post-date the 2018 fix; neither is referenced in any published Gitea advisory I could find. I'm filing this as a follow-up to CVE-2021-45331, not a duplicate.
routers/web/auth/2fa.go:55-88:
```go
54 id := idSess.(int64)
55 twofa, err := auth.GetTwoFactorByUID(ctx, id) // (A) read row
56 ...
62 ok, err := twofa.ValidateTOTP(form.Passcode) // (B) pure-function RFC 6238 check
...
68 if ok && twofa.LastUsedPasscode != form.Passcode { // (C) check against in-memory copy
...
84 twofa.LastUsedPasscode = form.Passcode // (D) mutate in-memory
85 if err = auth.UpdateTwoFactor(ctx, twofa); err != nil {// (E) UPDATE … AllCols where id=?
```
Step (E) is plain db.GetEngine(ctx).ID(t.ID).AllCols().Update(t) (models/auth/twofactor.go:128-131) — no row lock, no WHERE last_used_passcode = <previous> predicate, and no DB uniqueness on (uid, last_used_passcode). The model definition at models/auth/twofactor.go:48-57 shows LastUsedPasscode string is a plain column — no constraint, no version field.
routers/web/auth/password.go:179-196 shows the identical pattern in the password-reset flow:
```go
179 passcode := ctx.FormString("passcode")
180 ok, err := twofa.ValidateTOTP(passcode)
...
185 if !ok || twofa.LastUsedPasscode == passcode { // same check-against-in-memory pattern
...
192 twofa.LastUsedPasscode = passcode
193 if err = auth.UpdateTwoFactor(ctx, twofa); err != nil {
```
Same shape, same race window.
services/auth/basic.go:170-185:
```go
func validateTOTP(req *http.Request, u *user_model.User) error {
twofa, err := auth_model.GetTwoFactorByUID(req.Context(), u.ID)
...
if ok, err := twofa.ValidateTOTP(req.Header.Get("X-Gitea-OTP")); err != nil { // :179
return err
} else if !ok {
return util.NewInvalidArgumentErrorf("invalid provided OTP")
}
return nil
}
```
LastUsedPasscode is neither read nor written on this path. The same six-digit code in X-Gitea-OTP succeeds for the full totp.Validate acceptance window on every request.
Gitea's loginAttempts counter increments on failed sign-ins. A successful replay is a success — the counter is never touched, and two parallel successes produce two access tokens with no anomaly logged at the auth layer.
Inside TwoFactorPost, the critical region between (A) GetTwoFactorByUID and (E) UpdateTwoFactor covers:
1. one DB SELECT round-trip,
2. base64-decode + AES-decrypt of the secret (models/auth/twofactor.go:108-118),
3. totp.Validate (HMAC-SHA1 over the secret + time-step),
4. user_model.GetUserByID (a second SELECT),
5. optional linkAccountFromContext / OpenID link branch,
6. the assignment + UPDATE.
On a non-CPU-bound deployment this window is a few milliseconds on the fast path, tens of ms when linkAccount / OpenID branches are taken. An attacker who already holds both factors and can submit two POST /user/two_factor requests in parallel (HTTP/2 multiplexing, or two backgrounded curls) hits the race reliably — both goroutines enter step (C) with the same stale LastUsedPasscode, both reach step (D), both write the new value ba
This issue can be reached over the network, attack complexity is low, an attacker needs no privileges on the target. A user must be tricked into taking some action. The scope is unchanged, so the impact stays within the vulnerable component. Rated impact: confidentiality high, integrity low, availability none.
The score comes from this vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:L/A:N
CVE-2026-20779 is classified as CWE-294: Authentication Bypass by Capture-replay. A captured authentication exchange can be replayed later because it is not bound to a single use.
CVE-2026-20779 is recorded against 2 packages.
Published on 22 July 2026. No public exploit is currently recorded for this entry. Record sourced from NVD.
github.com (Advisory)
nvd.nist.gov (Advisory)
blog.gitea.com (Web)
github.com (Web)
github.com (Web)
github.com (Web)
code.gitea.io/gitea has other advisories on record. If you are patching this one, these are worth checking on the same host:
These advisories are the same class of weakness (CWE-294: Authentication Bypass by Capture-replay) in other software:
Details
CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:L/A:N
Affected Packages
| Software | From version | Fixed in |
|---|---|---|
| code.gitea.io/gitea | — | — |
| unknown | — | — |
References
Similar Threats
Site Security Check
CVE-2026-20779 is rated CVSS 8.0 High. BotEraser scans your installation against known CVE records and tells you whether this vulnerability applies to the versions you actually run.
Scan My Site Free →No credit card required · Results in minutes
ⓘ Data Notice: The information presented above has been compiled from publicly available internet sources. Boteraser aggregates this data solely for informational purposes and does not independently classify, evaluate, or endorse any findings about the vulnerabilities listed. The accuracy and completeness of this information is the sole responsibility of the original publishers. Boteraser and its operators accept no liability for any decisions made based on this data.
Stay up to date with the latest from Boteraser.
We use cookies to improve your experience on our site. By using our site, you consent to cookies.
Manage your cookie preferences below:
Essential cookies enable basic functions and are necessary for the proper function of the website.
CloudFlare provides web performance and security solutions, enhancing site speed and protecting against threats.
Service URL: developers.cloudflare.com (opens in a new window)
These cookies are needed for adding comments on this website.
These cookies are used for managing login functionality on this website.
Statistics cookies collect information anonymously. This information helps us understand how visitors use our website.
Google Analytics is a powerful tool that tracks and analyzes website traffic for informed marketing decisions.
Service URL: policies.google.com (opens in a new window)
You can find more information in our Cookie Policy and Privacy Policy.