Repository navigation
fix(auth): hold the CLI session lock across a refresh and keep its access token live - #4927
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: 2 new findings on 0c266fb. The verdict and the full list are in the summary comment.
|
Five problems in 0c266fb need fixes before this lands. The lock now covers the refresh, but it can hang a command, fail an approved login, return or report the wrong credentials, and expose a stored credential in an error. A second independent pass found that a stored-session command can wait forever for a lock it cannot get. When the cloud returns a live access token without rotating, a dead process may have left a lock claim with no owner record, or a live holder may be stopped (see process-lock.ts). At base this refresh took no lock and finished. At head, auth-session.ts:389-398 discards every typed timeout and waits again, so the command keeps warning and never shows the lock recovery hint. Use the bounded acquisition for stored-session refreshes and let the typed timeout reach the caller. Keep retrying only for the already-approved login write. A second independent pass found that an approved A second independent pass found that login can return another login's access token (auth-session.ts:320). Login A saves its session and releases the lock. Login B saves a different session before A takes the lock again. If A's token expired, A exchanges B's credential at lines 463-468 and returns B's token with A's session (lines 298-301). Implicit connect then pairs that token with A's cloud URL (lines 200-209). Keep the login's save and any immediate exchange in one lock acquisition, reuse the refresh routine inside it with the just-saved record, and leave device approval polling before the acquisition. A second independent pass found that A second independent pass found that a refresh error can expose a stored credential (auth-session.ts:573). An HTTP 200 response with a revoked status or error, an access token, and an echoed opaque refresh credential in the other field gets copied into Of the earlier inline threads, the login-failure thread still applies, and it is covered by the second fix above. The two-login race thread is not the race as stated, because the login that waited writes last, so the author can resolve it; the third-writer case is covered by the third fix above. Not blocking, and each can be taken or left: the expired-token retry loop in Not verified here: test results, since this review only read the code, and whether each new test fails at base, which was judged by reading base. Also not verified here: how the same-process parallel test behaves in host-kit's process lock, how the cloud handles a second exchange with a just-rotated credential, and whether read-only session stores are supported. The only two checks not passing are cancelled 'Body rules' runs, and a later 'Body rules' run passes on this commit, so they look unrelated to the diff. There are no conflicts. |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 finding moved to a new line on 2d67cec. The verdict and the full list are in the summary comment.
|
Round 1 (0c266fb, #4927 (comment)) is only partly fixed at 2d67cec. The lock now covers the refresh, but explicit Explicit login still fails after a successful save: auth-session.ts:291. When the approved access token is already expired, A second independent pass found that the exchange can return a sibling login's token: auth-session.ts:333. Login A saves its approved session and releases the lock (lines 286-290). A's access token is expired. If login B saves a different session before A's refresh re-reads the file (line 477), A sends B's stored credential (line 486) and returns B's access token (line 303). Implicit connect then uses that token (lines 149-157, 200-209). Base returned only the approved token and record. Please keep the session write and the immediate exchange in one acquisition of the existing lock, and run the exchange on A's just-saved record. Approval polling stays outside the lock. A second independent pass found that the post-exchange read can pair a token with a different session or a stale expiry: auth-session.ts:305. A's refresh rotates its session, then releases the lock (lines 421-424) before line 305 reads the record. If B logs in during that gap, A returns A's token and expiry with B's session metadata, and cloud connect uses that record's URL with A's token (lines 200-209). If a logout removes the file in the gap, the fallback reports the pre-rotation expiry. Explicit login prints these fields (commands/auth.ts:25-33). Please return the session snapshot and token pair produced by the exchange while the lock is held, and use that for login output. A second independent pass found that a revoked refresh response can expose untrusted text: auth-session.ts:604. When a 200 response carries tokens and one of A second independent pass found a regression for a readable but unwritable auth directory: auth-session.ts:474. State-root admission checks the root and the managed sessions directories (packages/daemon-contracts/src/daemon-resolution.ts:154-164), not the auth subdirectory. Base sent the refresh and accepted a live token, and the existing test covers a 0500 auth directory (auth-session.test.ts:320-336). At this head, the lock state must be written beside the session (lines 392-401), so the call fails before any refresh is sent, even if the cloud would make no rotation. A readable store with an unwritable auth directory can no longer authenticate remote commands. Please keep the lock mandatory and either repair the owner-owned directory's write permission through state admission, or, if that case is intentionally unsupported, document the change and return a typed storage error with a repair hint. Not blocking, and fine to take or leave: re-reading the file at line 305 after the lock is released could be replaced by returning the record from Does the extra machinery in this change need to be in this PR? Round 1 marked the in-command second exchange and the stored For a follow-up issue, not for this PR: refresh refusals should never carry response-supplied strings (the revoked and tokenless cases above, auth-session.ts:603), if the author prefers to fix that separately from the revoked-with-token route that this change newly reaches. Of the two earlier inline threads, #4927 (comment) still applies (login failing after a post-save 503 or lock timeout), and #4927 (comment) no longer applies, because the login that waited writes last (lines 286-290) and line 305 now returns the stored session with the exchanged token, so only the narrow gap above remains. Please resolve the second thread. The code needs the fixes above before merge, starting with the explicit-login failure at line 291. Required evidence is present. CI looks unrelated to this change: the diff touches only |
This comment has been minimized.
This comment has been minimized.
…se 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.
This comment has been minimized.
This comment has been minimized.
|
Round 2 items addressed in 11ea3a7. Each answer names the commit and the test that pins it. 1. Explicit 2. One lock acquisition, reusing the refresh routine, returning the snapshot. The save and the 3. Revoked and tokenless refusals carry one constant reason and no response text. Both refusals 4. Unwritable auth directory. I took the typed-error option: the store stays lock-mandatory, and 5. Bounded refresh lock acquisition. Confirmed still true at this head: Took the non-blocking suggestions that overlapped these: the post-exchange file re-read is gone, Validation at 11ea3a7: |
|
This follow-up covers 11ea3a7. The two earlier findings from the previous review are fixed: a non-revoked exchange failure after the save now returns the approved pair for explicit login, and the exchange runs on the record just written, inside the same lock acquisition. Six issues remain in the new code, and they must be fixed before merge. A second independent pass found that explicit A second independent pass found that a refresh that answers HTTP 401 with a JSON body reporting A second independent pass found that device approval can abort explicit login after its save (auth-session.ts#L307). If the approval returns an A second independent pass found that a failed second exchange makes A second independent pass found a related case where a failed rotation write makes login report an expiry it did not save (auth-session.ts#L572). The exchange returns a fresh token, a replacement credential and a new session expiry, but the write fails before rename. A second independent pass found that a normal remote command can now fail where base succeeded (auth-session.ts#L563). The cloud returns an access token its daemon would still accept, but the CLI clock is ahead of that token's expiry. Base (a9750b5 lines 425-442) forwards the token. Head makes a second refresh and aborts before dispatch if that request hits HTTP 503 or a transport failure. The rule: an existing session uses one locked exchange, or keeps the first token when an optional second exchange fails transiently, and a confirmed revocation still fails. Not blocking: the transient-failure fallback at auth-session.ts#L335 can print the pre-rotation Could the shape be smaller? The two open inline threads from the earlier round no longer apply, and both are safe to resolve: the 503 after save is fixed at 11ea3a7 and covered by the test 'auth login succeeds when the exchange of its expired approval token gets 503', and the exchange on the saved record now runs inside the acquisition that wrote it, with no file read in between. All 14 checks were queued or in progress when this was written, so CI has no result yet. The diff overlaps Lint/Typecheck, Coverage, Integration and Repo Guards & Compatibility, because the new reason table feeds the generated |
…ssion 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.
|
All six findings from this round are fixed in 8469cdf. Each was reproduced as a test first, and each test was then checked against a revert of its fix. 1. A tokenless refresh answer gets its own reason. The reason table declares 2. Exact revocation markers are classified before the generic HTTP rejection. 3. The approval's access-token expiry must be a nonempty string. Anything else is omitted before 4. and 5. The login's output is the last successfully saved session, and its next credential is kept separate. 6. An existing session uses one locked exchange, or keeps the first token. When the optional second exchange fails with a transport failure or a retriable status, the token the cloud already answered with is kept, paired with the record as last stored, so a command base dispatches is not aborted. A confirmed revocation is never treated as transient and still fails. Pinned by 'an optional second exchange that fails transiently keeps the token already answered' and 'a login whose second exchange fails reports the session its first exchange stored'. Each revert makes exactly the test that pins it fail: relabelling the tokenless reason, removing the classifier hook, trusting a non-string expiry, reporting an unsaved rotation, and aborting on the failed optional exchange. Validation from this worktree: This is the last round for this PR. Further hardening goes to issue #4849: giving the reasons a refreshing remote command publishes an owner that matches how they are reached, and refusing the unwritable-directory test under a root runner. |
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 follow-up on the earlier review (#4927 (comment)) covers 8469cdf. The earlier findings are fixed, but a second independent pass found five problems that remain. CI is green: 16 checks, none failing, including the guards that check the generated error-reasons.md against the new reason row. There are no conflicts. No tests were run here, and the author's local validation was not reproduced. Not verified here: whether the real cloud sends the A second independent pass found that a refresh can return a valid access token with an A second independent pass found that a stored-session connect can lose a usable first token (auth-session.ts:713). The service accepts the first token while the CLI clock calls it expired. The optional second refresh then returns a nonblank token with an internal newline, and head replaces the first token with it at lines 602-607. Building the profile request in cloud-profile.ts:74 throws a Node header error that includes the response-supplied value. That message is published through errors.ts:508 and cli.ts:665. Please reject access tokens that cannot be sent safely in a Bearer header at the refresh boundary, with a fixed error and no raw value or cause. The existing second-exchange fallback can then keep the first token. A second independent pass found that a later-arriving token can replace a better one (auth-session.ts:602). Say the CLI clock reads 01:00 while the cloud and daemon read 00:20. The first refresh returns a token that expires at 00:30. A second response, for example from a stale cache, returns one that expired at 00:10. Head replaces the first token unconditionally and sends the second, and the daemon rejects it. Base a9750b5 sends the first token and dispatch succeeds. When both responses have known expiries and the second is already expired locally, please keep the first token and its own expiry if its deadline is later. The saved session snapshot should be kept independently. A second independent pass found that a successful refresh with the JSON body A second independent pass found that the command docs promise more than the code does (commands.md:2686). A stored-session command can get a first token that is already expired on the CLI clock, then an HTTP 200 with an empty object on the optional second refresh. The exchange keeps the first token and dispatches (auth-session.ts:578), yet the docs say a tokenless response fails with Not blocking, take or leave: the docs sentence at commands.md:2686 should say "a successful refresh response that carries neither a token nor that marker", since other HTTP errors keep their status refusal; the comments at cloud-response.ts:8 and :21 say the opposite of the code (line 8 should read "a response whose status is not OK", line 21 "Only a body that parses can hold a marker"); and the test at auth-session-refusal.test.ts:123 should be renamed "a refresh that reports revocation over HTTP …", since it drives a refresh, not an approval. Of the earlier inline threads, the two on a 503 after the approved save (#4927 (comment)) and on exchanging the expired token inside one lock (#4927 (comment)) are fixed at head and can be resolved. No other earlier thread still applies. Once the five items above are fixed, nothing else stands between this PR and merge. The non-blocking notes can go to follow-up issue #4849. |
…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.
…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.
… code (#4833) * feat(auth): store a rotated CLI session credential and show the login code * fix(auth): state the rotation contract and name the flag for what the CLI accepts * fix(auth): serialize CLI session file changes under one process lock 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. * fix(auth): keep an approved login or rotated credential through session 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. * fix(auth): keep a committed session change when the lock warning cannot 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. * test(auth): cover a failed session lock release and a throwing stderr after a stored rotation * fix(auth): write the unsaved-rotation warning best effort too * fix(auth): never drop an approved login or a rotated credential on a 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. * fix(auth): absorb late stderr write errors with one listener per stream * fix(auth): hold the CLI session lock across a refresh and keep its access 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.
Summary
Stacked on #4833 (#4849). Retarget to
mainonce #4833 merges.auth loginsucceeds through a transient exchange failure; only callers that spend the token (connect) propagate it. A revoked answer refuses both.revokedis refused even when it also carries tokens, and every refresh refusal publishescli_session_revokedwith no response-supplied text in message or details.cli_session_store_unwritableand achmodhint naming the directory.Before / after
Validation
11ea3a7c11:pnpm check:affected --run --head HEADpassed (exactCommit: true) — format, lint, typecheck, layering, fallow, build, unit, command-docs, docs-links, docs-prose; replay and web lanes are CI-owned.a9750b54bf.