You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(auth): list stored environments after a restart with a per-provider index - #146
Stacked on #145 (oidc-discovery); this PR targets that branch so the diff shows only enumeration. The stack is #141, #143, #145, then this. Retarget to main as each merges.
Fix DEVEX-1435: auth status for a PkceAuthProvider listed only the environments in its in-process cache, so in a fresh process it reported none even when credentials were stored in the keychain or file store. GDX works around this by scanning its own cache directory, and cannot drop that code until the engine can answer the question.
The backends cannot enumerate. The system keychain has no list-by-prefix. A directory scan of the file store is ambiguous: <provider>-<env>.json reads the same for provider a with env b-c as for provider a-b with env c, so a scan would attribute one provider's credential to another.
Add EnvironmentIndex, which keeps the list itself as one small entry in the same CredentialStorage as the credentials. That makes listing identical for keyring, file and auto, and it survives a restart. PkceAuthProvider records an environment when it saves a credential and forgets it on logout. It is exported, so a custom provider built on CredentialStorage can use it.
list_environments returns the names the backend really holds a credential for, checked each time, so a name left by a crash, or by a credential deleted outside the CLI, is dropped (and removed from the entry) instead of reported. The index is treated as a hint, not a source of truth.
The entry is stored under (<app>.index, <provider>.index, index), derived from the app and the provider and never from an environment, so no environment name, which can come from a user, can address or overwrite it. The app part matters for the file store: a file beside the credentials (<provider>.index-index.json) could be reached by another provider's credential (provider a, env b.index-index, against index provider a-b), so the entry gets a directory of its own (refined during review). A write failure never fails a login. The entry holds at most 256 names of at most 128 bytes, and a stored document over 256 KiB is not parsed at all, so a damaged one cannot make listing probe or allocate without bound; an unreadable one reads as empty and the next save rewrites it.
Removing a name from the index (after a logout, or when listing finds the credential gone) checks under the index's lock that the credential is still absent, so a login that lands in between is not undone (refined during review).
A credential saved before there was an index is found when its environment is declared to the provider (with_environments): it is probed and, if present, indexed. With nothing declared there is nothing to probe, and a fresh login or any save records it. That is a real limit for existing installs and is documented.
bearer_credential and build_credential move to their own module (a pure move) to keep pkce/mod.rs under the 1000-line cap; it was at 995.
Test plan
cargo fmt --all --check
cargo clippy --all-targets -- -D warnings, and again with --features pkce-auth and --features id-token-verify
cargo test --all-targets, and again with each feature set; cargo test --doc likewise
RUSTDOCFLAGS='-D warnings' cargo doc --no-deps, and again with each feature set
cli-engine/scripts/check-module-size.sh
All pass locally: 909, 1090 and 1131 tests for the three feature sets, no failures.
auth/pkce/enumerate_tests.rs (22 tests) simulates a restart by building a brand-new provider over the same backend, so only what the backend kept carries over. It covers: listing after a restart, logout removing a name (checked on the index itself, since a listing would hide a failed update), saving the same environment repeatedly, a name deleted outside the CLI being dropped, backfill through declared environments and its limit, two providers whose file names collide not seeing each other's environments, two apps sharing a backend, a corrupt or foreign index, an oversized index, a document over the size limit, the index file not being reachable from another provider's credential on the real file backend, a login landing between a probe or logout and the index update, an environment too long to index, an index that cannot be written, Dispatcher::all_statuses end to end, and the real file backend. A model of AutoStorage with a switchable keychain checks that the index and credentials move together when the keychain goes away and returns.
To check the tests can fail, I removed each protection in turn (no record on save, no forget on logout, trusting the index without checking the credential, never dropping a stale name, no backfill, an index not scoped by provider, no size cap, no pre-parse size limit, an unnamespaced index, an unlocked removal, an index failure failing the save). Two mutations at first survived, which showed a real gap: nothing pinned that logout updates the index, because a listing drops stale names and hid it. I added a test that reads the index directly, and it fails when the forget is removed.
Manual verification
Not run against a real keychain. The keychain-or-file behavior is modelled, because the real keychain is not available to tests; the real file backend is exercised directly. Worth confirming on a machine with a keychain that auth status lists an environment after a restart.
Notes for reviewers
The two old tests that asserted the listing was "only the in-process cache" described the bug; they are rewritten to say that an environment is listed because a credential is stored for it, not because a token is in the cache.
An app or provider named as another one's name plus .index would collide with that one's index. This is documented; it seemed better than reserving names.
Two processes saving at the same moment can lose one of the two additions, and the next save of that environment repairs it. Within a process, updates through one EnvironmentIndex are serialized (a PkceAuthProvider has one); separate instances over the same app and provider do not share a lock, which the docs say.
With auto storage, credentials saved to the file while the keychain was down are not loadable once it returns, so they are not listed either; if a keychain save then replaces the file's index, a later outage lists them only if their environments are declared. A real limit of auto mode, not changed here, and documented.
An index entry is not encrypted or authenticated. It holds environment names only, and every name is checked against the credential before it is listed, so a tampered entry can at worst make listing probe up to 256 names, or omit one until the next save.
…der index
`auth status` for a PKCE provider listed only the environments in its in-process
cache, so in a fresh process it reported none even when credentials were stored.
The storage backends cannot enumerate: the system keychain has no list-by-prefix,
and a scan of the credentials directory is ambiguous, since `<provider>-<env>.json`
reads the same for provider `a` with env `b-c` as for provider `a-b` with env `c`.
Add `EnvironmentIndex`, which keeps the list itself as one small entry in the same
`CredentialStorage` as the credentials, so listing behaves identically for the
keychain, the file store and the automatic fallback between them, and survives a
restart. `PkceAuthProvider` records an environment when it saves a credential and
forgets it on logout, and `list_environments` returns the names the backend really
holds a credential for, dropping a stale one (a crash, or a deletion outside the
CLI) instead of reporting it.
- The entry is stored under `<provider>.index`, derived from the provider and not
from any environment, so no environment name can address or overwrite it.
- A failure to write it never fails a login.
- It holds at most 256 names of at most 128 bytes, so a damaged entry cannot make
listing probe an unbounded list; an unreadable one reads as empty and the next
save rewrites it.
- A credential saved before there was an index is found when its environment is
declared (`with_environments`), which is probed and then indexed.
`bearer_credential` and `build_credential` move to their own module to keep
`pkce/mod.rs` under the 1000-line cap.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Critical issues remain: file-path collisions can overwrite indexes or credentials, and a pruning race can remove newly saved credentials from enumeration after restart.
… prune it under its lock
The file backend names a credential `<provider>-<env>.json`, so the index kept as
`<provider>.index-index.json` could be reached by another provider's credential
(provider `a`, env `b.index-index`, against index provider `a-b`): saving the
credential overwrote the index, and an index write could destroy the credential. The
index now lives under its own app key, `<app>.index`, which the file backend turns
into a directory no credential is written to.
Listing probed a credential, found it missing, and then removed its name from the
index as a separate step, so a login that saved and recorded it in between was
undone and the credential dropped out of `auth status` after a restart. The probe
and the removal now happen under the index's lock.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Concurrent index updates, logout/login races, and fallback-index deletion can cause stored credentials to disappear from environment listings after restart or a later keychain outage.
No open issues 🎉
3 previously missed issues
In code that hasn't changed since last review
Separate index instances can overwrite each other's updates
cli-engine/src/auth/env_index.rs:117
Each call to EnvironmentIndex::new creates an independent mutex, so two indexes for the same backend/app/provider can read the same document and overwrite each other's additions even within one process. Both record calls succeed, but one environment disappears after restart, contrary to the documented in-process serialization guarantee. Share synchronization for indexes addressing the same entry, or explicitly require callers to reuse a single shared index and document that separate instances can lose updates.
Saving after recovery can drop index entries for fallback credentials
cli-engine/src/auth/env_index.rs:222
If dev and qa are saved while the keychain is unavailable, saving dev after recovery writes the index to the keychain and deletes its fallback file (AutoStorage::save), while qa's fallback credential remains. On the next outage, a fresh provider without declared environments cannot list qa because the fallback index is gone. Preserve backend-specific index entries for unmigrated credentials, and add a recovery/save/outage regression test.
Concurrent logout can remove a newly saved credential's index entry
cli-engine/src/auth/pkce/enumerate.rs:26
A concurrent logout can still remove a newly saved credential's index entry: logout deletes the credential, a login saves it and completes record, then logout reaches this unconditional forget. The credential remains stored but disappears from listing after restart when its environment is not declared. Check presence under the index lock here too, as the stale-name pruning path does, and add a regression for this logout/login interleaving.
A logout deleted the credential and then removed the environment from the index
unconditionally, so a login that saved and recorded the same environment in between
was undone: the credential stayed stored but dropped out of `auth status` after a
restart. The removal now runs under the index's lock and is skipped when the
credential is stored again, the same way the stale-name pruning already was.
Also documents that separate `EnvironmentIndex` values over the same app and
provider do not share a lock, and what happens to credentials saved to the file
while the keychain was unavailable once the keychain returns.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Round 2, three findings from the review body (no threads):
Separate index instances don't share a lock — true, and by design: the lock lives on the value. PkceAuthProvider builds exactly one per provider, so the provider path is serialized. Rather than add process-global state, the type and docs/auth.md now say outright to share one instance (and that two instances, like two processes, can lose an addition that the next record repairs). Docs only, in 0f6ecf7.
Logout can remove a newly saved credential's index entry — fixed in 0f6ecf7: forget now checks the credential under the index lock and skips the removal if it is stored again. New test a_login_that_lands_between_the_logout_delete_and_the_index_update_is_kept completes a login from inside the delete; it fails with an unconditional forget.
Save after keychain recovery drops the fallback index — a real limit of auto mode, not fixed here: once the keychain is back, file-saved credentials are not loadable at all (AutoStorage::load does not consult the file while the keychain is reachable), so they were already invisible to auth status and to login/token; the index matches what load can return. Declared environments (with_environments) are probed and re-indexed on the next outage, and the behavior is now written down in docs/auth.md. Migrating file credentials into the keychain on recovery would be a separate change to AutoStorage.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The index limits are enforced only after full deserialization, allowing oversized or damaged files to cause excessive memory allocation or terminate the process despite the 256-name cap.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Bound index size before deserializing names
cli-engine/src/auth/env_index.rs:205
The limits are applied only after deserializing the entire Vec<String>. An oversized index therefore still allocates every name (including arbitrarily long ones) on every listing or save before discarding most of them, so a damaged file can cause excessive memory use or terminate the process despite the 256-name cap. Reject oversized serialized documents before deserialization, or use a bounded deserializer.
The 256-name and 128-byte limits were applied after the whole document had been
deserialized, so a damaged or tampered entry still allocated every name in it on each
listing or save before most were discarded. A document over 256 KiB, more than this
type can ever write, is now read as empty without being parsed.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Round 3, one finding from the review body: the size limits ran after deserialization. Fixed in d870d9d: a stored document over 256 KiB (the most this type can write is about 197 KB) is read as empty without being parsed. New test an_index_document_over_the_size_limit_is_not_parsed seeds a valid but oversized document; it fails with the check removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #145 (
oidc-discovery); this PR targets that branch so the diff shows only enumeration. The stack is #141, #143, #145, then this. Retarget tomainas each merges.auth statusfor aPkceAuthProviderlisted only the environments in its in-process cache, so in a fresh process it reported none even when credentials were stored in the keychain or file store. GDX works around this by scanning its own cache directory, and cannot drop that code until the engine can answer the question.<provider>-<env>.jsonreads the same for providerawith envb-cas for providera-bwith envc, so a scan would attribute one provider's credential to another.EnvironmentIndex, which keeps the list itself as one small entry in the sameCredentialStorageas the credentials. That makes listing identical forkeyring,fileandauto, and it survives a restart.PkceAuthProviderrecords an environment when it saves a credential and forgets it on logout. It is exported, so a custom provider built onCredentialStoragecan use it.list_environmentsreturns the names the backend really holds a credential for, checked each time, so a name left by a crash, or by a credential deleted outside the CLI, is dropped (and removed from the entry) instead of reported. The index is treated as a hint, not a source of truth.(<app>.index, <provider>.index, index), derived from the app and the provider and never from an environment, so no environment name, which can come from a user, can address or overwrite it. The app part matters for the file store: a file beside the credentials (<provider>.index-index.json) could be reached by another provider's credential (providera, envb.index-index, against index providera-b), so the entry gets a directory of its own (refined during review). A write failure never fails a login. The entry holds at most 256 names of at most 128 bytes, and a stored document over 256 KiB is not parsed at all, so a damaged one cannot make listing probe or allocate without bound; an unreadable one reads as empty and the next save rewrites it.with_environments): it is probed and, if present, indexed. With nothing declared there is nothing to probe, and a fresh login or any save records it. That is a real limit for existing installs and is documented.bearer_credentialandbuild_credentialmove to their own module (a pure move) to keeppkce/mod.rsunder the 1000-line cap; it was at 995.Test plan
cargo fmt --all --checkcargo clippy --all-targets -- -D warnings, and again with--features pkce-authand--features id-token-verifycargo test --all-targets, and again with each feature set;cargo test --doclikewiseRUSTDOCFLAGS='-D warnings' cargo doc --no-deps, and again with each feature setcli-engine/scripts/check-module-size.shAll pass locally: 909, 1090 and 1131 tests for the three feature sets, no failures.
auth/pkce/enumerate_tests.rs(22 tests) simulates a restart by building a brand-new provider over the same backend, so only what the backend kept carries over. It covers: listing after a restart, logout removing a name (checked on the index itself, since a listing would hide a failed update), saving the same environment repeatedly, a name deleted outside the CLI being dropped, backfill through declared environments and its limit, two providers whose file names collide not seeing each other's environments, two apps sharing a backend, a corrupt or foreign index, an oversized index, a document over the size limit, the index file not being reachable from another provider's credential on the real file backend, a login landing between a probe or logout and the index update, an environment too long to index, an index that cannot be written,Dispatcher::all_statusesend to end, and the real file backend. A model ofAutoStoragewith a switchable keychain checks that the index and credentials move together when the keychain goes away and returns.To check the tests can fail, I removed each protection in turn (no record on save, no forget on logout, trusting the index without checking the credential, never dropping a stale name, no backfill, an index not scoped by provider, no size cap, no pre-parse size limit, an unnamespaced index, an unlocked removal, an index failure failing the save). Two mutations at first survived, which showed a real gap: nothing pinned that logout updates the index, because a listing drops stale names and hid it. I added a test that reads the index directly, and it fails when the forget is removed.
Manual verification
Not run against a real keychain. The keychain-or-file behavior is modelled, because the real keychain is not available to tests; the real file backend is exercised directly. Worth confirming on a machine with a keychain that
auth statuslists an environment after a restart.Notes for reviewers
.indexwould collide with that one's index. This is documented; it seemed better than reserving names.EnvironmentIndexare serialized (aPkceAuthProviderhas one); separate instances over the same app and provider do not share a lock, which the docs say.autostorage, credentials saved to the file while the keychain was down are not loadable once it returns, so they are not listed either; if a keychain save then replaces the file's index, a later outage lists them only if their environments are declared. A real limit ofautomode, not changed here, and documented.