Skip to content

feat(mocactl): login copies the code and opens the browser - #610

Merged
pdettori merged 3 commits into
rossoctl:mainfrom
esnible:feat/mocactl-login-open-browser
Oct 26, 2026
Merged

pdettori merged 3 commits into
rossoctl:mainfrom
esnible:feat/mocactl-login-open-browser

Conversation

@esnible

@esnible esnible commented Oct 26, 2026

Copy link
Copy Markdown
Member

Summary

mocactl login (and mocactl sbx login) printed the GitHub device URL and code and left the user to open a browser by hand. In a local terminal they now also:

  1. copy the user code to the clipboard, then
  2. open the verification URL in the default browser (open / xdg-open / start),

so the user pastes the code and approves. GitHub's device flow has no verification_uri_complete, so the code cannot be pre-filled in the URL. The TUI login overlay already offered this through its c/o keys; this reuses the same OsDeps.copy/openUrl, which opens only http(s) URLs.

  • Only when stdin and stderr are TTYs and there is no SSH_CONNECTION/SSH_TTY/SSH_CLIENT; scripts, CI and remote shells keep the print-only behaviour.
  • --no-browser turns it off. The flag is refused on other commands.
  • A clipboard failure is not an error: the browser still opens and the printed code still works.

Testing

  • New tests: copy-then-open order, browser still opens when the clipboard fails, loginHelpers TTY/SSH gating, --no-browser refused elsewhere.
  • tsc --noEmit and pre-commit pass. In the full mocactl suite, 1 of 1,492 tests failed: a TUI test in test/overlay-misc.test.tsx (not touched here) timed out under parallel load and passed when rerun on its own.

Assisted-By: Claude (Anthropic AI) [email protected]

In a local terminal (stdin and stderr are TTYs, not over SSH),
`mocactl login` and `mocactl sbx login` now copy the device code to the
clipboard and open the verification URL, so the user only pastes the
code and approves. GitHub's device flow has no
verification_uri_complete, so the code cannot be carried in the URL.
`--no-browser` keeps the print-only behaviour.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Ed Snible <[email protected]>
@esnible
esnible requested a review from pdettori as a code owner October 26, 2026 15:55
…#610 review)

- Skip the browser and clipboard under sudo, and off macOS/Windows
  without DISPLAY or WAYLAND_DISPLAY (headless, docker exec, virtual
  console, mosh/sudo sessions that lost SSH_*).
- Open the browser after 1 s if the clipboard helper hangs.
- Copy the user code only when it is device-code shaped, so a hostile
  control plane cannot put arbitrary text on the clipboard.
- Catch a throwing openUrl instead of leaving an unhandled rejection.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Ed Snible <[email protected]>
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Oct 26, 2026

@cwiklik cwiklik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A tight, well-bounded UX change with unusually careful hardening. The gating predicate (loginHelpers) defaults in the right direction — it touches neither the clipboard nor the browser unless there is a local TTY, no SSH_*, no SUDO_USER, and a display off macOS/Windows — and every branch has a test.

The security-relevant claims hold at their source. openableUrl allowlists http:/https: through new URL() and openCommand passes the normalized href as a separate argv element with no shell, so the server-supplied verificationUri cannot become a file: path or a custom scheme handler. USER_CODE is correctly anchored, and the 'ABCD-1234\nrm -rf ~\n' test proves a hostile control plane cannot plant shell-pasteable clipboard content.

Three things I checked that could have been problems and aren't:

  • Wiring is complete. main.ts is the only place the real implementation is constructed, and the single bin entry reaches both main() and sbxMain through deps.loginHelpers — so mocactl sbx login is not silently helper-less.
  • The fire-and-forget void copied… block is not awaited by cmdLogin, so in principle the process could exit before the browser opens; deviceLogin then polls at interval: 5s, so there is ample time.
  • timer.unref() + clearTimeout(timer) is the right pairing: unref keeps the 1s timer from holding the event loop open, and the clear runs on both race outcomes.

Two nits below, both cosmetic.

Areas reviewed: TypeScript (CLI, headless, os, sbx), tests, docs
Agent/IDE config (.claude/.vscode): none
Commits: 2, both signed off, Assisted-By per CLAUDE.md
CI: 15/15 passing, mergeable: MERGEABLE

Approving — no must-fix findings.

Assisted-By: Claude (Anthropic AI) [email protected]


`mocactl login` prints a code and the GitHub device URL, then `logged in as <name>`. Login failures that are fixed on
`mocactl login` prints a code and the GitHub device URL, then `logged in as <name>`. In a terminal on your own machine
(not over SSH or sudo; on Linux, only with a `DISPLAY` or `WAYLAND_DISPLAY`) it also copies the code to the clipboard and opens the URL in your browser; `--no-browser` turns that

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit — "on Linux" understates the scope. The condition in loginHelpers is !graphical, i.e. every platform except darwin and win32, which os.test.ts confirms by asserting freebsd alongside linux. The os.ts doc comment gets this right ("off macOS and Windows"); the guide could borrow that wording.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 4616d4d. It now says "on anything but macOS and Windows", matching the os.ts comment.

