Repository navigation
fix(auth): send a stored CLI session only to the cloud that issued it - #4921
Conversation
Size Report
Startup median (7 runs, lower is better):
|
This comment has been minimized.
This comment has been minimized.
|
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, 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. |
|
Reply to #4921 (comment), at 727d73c:
|
This comment has been minimized.
This comment has been minimized.
|
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 |
727d73c to
6c2535a
Compare
|
Rebased onto #4833 (18b6276) at 6c2535a. Both open items are in:
Generated docs are regenerated. All 654 |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Apex Sentinel: 1 new finding on c888fd9. The verdict and the full list are in the summary comment.
|
At c888fd9 two auth-redirect defects remain, and both need a fix before merge. The conflict from the earlier review is gone, there are no conflicts, and all 21 checks pass. A second independent pass found that cloud-auth-request.ts:101 can print the stored refresh credential. On the first refresh with a verified session, the issuing cloud can answer with a 307 or 308 and a same-origin A second independent pass also found that cloud-auth-request.ts:77 breaks auth routes that worked before. A cloud can accept the auth POST and answer with a same-origin 303 to a result URL that serves the JSON on GET. The old fetch in Not blocking: The one other review thread, on Not verified here: the tests and the No other code issue is known. The PR should be retargeted to main after #4833 merges. |
This comment has been minimized.
This comment has been minimized.
2fcb5e3 to
11abd8c
Compare
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.
auth-session.test.ts is at the 1000-line cap.
… 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.
…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.
…w bounded same-origin auth redirects The stored CLI session format moves to version 2, which records the origin that issued the credential. A version 1 record, or one whose issuer is not its URL's origin, fails with cli_session_issuer_unverified before any request, and agent-device auth login replaces it. Cloud auth requests (device-auth start, poll and CLI session refresh) follow at most three 307 or 308 redirects whose target has the original request origin, with the same POST body. A cross-origin target, a fourth redirect, or a 301, 302 or 303 fails with a typed reason before anything is sent again.
…fuse credential-bearing targets
This comment has been minimized.
This comment has been minimized.
…uth redirects with one reason Drop issuerOrigin: login always writes the origin of cloudBaseUrl, so version 2 alone marks a session saved through the origin-checked login. Collapse the three redirect reasons into cloud_auth_redirect_refused, drop the opaqueredirect branch Node never returns, and make the hint name the request origin instead of an override a refresh does not use. Prove the transport with Node's own fetch on loopback servers, and keep one cross-origin case per auth route to prove every route uses it.
11abd8c to
1abd533
Compare
This comment has been minimized.
This comment has been minimized.
|
The earlier findings from c888fd9 are fixed at 9c9da8c, but a second independent pass found one remaining defect in the redirect handling. A cloud can answer an auth POST with a 301, 302, 303, 307 or 308 and a present but empty Not blocking: the same empty- Of the earlier inline threads, the two about the test server's dynamic method call are fixed at 9c9da8c, because the tests now serve canned route data from a map (#4921 (comment), #4921 (comment)). The thread on auth-session.ts:353 does not need action here, because the maintainer deferred it to #4849 (#4921 (comment)). No open thread still applies. CI is green at 9c9da8c, with 21 checks and none failing, and there are no conflicts. Not verified here: the tests were not run locally, so regression validity comes from reading them against the pre-change code. Not verified here: no real cloud was exercised, and the redirect behavior is proven only by loopback Node fetch. After the empty- |
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.
|
An empty Location is now resolved against the current URL as Fetch does, with the origin, userinfo, hop and method checks unchanged, and the new test covers 301/302/303/307/308. This addresses the last finding; nothing else blocks 23803c9. |
* 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
A stored CLI session credential goes only to the cloud that issued it, and no auth request carries a credential or device code to another origin.
AGENT_DEVICE_CLOUD_BASE_URLwith another origin fails withcloud_login_origin_mismatchbefore any request. A rotation is stored only over a record with the samecloudBaseUrl.cli_session_issuer_unverifiedand the hintRun agent-device auth login.cloud_auth_redirect_refusedbefore a request reaches its target.auth statuswording for a version 1 session is tracked in #4849.Before / after
Validation
At
23803c95fd:cloud-auth-request.test.tsandauth-session-cloud-origin.test.ts(22 passed), typecheck, lint and format pass. Thesrc/clisuite,check:command-docsandcheck:fallowlast passed at1abd533d63(1579 tests); CI covers them at this head.cloud-auth-request.test.tsuses Node's fetch on loopback servers; removing the credential, origin, hop-limit or GET-switch check, usingredirect: 'follow', or treating an emptyLocationas missing fails it. No real cloud exercised.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.