Skip to content

CLI session rotation: lost-store recovery, refresh origin check, root-safe failure test #4849

Open

Description

@thymikee

Purpose

Follow-ups from the review of #4833 (CLI session refresh-credential rotation). None blocks that PR.

Required behavior

  1. Recovery after a lost store. When writing the replacement credential fails, or the process exits before the rename, the session file holds the credential that the cloud replaced. After the grace period the next command is refused. Define the recovery path with the cloud's side of the protocol, for example a re-issue of the current replacement to a caller that proves the replaced credential within the grace period.
  2. Refresh response origin. The refresh request follows redirects and does not check the final response origin, so a redirect to another origin could supply the replacement credential. Reject a response whose origin differs from the stored cloud origin.
  3. Failed-write test under root. The test forces the write to fail with chmod 0500 on the auth directory. That does not block writes when the suite runs as root. Force the failure in a way that does not depend on the user.

Completion conditions

  • A test covers a store that fails after the cloud rotated, and the next command either recovers or fails with a typed reason and the login hint.
  • A test covers a refresh response from a redirected origin, and no credential from it is stored.
  • The failed-write test fails when the fix is removed, also as root.

Dependencies

Blocked by: #4833

Activity

  1. apex-sentinel-staging commented on Nov 20, 2026

    @apex-sentinel-staging

    Apex Sentinel · Issue review

    Suggested label: feature

    The issue is a follow-up implementation task from #​4833, specifying new required behavior: recovery after a lost credential store, rejecting redirected refresh responses from a different origin, and making the failed-write test work as root.

  2. added
    enhancementNew feature or request
    and removed on Nov 21, 2026
  3. thymikee commented on Nov 21, 2026

    @thymikee
    MemberAuthor

    From the #4833 review, for this follow-up:

    • A process that reads the credential, pauses past the grace window, then refreshes, sends a stale credential; a cloud that revokes on post-grace reuse then invalidates the replacement another process saved. Holding the session lock across read, request and store would close it.
    • A 200 refresh response that reports revoked and also carries tokens is accepted, because the access-token branch returns before the revocation check.
    • The request always sends acceptsRotatedRefreshCredential; a non-rotating control plane that rejects unknown body keys would answer 400.
  4. thymikee commented on Nov 21, 2026

    @thymikee
    MemberAuthor

    From the #4833 review at c721659: if a live process holds the CLI session lock for longer than 60 s, a login after device approval or a refresh that received a rotated credential fails without saving it. Options: keep waiting with a progress message, save the credential to a recovery file, or make the replacement recoverable on the cloud side.

  5. thymikee commented on Nov 21, 2026

    @thymikee
    MemberAuthor

    From the #4833 review: the refresh response carries no CLI session expiry, so a cloud whose session expiry slides with use (idle window) still sees the CLI treat the session as expired at the login-time expiresAt. Proposal: the refresh response adds cliSession.expiresAt, and the CLI stores it with the rotated credential.

  6. thymikee commented on Nov 21, 2026

    @thymikee
    MemberAuthor

    Follow-up PR scope, from the #4833 reviews (stacked on feat/cli-session-refresh-rotation):

    1. loginWithDeviceAuth: after the locked save, if the returned access token has expired (a long lock wait), get a fresh token from the saved session before returning it.
    2. Refresh with rotation: after the rotated credential is saved, if the returned access token has expired, refresh again through the rotation-aware path.
    3. Hold the session lock from reading the stored credential, through the refresh request, to the replacement write. Keep device approval polling outside the lock. This also covers the stale-credential case after the grace window.
    4. Already noted: a refresh response that says revoked while carrying tokens; acceptsRotatedRefreshCredential sent to a non-rotating cloud; cliSession.expiresAt in refresh responses.
  7. thymikee commented on Nov 22, 2026

    @thymikee
    MemberAuthor

    Follow-ups from the #4927 round-4 review (#4927 (comment)), moved here because #4927 is closed for new hardening:

    • Check that the expiresAt of a refresh response is a non-empty string before use (src/cli/auth-session.ts around lines 606, 716, 976).
    • Refuse a refreshed access token that is not safe in a Bearer header. Give a fixed error and keep the first token (around line 713).
    • If the second token has already expired locally and the first token's deadline is later, keep the first token (around line 602).
    • After the revocation check, treat a null success body as a tokenless response (around line 713).
    • Limit the refusal and warning claims in website/docs/docs/commands.md (around line 2686) to the behavior that ships.
  8. thymikee commented on Nov 22, 2026

    @thymikee
    MemberAuthor

    From #4833 (comment):

    • Move the CLI session lock to a writable state location shared by every operation on the session. When the credential file cannot be replaced (a 0500 auth directory), allow a non-rotating exchange under the lock instead of failing with cli_session_store_unwritable (src/cli/auth-session.ts:535, test auth-session-refusal.test.ts:281-315).
    • Check the access token's remaining lifetime after the final awaited lock release, on every path that hands a token to a caller (auth-session.ts:415, :313, :606). If the release consumed the lifetime, do one bounded refresh and return a typed failure if there is still no live token.
  9. thymikee commented on Nov 22, 2026

    @thymikee
    MemberAuthor

    From #4921 (#4921 (comment)):

    • auth status should name cli_session_issuer_unverified and the re-login hint for a v1 or origin-unverified session, instead of plain "Not authenticated".
    • In an interactive connect, an unverified session could start auth login automatically, like a missing session does.
  10. thymikee commented on Nov 22, 2026

    @thymikee
    MemberAuthor

    From #4921 at 1abd533, outside its invariant:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions