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

2.8 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";

import PROVIDERS_MAP from "../../src/lib/oauth/providers/index.ts";
import {
  GITHUB_CONFIG,
  KIMI_CODING_CONFIG,
  MUSE_CODE_CONFIG,
} from "../../src/lib/oauth/constants/oauth.ts";

// Regression guard for the OAuth device-flow pollToken double body read.
//
// pollToken used to try `response.json()` first and fall back to
// `response.text()` in the catch. Once `.json()` rejects on a non-JSON body,
// the stream is already consumed, so the `.text()` fallback always throws
// `TypeError: Body is unusable` — rejecting pollToken and surfacing as a
// generic 500 on /api/oauth/<provider>/poll instead of the intended graceful
// `{ error: "invalid_response" }` payload. Non-JSON responses are realistic
// when the OAuth upstream sits behind a CDN/anti-bot HTML error page or a
// proxy interstitial (auth.kimi.com in particular).
//
// The guard drives the real provider modules with a stubbed global fetch.

function stubFetch(body: string, init?: ResponseInit) {
  const originalFetch = globalThis.fetch;
  globalThis.fetch = async () => new Response(body, init);
  return () => {
    globalThis.fetch = originalFetch;
  };
}

const providers = [
  { id: "kimi-coding", config: KIMI_CODING_CONFIG },
  { id: "github", config: GITHUB_CONFIG },
  { id: "muse-code", config: MUSE_CODE_CONFIG },
] as const;

for (const { id, config } of providers) {
  test(`${id} pollToken returns invalid_response (not a rejection) on non-JSON body`, async () => {
    const upstreamBody =
      id === "muse-code"
        ? "<html><body>502 Bad Gateway: muse-sensitive-fixture-15869</body></html>"
        : "<html><body>502 Bad Gateway</body></html>";
    const restore = stubFetch(upstreamBody, {
      status: 502,
      headers: { "content-type": "text/html" },
    });
    try {
      const result = await PROVIDERS_MAP[id].pollToken(config, "device-code-stub");
      assert.equal(result.ok, false);
      assert.equal(result.data.error, "invalid_response");
      if (id === "muse-code") {
        // Muse's fixed-error contract must not expose an arbitrary upstream body (#15869).
        assert.doesNotMatch(
          JSON.stringify(result),
          /502 Bad Gateway|muse-sensitive-fixture-15869|<html>/
        );
      } else {
        assert.match(result.data.error_description, /502 Bad Gateway/);
      }
    } finally {
      restore();
    }
  });

  test(`${id} pollToken still parses JSON bodies`, async () => {
    const restore = stubFetch(JSON.stringify({ error: "authorization_pending" }), {
      status: 400,
      headers: { "content-type": "application/json" },
    });
    try {
      const result = await PROVIDERS_MAP[id].pollToken(config, "device-code-stub");
      assert.equal(result.ok, false);
      assert.equal(result.data.error, "authorization_pending");
    } finally {
      restore();
    }
  });
}