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

1.0 KB
Raw Blame
/**
 * A model id is part of the upstream URL for many providers (`.../models/{id}:generateContent`,
 * `.../deployments/{id}/chat/completions`, ...), so it must not be able to change that path. The
 * URL parser folds `%2e%2e` into a dot-segment, ends the path at `#` and starts a query at `?`,
 * which lets a caller aim a request that carries the operator's credential at another endpoint of
 * the provider, or swap the model for one their key is not allowed to use while the name they
 * sent still matches an allow-list pattern.
 *
 * Returns true when `modelId` contains a query or fragment delimiter, a backslash, a
 * percent-encoded dot, slash, backslash, `#`, `?` or NUL, or a `.` / `..` path segment. Slashes,
 * colons, `@`, brackets and dots inside names are ordinary in model ids and are allowed.
 */
export function hasUnsafeModelIdSyntax(modelId: string): boolean {
  if (/[?#\\]/.test(modelId)) return true;
  if (/%(?:2e|2f|5c|23|3f|00)/i.test(modelId)) return true;
  return modelId.split("/").some((segment) => segment === "." || segment === "..");
}