Skip to content

Fix: match a cached login by control-plane endpoint, not by spelling - #667

Merged
cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/529-control-plane-url-login-match
Oct 30, 2026
Merged

cwiklik merged 1 commit into
rossoctl:mainfrom
cwiklik:fix/529-control-plane-url-login-match

Conversation

@cwiklik

@cwiklik cwiklik commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Summary

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 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_refused events and the original refresh family still live: the client
never presented its refresh token at all.

Fixes #529

What changed

1. The comparison (config.ts) — new sameControlPlane, used by loadAuth. It canonicalises
the 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 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.

$ mocactl run 'hi'
not logged in — run `mocactl login` first — auth.json holds a login for http://cp-a, but this
command targets http://cp-b; use one URL or run `mocactl login`

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.ts pins
normalizeUrl(' http://h/cp/ ') → http://h/cp, and api/control-plane.ts builds every request as
new URL(base + path)), so http://h/cp and http://h/other-cp share an origin while being two
different control planes. Comparing bare origin would 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://cp same (host case)
http://cp:80 ≡ http://cp same (default port)
https://cp:443 ≡ https://cp same (default port)
HTTP://cp ≡ http://cp same (scheme case)
http://h/cp vs http://h/other refused (path prefix)
http://cp vs https://cp refused (scheme)
http://cp:8080 vs http://cp refused (non-default port)
http://localhost vs http://127.0.0.1 refused (the same host only by convention)

normalizeUrl and what gets written to auth.json are untouched. storedLoginUrl was promoted
from sbx/commands.ts rather 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 :443 and path+slash; a 7-case refusal
    matrix (each pair also asserted to still match itself); storedLoginUrl.
  • headless.test.ts — new #529 block: readyCp names both URLs and still returns 2; auth token
    names 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 for
    when 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 another
control plane's login in place and names it"
; app.test.tsx 'onboarding with a trailing-slash
control-plane URL caches the login under the saved URL'
; config.test.ts 'normalizes whitespace,
empty strings and trailing slashes'
.

Local verification

Check Result
tsc --noEmit clean
config + headless + diagnostics + auth + auth-refresh 124 passed
full mocactl suite 1621 passed; 5 pre-existing timeout flakes, identical to the pre-change baseline, zero AssertionError — all 5 pass isolated at --testTimeout=30000
prettier --check, check-doc-links.py clean

Bug 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/cp stored, http://h/other loaded) → null both before and after.

Out of scope (worth a follow-up)

  • The TUI opens the login overlay with no explanation at all — the same silence, but off the
    reported headless path.
  • mocactl logout says a flat "not logged in" while leaving another control plane's login on disk;
    the sbx path already names it.
  • One cached login per control plane (the issue's option 3) — deferred by the issue author;
    it touches the refresh lock, logout and transcript wiring.
  • core/transcripts.ts gates a transcript on the same exact-string compare. Harmless today (both
    sides come from one normalised value), but it will drift from the auth comparison.

Assisted-By: Claude Code

`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]>
@cwiklik
cwiklik requested a review from pdettori as a code owner October 29, 2026 20:59
@cwiklik cwiklik added the ready-for-ai-review Request automated AI code review from clawgenti label Oct 29, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cwiklik
cwiklik merged commit 3ae3378 into rossoctl:main Oct 30, 2026
17 checks passed
webchang added a commit to webchang/moca that referenced this pull request Nov 2, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mocactl: login silently ignored when control-plane URL differs from the one at login

2 participants