gogcli icon indicating copy to clipboard operation
gogcli copied to clipboard

auth: use OIDC sub claim as stable account key, not email

Open Neelagiri65 opened this issue 7 months ago • 2 comments

When a user renames their Gmail address (Google enabled this on 31 March 2026), gogcli loses the link to the existing local account. Every data structure in the auth layer keys on email. The OIDC spec calls email mutable and specifies sub as the stable identifier.

What happens today

internal/googleauth/accounts_server.go:585 parses the ID token but reads only email:

var claims struct {
    Email string `json:"email"`
}

Downstream, email is the primary key throughout:

  • internal/config/config.go:17: AccountAliases map[string]string keys on email
  • internal/config/config.go:18: AccountClients map[string]string keys on email
  • internal/cmd/auth_add.go:251: normalizeEmail(authorizedEmail) != normalizeEmail(c.Email) rejects the token when the authorised email doesn't match the CLI arg
  • internal/cmd/auth_add.go:265: keyring entry stored as store.SetToken(client, authorizedEmail, …)
  • secrets.Token has an Email field, no Sub field

What breaks on rename

  1. The keyring entry under [email protected] is orphaned. gog auth add [email protected] creates a second entry, not an update.
  2. account_aliases and account_clients entries for the old email stay behind unless the user remembers to gog auth remove [email protected] first.
  3. Scripts and agent invocations hardcoding the old email silently target a stale or missing account.
  4. The line 251 check surfaces as authorized as X, expected Y rather than "looks like a rename, migrate?".

Minimal fix

Add sub to the claim struct, store it in secrets.Token, use sub as the canonical key in keyring and config. Email becomes a display label.

var claims struct {
    Sub   string `json:"sub"`
    Email string `json:"email"`
}

When a refresh returns an ID token whose sub matches an existing stored account but whose email differs, treat it as a rename: migrate the keyring entry, update account_aliases and account_clients to the new email, log a one-line notice. Existing email-keyed configs need a one-shot migration on first run after upgrade (detect by presence of Email without Sub, re-fetch the ID token, backfill).

The OIDC spec: "The sub Claim ... is never reassigned to another End-User." Google's identity docs call out sub as the stable ID: https://developers.google.com/identity/openid-connect/openid-connect#authenticationuriparameters

Prior art

ztnet landed the equivalent fix two days ago: https://github.com/sinamics/ztnet/pull/884. Their sign-in callback now looks up by provider+subject first, email second. Worth a read as a reference pattern; the Go translation is cleaner because you already have client as the per-account namespace.

Detection

The pattern is one of several flagged by authdrift, a Semgrep ruleset I wrote while auditing 2M+ repos on Sourcegraph for this specific bug. Breakdown of findings: https://neelagiri65.github.io/gmail-oauth-research/. 124 affected repos across Passport.js, NextAuth, Python/Django and Ruby/OmniAuth. Go isn't covered yet because the scan targeted web frameworks, not CLIs. gogcli is the first Go entry I've seen with this exact shape.

I can open a PR if this direction works.

Neelagiri65 avatar Apr 16 '26 00:04 Neelagiri65

Fixed on main in 532601a + follow-up 8addfce.

What changed:

  • OAuth tokens now persist the OIDC sub claim alongside email.
  • Subject-keyed token copies are the canonical stable key; email-keyed entries stay as compatibility aliases.
  • auth add / auth-manage migrate renamed Google accounts when the same sub appears with a new email.
  • Normal API refresh now backfills subject for old stored tokens and migrates renamed emails without deleting the subject-keyed canonical copy.
  • auth list --json and token export/import include subject.

Verification:

  • go test ./internal/googleapi ./internal/googleauth ./internal/secrets ./internal/cmd -run 'TestPersistingTokenSource|TestTokenSourceForAccountScopes|TestEmailFromIDToken|TestKeyringStore|TestMigrateStoredSubjectIdentity|TestAuthAdd|TestExecute_AuthAdd' -count=1
  • make lint
  • make test
  • make ci
  • Live: gog --json --account [email protected] people me succeeded, then gog --json auth list showed subject: 111833477811442819246 backfilled for [email protected].

steipete avatar Apr 28 '26 03:04 steipete

This is a really thorough fix .. subject keyd canonical with email as compatibility alias is exactly the right architecture. Thanks for taking this seriously and shipping it this fast.

Neelagiri65 avatar May 01 '26 00:05 Neelagiri65