Skip to content

fix(auth): send a stored CLI session only to the cloud that issued it - #4921

Open
thymikee wants to merge 4 commits into
feat/cli-session-refresh-rotationfrom
fix/cloud-login-origin
Open

thymikee wants to merge 4 commits into
feat/cli-session-refresh-rotationfrom
fix/cloud-login-origin

Conversation

@thymikee

@thymikee thymikee commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

The CLI stores the cloud origin with its CLI session, but AGENT_DEVICE_CLOUD_BASE_URL overrode it, so the stored refresh credential was sent to whatever origin that variable named.

  • A refresh refuses an override with a different origin before any request: UNAUTHORIZED, reason cloud_login_origin_mismatch (declared in a ReasonTable), with a hint to log in again or unset the variable. The same origin with a different path or trailing slash still works.
  • Cloud auth requests (device-auth start and poll, session refresh) send redirect: 'error', so a redirect cannot resend a credential or device code to another host.

Stacked on #4833; retarget to main after it merges.

Before / after

Stored CLI session issued by https://cloud.example, refresh with a different origin named
(the new test covers both rows; a same-origin override with another path still refreshes):

base: POSTs the stored refresh credential to https://elsewhere.example
head: refuses before any request
      UNAUTHORIZED: The stored CLI session belongs to another cloud API origin.
      reason: cloud_login_origin_mismatch

Validation

  • Tested at af2a887668. New test a stored CLI session is sent only to the cloud that issued it, without redirects fails on the base commit and passes at head.
  • Locally at head: pnpm format, pnpm typecheck, pnpm lint, pnpm check:layering, and npx vitest run src/cli pass (one unrelated timing test failed once under host load and passes alone).
  • CI obligations: GitHub lanes cover the rest. Unresolved risks: none known.

Run Proof on this PR
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

View guided diff Turn on auto-fix

AGENT_DEVICE_CLOUD_BASE_URL overrode the cloud origin stored with the CLI session, so the stored
refresh credential went to whatever origin the variable named. A refresh now refuses an override
with another origin (UNAUTHORIZED, reason cloud_login_origin_mismatch) before any request, and
cloud auth requests no longer follow redirects, which would resend a credential or device code to
the redirect target.
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.49 MB 6.49 MB +375 B
Package (unpacked) 6.49 MB 6.49 MB +375 B
Package (download) 1.98 MB 1.98 MB +155 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 18.7 ms 19.4 ms +0.7 ms
CLI --help 40.7 ms 40.5 ms -0.2 ms

… table

R89 measures wire-published reason literals against the merge-base.
Read the reason from a ReasonTable beside the origin check and regenerate
the error reasons page.
@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The change binds a stored CLI session to its issuing cloud as intended, but five problems remain at af2a887. A second independent pass found them.

A second independent pass found that existing version-1 session records can still name an origin that did not issue the credential (auth-session.ts:434). Before this change, a login for origin A could follow a device-auth poll redirect to origin B and store B's credential under A's URL (base a9750b5, src/cli/auth-session.ts lines 252-274 and 575-581). The base could also save B's rotated credential under A when the environment override was set. This head accepts those records and posts the credential to A when the override is unset. Please separate records created with a verified issuer binding from legacy version-1 records. A legacy record whose issuer cannot be established from persisted state should require a fresh login before any refresh.

A second independent pass found that a late rotation can replace the credential of a different origin (auth-session.ts:483). Two origins backed by a cloned session store can return the same session ID and initial credential. If a refresh at A is in flight while a login at B replaces the record, the lock-protected comparison accepts B's record and stores A's replacement under B's URL. The next refresh then sends it to B. The saved origin must be part of the lock-protected comparison before a rotation is applied.

A second independent pass found that same-origin auth redirects now fail (auth-session.ts:599). If a cloud answers a device-auth start, device-auth poll, or CLI-session refresh POST with a 307 or 308 to its canonical path, the base follows it. This head fails at the first redirect, even when the stored issuer and the requested origin match. A cross-origin redirect also gives a generic fetch failure instead of the origin-mismatch reason. The rule should be: every redirect target is resolved and its origin checked against the original request origin before it is followed, within a bounded chain. A cross-origin target should return a typed refusal before it is followed.

A second independent pass found that a same-origin override changes the identity of an existing cloud connection (auth-session.ts:461). Take a login at https://cloud.example and a named connection created on the base with AGENT_DEVICE_CLOUD_BASE_URL=https://cloud.example/path. If the profile leaves clientId or runId for the CLI to supply, repeating the same connect now hashes the stored URL instead of the override (cloud-profile.ts:43-56). The profile and its path change, and the existing connection is rejected unless the caller passes --force (connection.ts:162-171). The stored URL should stay the refresh destination, and the validated same-origin override should stay the connection identity input.

A second independent pass found that the cloud connect docs no longer match behavior (commands.md:2686). They say connect refreshes a stored CLI session into a short-lived token. With a different cloud API origin set and a valid stored session, connect now stops with cloud_login_origin_mismatch before it fetches the profile. Please qualify the refresh description with the stored-origin rule, and say the caller can log in at the new origin or unset the override.

