Skip to content

auth: add versioned secure-storage format for credentials [3/10] - #22938

Merged
sergiou87 merged 2 commits into
developmentfrom
short-lived-tokens-3-storage
Oct 2, 2026
Merged

sergiou87 merged 2 commits into
developmentfrom
short-lived-tokens-3-storage

Conversation

@sergiou87

@sergiou87 sergiou87 commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Description

Adds the secure-storage format for rotating credentials on its own, before anything writes them.

  • A credential with no refresh token is stored as the bare access token, the format every install already uses. No migration is needed, and older Desktop versions can still read it.
  • A rotating pair is stored as github-desktop-oauth:{"version":1,"credential":{...}}: access token, refresh token and both expiry times in one secure-store item.
  • A malformed record throws a fixed error that never includes the stored contents.
  • On load, AccountsStore deletes records that hold no usable credential and drops those accounts. Records it can't parse are handled like any other secure-storage read error: logged, reported, and the account is skipped.
  • addAccount writes through the serializer, which gives the same value as today for plain tokens.

No behavior change for existing accounts.

Stack

Merge in order, starting with 1. Each PR builds, passes tests, and keeps production behavior on its own.

  1. auth: add OAuth refresh-token protocol primitives [1/10] #22917 - OAuth protocol primitives
  2. auth: handle rejected API tokens in AccountsStore [2/10] #22937 - Token invalidation in AccountsStore
  3. auth: add versioned secure-storage format for credentials [3/10] #22938 - Versioned credential storage (this PR)
  4. auth: coordinate OAuth token renewal in AccountsStore [4/10] #22939 - Coordinated token renewal
  5. auth: hand Git fresh credentials [5/10] #22940 - Git credentials
  6. auth: resolve private image tokens through the renderer [6/10] #22941 - Private images
  7. auth: let Copilot sessions renew GitHub tokens [7/10] #22942 - Copilot SDK sessions
  8. auth: request refreshable OAuth tokens at sign-in [8/10] #22943 - Sign-in with refreshable tokens (behind flag)
  9. docs: describe refresh-token rollout and recovery [9/10] #22919 - Rollout and recovery documentation
  10. auth: log automatic sign-outs [10/10] #22959 - Log automatic sign-outs

Validation

  • tsc and lint pass.
  • Full suite: 1,932 pass, one skipped, same four Git failures.

Release notes

Notes: no-notes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Versioned records currently accept token values that the OAuth parser considers malformed.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds backward-compatible, versioned secure storage for OAuth credentials.

Changes:

  • Serializes legacy and rotating credentials.
  • Loads credentials and removes invalidated-account markers.
  • Tests persistence, corruption handling, and cleanup.
File Description
app/​src/​lib/​account-credential.ts Implements credential serialization and validation.
app/​src/​lib/​stores/​accounts-store.ts Integrates credential persistence and loading.
app/​test/​unit/​account-token-lifecycle-test.ts Tests credential storage behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread app/src/lib/account-credential.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The storage prefix can collide with valid opaque legacy tokens, preventing them from loading.

Review effort: Balanced
Findings: None

Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Use a sentinel outside the valid token grammar

app/​src/​lib/​account-credential.ts:3

The sentinel is itself a valid token according to isToken (it is non-empty and contains no whitespace). As a result, a plain opaque token beginning with github-desktop-oauth: is serialized verbatim but then parsed as a versioned record, so it cannot round-trip and an existing account using such a token will be skipped at startup. Use a sentinel outside the accepted token grammar (for example, one containing whitespace) and update the format tests accordingly.

@sergiou87

Copy link
Copy Markdown
Member Author

According to Copilot (which seems correct AFAICT):

Re the sentinel collision note on account-credential.ts: leaving it as is on purpose. GitHub and GHES tokens only use [A-Za-z0-9_] (classic 40-hex, gho_/ghu_/ghp_/ghs_/ghr_, github_pat_), so no real token can contain :, let alone start with github-desktop-oauth:. Only a forged OAuth response could produce one, and at that point the server has far worse options. A whitespace sentinel would make every stored value odd with no practical gain.

@sergiou87
sergiou87 marked this pull request as ready for review September 28, 2026 13:52
@sergiou87
sergiou87 requested a review from a team as a code owner September 28, 2026 13:52
@sergiou87
sergiou87 requested a review from niik September 28, 2026 13:52
@sergiou87
sergiou87 force-pushed the short-lived-tokens-3-storage branch from 220f345 to 3185d53 Compare October 2, 2026 11:28

@niik niik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The versioned credential format is well scoped: rotating token pairs are stored atomically, legacy bare-token compatibility is preserved, malformed records fail with secret-safe errors, and token/expiry validation is appropriately strict.

