Advisory

GHSA-gx3v-q759-g323

Details, part 2 of 2. The advisory

Details

Defect 2 — Basic-Auth API replay (no race needed)

# Attacker captured Alice's password + one live OTP (654321).
# Within the RFC 6238 window (~60–90 s):
curl -u "alice:<known-password>" \
     -H "X-Gitea-OTP: 654321" \
     https://gitea.example.com/api/v1/user
# → 200 OK. Repeat as many times as the time-step allows.

Each call succeeds. An attacker can mint a personal access token via POST /api/v1/users/{username}/tokens inside that window for long-lived access that outlives the captured OTP.

Impact

  • Defect 1 (Web TOCTOU). Narrow exploit window but completely deterministic on parallel submission. The victim's own legitimate login is itself the trigger — no second observation of the OTP is needed if the attacker can race the victim's submission. Net effect: two authenticated sessions for one OTP, defeating RFC 6238 §5.2 in the multi-session case.
  • Defect 2 (Basic-Auth stateless replay). The more serious of the two. Any captured OTP value remains valid on the API / git-clone basic-auth surface for the full totp.Validate window. An attacker who AiTM-relays one login can carve out 60–90 s of unattended API access during which they can mint a personal access token and persist past the OTP window. This surface specifically attracts attackers because (a) it is non-interactive (a script can hammer it), and (b) PAT minting via /api/v1/users/{username}/tokens does not require a second 2FA prompt once basic-auth + OTP have succeeded.
  • Successful-replay invisibility. Gitea's failed-login counter increments on FailedLoginException; a successful replay never throws. The audit log records two successful 2FA authentications for the same principal at near-identical timestamps — most SIEM rules will not flag this.

Conditions for exploit

| Required | Detail |
|----------|--------|
| Network reach to Gitea HTTP listener | Trivially available |
| Valid victim password | Credential dump / phishing relay / separate vuln |
| One captured OTP value within ~90 s | AiTM, infostealer log, shoulder-surf, MITM, malicious extension |
| Ability to fire two parallel HTTP requests | Trivial (curl -P 2, xargs -P 2, HTTP/2 multiplexing) — Defect 1 only |

No special role / permission required on Gitea. Both defects are exploitable from any unauthenticated network position that can reach the listener.

Suggested remediation

Two distinct fixes are needed; option (c) collapses both into one place and is the recommended path.

(a) Race fix — compare-and-swap on UPDATE (Defect 1):

// models/auth/twofactor.go
func UpdateTwoFactorCAS(ctx context.Context, t *TwoFactor, prevPasscode string) (bool, error) {
    n, err := db.GetEngine(ctx).ID(t.ID).
        Where("last_used_passcode = ?", prevPasscode).
        AllCols().Update(t)
    return n == 1, err
}

Each handler captures prev := twofa.LastUsedPasscode before mutating, calls UpdateTwoFactorCAS(ctx, twofa, prev), and rejects the request if n != 1. Fixes both web sites with no extra lock contention. A row-level lock (SELECT … FOR UPDATE inside a db.WithTx) is an equivalent surgical option. Equivalent atomicity can also be obtained by a unique index on (twofa_id, last_used_passcode) so a duplicate UPDATE collides at the DB layer.

(b) Basic-Auth fix (Defect 2):

Wrap the twofa.ValidateTOTP(...) call at services/auth/basic.go:179 in the same inequality check + update pattern used in routers/web/auth/2fa.go:68,84-85, ideally via the CAS helper above so the basic-auth path can't reintroduce the race either.

(c) Preferred — store the accepted time-step counter, route every call site through one consume helper:

Replace LastUsedPasscode string with LastTotpStep int64. Derive the matching step inside TwoFactor.ValidateTOTP (skew-aware) and CAS on the step value:

func (t *TwoFactor) ValidateAndConsumeTOTP(ctx context.Context, passcode string) (bool, error) {
    step, ok, err := validateAndReturnStep(passcode, t.Secret) // skew-aware
    if err != nil || !ok { return false, err }

    n, err := db.GetEngine(ctx).Table("two_factor").
        Where("id = ? AND last_totp_step < ?", t.ID, step).
        Cols("last_totp_step").
        Update(map[string]any{"last_totp_step": step})
    if err != nil || n == 0 { return false, err }   // already consumed — replay refused
    t.LastTotpStep = step
    return true, nil
}

All three call sites (routers/web/auth/2fa.go, routers/web/auth/password.go, services/auth/basic.go) then go through this single function and cannot accidentally skip the consume step. Same fix shape as django-otp (last_t) and Authentik (authentik/stages/authenticator_totp/models.py:184). Recommended option because it makes the defect impossible to reintroduce at a future call site.

A schema migration is required for (c); (a)+(b) is the surgical minimum.

References

  • RFC 6238 §5.2 — TOTP one-time use: https://datatracker.ietf.org/doc/html/rfc6238#section-5.2
  • CWE-294 — Authentication Bypass by Capture-replay: https://cwe.mitre.org/data/definitions/294.html
  • CWE-367 — Time-of-check Time-of-use (TOCTOU) Race Condition: https://cwe.mitre.org/data/definitions/367.html
  • Original Gitea fix this report builds on — CVE-2021-45331 / PR #3878 (introduced LastUsedPasscode): https://github.com/go-gitea/gitea/pull/3878

Sources & licences