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
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { type AccessScope } from "@/lib/accessTokens/scopes";
/**
* Required-scope inference for remote CLI access tokens.
*
* Policy (owner-confirmed 2026-06-19): infer from HTTP method —
* - GET/HEAD/OPTIONS → read
* - POST/PUT/PATCH/DELETE → write
* with three admin overrides:
* - ADMIN_SCOPE_PREFIXES: admin for ANY method (inherently sensitive surfaces).
* - ADMIN_MUTATION_PREFIXES: admin only when mutating; GET/HEAD stay read so a
* `read` token can still inspect status under these prefixes.
* - ADMIN_MUTATION_EXACT_PATHS: same as above, but for one route only (no sub-routes).
*
* This covers every existing management route with zero per-route edits. A NEW
* mutating route nasce exigindo `write` por padrão (não `admin`) — quando uma
* rota nova for sensível, adicione seu prefixo a uma das listas abaixo.
*
* Note: this only governs the access-token credential path. Dashboard JWT, the
* loopback CLI machine-id token, and manage-scope API keys remain full-access.
* Loopback-only routes that spawn processes are blocked before auth regardless.
*/
/** Sensitive management surfaces — require `admin` for ALL methods. */
export const ADMIN_SCOPE_PREFIXES: readonly string[] = [
"/api/cli/tokens", // access-token management (create/list/revoke)
"/api/oauth", // OAuth authorization flows
"/api/auth", // login / logout / session
"/api/policy", // policy engine
"/api/services", // embedded-service lifecycle (also loopback-blocked)
"/api/mcp", // MCP process surface (also loopback-blocked)
];
/** Require `admin` only for mutating methods; GET/HEAD under these stay `read`. */
export const ADMIN_MUTATION_PREFIXES: readonly string[] = [
"/api/providers", // POST add provider / rotate key = admin; GET status = read
"/api/cli-tools/apply", // writes config onto the host filesystem
// Creating/patching/regenerating an API key can attach the `manage` scope, and a
// manage-scoped key is full-access on every management route (including token mint),
// so a `write` token must not be able to reach it (GHSA-35gq-52m5-wgw2).
"/api/keys",
];
/**
* Require `admin` only for mutating methods on these EXACT paths (no sub-route matching).
*
* A write to the general settings route or to the login-settings route can change the
* operator's own authentication policy (login enforcement, the management password, OIDC
* identity, CORS origins, brute-force protection), so a delegated `write` token must not
* reach them; GET stays `read`. The other `/api/settings/*` sub-routes (proxies, combo
* defaults, model aliases, compression, purges…) are ordinary configuration and keep the
* method-inferred scope — matching `/api/settings` as a prefix would raise all of them.
*/
const ADMIN_MUTATION_EXACT_PATHS: readonly string[] = [
"/api/settings",
"/api/settings/require-login",
];
const READ_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
function matchesPrefix(path: string, prefixes: readonly string[]): boolean {
// Exact match or a true path-segment boundary (`pre/...`). A bare
// `startsWith(pre)` would over-match lookalikes (e.g. "/api/auth" vs
// "/api/authz-inventory"), so it is intentionally NOT used.
return prefixes.some((pre) => path === pre || path.startsWith(pre + "/"));
}
function matchesExactPath(path: string, paths: readonly string[]): boolean {
// A single trailing slash is the same route; anything deeper is a different route.
const normalized = path.length > 1 && path.endsWith("/") ? path.slice(0, -1) : path;
return paths.includes(normalized);
}
/**
* Determine the minimum access-token scope required to call `method path`.
* Pure + deterministic — safe to unit test exhaustively.
*/
export function inferRequiredScope(method: string, path: string): AccessScope {
const m = (method || "GET").toUpperCase();
const p = path || "/";
if (matchesPrefix(p, ADMIN_SCOPE_PREFIXES)) return "admin";
// Revealing a key hands back its plaintext secret — never a `read` operation.
if (matchesPrefix(p, ["/api/keys"]) && /\/reveal\/?$/.test(p)) return "admin";
const isMutation = !READ_METHODS.has(m);
if (isMutation && matchesPrefix(p, ADMIN_MUTATION_PREFIXES)) return "admin";
if (isMutation && matchesExactPath(p, ADMIN_MUTATION_EXACT_PATHS)) return "admin";
return isMutation ? "write" : "read";
}