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

7.2 KB
Raw Blame
/**
 * TDD — #6246 IP leak (part 1, fail-closed).
 *
 * When a proxy is ASSIGNED to a connection (account/provider/global scope) but is
 * dead/inactive, `resolveProxyForConnection` returns a direct result (no alive
 * proxy resolved). The chat path used to egress DIRECTLY in that case, leaking
 * the operator's real IP. The fix is a fail-closed guard: if the only reason a
 * connection resolves to direct is that its ASSIGNED proxy is dead, block instead
 * of leaking. Explicit "proxy off" toggles are a deliberate direct choice and must
 * NOT be treated as a leak.
 *
 * This test exercises the pure DB predicate `hasBlockingProxyAssignment`, which
 * encodes exactly that decision (honoring the global + connection proxy toggles).
 */
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-proxy-6246-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = "test-secret";

const core = await import("../../src/lib/db/core.ts");
const proxiesDb = await import("../../src/lib/db/proxies.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const { safeResolveProxy } = await import("../../src/sse/handlers/chatHelpers.ts");

async function resetStorage() {
  core.resetDbInstance();
  fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
  fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}

function setGlobalProxyEnabled(enabled: boolean) {
  const db = core.getDbInstance();
  db.prepare(
    "INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES ('settings', 'proxyEnabled', ?)"
  ).run(JSON.stringify(enabled));
}

async function makeConnection(): Promise<string> {
  const conn = await providersDb.createProviderConnection({
    provider: "openai",
    authType: "apiKey",
    name: `Conn ${Date.now()} ${Math.random()}`,
    apiKey: "sk-test",
  });
  return (conn as { id: string }).id;
}

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

test("BLOCKS: an account proxy assigned but marked inactive (the IP-leak case)", async () => {
  await resetStorage();
  const connId = await makeConnection();
  const proxy = await proxiesDb.createProxy({
    name: "Dead paid proxy",
    type: "http",
    host: "127.0.0.1",
    port: 9001,
  });
  await proxiesDb.updateProxy(proxy!.id, { status: "inactive" });
  await proxiesDb.assignProxyToScope("account", connId, proxy!.id);

  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId),
    true,
    "a dead assigned proxy must block, not fall back to a direct egress"
  );
});

test("ALLOWS DIRECT: a connection with no proxy assignment at all", async () => {
  await resetStorage();
  const connId = await makeConnection();
  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId),
    false,
    "no assignment = user never configured a proxy = direct is legitimate"
  );
});

test("NOT BLOCKING: an assigned proxy that is still ALIVE", async () => {
  await resetStorage();
  const connId = await makeConnection();
  const proxy = await proxiesDb.createProxy({
    name: "Live proxy",
    type: "http",
    host: "127.0.0.1",
    port: 9002,
  });
  await proxiesDb.assignProxyToScope("account", connId, proxy!.id);

  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId),
    false,
    "an alive assigned proxy resolves normally; nothing to block"
  );
});

test("EXPLICIT DIRECT: global proxyEnabled=false is a deliberate choice, not a leak", async () => {
  await resetStorage();
  const connId = await makeConnection();
  const proxy = await proxiesDb.createProxy({
    name: "Dead proxy",
    type: "http",
    host: "127.0.0.1",
    port: 9003,
  });
  await proxiesDb.updateProxy(proxy!.id, { status: "inactive" });
  await proxiesDb.assignProxyToScope("account", connId, proxy!.id);
  setGlobalProxyEnabled(false);

  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId),
    false,
    "operator turned proxying off globally — direct is intended, do not block"
  );
});

test("BLOCKS: a dead GLOBAL proxy assignment blocks any connection", async () => {
  await resetStorage();
  const connId = await makeConnection();
  const proxy = await proxiesDb.createProxy({
    name: "Dead global proxy",
    type: "http",
    host: "127.0.0.1",
    port: 9004,
  });
  await proxiesDb.updateProxy(proxy!.id, { status: "error" });
  await proxiesDb.assignProxyToScope("global", null, proxy!.id);

  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId),
    true,
    "a dead global proxy assignment must block, not leak direct"
  );
});

test("BLOCKS: a dead COMBO-scoped proxy assignment when the combo name is threaded through (#13469)", async () => {
  await resetStorage();
  const connId = await makeConnection();
  const proxy = await proxiesDb.createProxy({
    name: "Dead combo proxy",
    type: "http",
    host: "127.0.0.1",
    port: 9006,
  });
  await proxiesDb.updateProxy(proxy!.id, { status: "inactive" });
  await proxiesDb.assignProxyToScope("combo", "my-combo", proxy!.id);

  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId, "openai", "my-combo"),
    true,
    "a dead combo-scoped proxy must block when the combo name is passed"
  );
  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId, "openai", "some-other-combo"),
    false,
    "a dead combo-scoped proxy must NOT block a request routed under a different combo"
  );
  assert.equal(
    proxiesDb.hasBlockingProxyAssignment(connId, "openai"),
    false,
    "without a combo name (e.g. a non-combo request), the combo-scoped assignment is irrelevant"
  );
});

test("WIRING #13469: safeResolveProxy fails closed end-to-end for a dead combo-scoped pool", async () => {
  await resetStorage();
  const connId = await makeConnection();
  const proxy = await proxiesDb.createProxy({
    name: "Dead combo proxy (wiring)",
    type: "http",
    host: "127.0.0.1",
    port: 9007,
  });
  await proxiesDb.updateProxy(proxy!.id, { status: "inactive" });
  await proxiesDb.assignProxyToScope("combo", "wired-combo", proxy!.id);

  await assert.rejects(
    () => safeResolveProxy(connId, undefined, "openai", "wired-combo"),
    (err: unknown) => (err as { code?: string }).code === "PROXY_ASSIGNED_UNAVAILABLE",
    "safeResolveProxy must thread comboName through to the guard and block instead of leaking direct egress"
  );

  const resolved = await safeResolveProxy(connId, undefined, "openai");
  assert.deepEqual(
    resolved,
    { proxy: null, level: "direct", levelId: null },
    "the combo scope must not affect a plain (non-combo) request on the same connection"
  );
});

test("BLOCKS: a dead no-auth provider proxy assignment", async () => {
  await resetStorage();
  const proxy = await proxiesDb.createProxy({
    name: "Dead no-auth proxy",
    type: "http",
    host: "127.0.0.1",
    port: 9005,
  });
  await proxiesDb.updateProxy(proxy!.id, { status: "inactive" });
  await proxiesDb.assignProxyToScope("provider", "opencode", proxy!.id);

  assert.equal(
    proxiesDb.hasBlockingProxyAssignment("noauth", "opencode"),
    true,
    "a dead no-auth provider proxy must block instead of allowing direct egress"
  );
});