Files
wrong-notebook/src/lib/utils.ts
margrop 0ae5bc2e12 fix: 拉取模型列表只允许 http/https 地址,去掉末尾斜杠改用无回溯实现
- /api/ai/models 校验管理员填写的服务地址,拒绝 file: 等其他协议
- Gemini 的 API Key 在查询参数中编码
- 去掉末尾斜杠不再使用 /\/+$/ 正则,避免大量斜杠时的回溯开销

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:53:21 +08:00

28 lines
889 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
/** 去掉末尾的所有斜杠(不用正则,避免长串斜杠导致回溯) */
export function trimTrailingSlashes(value: string): string {
let end = value.length
while (end > 0 && value[end - 1] === "/") end--
return value.slice(0, end)
}
/** 校验并规范化 AI 服务地址:只接受 http/https,去掉末尾斜杠;不合法时抛出错误 */
export function normalizeHttpBaseUrl(value: string): string {
let url: URL
try {
url = new URL(value)
} catch {
throw new Error(`Invalid base URL: ${value}`)
}
if (url.protocol !== "http:" && url.protocol !== "https:") {
throw new Error(`Base URL must use http or https: ${value}`)
}
return trimTrailingSlashes(url.toString())
}