auth: a 429 from the Token Endpoint during refresh deletes a still-valid login

Aperta Adatta ai principianti
#2,869 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@linye157 ci sta già lavorando.

Dal 30/10/2026.

  • #2875 di @linye157 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Inizia da internal/auth/uat_client.go e leggi refreshOnce() per vedere come lo stato della risposta e gli errori di refresh determinano se il token memorizzato viene mantenuto o cancellato. Poi esamina TestRefreshFailureDeterminesStoredTokenDisposition e il client di refresh con risposte predefinite per capire come vengono rappresentati gli stati delle risposte. Il lavoro è completato quando le risposte HTTP 429 vengono ritentate mantenendo il token memorizzato, con un test di regressione che verifichi questo comportamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug domain/auth domain/im

Summary

When the Token Endpoint answers a user-token refresh with HTTP 429, lark-cli deletes the stored login, even though a rate-limited request was never processed and the refresh token is still valid. The user then has to run auth login again.

We hit this twice in a week on a long-running integration that makes about 10 refreshes a day (one refresh every 2 hours of use). Single, non-concurrent refreshes got two 429 responses in a row, and the token was cleared each time.

Evidence (from ~/.lark-cli/logs/auth-*.log)

time=2026-10-28T10:22:16+01:00 path=/oauth/v3/token status=429 cmdline=lark-cli im +chat-list ...
time=2026-10-28T10:22:17+01:00 path=/oauth/v3/token status=429 cmdline=lark-cli im +chat-list ...

After these two lines, lark-cli auth status reports User identity: missing (no token in keychain ...) and every user call returns token_missing / need_user_authorization. The same happened on 2026-10-22 (two 429s, token cleared). On other days a single 429 was followed by a 200 on the retry, and the login survived.

Cause

internal/auth/uat_client.go, refreshOnce() (v1.0.98) never checks resp.StatusCode:

  1. A 429 with a non-JSON body (for example plain-text rate-limit text) fails json.Unmarshal, which returns refreshRetryAndClear.
  2. A 429 with a JSON body and an unregistered code goes through refreshActionForCode(), which returns refreshStopAndClear for unknown codes.

With refreshMaxAttempts = 2, two 429s in a row end in compareAndDeleteStoredToken().

Suggested fix

Treat HTTP 429 as "not processed": retry and preserve the stored token.

// after reading the body, before json.Unmarshal
if resp.StatusCode == http.StatusTooManyRequests {
    return refreshResult{
        action: refreshRetryAndPreserve,
        err: errs.NewNetworkError(errs.SubtypeNetworkTransport,
            "token refresh rate limited (HTTP 429); the stored token was preserved").
            WithRetryable(),
    }
}

And a case for TestRefreshFailureDeterminesStoredTokenDisposition (plus an optional status int field on refreshHTTPTestStep, applied in scriptedRefreshClient):

{
    name: "rate limited (HTTP 429) responses preserve after retry",
    steps: []refreshHTTPTestStep{
        {status: http.StatusTooManyRequests, body: `rate limit exceeded`},
        {status: http.StatusTooManyRequests, body: `{"code":99991400,"msg":"request trigger frequency limit"}`},
    },
    wantCalls:     2,
    wantPreserved: true,
    wantCategory:  errs.CategoryNetwork,
    wantSubtype:   errs.SubtypeNetworkTransport,
    wantRetryable: true,
},

With the current code this case fails (the token is cleared). With the fix it passes, and go test ./internal/auth/... stays green. A backoff before the second attempt would also help, since our two 429s arrived one second apart.

Environment

  • lark-cli 1.0.98 (npm), macOS, brand lark
  • User identity (UAT), default DPoP mode (preferred)
Lingua principale
Go
Stelle
18.2k
Fork
1.4k
Merge medio
1g 3h
PR unite (30g)
18

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di larksuite/cli

Tutte le issue di larksuite/cli

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.