Not blocking, and these can be taken or left: add one sentence to the AGENT_DEVICE_CLOUD_BASE_URL row in configuration.md that, with a stored login, the variable must name the same origin or the user must run auth login with it; catch the redirect failure in postJson and rethrow a typed AppError with a hint naming the cloud base URL (the default cloud answers the refresh POST with a 307 to /unlock today, and it was already broken at base); compare the stored origin with the origin of the override only when it is set, so a hand-edited malformed stored URL is not reported as an invalid override (also true at base); open a follow-up issue to bind use of the minted access token to a daemonBaseUrl allowed for the issuing cloud, since with both settings it goes to flags.daemonBaseUrl while the refresh credential itself stays home; and split the origin test into two tests with the temp dir removed in a finally, as the sibling tests do.

CI is green: all 17 checks pass or skip at af2a887, and Body rules failed at packet time but passes on rerun. There are no conflicts. The five items above must be fixed before merge.

Not verified here: the new test and the src/cli suite were not run locally, so the claim that the test fails on base comes from reading the base path. No real cloud was exercised; a request to the default cloud showed a same-origin 307 to /unlock on the refresh path, and what a working cloud answers was not tested. The error-reasons.md page appears to be generated from ReasonTable comments, and the generator was not rerun.

…e a rotation, and document the rule

A same-origin AGENT_DEVICE_CLOUD_BASE_URL still names the connection, while the refresh goes to the
URL stored with the session. A rotation is not stored over a session that a login moved to another
cloud. The commands docs state the stored-origin rule and the cloud_login_origin_mismatch outcome.
@thymikee

Copy link
Copy Markdown
Member Author

Reply to #4921 (comment), at 727d73c:

  • Rotation compares the stored origin (:483): fixed. The locked re-read also requires the same cloudBaseUrl; new test a rotation is not stored over a session that a login moved to another cloud.
  • Refresh destination vs. connection identity (:461, cloud-profile.ts:43-56): fixed. The refresh goes to the stored URL; a validated same-origin override is returned as the connection base URL, as before this PR.
  • Docs (commands.md:2686): fixed; the refresh description states the stored-origin rule, cloud_login_origin_mismatch, and no redirects.
  • Legacy version-1 records (:434): not changed. cloudBaseUrl is a required field of every version-1 record (readCliSession rejects a record without it), and it is the URL the login ran against, so every stored record carries its issuer. A version bump would force every user to log in again for no gain.
  • Bounded same-origin redirects (:599): not changed. The cloud auth endpoints are POST APIs that do not redirect; refusing every redirect is the smaller and safer rule, and a cloud that needs one can serve the final URL. A same-origin redirect chain can be added if a cloud ever needs it.

src/cli tests, typecheck, lint, check:command-docs and check:layering pass.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 727d73c

Since the review of af2a887: 0 new · 0 still open · 0 no longer raised

No open findings.

1 new finding away from what changed since af2a887 not shown. Comment @apex-sentinel-staging review for a full review.

Reviewed with this repository's own Sentinel configuration (d6767e0910fc).

💬 Talking to Sentinel

In a finding's thread, reply with:

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.

@thymikee

Copy link
Copy Markdown
Member Author

At 727d73c, three items from the earlier review are fixed, and two remain open on the code. The two declined items are outside the scope that blocks this PR. CI shows 16 checks with none failing, and there are no conflicts.

A second independent pass found that a version-1 record can already hold cloud A's URL with a credential issued by cloud B. The old login could follow a cross-origin poll redirect and save the response credential under its original login URL. The old refresh could also run against cloud B through the environment override and store B's replacement under A's URL. Both records pass the version-1 parser at auth-session.ts#L296-L310. With the override unset, either refresh path then sends B's credential to A, as in auth-session.ts#L435. This breaks the rule that a credential goes only to the cloud that issued it, even though the request matches the stored URL. The rule to enforce: a stored session is used only when its issuer is established from persisted state. Please version the session format so a fresh login through the origin-checked flow records the issuer. Please also require a fresh login before refreshing any legacy record whose issuer cannot be established.

A second independent pass also found a regression for clouds that serve an auth POST through a 307 or 308 to a working path on the same origin. Before this PR, fetch followed the redirect and kept the POST. Now the shared transport at auth-session.ts#L605 rejects the first response. A redirect at device-auth start stops login from starting. A redirect at device-auth poll stops login from finishing. A redirect at refresh stops a valid stored session from refreshing. The rule to enforce: every auth request, in the shared transport that all three call, follows a bounded redirect chain only when each resolved target has the original request origin. Accepted hops keep Fetch redirect semantics, and cross-origin targets are rejected before anything is resent. Please update the command documentation to describe this behavior.

Not verified here: the new tests and the src/cli suite were not run locally, so regression validity comes from reading the earlier code at a9750b5. Not verified here: any real cloud, since the redirect: 'error' setting is asserted only as the value passed to an injected fetch, not against Node's fetch on a live 307 or 308. The same-origin path override used as the connection identity input was checked by reading auth-session.ts#L461 and cloud-profile.ts, and no test asserts it. For validation, please add a test for each redirect route above that reaches the shared transport with a same-origin 307 or 308 and a cross-origin one. The same-origin case should complete the request, and the cross-origin case should fail without sending a second request. Retarget to main after #4833 merges.

This branch has not been deployed

No deployments
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.

1 participant