Skip to content

fix(xai-oauth): add --paste-code flag for WSL2 firewall workaround#27523

Closed
levi951 wants to merge 1 commit into
NousResearch:mainfrom
levi951:fix/xai-oauth-paste-code-wsl2
Closed

fix(xai-oauth): add --paste-code flag for WSL2 firewall workaround#27523
levi951 wants to merge 1 commit into
NousResearch:mainfrom
levi951:fix/xai-oauth-paste-code-wsl2

Conversation

@levi951

@levi951 levi951 commented May 17, 2026

Copy link
Copy Markdown

Summary

WSL2 ships with the Hyper-V Firewall DefaultInboundAction=Block (Microsoft's current default with mirrored networking). xAI's OAuth redirect to http://127.0.0.1:56121/callback is silently dropped before reaching the WSL VM. xAI then renders its "Could not establish connection" fallback page showing the auth code as text — but stock Hermes has no way to accept that code, so the listener times out and the code is orphaned.

This PR adds the --code-flag-equivalent that PR #27305 mentioned was needed but not yet shipped.

Changes

  • New --paste-code flag on hermes auth add (auto-enabled when --no-browser is set, so the existing remote-session UX gets the fix for free)
  • _xai_wait_for_callback spins up a daemon thread that reads one line from stdin and feeds the pasted code into the same callback handler via internal HTTP (127.0.0.1:<port>/callback?code=...&state=...)
  • First-write-wins: whichever channel (browser redirect or stdin paste) delivers a code first short-circuits the wait loop
  • No behavior change when the flag is absent — strict superset

UX

hermes auth add xai-oauth --type oauth --no-browser --paste-code
# → Open this URL: https://auth.x.ai/...
# → Waiting for callback on http://127.0.0.1:56121/callback
# → If your browser shows "Could not establish connection" with a code,
# → paste the code here and press Enter (or wait for browser callback):
# (user pastes "abc-123-xyz", hits Enter)
# → Login successful!

Why it works

The stdin reader doesn't bypass any OAuth security — the pasted code still flows through the same _make_xai_callback_handler that the browser redirect would hit, which validates state and exchanges the code via PKCE. We just route the code over loopback inside the same kernel that's running the listener, avoiding the Windows-side firewall entirely.

Affected platforms

Refs

Test plan

  • hermes auth add xai-oauth --type oauth --no-browser --paste-code on Beelink WSL2 — paste-from-fallback-page completes auth, auth.json populated
  • hermes auth add --help shows the new --paste-code flag
  • Backwards compat: hermes auth add xai-oauth --type oauth without --paste-code behaves identically to before
  • First-write-wins: if both browser callback AND stdin paste arrive, the handler short-circuits on whichever lands first
  • No stdin available (non-TTY background process): paste reader skipped silently, listener runs as before

🤖 Generated with Claude Code

WSL2 ships with Hyper-V Firewall DefaultInboundAction=Block (Microsoft's
current default with mirrored networking). xAI's OAuth redirect to
http://127.0.0.1:56121/callback is silently dropped before reaching the
WSL VM. xAI then renders its "Could not establish connection" fallback
page showing the auth code as text — but stock Hermes has no way to
accept that code, so the listener times out and the code is orphaned.

This adds a stdin-paste path to `hermes auth add xai-oauth`:
- New `--paste-code` flag (auto-enabled when `--no-browser` is set)
- `_xai_wait_for_callback` spins up a daemon thread that reads one line
  from stdin and feeds the pasted code into the same callback handler
  via internal HTTP (127.0.0.1:<port>/callback?code=...&state=...)
- First-write-wins: whichever channel (browser redirect or stdin paste)
  delivers a code first short-circuits the wait loop

Works on any platform with the loopback callback blocked: WSL2 + Hyper-V
Firewall, GCP Cloud Shell, GitHub Codespaces, AWS Instance Connect,
restrictive corporate proxies, etc.

Refs: PR #27305 (community wrapper writeup), issue #27385 (loopback
callback edge cases), issue #26923 (remote console OAuth fallback).
@alt-glitch alt-glitch added type/feature New feature or request P3 Low — cosmetic, nice to have comp/cli CLI entry point, hermes_cli/, setup wizard area/auth Authentication, OAuth, credential pools provider/xai xAI (Grok) labels May 17, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Closing in favor of #26929 (submitted first, same gap). #26929 covers the same use case with broader scope:

  • --manual-paste flag on both hermes auth add and hermes model
  • Broadens _is_remote_session() to include CLOUD_SHELL / CODESPACES / GITPOD_WORKSPACE_ID / REPL_ID / STACKBLITZ
  • Tests + docs (oauth-over-ssh.md, xai-grok-oauth.md)

Your stdin-thread first-write-wins pattern is clever; we may revisit it as a future enhancement to allow the listener and stdin path to race when both are available. Thanks for surfacing the WSL2 firewall workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/auth Authentication, OAuth, credential pools comp/cli CLI entry point, hermes_cli/, setup wizard P3 Low — cosmetic, nice to have provider/xai xAI (Grok) type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants