Spaces:
Runtime error
Runtime error
File size: 5,847 Bytes
cd8bd0a | 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 | import test from "node:test";
import assert from "node:assert/strict";
import { classifyRoute } from "../../../src/server/authz/classify.ts";
import type { RouteClass } from "../../../src/server/authz/types.ts";
interface Case {
name: string;
path: string;
method?: string;
expectedClass: RouteClass;
expectedNormalized?: string;
}
const cases: Case[] = [
{ name: "root /", path: "/", expectedClass: "MANAGEMENT", expectedNormalized: "/" },
{ name: "dashboard root", path: "/dashboard", expectedClass: "MANAGEMENT" },
{ name: "dashboard nested", path: "/dashboard/settings", expectedClass: "MANAGEMENT" },
{ name: "dashboard onboarding", path: "/dashboard/onboarding", expectedClass: "PUBLIC" },
{
name: "/api/v1 base",
path: "/api/v1",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1",
},
{
name: "/api/v1/chat/completions",
path: "/api/v1/chat/completions",
expectedClass: "CLIENT_API",
},
{ name: "/api/v1/responses", path: "/api/v1/responses", expectedClass: "CLIENT_API" },
{ name: "/api/v1/models", path: "/api/v1/models", expectedClass: "CLIENT_API" },
{ name: "/api/v1/embeddings", path: "/api/v1/embeddings", expectedClass: "CLIENT_API" },
{ name: "/api/v1/files", path: "/api/v1/files", expectedClass: "CLIENT_API" },
{ name: "/api/v1/batches", path: "/api/v1/batches", expectedClass: "CLIENT_API" },
{ name: "/api/v1/ws", path: "/api/v1/ws", expectedClass: "CLIENT_API" },
{ name: "/api/mcp/* stays management", path: "/api/mcp/status", expectedClass: "MANAGEMENT" },
{
name: "/v1 alias",
path: "/v1",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1",
},
{
name: "/v1/chat/completions alias",
path: "/v1/chat/completions",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1/chat/completions",
},
{
name: "/v1/v1 double-prefix",
path: "/v1/v1",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1",
},
{
name: "/v1/v1/embeddings double-prefix",
path: "/v1/v1/embeddings",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1/embeddings",
},
{
name: "/chat/completions alias",
path: "/chat/completions",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1/chat/completions",
},
{
name: "/responses alias",
path: "/responses",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1/responses",
},
{
name: "/responses/abc alias",
path: "/responses/abc",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1/responses/abc",
},
{
name: "/codex/* alias collapses to /api/v1/responses",
path: "/codex/anything",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1/responses",
},
{
name: "/models alias",
path: "/models",
expectedClass: "CLIENT_API",
expectedNormalized: "/api/v1/models",
},
{
name: "/api/auth/login is PUBLIC",
path: "/api/auth/login",
method: "POST",
expectedClass: "PUBLIC",
},
{
name: "/api/auth/logout is PUBLIC",
path: "/api/auth/logout",
method: "POST",
expectedClass: "PUBLIC",
},
{
name: "/api/auth/status is PUBLIC",
path: "/api/auth/status",
method: "GET",
expectedClass: "PUBLIC",
},
{ name: "/api/init is PUBLIC", path: "/api/init", method: "POST", expectedClass: "PUBLIC" },
{
name: "/api/monitoring/health is PUBLIC",
path: "/api/monitoring/health",
method: "GET",
expectedClass: "PUBLIC",
},
{
name: "/api/cloud/* is PUBLIC",
path: "/api/cloud/something",
method: "GET",
expectedClass: "PUBLIC",
},
{
name: "/api/oauth/* is PUBLIC",
path: "/api/oauth/callback",
method: "GET",
expectedClass: "PUBLIC",
},
{
name: "/api/sync/bundle is PUBLIC",
path: "/api/sync/bundle",
method: "POST",
expectedClass: "PUBLIC",
},
{
name: "/api/settings/require-login GET is PUBLIC readonly",
path: "/api/settings/require-login",
method: "GET",
expectedClass: "PUBLIC",
},
{
name: "/api/settings/require-login POST is MANAGEMENT",
path: "/api/settings/require-login",
method: "POST",
expectedClass: "MANAGEMENT",
},
{
name: "/api/providers/* MUST stay MANAGEMENT",
path: "/api/providers/openai",
expectedClass: "MANAGEMENT",
},
{ name: "/api/keys MANAGEMENT", path: "/api/keys", expectedClass: "MANAGEMENT" },
{ name: "/api/db/health MANAGEMENT", path: "/api/db/health", expectedClass: "MANAGEMENT" },
{ name: "/api/settings MANAGEMENT", path: "/api/settings", expectedClass: "MANAGEMENT" },
{ name: "/api/audit MANAGEMENT", path: "/api/audit", expectedClass: "MANAGEMENT" },
{
name: "Unknown top-level path defaults MANAGEMENT (fail-closed)",
path: "/totally-unknown",
expectedClass: "MANAGEMENT",
},
];
for (const c of cases) {
test(`classifyRoute: ${c.name}`, () => {
const r = classifyRoute(c.path, c.method ?? "GET");
assert.equal(r.routeClass, c.expectedClass, `routeClass for ${c.path}`);
if (c.expectedNormalized) {
assert.equal(r.normalizedPath, c.expectedNormalized, `normalizedPath for ${c.path}`);
}
});
}
test("classifyRoute returns deterministic result for trailing slash", () => {
const a = classifyRoute("/api/v1/chat/completions", "POST");
const b = classifyRoute("/api/v1/chat/completions/", "POST");
assert.equal(a.routeClass, b.routeClass);
assert.equal(a.normalizedPath, b.normalizedPath);
});
test("classifyRoute strips trailing slash on root only when not '/'", () => {
assert.equal(classifyRoute("/").normalizedPath, "/");
});
test("classifyRoute treats /api/v1 prefix exactly", () => {
assert.equal(classifyRoute("/api/v1abc").routeClass, "MANAGEMENT");
assert.equal(classifyRoute("/api/v1/x").routeClass, "CLIENT_API");
});
|