Repository navigation
feat(auth): store a rotated CLI session credential and show the login code - #4833
Conversation
Size Report
Startup median (7 runs, lower is better):
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on 333f2cc. The verdict and the full list are in the summary comment.
|
At 333f2cc the code does what it says, CI is green and there are no conflicts. One point should be settled before a cloud turns rotation on, because it is the case this PR exists for. Every remote CLI command refreshes the stored session, so two commands that start together against one state directory both send the same old credential with Notes for a follow-up issue, not for this PR:
Not verified here: the diff was read and the two concurrency outcomes were confirmed with an in-memory run of the guard; no tests were run. The cloud side was not checked: whether it honors the flag, applies reuse detection or a grace window, or shows the user code on the approval page. |
|
Answering "which is intended": the grace-window contract, not a lock. It is now stated in the PR body and at the refresh request in A cloud that rotates must:
Under 1 and 2, two commands that start together never hold different replacements: one response carries the replacement, the other carries none and writes nothing. So the "earlier saved, later discarded" outcome needs a cloud that issues successive replacements, which the contract forbids. A new test runs two refreshes in flight against a cloud that follows the contract and checks that the one replacement is stored whichever response lands first. Also taken in this push:
Left for a follow-up issue, as you listed: recovery when the write fails or the process exits before the rename; rejecting a refresh response from a redirected origin; the chmod-based failed-write test under root. Cloud side, verified against one implementation: it honours the flag, rotates at most once per hour, answers the replaced credential for 5 minutes without a new one, and revokes the session when a replaced credential arrives after that. |
This comment has been minimized.
This comment has been minimized.
|
At 735ea78 the earlier concurrency question is answered for two refreshes: the documented contract leaves only one process holding a replacement. All checks pass and there are no conflicts. One case inside that same guarantee remains. A second independent pass found that a rotating refresh can overwrite a completed login or recreate a logged-out session (auth-session.ts#L406). The refresh reads the original record at line 397 and passes the guard. A second process then signs in (line 273) or signs out (lines 312–316) before the rename. The refresh publishes the old session with its replacement credential. Later commands then use the old account or workspace, or For #4849, not for this PR:
Not blocking, take or leave: the comment at auth-session.test.ts#L256 implies a release order, but the test passes in either order. The open inline thread on the non-atomic compare-and-swap no longer applies as written and can be resolved; the login and logout case above is what remains of it (#4833 (comment)). Not verified here: no tests were run; the new test was read and traced by hand. Whether the cloud enforces the rotation contract (one replacement, none for a replaced credential, the grace period) lives outside this repository. |
|
Fixed at 0756331, and rebased on main. The login write, the logout removal, and the rotated-credential re-read and write now run under one process lock on the session file ( New test: The three items for #4849 go there. The test comment at auth-session.test.ts is left as is. |
735ea78 to
0756331
Compare
This comment has been minimized.
This comment has been minimized.
|
At 0756331, the rotation compare-and-swap from the earlier review (#4833 (comment)) is fixed: the re-read and write now run under the session lock, and login and logout take the same lock. Five new problems remain in the locking itself. Only the "Body rules" check fails. It grades the PR title and body, not the diff, so it is likely unrelated to these changes. Not verified here: the exact failing rule, because the job log was not read. No conflicts are known. A second independent pass found that login throws on lock timeout after the user approved the device. If a logout holds the lock past the 10-second wait, the approved credentials are discarded and the retry starts a new approval. On base, the same approval completed without a lock wait. On a typed A second independent pass found that a refresh loses the cloud's only replacement credential on lock timeout. The rotation request runs outside the lock, so the store can time out, warn, and return the access token while R1 is never written. The cloud issues R1 once and later rejects R0, so no later refresh recovers it. Keep the returned replacement through a typed acquisition timeout and retry the locked compare and publish without requesting another rotation. If the command must exit at the first timeout, make the save resumable or make the replacement recoverable on the cloud side. A second independent pass found that an owner-masking umask such as 0200 blocks login and logout on an existing session. The lock directory made by process-lock.ts:278 loses owner write, so the owner record at line 286 cannot be written. Base replaced or removed the session under that umask. The new umask test at 0700 writes the raw file and never creates a lock. Set exact private owner permissions on every freshly created lock directory, owner record, and guard file in the shared lock publisher, and test login and logout through the locked entry points under an owner-masking umask. A second independent pass found that logout needs write access even when no session is stored. With a readable but non-writable empty auth directory, base prints "No stored cloud CLI session." and this head fails while creating the guard. A fresh writable state root also now gains an auth directory from what used to be a read-only no-op. Return false when the session is absent before taking any lock, then keep the second existence check and all removal under the lock. A second independent pass found that a lock-release failure can report a completed mutation as failed. If a contender holds the mutation guard while the session-lock holder finishes, process-lock.ts:80-85 and :499-533 reject after the login, logout, or rotation already took effect. The CLI then prints a failure while The earlier inline thread on the non-atomic compare-and-swap no longer applies; the locked re-read and write fix it, so it can be resolved: #4833 (comment). No other open threads were carried over. Evidence limits: no tests were run here. Only the rotation-store lock has a test; that login and logout take the lock was checked by reading the code. Whether the cloud enforces the single-issue rotation contract lives outside this repository. Before merge, these five problems need fixes and the "Body rules" check must pass. |
|
Reply to #4833 (comment), fixed at c721659:
|
This comment has been minimized.
This comment has been minimized.
|
The five findings from the earlier review (0756331) are fixed at c721659, and all 21 checks pass. A second independent pass found four remaining problems. No conflicts are known. Merge needs these four fixed. A second independent pass found that an approved login can still lose its credentials. If another live CLI process holds the session lock past the 60-second wait, A second independent pass found the same loss on refresh at auth-session.ts:450. If the cloud returns replacement R1 once while rotating R0, and the locked re-read times out, the catch warns and leaves R0 on disk. After the grace period the cloud rejects R0, so the user must log in again. The rule: a received replacement must not be dropped on a lock timeout. Retain R1 and retry only the locked re-read and conditional publish on A second independent pass found that the lock is not private under a permissive umask (process-lock.ts:278). With umask 000 and an existing auth directory, the lock directory is created at 0777. The owner record goes through atomic-publish.ts:29-34 without a mode, so it is 0666. Other local users can then write to the lock directory and read its metadata. The rule: every lock directory and owner record this path creates must be owner-only. That includes the record restored on release at process-lock.ts:605-609. Create the directory with owner-only permissions and write owner records at 0600. A second independent pass found that the new release warning can fail a committed command (auth-session.ts:358). If another process holds the lock past the release wait and the caller has closed its stderr pipe, the write to The earlier compare-and-swap thread is fixed at this head: re-read, compare and write now run under Not verified here: no tests were run, and the new logout test's failure on 0756331 was judged from reading the code. Not verified here: the cloud's single-issue rotation contract, which lives outside this repository. After the fixes, please show the lock-timeout cases above: a timed-out login publish that succeeds on retry without a second approval, and a timed-out refresh that keeps R1 on disk. |
|
Reply to #4833 (comment), at 69648bb:
This was review round 4. Further hardening of the lock path is tracked in #4849 and #4903. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on 0be628f. The verdict and the full list are in the summary comment.
This comment has been minimized.
This comment has been minimized.
|
The EPIPE finding from the earlier review (#4833 (comment)) is fixed at dbea97e: both post-commit warning sites now use A second independent pass found that an approved login can be lost when the session lock times out at src/cli/auth-session.ts:278. If another live CLI process holds the lock past the 60-second deadline, acquisition rejects with A second independent pass found that a lock timeout can drop the only rotated refresh credential at src/cli/auth-session.ts:450. R0 is on disk, the cloud returns R1 once, and another process holds the lock past the deadline. The catch warns and drops R1, so R0 stays stored and stops working after the cloud's grace period. This contradicts website/docs/docs/commands.md:2320. The rule: a returned rotated credential must be retained on A second independent pass found that the lock is exposed to other accounts under a permissive umask at process-lock.ts:278. On POSIX with umask 000 and an existing auth directory reached through a symlink, which the CLI admits (src/cli.ts:412-417 and auth-session.ts:342-346,365), the lock directory is created 0777 and the owner record 0666. Initial publication and restoration pass no mode (process-lock.ts:542-546,605-609 and atomic-publish.ts:29-34). Another local account can edit the record that proves ownership, so exclusion is not guaranteed. Base login worked with this layout. The rule: the lock directory must be 0700 and every owner record 0600 at both publication sites. No review thread still applies. These two are fixed at this head and can be resolved: the stderr write in the rotation catch now uses Not verified here: no tests were run, and the new test was judged from code to fail on c721659. The async EPIPE path of |
|
Reply to #4833 (comment), fixed at f4aada0:
Tests: |
f4aada0 to
49dfd5f
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 2 new findings on 49dfd5f. The verdict and the full list are in the summary comment.
This comment has been minimized.
This comment has been minimized.
|
The three findings from the earlier review at dbea97e (#4833 (comment)) are fixed at a9750b5. All 21 checks pass or skip, and none fail. A second independent pass found three new problems, all from waiting on the session lock; no conflicts are known. A second independent pass found that an interactive cloud connect without a stored session can return an expired access token. The user approves and gets a short-lived access token. If a live holder keeps the session lock past that expiry, the head saves the session afterwards and still returns the original token (auth-session.ts:198). The first profile request sends it as a bearer token (cloud-profile.ts:38) and fails authentication. The base used the token right after approval. Keep the locked write, then check the access-token expiry from the approval before returning the token, and get a fresh one from the saved session if it expired during the wait. A second independent pass found the same problem on refresh (auth-session.ts:439). The refresh returns a new credential and a short-lived access token. If the lock is acquired after that token expires, the head saves the credential and returns the expired token to the profile request or remote command. The base used the refresh response at once. After the rotation is saved, check the returned expiry before handing the token to the caller, and refresh again through the rotation-aware path if it expired. A second independent pass found that a refresh during a long lock wait can revoke the pending replacement (auth-session.ts:425). While process A waits to store the only new credential, the old one is still on disk. Process B starts after the cloud's grace period and sends the old credential before taking any lock. With the cloud behavior described in #4833 (comment), this post-grace replay revokes the session. A then writes a credential that is already unusable, and the user must log in again. The base did not opt in to rotation, so this sequence did not exist. Take the same session lock before reading and sending the stored credential, and hold it through any replacement write. Keep device approval polling outside the lock. Not blocking: isProcessLockTimeout is the third inline check of the process_lock_timeout reason (also in packages/host-swift/src/swift-cache.ts:542 and packages/daemon-contracts/src/daemon-registration-owner.ts:563), so a follow-up could export one predicate from @agent-device/host-kit/file and use it at all three sites; take it or leave it. Of the open inline threads, the one on the absorbing stream listener is fixed at this head (#4833 (comment)), and the one on copying the refresh expiry (#4833 (comment)) does not apply because that value is the access-token expiry, not the session expiry, so it can be resolved. Not verified here: no tests were run, and the new tests were judged from the code to fail on 31afdcc. The lock-timeout test builds the error by hand, matching processLockTimeoutError in shape, so the real 60-second timeout path was not exercised. The unbounded wait while a live holder keeps the lock was also not exercised. |
|
Reply to #4833 (comment): these three move to a follow-up PR on #4849, stacked on this branch.
This PR keeps its scope: rotation storage, the login code, and the session-file lock. |
|
This head (a9750b5) now conflicts with main. Please rebase onto main and push; the rebased head gets a delta review. |
This comment has been minimized.
This comment has been minimized.
|
The conflict reported in the earlier review (#4833 (comment)) no longer applies to the code, and the two earlier inline threads are addressed at 1b90bcd. Two defects remain in the session refresh path, and the first one must be fixed before merge. All 7 checks pass. The PR still conflicts with main in website/docs/docs/commands.md and website/docs/docs/error-reasons.md, both generated since #4919 and #4928, so CI has not run on the merged result. A second independent pass found that a read-only session store can no longer refresh against a non-rotating cloud (auth-session.ts:535). Take an unprivileged caller with a valid session in a readable auth directory with mode 0500, where the cloud returns a valid access token and no replacement credential. Main and a9750b5 send the refresh and use the token without writing anything. At this head, lock creation fails first and the caller gets A second independent pass found that a bearer token can expire while the session lock is released (auth-session.ts:415). A cloud connect refreshes its stored session, and the endpoint returns an access token with two seconds left and no rotated credential. The check at auth-session.ts:606 passes. A paused contender holds the mutation guard during release, which can wait five seconds (process-lock.ts:30, :511, :809-821). The same token then reaches the profile request at cloud-profile.ts:38-41 and fails authentication. Main and a9750b5 return a non-rotating response without a lock release. The interactive connect approval has the same gap between its check at auth-session.ts:313 and delivery to the caller. Every path that hands an access token to a caller that spends it should check the token after the final awaited release. If the release used up the remaining lifetime, use the saved session for one bounded refresh and return a typed failure if there is still no live token. Not blocking: the PR body still says "A failed lock or write keeps the access token and warns", but a failed lock now fails the command with For a follow-up issue, not for this PR: the docs in commands.md:2686 and the matching error-reasons.md row say a refresh with neither a token nor the revoked marker fails with Simplicity question: with the session lock now held across read, refresh and write, no lock-respecting writer can change the file under the holder. Could No high-priority thread from other reviewers still applies, and no lower-priority one does either. These two threads can be resolved: #4833 (comment) was fixed in a9750b5 and the delta keeps that fix, and #4833 (comment) is addressed because Not verified here: the new auth-session exchange, refresh and refusal tests were not run, so whether they fail without the change is unconfirmed; a real cloud implementation of the rotation contract was not exercised, although the PR body says one was; and the conflict resolution of the generated docs blocks against the generator from #4919 was not judged, so the rebased head needs a regenerate. |
|
Rebased onto main at c8cffcd. commands.md keeps main's shorter cloud auth bullet plus the session and refresh sentences. error-reasons.md is regenerated. All 643 CLI tests pass. The two findings in #4833 (comment) go to #4849, because this PR is closed for new hardening:
|
1b90bcd to
c8cffcd
Compare
This comment has been minimized.
This comment has been minimized.
A rotating refresh re-read the session file and then wrote it. A login or a logout from another process between the two steps was overwritten: later commands used the old session, or a logout that reported success left a session. The login write, the logout removal, and the re-read and write of a rotated credential now run under one lock on the session file. Device approval polling stays outside the lock. A store that cannot take the lock keeps the access token and warns.
…on lock contention The session lock waits up to 60 s instead of 10 s: a holder keeps it for one file operation and a dead holder is reclaimed, so only a stuck live process makes a contender wait that long, and the change that waits is one the cloud will not repeat. A change that took effect is reported as done when the lock release fails afterwards; the release failure is a warning. Logout returns false before taking the lock when no session is stored, so it needs no write access and creates no auth directory.
…ot be written The warning for a lock release failure is written best effort: a closed stderr fails the write synchronously or with a later EPIPE error event, and neither may turn a login, logout or stored rotation that took effect into a failed command.
… after a stored rotation
…session lock timeout A login after device approval and the store of a rotated credential keep waiting on a typed process_lock_timeout and print a notice, so the cloud's one-time result is never discarded; device approval stays outside the lock, and the locked re-read still lets a logout or another login win. Logout still fails on a timeout, because it loses nothing. The shared process lock creates its directory 0700 and writes owner records, including the record restored on release, at exactly 0600 under any umask.
…cess token live (#4927) * fix(auth): hold the CLI session lock across a refresh and keep its access token live * fix(auth): fail a refresh lock wait typed and keep a login token with its own expiry * test(auth): cover the login exchange's expiry pairing and lock timeout * fix(auth): keep an approved login and refuse a refresh without response text An explicit `auth login` prints its session, not its access token, so a session that is saved already answers the command: the exchange of an already-expired approval token now runs inside the acquisition that saved the session, on the record just written, and only a caller that spends the token itself, such as `connect`, propagates that exchange's failure. A refresh refusal names `cli_session_revoked` and copies no `status` or `error` text out of the cloud's response, which could echo a credential into `--json` output. A session store that cannot be written answers `cli_session_store_unwritable` with the `chmod` hint that names the directory. * fix(auth): name confirmed revocation and keep a login's last saved session A refresh that answers without an access token was labelled `cli_session_revoked`, so an explicit login treated a tokenless answer as proof its saved session was gone. The reason now names only the exact `revoked` marker; the tokenless answer gets `cli_session_refresh_unanswered` and keeps the session. `cloud-response.ts` offers a rejected body to its caller's classifier before the generic status refusal, so a 401 whose body reports revocation is classified as one, with no body text copied. The approval's access-token expiry counts only as a nonempty string, an exchange keeps the next rotation's credential apart from the session it reports, and a transient failure of the optional second exchange keeps the token already answered.
c8cffcd to
18b6276
Compare
Apex Sentinel · ✅ Approve · review of
|
| Command | What it does |
|---|---|
@apex-sentinel-staging note <context> |
Adds context; Sentinel re-checks the cited code |
@apex-sentinel-staging pivot <why> |
Says the finding is wrong; Sentinel re-checks the cited code |
@apex-sentinel-staging like |
Marks the finding as useful |
@apex-sentinel-staging dislike <why> |
Marks it as not useful, so it is not raised again |
On this conversation, comment @apex-sentinel-staging review to run a full review once again.
|
This PR is ready to merge at 18b6276. The two findings from the 1b90bcd review are re-scoped to #4849, and the delta since then is a clean rebase plus a shortened, accurate commands.md bullet. CI is green: 22 checks, 0 not passing at 18b6276, and there are no conflicts. Not verified here: the CLI tests were not run locally, and the lock location and post-release token lifetime were not re-examined (re-scoped to #4849). Not blocking, and fine to take or leave: isProcessLockTimeout compares the reason to the literal 'process_lock_timeout', while PROCESS_LOCK_REASONS.timeout from @agent-device/host-kit/file now owns that reason. |
* origin/main: fix(auth): send a stored CLI session only to the cloud that issued it (#4921) refactor(reasons): declare the daemon reasons in ReasonTable rows (#4936) fix(cli): drop --remote-config from auth and skip reading it for unsupported commands (#4957) refactor(snapshot): deduplicate system-surface projections and warnings (#4959) fix(find): enforce offscreen and non-hittable guarantees on focus (#4954) feat(auth): store a rotated CLI session credential and show the login code (#4833) fix(daemon): report Windows SIGTERM daemon exits as forced (#4659) # Conflicts: # packages/capture-kit/src/snapshot-quality-verdict.ts # packages/command-runtime/src/commands/interaction/runtime/target-visibility-stages.ts # packages/contracts/src/device-inventory.ts # packages/daemon/src/execution/interaction/internal/__tests__/find.test.ts # packages/daemon/src/execution/interaction/internal/find-target-capture.ts # packages/replay-port/src/daemon-port/session-replay-runtime-failure-response.ts # website/docs/docs/error-reasons.md
Summary
Two hardening changes to the cloud CLI login:
acceptsRotatedRefreshCredential: true. A responserefreshCredentialreplaces the stored one. A cloud that does not rotate sees no change.Rotation contract (written at the request in
auth-session.ts): a rotating cloud issues at most one replacement per credential, answers the replaced credential with an access token and no new replacement, and keeps it valid for a grace period.Login, logout and the store of a rotated credential run under one process lock on the session file. The store re-reads the file under the lock, so a login or logout wins. A failed lock or write keeps the access token and warns.
Before / after
Validation
0756331887:src/clitests (621 pass), typecheck, lint, layering guard.