We reviewed the startup case where cleaning up one unusable credential can persist an account list that excludes another account whose keychain read failed. The consequence is reauthentication rather than repository or server-side data loss, and we consider that an acceptable recovery tradeoff for this layer. The remaining observations around repeated corrupted-record errors, explicit undefined expiry properties, and expiry-only non-refreshable tokens are non-blocking.

Base automatically changed from short-lived-tokens-2-invalidation to development October 2, 2026 11:42
sergiou87 and others added 2 commits October 2, 2026 13:42
Short-lived OAuth tokens come with a refresh token and expiry times,
and all of it has to live in the OS secure store: the refresh token is
a secret, and splitting the pair across items could leave a new access
token next to an old refresh token. Introduce the storage format on its
own, before anything writes rotating credentials, so it can be
reviewed in isolation.

`account-credential.ts` serializes a credential as:

- the bare access token when there is no refresh token. This is the
  format every existing install already uses, so plain tokens keep
  working, need no migration, and stay readable by older Desktop
  versions;
- `github-desktop-oauth:` followed by
  `{"version":1,"credential":{...}}` for a rotating pair, holding the
  access token, refresh token and both expiry times in one item.

Reading accepts both formats. A versioned record whose credential is
`null` means "no usable credential". A malformed or unsupported record
throws a fixed error that never includes the stored contents, so
secrets can't leak into logs or error dialogs.

AccountsStore reads secure storage through this parser:

- plain tokens load exactly as before;
- a record with no usable credential (including an empty value) is
  deleted, and the account is dropped from the saved account list;
- a record that can't be parsed is treated like any other secure
  storage read error: logged, reported, and the account is skipped.

`addAccount` writes through `serializeAccountCredential`, which for
today's plain tokens produces the same value as before. Nothing writes
rotating pairs yet, so this change has no behavior change for existing
accounts.

Tests cover reading plain tokens, round-tripping a rotating pair and
the `null` marker, rejecting corrupted records without exposing
secrets, and removing accounts whose stored credential is the `null`
marker.

Co-authored-by: Copilot <[email protected]>
Versioned secure-store records (`github-desktop-oauth:` + JSON) were
validated more loosely than the OAuth responses they come from.
`deserializeAccountCredential` only required `accessToken` and
`refreshToken` to be non-empty strings, while `parseOAuthToken` also
rejects any value containing whitespace. A record such as
`{"accessToken":" ","refreshToken":"secret"}` was therefore loaded as
a usable credential instead of producing the fixed "Unable to read
stored OAuth credentials. Please sign in again." error.

Desktop only ever writes versioned records with tokens that already
passed `parseOAuthToken`, so this can only happen through external
tampering or corruption of the secure store. The practical impact was
small: the malformed token would be sent, rejected with a 401 and the
account signed out. With this change the record is instead reported
as unreadable at load time and the account is skipped, which is the
behavior the storage layer already promises for every other malformed
record.

Changes:

- `oauth-token.ts`: export the existing `isToken` guard (non-empty
  string without whitespace) with a JSDoc comment so both parsers
  share one definition of a valid token value.
- `account-credential.ts`: use `isToken` for both `accessToken` and
  `refreshToken` in versioned records instead of the ad-hoc
  `typeof === 'string' && length > 0` checks.
- `account-token-lifecycle-test.ts`: add a whitespace-only access
  token and an embedded-whitespace refresh token to the "rejects
  corrupted or unsupported records without exposing secrets" cases.
  The test fails without the source change and passes with it.

Legacy bare-token records are intentionally left unvalidated. That is
the format every existing install uses and matches the `development`
behavior this stack preserves.

Addresses review feedback on #22938.

Co-authored-by: Copilot <[email protected]>
@sergiou87
sergiou87 force-pushed the short-lived-tokens-3-storage branch from 3185d53 to bd48d34 Compare October 2, 2026 11:42
@sergiou87 sergiou87 changed the title auth: add versioned secure-storage format for credentials [3/9] auth: add versioned secure-storage format for credentials [3/10] Oct 2, 2026
@sergiou87
sergiou87 merged commit 3754e26 into development Oct 2, 2026
22 of 23 checks passed
@sergiou87
sergiou87 deleted the short-lived-tokens-3-storage branch October 2, 2026 12:18
niik added a commit that referenced this pull request Oct 7, 2026
Brings in short-lived token support (#22917, #22937, #22938, #22939) and
adapts it to multiple accounts per endpoint:

- Credential sessions are keyed per account (endpoint + login) instead of
  per endpoint, so renewal, invalidation and storage writes are isolated.
- AccountsStore lookups, retirement and refresh match the exact account.
- The invalidated token dialog names the signed-out login and offers a
  targeted sign-in for that account.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants