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.2 KB
Raw Blame
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";

// A Redis that always claims the credential is valid, and whose DEL fails.
const store = new Map<string, string>();
vi.mock("@/shared/utils/rateLimiter", () => ({
  isRedisConfigured: () => true,
  getRedisClient: async () => ({
    get: async (k: string) => store.get(k) ?? null,
    set: async (k: string, v: string) => {
      store.set(k, v);
    },
    del: async () => {
      throw new Error("redis down");
    },
  }),
}));

let apiKeys: typeof import("@/lib/db/apiKeys");
let core: typeof import("@/lib/db/core");
let dataDir: string;

beforeAll(async () => {
  dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-redis-auth-"));
  process.env.DATA_DIR = dataDir;
  process.env.API_KEY_SECRET = "test-api-key-secret-0123456789abcdef";
  vi.stubEnv("NODE_ENV", "production");
  delete process.env.OMNIROUTE_DISABLE_REDIS_AUTH_CACHE;
  core = await import("@/lib/db/core");
  apiKeys = await import("@/lib/db/apiKeys");
});

afterAll(() => {
  core.resetDbInstance();
  fs.rmSync(dataDir, { recursive: true, force: true });
  vi.unstubAllEnvs();
});

describe("API-key validation with the Redis auth cache (GHSA-66vh-35g3-78qv)", () => {
  it("rejects a revoked key even when Redis still holds a positive entry and DEL fails", async () => {
    const created = await apiKeys.createApiKey("redis-revoke", "machine-1");
    expect(await apiKeys.validateApiKey(created.key)).toBe(true);
    expect(store.size).toBeGreaterThan(0);

    await apiKeys.revokeApiKey(created.id);
    // The stale positive entry is still there; the process-local cache is cleared by revoke.
    expect(store.size).toBeGreaterThan(0);
    expect(await apiKeys.validateApiKey(created.key)).toBe(false);
  });

  it("rejects the old credential after regeneration when the Redis entry survives", async () => {
    const created = await apiKeys.createApiKey("redis-regen", "machine-1");
    expect(await apiKeys.validateApiKey(created.key)).toBe(true);

    await apiKeys.regenerateApiKey(created.id);
    expect(await apiKeys.validateApiKey(created.key)).toBe(false);
  });
});