Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

1.8 KB
Raw Blame
import { env } from "../../env";
import {
  type OutboundUrlValidationWhitelist,
  OutboundUrlValidationError,
  parseOutboundUrl,
  validateOutboundUrlHost,
} from "../outbound-url/validation";

export type LlmBaseUrlValidationWhitelist = OutboundUrlValidationWhitelist;

const EMPTY_LLM_BASE_URL_WHITELIST: LlmBaseUrlValidationWhitelist = {
  hosts: [],
  ips: [],
  ip_ranges: [],
  allowedHosts: [],
};

export function llmBaseUrlWhitelistFromEnv(): LlmBaseUrlValidationWhitelist {
  if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {
    return EMPTY_LLM_BASE_URL_WHITELIST;
  }

  return {
    hosts: env.LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST || [],
    ips: env.LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS || [],
    ip_ranges: env.LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS || [],
    allowedHosts: env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS,
  };
}

export async function validateLlmConnectionBaseURL(
  urlString: string,
  whitelist: LlmBaseUrlValidationWhitelist = llmBaseUrlWhitelistFromEnv(),
): Promise<void> {
  const effectiveWhitelist = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION
    ? EMPTY_LLM_BASE_URL_WHITELIST
    : whitelist;

  const url = parseOutboundUrl(urlString);

  if (!["https:", "http:"].includes(url.protocol)) {
    throw new OutboundUrlValidationError(
      "protocol-not-allowed",
      "Only HTTP and HTTPS protocols are allowed",
    );
  }

  await validateOutboundUrlHost({
    url,
    whitelist: effectiveWhitelist,
    logContext: "LLM base URL",
    // Existing LLM validation accepts public IP literals after CIDR checks so
    // custom gateways are not forced through DNS at write time.
    shouldSkipDnsCheckForLiteralIps: true,
  });

  if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION && url.protocol !== "https:") {
    throw new OutboundUrlValidationError(
      "https-required",
      "Only HTTPS base URLs are allowed on Langfuse Cloud",
    );
  }
}