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

3.8 KB
Raw Blame
/**
 * #9057 — API key `allowedConnections` MUST gate no-auth synthetic credentials
 *
 * TDD regression test: an API key pinned via `allowedConnections` to a specific
 * connection must NOT receive synthetic no-auth credentials for free providers
 * (e.g. DuckDuckGo AI Chat).
 */

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-9057-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "9057-test-secret";

const coreDb = await import("../../src/lib/db/core.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const { getProviderCredentials } = await import("../../src/sse/services/auth.ts");
const { isModelAllowedForKey } = await import("../../src/lib/db/apiKeys.ts");

const RESTRICTED_CONNECTION_UUID = "00000000-0000-4000-8000-000000000001";

test.after(() => {
  coreDb.resetDbInstance();
  try {
    fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  } catch {}
});

test("#9057 LAYER1: restricted key gets NO synthetic credentials for DuckDuckGo AI Chat", async () => {
  // LAYER1: getProviderCredentials() with explicit allowedConnections
  // must NOT return synthetic noauth credentials because the synthetic
  // "noauth" connection is never in an explicit allowed-connections list.
  const creds = await getProviderCredentials(
    "duckduckgo-web",
    null,
    [RESTRICTED_CONNECTION_UUID], // allowedConnections restricts to a real UUID
    "gpt-4o-mini"
  );
  assert.equal(
    creds,
    null,
    "DuckDuckGo AI Chat must not leak synthetic credentials for a connection-restricted key"
  );
});

test("#9057 LAYER1: unrestricted key still gets synthetic credentials for DuckDuckGo AI Chat", async () => {
  const creds = await getProviderCredentials(
    "duckduckgo-web",
    null,
    null, // allowedConnections=null means unrestricted
    "gpt-4o-mini"
  );
  assert(creds, "unrestricted key must receive synthetic credentials for DuckDuckGo AI Chat");
  assert.equal(
    (creds as Record<string, unknown>)?.connectionId,
    "noauth",
    "synthetic noauth connection"
  );
});

// The auto combo pins keyless targets to the synthetic connection id, and
// pin-fail-closed turns that pin into the allowlist ["noauth"]. An allowlist
// that explicitly names the synthetic id must still receive it — otherwise
// every keyless target of a fresh install is skipped as "availability".
test("#9057 LAYER1: allowlist that names the synthetic noauth id still gets credentials", async () => {
  const creds = await getProviderCredentials("duckduckgo-web", null, ["noauth"], "gpt-4o-mini");
  assert(creds, "a combo pin to the synthetic noauth connection must resolve");
  assert.equal((creds as Record<string, unknown>)?.connectionId, "noauth");
});

test("#9057 LAYER1: allowlist mixing a real UUID and noauth still gets synthetic credentials", async () => {
  const creds = await getProviderCredentials(
    "duckduckgo-web",
    null,
    [RESTRICTED_CONNECTION_UUID, "noauth"],
    "gpt-4o-mini"
  );
  assert.equal((creds as Record<string, unknown>)?.connectionId, "noauth");
});

test("#9057 LAYER2: isModelAllowedForKey rejects keyless model for disableNonPublicModels key", async () => {
  // Create a key with disableNonPublicModels=true
  const created = await apiKeysDb.createApiKey("dnp-9057", "machine-dnp");
  assert(created, "key must be created");
  const key = created.key;
  await apiKeysDb.updateApiKeyPermissions(created.id, {
    disableNonPublicModels: true,
  });

  const allowed = await isModelAllowedForKey(key, "big-pickle");
  assert.equal(allowed, false, "disableNonPublicModels key must reject keyless models");
});