Comment thread packages/mocactl/src/headless.ts Outdated
clearTimeout(timer);
io.err(
ok ? 'The code is copied to the clipboard; opening the browser…' : 'Opening the browser…',
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit — "Opening the browser…" is printed unconditionally, but openUrl is a silent no-op on two paths: openableUrl rejecting the URL (not http/https), and spawn failing, which realOs swallows via child.on('error', () => undefined).

Neither is reachable in practice from a GitHub device flow, and the printed URL and code above remain the real instruction either way — so this is cosmetic. Worth knowing only because a user whose xdg-open is missing sees a line saying the browser is opening and then nothing happens; the code they need is already on screen, which is what makes it a nit rather than a problem.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Partly addressed in 4616d4d. A URL that openUrl would refuse now gets no browser line, and openUrl is not called. A spawn failure (e.g. no xdg-open) is asynchronous and still cannot be reported at print time; as you note, the printed URL and code cover it.

@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.

Adds clipboard copy and browser open to the device-code login (mocactl login, mocactl sbx login), with a --no-browser opt-out and a TTY/SSH/sudo/display gate. The 44fe063 hardening is good. It only opens http(s) URLs, avoids shell: true, sends the code through clipboardy's stdin, and sanitizes remote strings.

Must-fix: the new automatic openUrl of the server-supplied verificationUri reaches an unescaped cmd /c start "" <url> on Windows, so &/|/%VAR% in the URL are command injection by a hostile or compromised control plane (headless.ts:108; the root cause is in os.ts:37). Also: a clipboard helper that hangs keeps the process alive after login, the --no-browser path itself is untested, and two nits.

Author: esnible (MEMBER — maintainer)
Areas reviewed: TypeScript (mocactl login/os helpers, CLI flags), tests, docs
Agent/IDE config (.claude/.vscode): none
Commits: 2, all signed-off: yes
CI status: passing (15/15)

ok ? 'The code is copied to the clipboard; opening the browser…' : 'Opening the browser…',
);
// openUrl restricts to http/https, so it gets the raw value; the sanitized form is for display.
helpers.openUrl(s.verificationUri);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

must-fix — this now opens the server-supplied verificationUri with no user action. On win32 that goes through openCommand (os.ts:37): cmd /c start "" <url>. libuv only quotes an argument that contains a space or a quote, so cmd.exe parses the URL itself. new URL() leaves &, | and % alone, which means https://evil.example/?a&calc.exe opens the page and then runs calc.exe, and %VAR% gets expanded. USER_CODE already treats the control plane as untrusted, and before this PR the TUI only opened the URL when the user pressed o. Fix: on Windows, open through something that bypasses cmd (rundll32 url.dll,FileProtocolHandler <url> or explorer.exe <url>), or have openableUrl refuse &|<>^%". Add a win32 openCommand test with a URL that contains &. (The bug is in os.ts:37, which is outside the diff, so the comment sits here at the new call site.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 4616d4d. On win32 openCommand now returns rundll32 url.dll,FileProtocolHandler <url>, so no shell parses the URL. The TUI o key goes through the same path, so it is covered too. os.test.ts has a new case with https://evil.example/?a&calc.exe|x%PATH% that asserts the command is rundll32, the URL is passed as one unchanged argument, and neither cmd nor /c appears.

