Files
magpie/internal/provider/codex_expired_test.go
T
yetoneandggbdpq 3b8bcc3654 provider: a ChatGPT token the backend calls expired before its exp is refreshed once and read again (willz on Discord, #2973)
willz on Discord (macOS, v0.1.1330): two saved Team Codex accounts showed
only "Unauthorized". /backend-api/wham/usage answered 401 "authentication
token has expired. Please try refreshing it." while each access token's
JWT exp was still ahead, so codexToken and savedLoginToken handed the
same token back on every read, and on every request, until the account
was signed in again. #2973 is the same 401 on the request path.

What changed
- SignInExpired (codex_expired.go) tells such a 401 by its words:
  token_expired, "authentication token is/has expired", "Please try
  refreshing it". It never matches refresh_token_expired, which is a
  revoked sign-in. accountJSON now keeps the first 4KB of a refusal's
  body on accountStatusError for it.
- Refreshes are keyed on the token that was turned away:
  codexTokenRenewing(dir, stale) for Codex's own sign-in and
  renewSavedLoginFrom(user, stale) for a saved one. Under the lock that
  signing waits on, each reads its store afresh. If the store holds
  another token by then (renewed by Codex or by another reply), that
  token is used and no refresh is spent, so two replies for one token
  spend one rotation. Otherwise they refresh. Codex's own sign-in is
  written back through codexStore.write (temp and rename, every other
  field kept). A saved one goes through writeLogins and only its own
  row is changed; nothing is matched by name.
- The usage read (codexWindowsRenewing) refreshes once on such a 401 and
  reads once more, never again. It is used by loginQuota (saved and
  current accounts), codexSubscriptionUsage, and the reset spenders
  through codexUserWindows (autoreset, expiring, stillExpiring), which
  read the same windows with the same tokens.
  - A refresh that is refused shows its own words on the card: a saved
    account is marked lapsed, "add the account again to use it"
    (savedRefreshFailed); Codex's own says "run codex login".
  - A token that was refreshed and is still called expired says "sign in
    to this account again" (errCodexStillExpired).
  - A refresh that got no answer is shown as that, not as signed out.
- Request path: noteSignIn renews the account a request went with
  (NoteExpiredSignIn, at most 20s, through Account.renew, which is set
  on Codex's own account and each saved one). Nothing is marked. The
  request fails over as a 401 did before, and the next one goes with
  the new token.
- Docs: providers-accounts.md ("A token called expired") and
  gateway-routing.md.

The request-path hook and its gateway tests (codex_refresh_401_test.go,
taken as written) are ggbdpq's patch on #2973. Here they rest on the
stale-keyed refresh. The patch's forced refresh skipped the fresh
keychain read, so it could spend a refresh token Codex had already
rotated.

Tests
- TestCodexUsageRenewsTokenCalledExpired: a saved and a current account,
  both with an hour left, get 401 token_expired. Each is refreshed once,
  read once more, and kept where it lives (auth.json in the sandbox
  HOME, logins.json). A second read refreshes nothing.
- TestCodexUsageExpiredOnce: a refused refresh says to sign in again; a
  token still called expired is read no third time.
- TestCodexUsageOther401NotRenewed, TestSignInExpiredWords,
  TestNoteExpiredSignIn (two replies for one token make one refresh).
- ggbdpq's TestCodexExpired401RefreshesLogin and
  TestCodexOther401DoesntRefresh.

Run at 482d6707a (this change) (rebased on 8ef9fc578):
- gofmt -l internal clean; go vet ./... (darwin), GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./... clean
- set -o pipefail; go test -tags nogui -timeout 45m -count=1 ./... in a
  sandbox HOME, log teed: every package ok (69 ok, no FAIL), provider
  221.1s, gateway 574.4s, agent 434.4s. No other full suite was running
  (another session's gateway-only run was).
Broken by hand: with codexWindowsRenewing's retry and NoteExpiredSignIn
turned off (the old behaviour), TestCodexUsageRenewsTokenCalledExpired,
TestCodexUsageExpiredOnce, TestNoteExpiredSignIn and
TestCodexExpired401RefreshesLogin are red. The card read "Unauthorized",
the reporter's words, and the gateway test's sign-in was "renewed 0
times, want once". The new tests passed under -race.
Not tried:
- the real ChatGPT backend or a real Team account; the live ~/.codex and
  logins.json are off limits to this session;
- a keychain-stored Codex sign-in (the fresh keychain read is the
  existing codexToken path, run here only through file stores);
- Windows and Linux beyond go vet.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-23 18:15:15 +08:00

237 lines
8.4 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
)
// expiredBody is the backend's 401 for an access token it calls expired
// while the token's own exp is an hour ahead (willz on Discord, #2973).
const expiredBody = `{"error":{"message":"Provided authentication token is expired. Please try refreshing it.","type":null,"code":"token_expired","param":null},"status":401}`
// codexExpiredFakes signs in me@ (saved) and work@ (Codex's own), both
// with an hour left, and points usage and the token refresh at fakes.
// usage answers each token as refused says; refresh answers each
// refresh token with access token "fresh-<refresh token>", or as
// refuseRefresh says. It returns the refresh tokens sent and the usage
// reads made, by account and token.
func codexExpiredFakes(t *testing.T, refused func(tok string) (int, string), refuseRefresh bool) (home string, refreshes func() []string, reads func() map[string]int) {
t.Helper()
home = signIn(t)
rememberLogins(true)
codexSignIn(t, home, "work@example.com", "r-work")
rememberLogins(true)
loginUsageCache.Lock()
loginUsageCache.m = nil
loginUsageCache.Unlock()
var mu sync.Mutex
var sent []string
seen := map[string]int{}
usage := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/backend-api/wham/usage" {
w.WriteHeader(404)
return
}
tok := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
mu.Lock()
seen[r.Header.Get("chatgpt-account-id")+" "+tok]++
mu.Unlock()
if code, body := refused(tok); code != 0 {
w.WriteHeader(code)
w.Write([]byte(body))
return
}
json.NewEncoder(w).Encode(map[string]any{"plan_type": "team", "rate_limit": map[string]any{
"primary_window": map[string]any{"used_percent": 12, "limit_window_seconds": 18000}}})
}))
t.Cleanup(usage.Close)
refresh := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
mu.Lock()
sent = append(sent, body["refresh_token"])
mu.Unlock()
if refuseRefresh {
w.WriteHeader(400)
w.Write([]byte(`{"error":"invalid_grant"}`))
return
}
json.NewEncoder(w).Encode(map[string]any{"access_token": "fresh-" + body["refresh_token"], "refresh_token": body["refresh_token"] + "-2"})
}))
t.Cleanup(refresh.Close)
oldBase, oldURL := CodexBase, codexTokenURL
CodexBase, codexTokenURL = usage.URL+"/backend-api/codex", refresh.URL
t.Cleanup(func() { CodexBase, codexTokenURL = oldBase, oldURL })
refreshes = func() []string {
mu.Lock()
defer mu.Unlock()
return append([]string(nil), sent...)
}
reads = func() map[string]int {
mu.Lock()
defer mu.Unlock()
out := map[string]int{}
for k, v := range seen {
out[k] = v
}
return out
}
return home, refreshes, reads
}
// staleExpired answers every token magpie didn't refresh as expired.
func staleExpired(tok string) (int, string) {
if strings.HasPrefix(tok, "fresh-") {
return 0, ""
}
return http.StatusUnauthorized, expiredBody
}
func TestCodexUsageRenewsTokenCalledExpired(t *testing.T) {
home, refreshes, reads := codexExpiredFakes(t, staleExpired, false)
u := LoginUsage(context.Background(), "codex")
for _, user := range []string{"me@example.com", "work@example.com"} {
q := u[user]
if q.Error != "" || len(q.Windows) != 1 || q.Windows[0].Used != 12 {
t.Fatalf("%s: %+v", user, q)
}
}
got := refreshes()
if len(got) != 2 || !(got[0] == "r" && got[1] == "r-work" || got[0] == "r-work" && got[1] == "r") {
t.Fatalf("refreshed with %q, want r and r-work once each", got)
}
if n := reads(); n["acct-1 fresh-r"] != 1 || n["acct-work@example.com fresh-r-work"] != 1 {
t.Fatalf("reads %v", n)
}
// each renewed where it is kept: the saved one in logins.json, Codex's
// own in its auth.json, every other field as it was
var live codexAuth
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
if live.Tokens.AccessToken != "fresh-r-work" || live.Tokens.RefreshToken != "r-work-2" || live.Tokens.AccountID != "acct-work@example.com" {
t.Fatalf("Codex's own sign-in: %+v", live.Tokens)
}
for _, l := range readLogins() {
if l.User == "me@example.com" && !strings.Contains(string(l.Auth), `"fresh-r"`) {
t.Fatalf("the saved account's refresh wasn't kept: %s", l.Auth)
}
}
// read again: the new tokens go out, with no refresh
loginUsageCache.Lock()
loginUsageCache.m = nil
loginUsageCache.Unlock()
u = LoginUsage(context.Background(), "codex")
if u["me@example.com"].Error != "" || u["work@example.com"].Error != "" || len(refreshes()) != 2 {
t.Fatalf("again: %+v, refreshes %q", u, refreshes())
}
}
// A refresh refused says to sign in again, once; a token still called
// expired after its refresh is read no third time.
func TestCodexUsageExpiredOnce(t *testing.T) {
t.Run("refresh refused", func(t *testing.T) {
_, refreshes, reads := codexExpiredFakes(t, staleExpired, true)
u := LoginUsage(context.Background(), "codex")
if e := u["me@example.com"].Error; !strings.Contains(e, "add the account again") {
t.Fatalf("saved account: %q", e)
}
if e := u["work@example.com"].Error; !strings.Contains(e, "codex login") {
t.Fatalf("Codex's own account: %q", e)
}
if n := len(refreshes()); n != 2 {
t.Fatalf("%d refreshes, want one each", n)
}
for k, n := range reads() {
if n != 1 {
t.Fatalf("%s read %d times", k, n)
}
}
})
t.Run("still expired", func(t *testing.T) {
always := func(string) (int, string) { return http.StatusUnauthorized, expiredBody }
_, refreshes, reads := codexExpiredFakes(t, always, false)
u := LoginUsage(context.Background(), "codex")
for _, user := range []string{"me@example.com", "work@example.com"} {
if e := u[user].Error; e != errCodexStillExpired.Error() {
t.Fatalf("%s: %q", user, e)
}
}
if n := len(refreshes()); n != 2 {
t.Fatalf("%d refreshes, want one each", n)
}
if n := reads(); n["acct-1 fresh-r"] != 1 || n["acct-work@example.com fresh-r-work"] != 1 {
t.Fatalf("reads %v", n)
}
})
}
// Any other 401 isn't the token's age: nothing is refreshed for it.
func TestCodexUsageOther401NotRenewed(t *testing.T) {
other := func(string) (int, string) {
return http.StatusUnauthorized, `{"error":{"message":"Unauthorized","code":"unauthorized"}}`
}
_, refreshes, _ := codexExpiredFakes(t, other, false)
u := LoginUsage(context.Background(), "codex")
if u["me@example.com"].Error != "Unauthorized" || len(refreshes()) != 0 {
t.Fatalf("%+v, refreshes %q", u, refreshes())
}
}
func TestSignInExpiredWords(t *testing.T) {
for body, want := range map[string]bool{
expiredBody: true,
`{"detail":"Your authentication token has expired. Please try signing in again."}`: true,
`{"error":{"code":"refresh_token_expired"}}`: false,
`{"error":{"code":"token_revoked"}}`: false,
`Unauthorized`: false,
} {
if got := SignInExpired(http.StatusUnauthorized, []byte(body)); got != want {
t.Errorf("%s: %v", body, got)
}
}
if SignInExpired(http.StatusForbidden, []byte(expiredBody)) {
t.Error("a 403")
}
}
// The gateway's 401 token expired renews the account the request went
// with: the saved one in logins.json, and only while the token sent is
// the one still saved.
func TestNoteExpiredSignIn(t *testing.T) {
_, refreshes, _ := codexExpiredFakes(t, staleExpired, false)
if err := SetLoginOn("codex", "me@example.com", true); err != nil {
t.Fatal(err)
}
p, err := Find("codex")
if err != nil {
t.Fatal(err)
}
also := p.AlsoOn()
if len(also) != 1 || also[0].Account.User != "me@example.com" {
t.Fatalf("also on: %+v", also)
}
a := also[0].Account
req, _ := http.NewRequest("POST", "http://x", nil)
if err := a.sign(context.Background(), req, nil); err != nil {
t.Fatal(err)
}
stale := req.Header.Get("Authorization")
NoteExpiredSignIn(context.Background(), a, req)
NoteExpiredSignIn(context.Background(), a, req) // a second reply for the same token
if got := refreshes(); len(got) != 1 || got[0] != "r" {
t.Fatalf("refreshed with %q", got)
}
req, _ = http.NewRequest("POST", "http://x", nil)
if err := a.sign(context.Background(), req, nil); err != nil || req.Header.Get("Authorization") != "Bearer fresh-r" || stale == "Bearer fresh-r" {
t.Fatalf("next request signed %q (was %q), %v", req.Header.Get("Authorization"), stale, err)
}
}