Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

4.2 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
import { buildPkceLoopbackMismatchWarning } from "../../src/lib/oauth/utils/pkceLoopbackWarning";

// #8046: Codex (and the other PKCE_CALLBACK_SERVER_PROVIDERS: xai-oauth, grok-cli)
// register a FIXED loopback redirect_uri with the upstream OAuth app. On a LAN-IP
// origin (isLocalhost true, isTrueLocalhost false — e.g. 192.168.*/10.*/172.16-31.*),
// OAuthModal used to fall straight through to the standard authorize flow and
// window.open() an authUrl whose embedded redirect_uri can never resolve back to the
// dashboard, with zero warning — surfacing as a silent Auth0 `invalid_state` failure.
//
// Source-level guard (like the sibling oauth-modal-grok-cli-browser-login-7013.test.ts):
// OAuthModal is a "use client" component with heavy runtime deps (next-intl,
// popup/fetch orchestration); pinning the fix by source inspection + a direct unit
// test of the extracted warning-message helper is the reliable check here.
const here = dirname(fileURLToPath(import.meta.url));
const modal = readFileSync(resolve(here, "../../src/shared/components/OAuthModal.tsx"), "utf8");

function extractSet(constName: string): string[] {
  const match = modal.match(new RegExp(`const ${constName} = new Set\\(\\[([^\\]]*)\\]\\)`));
  assert.ok(match, `expected to find ${constName} in OAuthModal.tsx`);
  return match![1]
    .split(",")
    .map((s) => s.trim().replace(/^"|"$/g, ""))
    .filter(Boolean);
}

test("PKCE_CALLBACK_SERVER_PROVIDERS still includes codex (anchor)", () => {
  assert.ok(extractSet("PKCE_CALLBACK_SERVER_PROVIDERS").includes("codex"));
});

test("codex/openai redirectUri is still hardcoded to localhost:1455 (anchor, unchanged)", () => {
  assert.match(modal, /redirectUri = "http:\/\/localhost:1455\/auth\/callback"/);
});

test("PKCE callback-server providers now warn (not window.open) on a LAN-IP origin (#8046 fix)", () => {
  // The callback-server branch must gain an isLocalhost-but-not-isTrueLocalhost arm
  // that surfaces the loopback mismatch instead of falling through silently.
  // The follow-up swapped the flat warning string for the structured hint that feeds
  // the dedicated panel (buildPkceLoopbackMismatchHint) — the guard itself is unchanged.
  const arm = modal.match(/else if \(isLocalhost(?: && [^)]+)?\) \{[\s\S]{0,300}?\n {10}\}/);
  assert.ok(arm, "expected an `else if (isLocalhost)` arm inside the callback-server branch");
  assert.match(
    arm![0],
    /buildPkceLoopbackMismatchHint/,
    "OAuthModal.tsx should surface the loopback mismatch for isLocalhost && !isTrueLocalhost " +
      "inside the PKCE_CALLBACK_SERVER_PROVIDERS branch, instead of silently falling through to " +
      "window.open() an authUrl with an unreachable hardcoded loopback redirect_uri."
  );
  assert.match(
    arm![0],
    /\breturn;/,
    "the arm must stop the flow, not merely annotate it before falling through"
  );
});

test("loopback warning offers the existing manual callback flow", () => {
  const panels = readFileSync(
    resolve(here, "../../src/shared/components/OAuthModalPanels.tsx"),
    "utf8"
  );

  assert.match(
    modal,
    /isLocalhost && !opts\?\.manualLoopback/,
    "the warning guard must be bypassable only through an explicit manual-flow action"
  );
  assert.match(
    modal,
    /onManualInput=\{\(\) => void startOAuthFlow\(\{ manualLoopback: true \}\)\}/,
    "the mismatch panel action must initialize the existing authorization-code flow"
  );
  assert.match(
    panels,
    /<Button onClick=\{onManualInput\} fullWidth>/,
    "the mismatch panel must render a visible manual-entry action next to Cancel"
  );
});

test("buildPkceLoopbackMismatchWarning mentions the fixed redirect and a way forward", () => {
  const msg = buildPkceLoopbackMismatchWarning("codex");
  assert.match(msg, /localhost:1455/);
  assert.match(msg, /LAN IP/i);
  assert.match(msg, /localhost/i);
});

test("buildPkceLoopbackMismatchWarning has a generic fallback for unknown providers", () => {
  const msg = buildPkceLoopbackMismatchWarning("some-future-pkce-provider");
  assert.match(msg, /fixed localhost callback URL/);
});