File size: 6,893 Bytes
88c4c60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({
nextResponse: Symbol("next"),
jsonResponse: vi.fn((body, init) => ({
status: init?.status || 200,
body,
})),
getSettings: vi.fn(),
validateApiKey: vi.fn(),
getConsistentMachineId: vi.fn(),
verifyDashboardAuthToken: vi.fn(),
}));
vi.mock("next/server", () => ({
NextResponse: {
next: vi.fn(() => mocks.nextResponse),
json: mocks.jsonResponse,
redirect: vi.fn((url) => ({ status: 307, url })),
},
}));
vi.mock("@/lib/localDb", () => ({
getSettings: mocks.getSettings,
validateApiKey: mocks.validateApiKey,
}));
vi.mock("@/shared/utils/machineId", () => ({
getConsistentMachineId: mocks.getConsistentMachineId,
}));
vi.mock("@/lib/auth/dashboardSession", () => ({
verifyDashboardAuthToken: mocks.verifyDashboardAuthToken,
}));
const { proxy, __test__ } = await import("../../src/dashboardGuard.js");
function request(pathname, headers = {}) {
const normalizedHeaders = new Headers(headers);
return {
nextUrl: { pathname },
headers: normalizedHeaders,
cookies: { get: vi.fn(() => undefined) },
url: `http://localhost${pathname}`,
};
}
describe("dashboard guard public LLM API access", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.getSettings.mockResolvedValue({ requireLogin: true });
mocks.validateApiKey.mockResolvedValue(false);
mocks.getConsistentMachineId.mockResolvedValue("cli-token");
mocks.verifyDashboardAuthToken.mockResolvedValue(false);
});
it("allows loopback public LLM API without API key", async () => {
const response = await proxy(request("/v1/chat/completions", { host: "localhost:20128" }));
expect(response).toBe(mocks.nextResponse);
expect(mocks.validateApiKey).not.toHaveBeenCalled();
});
it("rejects remote rewritten public LLM API without API key", async () => {
const response = await proxy(request("/api/v1/chat/completions", { host: "router.example.com" }));
expect(response.status).toBe(401);
expect(response.body.error).toBe("API key required for remote API access");
});
it("allows loopback rewritten public LLM API without API key", async () => {
const response = await proxy(request("/api/v1/chat/completions", { host: "localhost:20128" }));
expect(response).toBe(mocks.nextResponse);
expect(mocks.validateApiKey).not.toHaveBeenCalled();
});
it("rejects remote beta public LLM API without API key", async () => {
const response = await proxy(request("/v1beta/models", { host: "router.example.com" }));
expect(response.status).toBe(401);
expect(response.body.error).toBe("API key required for remote API access");
});
it("rejects remote rewritten beta public LLM API without API key", async () => {
const response = await proxy(request("/api/v1beta/models", { host: "router.example.com" }));
expect(response.status).toBe(401);
expect(response.body.error).toBe("API key required for remote API access");
});
it("allows remote public LLM API with valid bearer API key", async () => {
mocks.validateApiKey.mockResolvedValue(true);
const response = await proxy(request("/api/v1/chat/completions", {
host: "router.example.com",
authorization: "Bearer sk-valid",
}));
expect(response).toBe(mocks.nextResponse);
expect(mocks.validateApiKey).toHaveBeenCalledWith("sk-valid");
});
it("allows remote public LLM API with valid x-api-key", async () => {
mocks.validateApiKey.mockResolvedValue(true);
const response = await proxy(request("/v1/web/fetch", {
host: "router.example.com",
"x-api-key": "sk-valid",
}));
expect(response).toBe(mocks.nextResponse);
expect(mocks.validateApiKey).toHaveBeenCalledWith("sk-valid");
});
it("allows remote rewritten beta public LLM API with valid API key", async () => {
mocks.validateApiKey.mockResolvedValue(true);
const response = await proxy(request("/api/v1beta/models", {
host: "router.example.com",
"x-api-key": "sk-valid",
}));
expect(response).toBe(mocks.nextResponse);
expect(mocks.validateApiKey).toHaveBeenCalledWith("sk-valid");
});
});
describe("dashboard guard local-only access", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.getSettings.mockResolvedValue({ requireLogin: true });
mocks.validateApiKey.mockResolvedValue(false);
mocks.getConsistentMachineId.mockResolvedValue("cli-token");
mocks.verifyDashboardAuthToken.mockResolvedValue(false);
});
it("rejects local-only route from non-loopback host without CLI token", async () => {
const response = await proxy(request("/api/mcp/filesystem/sse", {
host: "router.example.com",
}));
expect(response.status).toBe(403);
expect(response.body.error).toBe("Local only: CLI token required");
});
it("rejects local-only route on loopback when requireLogin=true and no JWT", async () => {
const response = await proxy(request("/api/mcp/filesystem/sse", {
host: "localhost:20128",
origin: "http://localhost:20128",
}));
expect(response.status).toBe(403);
expect(response.body.error).toBe("Local only: CLI token required");
});
it("allows local-only route on loopback when requireLogin=false", async () => {
mocks.getSettings.mockResolvedValue({ requireLogin: false });
const response = await proxy(request("/api/cli-tools/antigravity-mitm", {
host: "localhost:20128",
origin: "http://localhost:20128",
}));
expect(response).toBe(mocks.nextResponse);
});
it("rejects local-only route from tunnel host even when requireLogin=false", async () => {
mocks.getSettings.mockResolvedValue({ requireLogin: false });
const response = await proxy(request("/api/cli-tools/antigravity-mitm", {
host: "router.example.com",
}));
expect(response.status).toBe(403);
});
it("rejects local-only route when Origin is non-loopback (CSRF block)", async () => {
mocks.getSettings.mockResolvedValue({ requireLogin: false });
const response = await proxy(request("/api/cli-tools/antigravity-mitm", {
host: "localhost:20128",
origin: "http://evil.example.com",
}));
expect(response.status).toBe(403);
});
it("allows local-only route with valid CLI token", async () => {
const response = await proxy(request("/api/mcp/filesystem/sse", {
host: "router.example.com",
"x-9r-cli-token": "cli-token",
}));
expect(response).toBe(mocks.nextResponse);
});
});
describe("dashboard guard helpers", () => {
it("extracts bearer API keys before x-api-key", () => {
const apiRequest = request("/v1/chat/completions", {
authorization: "Bearer bearer-key",
"x-api-key": "header-key",
});
expect(__test__.extractApiKey(apiRequest)).toBe("bearer-key");
});
});
|