let timer: NodeJS.Timeout | undefined;
const copied = USER_CODE.test(s.userCode)
? Promise.race([
helpers.copy(s.userCode).then(() => true),

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 1 s race lets the browser open, but it never stops the clipboard child. clipboardy's execa process is neither killed nor unref'd, and its pipes keep the event loop alive. main.ts ends with process.exitCode = code, not process.exit. So with a helper that really hangs (an X server that does not answer, a stuck wl-copy), mocactl login prints logged in as … and then never exits, which is the very case the timeout was added for. Kill the child on timeout: spawn the platform tool directly with timeout/AbortSignal, or exit explicitly once login finishes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 4616d4d. clipboardy has no signal or timeout option, so the child cannot be killed from our side. Instead, loginHelpers counts unsettled copies (busy()), and main.ts takes its existing explicit-exit branch when one is still pending: if (ac.signal.aborted || helpers?.busy()) process.stdout.write('', () => process.exit(code)). Every other run still sets process.exitCode. I checked by hand with a pbcopy shim that runs sleep 1000. Before the fix, login printed logged in as … and hung until killed at 15 s. After it, the browser opens at about 1 s and the process exits once login completes.

Comment thread packages/mocactl/src/headless.ts Outdated
.then((ok) => {
clearTimeout(timer);
io.err(
ok ? 'The code is copied to the clipboard; opening the browser…' : 'Opening the browser…',

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 — Opening the browser… is printed even when nothing will open. openUrl silently ignores a non-http(s) verificationUri, and a missing xdg-open (common on WSL, where WSLg sets DISPLAY) is swallowed by child.on('error'). Checking openableUrl(s.verificationUri) before printing would at least avoid the claim when the URL is refused.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 4616d4d. devicePrompt now checks openableUrl(verificationUri) first. If the URL is refused, it skips openUrl and prints only "The code is copied to the clipboard." (when a copy happened). A missing xdg-open (e.g. WSL) still cannot be detected when the line is printed, since spawn fails asynchronously. The printed URL and code remain the instruction in that case.

[['credentials', 'show', 'x'], 'unknown credentials command "show"'],
[['run', 'hi', '--kind', 'bearer'], '--kind only applies to `mocactl credentials add`'],
[['sessions', '--host', 'x'], '--host only applies to `mocactl credentials add`'],
[['sessions', '--no-browser'], '--no-browser only applies to `mocactl login`'],

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 — this only checks that --no-browser is refused on other commands. Nothing asserts that mocactl login --no-browser and mocactl sbx login --no-browser actually drop the helpers (cli.ts values['no-browser'] ? undefined : deps.loginHelpers, and the same in sbx/cli.ts), and the sbx SYNC_FLAGS entry has no refusal test. Add a case with spy helpers plus --no-browser that asserts neither copy nor openUrl is called, and one for sbx templates --no-browser that expects exit 2.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in 4616d4d. cli.test.ts runs login with spy helpers, without the flag (copy and open are both called) and with --no-browser (neither is called). sbx-cli.test.ts does the same for sbx login, and sbx templates --no-browser now exits 2 and names the flag. I also checked the tests catch a regression: with the --no-browser handling removed from cli.ts and sbx/cli.ts, both "with the flag" tests fail.


`mocactl login` prints a code and the GitHub device URL, then `logged in as <name>`. Login failures that are fixed on
`mocactl login` prints a code and the GitHub device URL, then `logged in as <name>`. In a terminal on your own machine
(not over SSH or sudo; on Linux, only with a `DISPLAY` or `WAYLAND_DISPLAY`) it also copies the code to the clipboard and opens the URL in your browser; `--no-browser` turns that

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 — the code (loginHelpers in os.ts) requires DISPLAY/WAYLAND_DISPLAY on every platform except darwin and win32, not only on Linux. It also requires stdin and stderr to both be TTYs, which the docs do not mention. This line is about 180 characters, while the surrounding prose wraps at about 120, so please reflow it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 4616d4d. The paragraph now states that stdin and stderr must both be terminals, says the display check applies to "anything but macOS and Windows", and is reflowed so lines are 108 to 121 columns.

…it past a hung clipboard

- Open URLs on Windows with `rundll32 url.dll,FileProtocolHandler`, not
  `cmd /c start`: cmd.exe parsed `&`, `|` and `%VAR%` in the
  server-supplied verificationUri. This also covers the TUI's `o` key.
- Exit explicitly when a clipboard helper is still running at the end of
  login: clipboardy cannot cancel it, and its pipes kept `mocactl login`
  alive after `logged in as …`.
- Neither open nor mention a browser for a URL openUrl would refuse.
- Test that `login --no-browser` and `sbx login --no-browser` drop the
  helpers, and that sbx refuses the flag elsewhere.
- Guide: state the TTY requirement and that the display check applies to
  every platform but macOS and Windows; reflow to ~120 columns.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Ed Snible <[email protected]>

@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.

Follow-up to the 44fe063 review. I verified all five items fixed at 4616d4d: on Windows, openCommand now uses rundll32 url.dll,FileProtocolHandler with no cmd.exe, which is tested with &/|/%VAR%. A pending clipboard copy now makes main.ts exit explicitly (busy()). No browser line is printed for a URL openUrl would refuse. --no-browser is tested for login and sbx login, and refused for sbx templates. The docs are corrected and reflowed. One nit inline. CI is green.

* Not detected: a tmux or screen server started locally and reattached over SSH (its environment
* is the local one); `--no-browser` covers that.
*/
export interface LoginHelpersWithState extends Pick<OsDeps, 'copy' | 'openUrl'> {

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: LoginHelpersWithState was inserted between the loginHelpers doc block ("The clipboard and browser for a device login, or undefined when…") and the function. So that comment now documents the interface in editors and hovers, and loginHelpers has none. Move the interface above the doc block, or below the function.

@pdettori
pdettori merged commit 2b61839 into rossoctl:main Oct 26, 2026
15 checks passed
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.

4 participants