chat-ui / src /lib /server /urlSafety.ts
victor's picture
victor HF Staff
mcp: clarify pre-safety empty selection vs safety rejection; trim URLs in isValidUrl to avoid whitespace false negatives
11e48d8
raw
history blame
629 Bytes
// Shared server-side URL safety helper (exact behavior preserved)
export function isValidUrl(urlString: string): boolean {
try {
const url = new URL(urlString.trim());
// Only allow HTTPS protocol
if (url.protocol !== "https:") {
return false;
}
// Prevent localhost/private IPs (basic check)
const hostname = url.hostname.toLowerCase();
if (
hostname === "localhost" ||
hostname.startsWith("127.") ||
hostname.startsWith("192.168.") ||
hostname.startsWith("172.16.") ||
hostname === "[::1]" ||
hostname === "0.0.0.0"
) {
return false;
}
return true;
} catch {
return false;
}
}