Repository navigation
Fix: match a cached login by control-plane endpoint, not by spelling - #667
Conversation
`mocactl` compared the stored `controlPlaneUrl` to the one in use with a raw string `!==`, so the same control plane spelled differently -- host case, an explicit `:80`/`:443`, a scheme in capitals -- read as a different deployment. The cached login was dropped, silently: `loadAuth` returned null and threw the stored URL away, so every gate said a flat "not logged in". The user logged in again, overwriting the one `auth.json`, and met the same thing on switching back. That is what rossoctl#529 was reported as: "the GitHub token only lasts an hour". The audit stream showed no `refresh_refused` events and the original refresh family still live -- the client never presented its refresh token at all. Two fixes, both halves of the issue: - `sameControlPlane` canonicalises the origin (lowercase scheme and host, drop a default port) and KEEPS THE PATH. The issue proposed comparing `origin`; that would be a token leak here, because a control-plane base carries a load-bearing path prefix (`http://h/cp` vs `http://h/other-cp` share an origin) and the rule that a refresh token only ever goes to the control plane that issued it (spec §4.6) must hold. A scheme, host, port or path-prefix difference is still refused, and `localhost` is not aliased to `127.0.0.1`. Unparseable input falls back to the exact compare, so nothing widens by accident. - `readyCp`, `mocactl auth token` and `mocactl doctor` now name the control plane the login on this machine belongs to, instead of sending the user to do the one thing that makes it worse. Exit codes are unchanged, including the 3 that the Claude Code hook branches on. `normalizeUrl` and what gets written to `auth.json` are untouched: only the comparison got more tolerant of spelling, never of the endpoint. `storedLoginUrl` was promoted from `sbx/commands.ts` rather than written again. The TUI login overlay and `mocactl logout` share the same silence but are off the reported path; they are left for a follow-up, as is the issue's option 3 (one cached login per control plane), deferred by its author. Fixes rossoctl#529 Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: cwiklik <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Approving. sameControlPlane widens the match for spelling only (scheme/host case, default port, trailing slash), and still refuses on scheme, host, non-default port, path prefix and localhost vs 127.0.0.1. Unparseable input falls back to exact equality. I checked that the canonical form (origin + pathname, with dot segments resolved by new URL) names the same endpoint ControlPlaneClient's new URL(base + path) reaches, and that both loadAuth call sites in ensureAuth go through it, so the spec §4.6 refresh-token rule holds. The "not logged in" messages now name the stored login's control plane, and the exit codes are unchanged. CI is green.
One suggestion: the transcript store still compares controlPlaneUrl exactly, which can now orphan transcripts across spellings (see the inline comment). One nit on a test assertion that can't fail.
Author: cwiklik (MEMBER — maintainer) · Agent/IDE config: none · Commits: 1, signed-off · CI: passing
| * Anything `new URL()` cannot parse falls back to the exact compare this replaced, so a string that | ||
| * is not a URL still matches itself and nothing widens by accident. | ||
| */ | ||
| export function sameControlPlane(a: string, b: string): boolean { |
There was a problem hiding this comment.
suggestion: The transcript store still uses an exact compare: core/transcripts.ts:128 (ownsFile) and :297 (read) check header.controlPlaneUrl !== this.owner.controlPlaneUrl, and the owner is rt.endpoints.controlPlaneUrl verbatim (runtime.ts:91). The PR body calls that drift harmless because both sides come from one normalised value, but that only holds within a single run. Before this PR, a spelling change dropped the login, so the transcript path was never reached. Now the login is accepted across http://CP ↔ http://cp while transcripts written under the other spelling are not: ownsFile returns false, so frames for that session stop being recorded and the read returns null on resume. Using sameControlPlane at those two sites would keep both checks consistent. It's a two-line change, so it could go in here rather than in the follow-up.
| const login = results.at(-1); | ||
| expect(login).toMatchObject({ id: 3, status: 'fail' }); | ||
| expect(login?.detail).toContain('http://cp-a'); | ||
| expect(login?.detail).toContain('http://cp'); |
There was a problem hiding this comment.
nit: toContain('http://cp') is already satisfied by 'http://cp-a', so this can't catch a detail line that omits the target URL. Assert the exact string, or use a distinct target such as http://cp-b as headless.test.ts does.
Takes upstream's 11 commits after 071b213: gh vendored into the sandbox images (rossoctl#661), the sandbox image pulled on every run in both VM paths (rossoctl#665) and in compose (rossoctl#664), a cached login matched by endpoint (rossoctl#667), a kind-ci fix (rossoctl#663). No conflicts. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Rong Chang <[email protected]>
Summary
mocactlcompared the storedcontrolPlaneUrlto the one in use with a raw string!==, so thesame control plane spelled differently — host case, an explicit
:80/:443, a scheme in capitals —read as a different deployment. The cached login was dropped, silently:
loadAuthreturnednulland threw the stored URL away, so every gate said a flatnot logged in. The user logged inagain, overwriting the single
auth.json, and met the same thing on switching back.That round trip is what #529 was reported as — "the GitHub token only lasts an hour". The audit
stream showed no
refresh_refusedevents and the original refresh family still live: the clientnever presented its refresh token at all.
Fixes #529
What changed
1. The comparison (
config.ts) — newsameControlPlane, used byloadAuth. It canonicalisesthe origin (lowercase scheme and host, drop a port that is the scheme's default) and keeps the
path. Unparseable input falls back to the exact compare that was there before, so a non-URL string
still equals itself and nothing widens by accident.
2. The silence (
headless.ts,core/diagnostics.ts) —readyCp,mocactl auth tokenandmocactl doctornow name the control plane the login on this machine belongs to, instead of sendingthe user to do the one thing that makes it worse. Exit codes are unchanged, including the
3that the Claude Code hook branches on.
A note on the issue's own suggestion
The issue proposes "compare origins instead of strings". That would be a token leak here.
Control-plane bases carry a load-bearing path prefix (
config.test.tspinsnormalizeUrl(' http://h/cp/ ')→http://h/cp, andapi/control-plane.tsbuilds every request asnew URL(base + path)), sohttp://h/cpandhttp://h/other-cpshare an origin while being twodifferent control planes. Comparing bare
originwould hand one's refresh token to the other —exactly what spec §4.6 forbids ("never send one control plane's token to another").
So this widens the tolerance for spelling only, never for the endpoint:
http://CP≡http://cphttp://cp:80≡http://cphttps://cp:443≡https://cpHTTP://cp≡http://cphttp://h/cpvshttp://h/otherhttp://cpvshttps://cphttp://cp:8080vshttp://cphttp://localhostvshttp://127.0.0.1normalizeUrland what gets written toauth.jsonare untouched.storedLoginUrlwas promotedfrom
sbx/commands.tsrather than written a second time.Tests
The security direction matters as much as the fix, so the matrix is half refusals.
config.test.ts— 5 accepted spellings × 2 targets, plus:443and path+slash; a 7-case refusalmatrix (each pair also asserted to still match itself);
storedLoginUrl.headless.test.ts— new#529block:readyCpnames both URLs and still returns 2;auth tokennames both and still exits 3 with empty stdout; doctor check 3 names both; and with no
auth.json the message is exactly the old plain one (no phantom
undefined).diagnostics.test.ts— check 3 names the other control plane; the unchanged wording is pinned forwhen there is no login at all.
Regression, green and unmodified — the assertions that prove nothing widened:
auth-refresh.test.ts'never sends a refresh token to another control plane (Review Focus 2)';sbx-cli.test.ts'keeps a MOCA login and an sbx login side by side' and "logout leaves anothercontrol plane's login in place and names it";
app.test.tsx'onboarding with a trailing-slashcontrol-plane URL caches the login under the saved URL';
config.test.ts'normalizes whitespace,empty strings and trailing slashes'.
Local verification
tsc --noEmitmocactlsuiteAssertionError— all 5 pass isolated at--testTimeout=30000--check,check-doc-links.pyBug proof, run before and after on the same temporary case (login stored as
http://cp:80,loaded as
http://cp): before →AssertionError: expected undefined to be 'mrt_1'; after → passes.And the negative (
http://h/cpstored,http://h/otherloaded) →nullboth before and after.Out of scope (worth a follow-up)
reported headless path.
mocactl logoutsays a flat "not logged in" while leaving another control plane's login on disk;the
sbxpath already names it.it touches the refresh lock,
logoutand transcript wiring.core/transcripts.tsgates a transcript on the same exact-string compare. Harmless today (bothsides come from one normalised value), but it will drift from the auth comparison.
Assisted-By: Claude Code