🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat: add self-hosted outbound host allowlists (#18803)
Tobias Wochingerfa218edcabd7
diff --git a/.agents/skills/security-review/references/outbound-url-validation.md b/.agents/skills/security-review/references/outbound-url-validation.mdindex 3b3617fc68..b24a567553 100644--- a/.agents/skills/security-review/references/outbound-url-validation.md+++ b/.agents/skills/security-review/references/outbound-url-validation.md@@ -91,12 +91,15 @@ Every outbound-URL surface MUST apply **all three** of:new wrapper under `packages/shared/src/server/...` that delegates to`validateOutboundUrlHost` so blocklist behavior, DNS rebinding handling,and credential checks stay centralized.-3. Define the env allowlist trio for self-hosted users who legitimately point+3. Define the env exemption trio for self-hosted users who legitimately pointat private network targets (mirroring`LANGFUSE_WEBHOOK_WHITELISTED_HOST/IPS/IP_SEGMENTS`,`LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST/IPS/IP_SEGMENTS`, and- `LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_HOST/IPS/IP_SEGMENTS`). Do- not share another surface's allowlist; each surface keeps its own.+ `LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_HOST/IPS/IP_SEGMENTS`). Those+ variables only exempt listed targets from the private-address block. A+ positive host list is a separate `*_ALLOWED_HOSTS` variable: when it is+ non-empty, every other host is rejected. Do not share another surface's+ list; each surface keeps its own.4. Add the env vars to `.env*.example` and the package `env.mjs/ts`.5. Call the wrapper from:- every tRPC mutation that writes the URL,@@ -133,19 +136,35 @@ Every outbound-URL surface MUST apply **all three** of:## Env Allowlist Behavior-- Cloud (`NEXT_PUBLIC_LANGFUSE_CLOUD_REGION` set) forces strict mode.- Allowlist env vars are ignored on Cloud, and HTTPS is enforced for surfaces- that require it (LLM base URL, blob storage endpoint).-- Self-hosted reads the per-surface env trio:- - `LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST/IPS/IP_SEGMENTS`- - `LANGFUSE_WEBHOOK_WHITELISTED_HOST/IPS/IP_SEGMENTS`- - `LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_HOST/IPS/IP_SEGMENTS`+- On Langfuse Cloud, LLM connections, blob storage, and external media+ storage ignore both the exemption lists and `*_ALLOWED_HOSTS`. Webhooks and+ SSO discovery still read theirs. HTTPS is enforced for surfaces that require+ it (LLM base URL, blob storage endpoint).+- Self-hosted reads the per-surface exemption trio (`*_WHITELISTED_HOST`,+ `*_WHITELISTED_IPS`, `*_WHITELISTED_IP_SEGMENTS`) for LLM connections,+ webhooks, blob storage, external media storage, and SSO discovery. A+ non-empty `*_ALLOWED_HOSTS` on the same surface is a positive host list:+ listed hosts are trusted (including private targets) and every other host+ is rejected. Unset and empty leave the default-allow behavior in place.+ `LANGFUSE_WEBHOOK_ALLOWED_HOSTS` applies to webhook saves and delivery+ only. `LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS` applies to every outbound LLM+ request, including provider default hosts and Langfuse AI. Bedrock keeps+ the AWS transport (VPC endpoints resolve to private IPs). When the list is+ set, that provider checks the host the transport calls:+ `bedrock-runtime.<region>.<partition>`, or the host of+ `AWS_ENDPOINT_URL_BEDROCK_RUNTIME` / `AWS_ENDPOINT_URL` when set. Blob storage+ has no positive host list. For external media S3 virtual-hosted addressing,+ connect-time validation also trusts `{bucket}.{allowlisted endpoint host}`+ for that bucket.+- `LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS` adds CIDR ranges to the built-in+ private-address block for outbound URL checks that already run. Blob+ storage stays opt-in, so these ranges do not by themselves turn blob+ validation on. Per-surface exemptions still win over those ranges.- Blob storage endpoint validation is **opt-in** today on self-hosted (the- helper is a no-op until the operator configures one of the allowlist env- vars). There is a `TODO(next major)` in- `blobStorageEndpointValidation.ts` to flip the default; until then, do not- rely on blob storage validation for new surfaces — wire your own wrapper- that defaults to strict.+ helper is a no-op until the operator configures one of the exemption env+ vars). There is a `TODO(next major)` in `blobStorageEndpointValidation.ts`+ to flip the default; until then, do not rely on blob storage validation+ for new surfaces — wire your own wrapper that defaults to strict.## Negative Tests (Required)diff --git a/.env.dev.example b/.env.dev.exampleindex 134890d95e..255a3bc42a 100644--- a/.env.dev.example+++ b/.env.dev.example@@ -265,22 +265,36 @@ LANGFUSE_AI_FEATURES_SECRET_KEY="sk-lf-1234567890"LANGFUSE_AI_FEATURES_HOST="http://localhost:3000"LANGFUSE_AI_FEATURES_PROJECT_ID=7a88fb47-b4e2-43b8-a06c-a5ce950dc53a-# Self-hosted only: allow internal LLM proxy hosts/IPs for LLM connection base URLs.+# Self-hosted only: exempt internal LLM proxy hosts/IPs from the private-address block.# LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST=localhost# LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS=127.0.0.1,::1# LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS=127.0.0.0/8+# Self-hosted only: when set, every outbound LLM request must use one of these hosts.+# Includes provider default hosts and Langfuse AI. Bedrock uses bedrock-runtime.<region>.<partition>,+# or the host of AWS_ENDPOINT_URL_BEDROCK_RUNTIME / AWS_ENDPOINT_URL when set.+# Entries are exact hostnames (no scheme or port; IPv6 in brackets).+# LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS=# Self-hosted only: allow internal hosts/IPs for user-configured blob storage endpoints.# LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_HOST=localhost# LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_IPS=127.0.0.1,::1# LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_IP_SEGMENTS=127.0.0.0/8-# Self-hosted only: allow internal hosts/IPs for external media storage endpoints.+# Self-hosted only: exempt internal hosts/IPs for external media storage endpoints.# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_HOST=localhost# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_IPS=127.0.0.1,::1# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_IP_SEGMENTS=127.0.0.0/8-# Allow internal IdP hosts/IPs for SSO OIDC discovery validation.+# Self-hosted only: when set, external media storage endpoints must use one of these hosts.+# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_ALLOWED_HOSTS=+# Exempt internal IdP hosts/IPs for SSO OIDC discovery validation.# LANGFUSE_SSO_DISCOVERY_WHITELISTED_HOST=localhost# LANGFUSE_SSO_DISCOVERY_WHITELISTED_IPS=127.0.0.1,::1# LANGFUSE_SSO_DISCOVERY_WHITELISTED_IP_SEGMENTS=127.0.0.0/8+# When set, OIDC discovery URLs must use one of these hosts. Checked when SSO is saved.+# LANGFUSE_SSO_DISCOVERY_ALLOWED_HOSTS=+# When set, webhook URLs must use one of these hosts. Other outbound calls are unchanged.+# LANGFUSE_WEBHOOK_ALLOWED_HOSTS=+# Extra CIDR ranges blocked on outbound URL checks that already run. Blob storage+# stays opt-in until its own exemption list or allowed-host list is set.+# LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS=# Langfuse AI. Unset LANGFUSE_AI_PROVIDER is unconfigured. Bedrock requires# LANGFUSE_AI_PROVIDER=bedrock.diff --git a/.env.prod.example b/.env.prod.exampleindex aec0121cf6..46f1d9783b 100644--- a/.env.prod.example+++ b/.env.prod.example@@ -487,22 +487,36 @@ LANGFUSE_ENABLE_BLOB_STORAGE_FILE_LOG=true# Admin API# ADMIN_API_KEY=-# Self-hosted only: allow internal LLM proxy hosts/IPs for LLM connection base URLs.+# Self-hosted only: exempt internal LLM proxy hosts/IPs from the private-address block.# LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST=# LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS=# LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS=+# Self-hosted only: when set, every outbound LLM request must use one of these hosts.+# Includes provider default hosts and Langfuse AI. Bedrock uses bedrock-runtime.<region>.<partition>,+# or the host of AWS_ENDPOINT_URL_BEDROCK_RUNTIME / AWS_ENDPOINT_URL when set.+# Entries are exact hostnames (no scheme or port; IPv6 in brackets).+# LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS=# Self-hosted only: allow internal hosts/IPs for user-configured blob storage endpoints.# LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_HOST=# LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_IPS=# LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_IP_SEGMENTS=-# Self-hosted only: allow internal hosts/IPs for external media storage endpoints.+# Self-hosted only: exempt internal hosts/IPs for external media storage endpoints.# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_HOST=# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_IPS=# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_IP_SEGMENTS=-# Allow internal IdP hosts/IPs for SSO OIDC discovery validation.+# Self-hosted only: when set, external media storage endpoints must use one of these hosts.+# LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_ALLOWED_HOSTS=+# Exempt internal IdP hosts/IPs for SSO OIDC discovery validation.# LANGFUSE_SSO_DISCOVERY_WHITELISTED_HOST=# LANGFUSE_SSO_DISCOVERY_WHITELISTED_IPS=# LANGFUSE_SSO_DISCOVERY_WHITELISTED_IP_SEGMENTS=+# When set, OIDC discovery URLs must use one of these hosts. Checked when SSO is saved.+# LANGFUSE_SSO_DISCOVERY_ALLOWED_HOSTS=+# When set, webhook URLs must use one of these hosts. Other outbound calls are unchanged.+# LANGFUSE_WEBHOOK_ALLOWED_HOSTS=+# Extra CIDR ranges blocked on outbound URL checks that already run. Blob storage+# stays opt-in until its own exemption list or allowed-host list is set.+# LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS=# LANGFUSE_CACHE_MODEL_MATCH_ENABLED=# LANGFUSE_CACHE_MODEL_MATCH_TTL_SECONDS=diff --git a/docker-compose.yml b/docker-compose.ymlindex 2c545ba6f5..9b1ac38e73 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -32,6 +32,12 @@ services:LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST: ${LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST:-}LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS: ${LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS:-}LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS: ${LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS:-}+ # Empty defaults leave egress policy unchanged until an operator sets them.+ LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS: ${LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS:-}+ LANGFUSE_WEBHOOK_ALLOWED_HOSTS: ${LANGFUSE_WEBHOOK_ALLOWED_HOSTS:-}+ LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_ALLOWED_HOSTS: ${LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_ALLOWED_HOSTS:-}+ LANGFUSE_SSO_DISCOVERY_ALLOWED_HOSTS: ${LANGFUSE_SSO_DISCOVERY_ALLOWED_HOSTS:-}+ LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS: ${LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS:-}CLICKHOUSE_MIGRATION_URL: ${CLICKHOUSE_MIGRATION_URL:-clickhouse://clickhouse:9000}CLICKHOUSE_URL: ${CLICKHOUSE_URL:-http://clickhouse:8123}CLICKHOUSE_USER: ${CLICKHOUSE_USER:-clickhouse}diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.tsindex 628ebdca33..b2d7da2efb 100644--- a/packages/shared/src/env.ts+++ b/packages/shared/src/env.ts@@ -1,4 +1,5 @@import { z } from "zod";+import { parseBlockedCidr } from "./server/webhooks/parseBlockedCidr";import { removeEmptyEnvVariables } from "./utils/environment";// Per-segment byte budget for S3 event keys. Off by default (2048 > 800-byte@@ -27,6 +28,23 @@ export const redisSocketTimeoutMsSchema = z.coerce}).default(30_000);+function commaSeparatedLowercase(value: string | undefined): string[] {+ if (!value) return [];+ return value+ .split(",")+ .map((part) => part.toLowerCase().trim())+ .filter((part) => part.length > 0);+}++const optionalCommaSeparatedLowercase = z+ .string()+ .optional()+ .transform(commaSeparatedLowercase);++function isCidr(value: string): boolean {+ return parseBlockedCidr(value) !== undefined;+}+const DEFAULT_LLM_COMPLETION_TIMEOUT_MS = 120_000;const EnvSchema = z.object({@@ -477,6 +495,11 @@ const EnvSchema = z.object({.transform((s) =>s ? s.split(",").map((s) => s.toLowerCase().trim()) : [],),+ // When non-empty, webhook URLs must use one of these hosts. Distinct from+ // LANGFUSE_WEBHOOK_WHITELISTED_HOST, which only exempts a host from the+ // private-address block. Does not apply to other callers of that exemption+ // list. Entries are exact hostnames (no scheme or port; IPv6 in brackets).+ LANGFUSE_WEBHOOK_ALLOWED_HOSTS: optionalCommaSeparatedLowercase,LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS: z.string().optional()@@ -495,6 +518,12 @@ const EnvSchema = z.object({.transform((s) =>s ? s.split(",").map((s) => s.toLowerCase().trim()) : [],),+ // When non-empty, every outbound LLM request must use one of these hosts,+ // including provider default hosts and Langfuse AI. Bedrock checks+ // bedrock-runtime.<region>.<partition>, or the host of+ // AWS_ENDPOINT_URL_BEDROCK_RUNTIME / AWS_ENDPOINT_URL when one of those is+ // set. Ignored on Langfuse Cloud, same as the LLM exemption list above.+ LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS: optionalCommaSeparatedLowercase,LANGFUSE_BLOB_STORAGE_ENDPOINT_WHITELISTED_IPS: z.string().optional()@@ -531,6 +560,10 @@ const EnvSchema = z.object({.transform((s) =>s ? s.split(",").map((s) => s.toLowerCase().trim()) : [],),+ // When non-empty, external media storage endpoints must use one of these+ // hosts. Ignored on Langfuse Cloud.+ LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_ALLOWED_HOSTS:+ optionalCommaSeparatedLowercase,// Buckets that keyless GOOGLE_CLOUD_STORAGE blob exports (default credentials,// no service account key) may write to. Those run as the deployment's own GCP// identity (ADC), so without this allowlist any project owner could export into@@ -565,6 +598,21 @@ const EnvSchema = z.object({.transform((s) =>s ? s.split(",").map((s) => s.toLowerCase().trim()) : [],),+ // When non-empty, OIDC discovery URLs and the endpoints in the discovery+ // document must use one of these hosts. Checked when an SSO configuration+ // is saved, same as the exemption list above.+ LANGFUSE_SSO_DISCOVERY_ALLOWED_HOSTS: optionalCommaSeparatedLowercase,+ // Extra CIDR ranges merged into the private-address block for outbound URL+ // checks that already run. Blob storage stays opt-in: these ranges apply+ // there only when blob endpoint validation is already enabled. Exemptions+ // in the per-surface *_WHITELISTED_* lists still win.+ LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS: optionalCommaSeparatedLowercase.refine(+ (segments) => segments.every(isCidr),+ {+ message:+ "LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS must be a comma-separated list of CIDR ranges",+ },+ ),SLACK_CLIENT_ID: z.string().optional(),SLACK_CLIENT_SECRET: z.string().optional(),SLACK_STATE_SECRET: z.string().optional(),diff --git a/packages/shared/src/server/llm/ai-sdk/providers/bedrock.test.ts b/packages/shared/src/server/llm/ai-sdk/providers/bedrock.test.tsindex 6e49fedd1a..59e78cf14b 100644--- a/packages/shared/src/server/llm/ai-sdk/providers/bedrock.test.ts+++ b/packages/shared/src/server/llm/ai-sdk/providers/bedrock.test.ts@@ -9,6 +9,7 @@ vi.mock("@aws-sdk/credential-providers", () => ({fromNodeProviderChain,}));+import { OutboundUrlValidationError } from "../../../outbound-url";import {assertValidBedrockRegion,buildBedrockModel,@@ -191,3 +192,71 @@ describe("resolveBedrockProviderAuth", () => {).toThrow("Invalid Bedrock credentials");});});++function writeProcessEnv(name: string, value: string | undefined) {+ if (value === undefined) delete process.env[name];+ else process.env[name] = value;+}++describe("Bedrock LLM host allowlist", () => {+ const originalAllowedHosts = env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS;+ const originalCloudRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;+ const runtimeEndpointEnv = "AWS_ENDPOINT_URL_BEDROCK_RUNTIME";+ const originalRuntimeEndpoint = process.env[runtimeEndpointEnv];++ const userConnection = {+ modelId: "test-model",+ apiKey: JSON.stringify({ apiKey: "bedrock-key" }),+ config: { region: "eu-west-1" },+ credentialSource: "user" as const,+ };++ beforeEach(() => {+ env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;+ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = [];+ writeProcessEnv(runtimeEndpointEnv, undefined);+ });++ afterEach(() => {+ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = originalAllowedHosts;+ env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalCloudRegion;+ writeProcessEnv(+ runtimeEndpointEnv,+ typeof originalRuntimeEndpoint === "string"+ ? originalRuntimeEndpoint+ : undefined,+ );+ });++ it("allows the region host only when it is listed", () => {+ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = ["litellm.corp"];+ expect(() => buildBedrockModel(userConnection)).toThrow(+ OutboundUrlValidationError,+ );++ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = [+ "bedrock-runtime.eu-west-1.amazonaws.com",+ ];+ expect(() => buildBedrockModel(userConnection)).not.toThrow();+ });++ it("checks the Bedrock endpoint override instead of the region host", () => {+ writeProcessEnv(runtimeEndpointEnv, "https://vpce.internal/bedrock");+ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = [+ "bedrock-runtime.eu-west-1.amazonaws.com",+ ];+ expect(() => buildBedrockModel(userConnection)).toThrow(+ OutboundUrlValidationError,+ );++ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = ["vpce.internal"];+ expect(() => buildBedrockModel(userConnection)).not.toThrow();+ });++ it("ignores the allowlist on Langfuse Cloud", () => {+ env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "US";+ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = ["litellm.corp"];++ expect(() => buildBedrockModel(userConnection)).not.toThrow();+ });+});diff --git a/packages/shared/src/server/llm/ai-sdk/providers/bedrock.ts b/packages/shared/src/server/llm/ai-sdk/providers/bedrock.tsindex 25ab6e71c6..27e88cc7f6 100644--- a/packages/shared/src/server/llm/ai-sdk/providers/bedrock.ts+++ b/packages/shared/src/server/llm/ai-sdk/providers/bedrock.ts@@ -9,6 +9,11 @@ import {BedrockCredentialSchema,type LLMConnectionConfig,} from "../../../../interfaces/customLLMProviderConfigSchemas";+import {+ OutboundUrlValidationError,+ parseOutboundUrl,+} from "../../../outbound-url";+import { llmBaseUrlWhitelistFromEnv } from "../../baseUrlValidation";import type { LLMCredentialSource, TranslatedProviderOptions } from "./types";import { isPlainObject } from "./utils";@@ -20,6 +25,17 @@ import { isPlainObject } from "./utils";// validation, but this provider builds its own endpoint.const AWS_REGION_PATTERN = /^[a-z0-9-]+$/;+// Same order as @ai-sdk/amazon-bedrock resolveAmazonBedrockBaseURL. An earlier+// prefix wins, including where a longer prefix also matches.+const BEDROCK_PARTITION_DNS_SUFFIXES = [+ { regionPrefix: "cn-", dnsSuffix: "amazonaws.com.cn" },+ { regionPrefix: "us-iso-", dnsSuffix: "c2s.ic.gov" },+ { regionPrefix: "us-isob-", dnsSuffix: "sc2s.sgov.gov" },+ { regionPrefix: "eu-isoe-", dnsSuffix: "cloud.adc-e.uk" },+ { regionPrefix: "us-isof-", dnsSuffix: "csp.hci.ic.gov" },+ { regionPrefix: "eusc-", dnsSuffix: "amazonaws.eu" },+] as const;+export function assertValidBedrockRegion(region: string | undefined): void {if (region !== undefined && !AWS_REGION_PATTERN.test(region)) {throw new Error(@@ -174,11 +190,60 @@ export function resolveBedrockProviderAuth(params: {}}+/** bedrockPartitionDnsSuffix returns the AWS partition suffix for a region. */+function bedrockPartitionDnsSuffix(region: string): string {+ return (+ BEDROCK_PARTITION_DNS_SUFFIXES.find(({ regionPrefix }) =>+ region.startsWith(regionPrefix),+ )?.dnsSuffix ?? "amazonaws.com"+ );+}++/** readProcessEnv reads one variable at call time, matching the Bedrock SDK. */+function readProcessEnv(name: string): string | undefined {+ const value = process.env[name];+ return typeof value === "string" ? value : undefined;+}++/** bedrockRuntimeHostname returns the host the Bedrock runtime transport calls. */+function bedrockRuntimeHostname(region: string | undefined): string {+ const endpoint =+ readProcessEnv("AWS_ENDPOINT_URL_BEDROCK_RUNTIME") ??+ readProcessEnv("AWS_ENDPOINT_URL");+ if (endpoint != null) {+ return parseOutboundUrl(endpoint).hostname;+ }++ const resolvedRegion = region ?? readProcessEnv("AWS_REGION");+ if (!resolvedRegion) {+ throw new Error(+ "Bedrock region is required when an LLM host allowlist is configured.",+ );+ }+ assertValidBedrockRegion(resolvedRegion);+ return `bedrock-runtime.${resolvedRegion}.${bedrockPartitionDnsSuffix(resolvedRegion)}`;+}++/** assertBedrockHostAllowed rejects a Bedrock host outside a configured LLM allowlist. */+function assertBedrockHostAllowed(region: string | undefined): void {+ const allowedHosts = llmBaseUrlWhitelistFromEnv().allowedHosts ?? [];+ if (allowedHosts.length === 0) return;++ const hostname = bedrockRuntimeHostname(region);+ if (allowedHosts.includes(hostname)) return;++ throw new OutboundUrlValidationError(+ "host-not-allowed",+ "Host is not in the outbound URL allowlist",+ );+}+/*** No custom fetch is used: Bedrock has no user-controlled base URL (the host* is derived from the region), and self-hosted deployments commonly reach* Bedrock through VPC endpoints resolving to private IPs, which the secure* LLM fetch would block. Requests therefore use the provider's AWS transport.+ * A configured LLM host allowlist is still applied to that transport's host.*/function createBedrockProvider(params: {projectId?: string;@@ -193,6 +258,7 @@ function createBedrockProvider(params: {? { region: getLangfuseAIBedrockRegion() }: BedrockConfigSchema.parse(config);assertValidBedrockRegion(region);+ assertBedrockHostAllowed(region);const auth = resolveBedrockProviderAuth({secretKey: apiKey,diff --git a/packages/shared/src/server/llm/baseUrlValidation.ts b/packages/shared/src/server/llm/baseUrlValidation.tsindex cf0c6f100c..b8db45ae6f 100644--- a/packages/shared/src/server/llm/baseUrlValidation.ts+++ b/packages/shared/src/server/llm/baseUrlValidation.ts@@ -8,19 +8,23 @@ import {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 {- hosts: [],- ips: [],- ip_ranges: [],- };+ 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,};}@@ -29,11 +33,7 @@ export async function validateLlmConnectionBaseURL(whitelist: LlmBaseUrlValidationWhitelist = llmBaseUrlWhitelistFromEnv(),): Promise<void> {const effectiveWhitelist = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION- ? {- hosts: [],- ips: [],- ip_ranges: [],- }+ ? EMPTY_LLM_BASE_URL_WHITELIST: whitelist;const url = parseOutboundUrl(urlString);diff --git a/packages/shared/src/server/llm/secureLlmFetch.ts b/packages/shared/src/server/llm/secureLlmFetch.tsindex 7059e6495c..e3a9ad90bc 100644--- a/packages/shared/src/server/llm/secureLlmFetch.ts+++ b/packages/shared/src/server/llm/secureLlmFetch.ts@@ -5,7 +5,6 @@ import {MaxRedirectsExceededError,OutboundUrlValidationError,RedirectValidationError,- type OutboundUrlValidationWhitelist,type RequestInitWithDispatcher,} from "../outbound-url";import {@@ -17,13 +16,11 @@ import { LLMValidationError } from "./errors";const MAX_LLM_REDIRECTS = 10;type SecureLlmFetchParams = {- whitelist?: OutboundUrlValidationWhitelist;logContext: string;additionalSensitiveHeaders?: string[];};export function createSecureLlmFetch({- whitelist = llmBaseUrlWhitelistFromEnv(),logContext,additionalSensitiveHeaders,}: SecureLlmFetchParams): typeof fetch {@@ -32,7 +29,6 @@ export function createSecureLlmFetch({const { url, options } = await normalizeFetchInput(input, init);return await fetchSecureLlmUrl(url, options, {- whitelist,logContext,additionalSensitiveHeaders,});@@ -56,12 +52,9 @@ export function createSecureLlmFetch({export async function fetchSecureLlmUrl(url: string,options: RequestInit,- {- whitelist = llmBaseUrlWhitelistFromEnv(),- logContext,- additionalSensitiveHeaders,- }: SecureLlmFetchParams,+ { logContext, additionalSensitiveHeaders }: SecureLlmFetchParams,): Promise<Response> {+ const whitelist = llmBaseUrlWhitelistFromEnv();await validateLlmConnectionBaseURL(url, whitelist);const optionsWithoutDispatcher = stripCallerDispatcher(options);// If the operator configured HTTPS_PROXY, attach the NO_PROXY-aware proxydiff --git a/packages/shared/src/server/outbound-url/connection.ts b/packages/shared/src/server/outbound-url/connection.tsindex f5fd359a4c..da22adc8f1 100644--- a/packages/shared/src/server/outbound-url/connection.ts+++ b/packages/shared/src/server/outbound-url/connection.ts@@ -262,5 +262,6 @@ function normalizeWhitelist(hosts: [...whitelist.hosts].sort(),ips: [...whitelist.ips].sort(),ip_ranges: [...whitelist.ip_ranges].sort(),+ allowedHosts: [...(whitelist.allowedHosts ?? [])].sort(),};}diff --git a/packages/shared/src/server/outbound-url/validation.ts b/packages/shared/src/server/outbound-url/validation.tsindex e4b0bdd9c3..9eda9bd10d 100644--- a/packages/shared/src/server/outbound-url/validation.ts+++ b/packages/shared/src/server/outbound-url/validation.ts@@ -1,5 +1,6 @@import dns from "node:dns/promises";import { URL } from "node:url";+import { env } from "../../env";import { logger } from "../logger";import {isHostnameBlocked,@@ -11,6 +12,7 @@ export type OutboundUrlValidationErrorCode =| "blocked-hostname"| "blocked-ip"| "dns-lookup-failed"+ | "host-not-allowed"| "https-required"| "invalid-encoding"| "invalid-syntax"@@ -32,11 +34,16 @@ export class OutboundUrlValidationError extends Error {// can detect this failure by message when the typed `code` is lost — e.g. after// RedirectValidationError re-wraps the inner error by message text only.export const DNS_LOOKUP_FAILED_MESSAGE_PREFIX = "DNS lookup failed for";+const HOST_NOT_IN_ALLOWLIST_MESSAGE =+ "Host is not in the outbound URL allowlist";export interface OutboundUrlValidationWhitelist {hosts: string[];ips: string[];ip_ranges: string[];+ // Positive host list. Empty or omitted keeps the default-allow behavior:+ // any public host is accepted, and `hosts` only exempts private targets.+ allowedHosts?: string[];}export interface ValidateOutboundUrlHostOptions {@@ -126,6 +133,10 @@ export async function validateOutboundUrlHost({// host safety checks stay tied to the parsed URL component.const hostname = url.hostname;+ if (enforcePositiveAllowlist(hostname, whitelist) === "allowed") {+ return;+ }+if (whitelist.hosts.includes(hostname)) {return;}@@ -170,11 +181,22 @@ export function validateOutboundResolvedIp({whitelist,logContext,}: ValidateOutboundResolvedIpOptions): void {+ if (enforcePositiveAllowlist(hostname, whitelist) === "allowed") {+ return;+ }+if (whitelist.hosts.includes(hostname)) {return;}- if (isIPBlocked(ip, whitelist.ips, whitelist.ip_ranges)) {+ if (+ isIPBlocked(+ ip,+ whitelist.ips,+ whitelist.ip_ranges,+ env.LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS ?? [],+ )+ ) {logger.warn(`${logContext} validation blocked resolved IP address: ${ip} for hostname: ${hostname}`,);@@ -185,6 +207,20 @@ export function validateOutboundResolvedIp({}}+/** enforcePositiveAllowlist returns whether a configured positive host list allows this hostname. */+function enforcePositiveAllowlist(+ hostname: string,+ whitelist: OutboundUrlValidationWhitelist,+): "allowed" | "not-configured" {+ const allowedHosts = whitelist.allowedHosts ?? [];+ if (allowedHosts.length === 0) return "not-configured";+ if (allowedHosts.includes(hostname)) return "allowed";+ throw new OutboundUrlValidationError(+ "host-not-allowed",+ HOST_NOT_IN_ALLOWLIST_MESSAGE,+ );+}+function assertValidUrlEncoding(urlString: string): void {// This intentionally checks encoding validity only. Do not parse or validate// the decoded result: decoding the whole URL can turn encoded data into URLdiff --git a/packages/shared/src/server/services/externalMediaStorageEndpointValidation/externalMediaStorageEndpointValidation.ts b/packages/shared/src/server/services/externalMediaStorageEndpointValidation/externalMediaStorageEndpointValidation.tsindex 4da131da92..4b78719624 100644--- a/packages/shared/src/server/services/externalMediaStorageEndpointValidation/externalMediaStorageEndpointValidation.ts+++ b/packages/shared/src/server/services/externalMediaStorageEndpointValidation/externalMediaStorageEndpointValidation.ts@@ -6,6 +6,10 @@ import {parseOutboundUrl,validateOutboundUrlHost,} from "../../outbound-url";+import {+ type S3VirtualHostTarget,+ withS3VirtualHostedBucket,+} from "../s3VirtualHostedAllowlist";const EXTERNAL_MEDIA_STORAGE_ENDPOINT_VALIDATION_LOG_CONTEXT ="External media storage endpoint";@@ -15,6 +19,7 @@ const STRICT_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELIST: OutboundUrlValidationWhihosts: [],ips: [],ip_ranges: [],+ allowedHosts: [],};function isLangfuseCloud() {@@ -32,6 +37,7 @@ export function externalMediaStorageEndpointWhitelistFromEnv(): OutboundUrlValidip_ranges:env.LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_IP_SEGMENTS ||[],+ allowedHosts: env.LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_ALLOWED_HOSTS,};}@@ -69,22 +75,33 @@ export async function validateExternalMediaStorageEndpoint(if (error instanceof OutboundUrlValidationError) {throw new OutboundUrlValidationError(error.code,- `${error.message}${getSelfHostedWhitelistGuidance()}`,+ `${error.message}${getSelfHostedWhitelistGuidance(error.code)}`,);}throw error;}}-export function externalMediaStorageEndpointConnectionValidationOptions(): OutboundUrlConnectionValidationOptions {+export function externalMediaStorageEndpointConnectionValidationOptions(+ virtualHost?: S3VirtualHostTarget,+): OutboundUrlConnectionValidationOptions {return {- whitelist: externalMediaStorageEndpointWhitelistFromEnv(),+ whitelist: withS3VirtualHostedBucket(+ externalMediaStorageEndpointWhitelistFromEnv(),+ virtualHost,+ ),logContext: EXTERNAL_MEDIA_STORAGE_ENDPOINT_VALIDATION_LOG_CONTEXT,};}-function getSelfHostedWhitelistGuidance() {+function getSelfHostedWhitelistGuidance(+ code: OutboundUrlValidationError["code"],+) {if (isLangfuseCloud()) return "";+ if (code === "host-not-allowed") {+ return " Add the host to LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_ALLOWED_HOSTS.";+ }+return " For self-hosted deployments with internal external media storage endpoints, configure LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_HOST, LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_IPS, or LANGFUSE_EXTERNAL_MEDIA_STORAGE_ENDPOINT_WHITELISTED_IP_SEGMENTS.";}diff --git a/packages/shared/src/server/services/s3VirtualHostedAllowlist.ts b/packages/shared/src/server/services/s3VirtualHostedAllowlist.tsnew file mode 100644index 0000000000..b26144f876--- /dev/null+++ b/packages/shared/src/server/services/s3VirtualHostedAllowlist.ts@@ -0,0 +1,99 @@+import { BLOB_STORAGE_REGION_REGEX } from "../../utils/stringChecks";+import {+ type OutboundUrlValidationWhitelist,+ parseOutboundUrl,+} from "../outbound-url";++/** Bucket names the AWS SDK virtual-hosts as `{bucket}.{endpoint host}`. */+const DNS_COMPATIBLE_BUCKET_NAME = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;+const IP_SHAPED_BUCKET_NAME = /(?:\d+\.){3}\d+/;+const ADJACENT_DOTS = /\.\./;++export type S3VirtualHostTarget = {+ endpoint?: string | null;+ bucketName?: string | null;+ forcePathStyle?: boolean;+ region?: string | null;+};++/** withS3VirtualHostedBucket trusts `{bucket}.{allowlisted endpoint}` and rejects other names under that parent. */+export function withS3VirtualHostedBucket(+ whitelist: OutboundUrlValidationWhitelist,+ target?: S3VirtualHostTarget,+): OutboundUrlValidationWhitelist {+ const allowedHosts = whitelist.allowedHosts ?? [];+ const bucketName = target?.bucketName?.trim() ?? "";+ if (+ allowedHosts.length === 0 ||+ !target ||+ target.forcePathStyle ||+ !isDnsCompatibleBucketName(bucketName)+ ) {+ return whitelist;+ }++ const virtualHosts = virtualHostedNames(target, bucketName, allowedHosts);+ if (virtualHosts.length === 0) return whitelist;+ return {+ ...whitelist,+ allowedHosts: [...allowedHosts, ...virtualHosts],+ };+}++function isDnsCompatibleBucketName(bucketName: string): boolean {+ return (+ DNS_COMPATIBLE_BUCKET_NAME.test(bucketName) &&+ !IP_SHAPED_BUCKET_NAME.test(bucketName) &&+ !ADJACENT_DOTS.test(bucketName)+ );+}++function virtualHostedNames(+ target: S3VirtualHostTarget,+ bucketName: string,+ allowedHosts: string[],+): string[] {+ const names: string[] = [];+ for (const base of addressBases(target)) {+ // HTTPS virtual-hosting skips bucket names that contain a dot; the SDK+ // keeps the endpoint host so the certificate still matches.+ if (base.https && bucketName.includes(".")) continue;+ if (!allowedHosts.includes(base.hostname)) continue;+ const virtualHost = `${bucketName}.${base.hostname}`;+ if (allowedHosts.includes(virtualHost) || names.includes(virtualHost)) {+ continue;+ }+ names.push(virtualHost);+ }+ return names;+}++function addressBases(+ target: S3VirtualHostTarget,+): Array<{ hostname: string; https: boolean }> {+ if (target.endpoint) {+ try {+ const url = parseOutboundUrl(target.endpoint);+ if (isIpHostname(url.hostname)) return [];+ return [{ hostname: url.hostname, https: url.protocol === "https:" }];+ } catch {+ return [];+ }+ }++ const region = target.region?.trim().toLowerCase();+ if (!region || region === "auto" || !BLOB_STORAGE_REGION_REGEX.test(region)) {+ return [];+ }+ const bases = [+ { hostname: `s3.${region}.amazonaws.com`, https: true as const },+ ];+ if (region === "us-east-1") {+ bases.push({ hostname: "s3.amazonaws.com", https: true });+ }+ return bases;+}++function isIpHostname(hostname: string): boolean {+ return hostname.startsWith("[") || /^\d+\.\d+\.\d+\.\d+$/.test(hostname);+}diff --git a/packages/shared/src/server/webhooks/ipBlocking.ts b/packages/shared/src/server/webhooks/ipBlocking.tsindex ee06aeadda..12c9c60759 100644--- a/packages/shared/src/server/webhooks/ipBlocking.ts+++ b/packages/shared/src/server/webhooks/ipBlocking.ts@@ -1,4 +1,5 @@import ipaddr from "ipaddr.js";+import { parseBlockedCidr } from "./parseBlockedCidr";// Static deny-list (IPv4 + IPv6) for SSRF protectionconst BLOCKED_CIDRS = [@@ -52,6 +53,7 @@ export function isIPBlocked(ipString: string,whitelistedIPs: string[],whiteListedIpSegments: string[],+ blockedIpSegments: string[] = [],): boolean {try {const cleanedIp = normalizeIPAddress(ipString);@@ -78,6 +80,10 @@ export function isIPBlocked(if (ip.match(network, mask)) return false;}+ for (const cidr of blockedIpSegments ?? []) {+ if (ipMatchesCidr(ip, cidr)) return true;+ }+for (const { network, mask, kind } of blockedNetworks) {if (ip.kind() !== kind) continue;if (ip.match(network, mask)) return true;@@ -143,6 +149,17 @@ export function isHostnameBlocked(hostname: string): boolean {return false;}+function ipMatchesCidr(+ ip: ReturnType<typeof ipaddr.parse>,+ cidr: string,+): boolean {+ // A malformed operator CIDR must not fail the whole check closed: that+ // would block every public address. Startup validation uses this parser.+ const parsed = parseBlockedCidr(cidr);+ if (!parsed || ip.kind() !== parsed.network.kind()) return false;+ return ip.match(parsed.network, parsed.prefix);+}+function normalizeIPAddress(ipString: string): string {return ipString.toLowerCase()diff --git a/packages/shared/src/server/webhooks/parseBlockedCidr.ts b/packages/shared/src/server/webhooks/parseBlockedCidr.tsnew file mode 100644index 0000000000..ad2d770c57--- /dev/null+++ b/packages/shared/src/server/webhooks/parseBlockedCidr.ts@@ -0,0 +1,32 @@+import ipaddr from "ipaddr.js";++const DECIMAL_CIDR_PREFIX = /^\d{1,3}$/;++/** parseBlockedCidr parses a canonical CIDR for startup checks and the runtime block. */+export function parseBlockedCidr(+ cidr: string,+): { network: ReturnType<typeof ipaddr.parse>; prefix: number } | undefined {+ const slash = cidr.indexOf("/");+ if (slash <= 0 || slash !== cidr.lastIndexOf("/")) return undefined;+ const bits = cidr.slice(slash + 1);+ if (!DECIMAL_CIDR_PREFIX.test(bits)) return undefined;+ const prefix = Number(bits);+ const address = cidr.slice(0, slash);+ try {+ const network = ipaddr.parse(address);+ // inet_aton forms (`203.0.114`, `010.1.2.0`) parse as a different address than written.+ if (+ network.kind() === "ipv4" &&+ !ipaddr.IPv4.isValidFourPartDecimal(address)+ ) {+ return undefined;+ }+ const maxPrefix = network.kind() === "ipv4" ? 32 : 128;+ if (!Number.isInteger(prefix) || prefix < 0 || prefix > maxPrefix) {+ return undefined;+ }+ return { network, prefix };+ } catch {+ return undefined;+ }+}diff --git a/packages/shared/src/server/webhooks/validation.ts b/packages/shared/src/server/webhooks/validation.tsindex bc3e1c564b..681594182f 100644--- a/packages/shared/src/server/webhooks/validation.ts+++ b/packages/shared/src/server/webhooks/validation.ts@@ -25,6 +25,14 @@ export function whitelistFromEnv(): WebhookValidationWhitelist {};}+/** webhookWhitelistFromEnv adds the webhook-only positive host list to the shared exemption list. */+export function webhookWhitelistFromEnv(): WebhookValidationWhitelist {+ return {+ ...whitelistFromEnv(),+ allowedHosts: env.LANGFUSE_WEBHOOK_ALLOWED_HOSTS,+ };+}+/*** Validates a webhook URL to prevent SSRF attacks by blocking internal/private IP addresses* Should be called when saving webhook URLs and before sending webhooksdiff --git a/web/src/ee/features/multi-tenant-sso/validateSsoConfig.ts b/web/src/ee/features/multi-tenant-sso/validateSsoConfig.tsindex 50be0f5643..75c85ac670 100644--- a/web/src/ee/features/multi-tenant-sso/validateSsoConfig.ts+++ b/web/src/ee/features/multi-tenant-sso/validateSsoConfig.ts@@ -99,12 +99,13 @@ async function fetchWithSsrfDefense(url: string): Promise<Response> {}}-/** discoveryWhitelistFromEnv builds the operator-configured exemptions to the internal-address blocklist. */+/** discoveryWhitelistFromEnv builds the operator exemptions and, when configured, the positive host allowlist. */function discoveryWhitelistFromEnv() {return {hosts: env.LANGFUSE_SSO_DISCOVERY_WHITELISTED_HOST || [],ips: env.LANGFUSE_SSO_DISCOVERY_WHITELISTED_IPS || [],ip_ranges: env.LANGFUSE_SSO_DISCOVERY_WHITELISTED_IP_SEGMENTS || [],+ allowedHosts: env.LANGFUSE_SSO_DISCOVERY_ALLOWED_HOSTS,};}diff --git a/web/src/features/automations/server/webhookHelpers.ts b/web/src/features/automations/server/webhookHelpers.tsindex 48d8896484..a8ed307858 100644--- a/web/src/features/automations/server/webhookHelpers.ts+++ b/web/src/features/automations/server/webhookHelpers.ts@@ -12,6 +12,7 @@ import {createDisplayHeaders,encryptSecretHeaders,validateWebhookURL,+ webhookWhitelistFromEnv,} from "@langfuse/shared/src/server";import { TRPCError } from "@trpc/server";@@ -51,7 +52,7 @@ export async function processWebhookActionConfig({}try {- await validateWebhookURL(actionConfig.url);+ await validateWebhookURL(actionConfig.url, webhookWhitelistFromEnv());} catch (error) {throw new TRPCError({code: "BAD_REQUEST",diff --git a/web/src/features/external-media-storage/server/service.ts b/web/src/features/external-media-storage/server/service.tsindex 726fd9c12d..d789ee80fc 100644--- a/web/src/features/external-media-storage/server/service.ts+++ b/web/src/features/external-media-storage/server/service.ts@@ -77,7 +77,12 @@ function createStorageService(integration: ExternalMediaStorageRecord) {awsSseKmsKeyId: undefined,externalEndpoint: undefined,connectionValidation:- externalMediaStorageEndpointConnectionValidationOptions(),+ externalMediaStorageEndpointConnectionValidationOptions({+ endpoint: integration.endpoint,+ bucketName: integration.bucketName,+ forcePathStyle: integration.forcePathStyle,+ region: integration.region,+ }),});}diff --git a/worker/src/__tests__/analyticsIntegrationOutboundUrlNegative.test.ts b/worker/src/__tests__/analyticsIntegrationOutboundUrlNegative.test.tsindex 50019e1346..fa72adaec8 100644--- a/worker/src/__tests__/analyticsIntegrationOutboundUrlNegative.test.ts+++ b/worker/src/__tests__/analyticsIntegrationOutboundUrlNegative.test.ts@@ -162,7 +162,11 @@ const CREDENTIALED_URL = "http://exporter:[email protected]/";describe("negative-suite premise", () => {it("runs with a genuinely empty allowlist", () => {- expect(whitelistFromEnv()).toEqual({ hosts: [], ips: [], ip_ranges: [] });+ expect(whitelistFromEnv()).toEqual({+ hosts: [],+ ips: [],+ ip_ranges: [],+ });});});diff --git a/worker/src/__tests__/ip-blocking.test.ts b/worker/src/__tests__/ip-blocking.test.tsindex 49e8153b8d..cb6eece4b6 100644--- a/worker/src/__tests__/ip-blocking.test.ts+++ b/worker/src/__tests__/ip-blocking.test.ts@@ -181,6 +181,37 @@ describe("IP Blocking Module", () => {);});});++ describe("operator blocked IP segments", () => {+ it("blocks a public CIDR and still honors exemptions inside it", () => {+ expect(isIPBlocked("8.8.8.8", [], [], ["8.8.8.0/24"])).toBe(true);+ expect(isIPBlocked("1.1.1.1", [], [], ["8.8.8.0/24"])).toBe(false);+ expect(isIPBlocked("8.8.8.8", ["8.8.8.8"], [], ["8.8.8.0/24"])).toBe(+ false,+ );+ });++ it("ignores prefixes that are not plain decimal numbers", () => {+ expect(isIPBlocked("1.1.1.1", [], [], ["203.0.113.0/0x10"])).toBe(+ false,+ );+ expect(isIPBlocked("8.8.8.8", [], [], ["8.8.8.0/"])).toBe(false);+ expect(isIPBlocked("1.1.1.1", [], [], ["203.0.113.0/1e1"])).toBe(false);+ });++ it("ignores short, zero-padded, and hex IPv4 addresses", () => {+ expect(isIPBlocked("203.0.0.1", [], [], ["203.0.114/24"])).toBe(false);+ expect(isIPBlocked("8.1.2.1", [], [], ["010.1.2.0/24"])).toBe(false);+ expect(isIPBlocked("16.1.2.1", [], [], ["0x10.1.2.0/24"])).toBe(false);+ });++ it("blocks addresses inside a /0 prefix and a compressed IPv6 range", () => {+ expect(isIPBlocked("1.1.1.1", [], [], ["203.0.113.0/0"])).toBe(true);+ expect(+ isIPBlocked("2001:4860:4860::8888", [], [], ["2001:4860:4860::/32"]),+ ).toBe(true);+ });+ });});describe("isIPAddress", () => {diff --git a/worker/src/__tests__/llm-base-url-validation.test.ts b/worker/src/__tests__/llm-base-url-validation.test.tsindex b7e9ac3356..a38e1448cf 100644--- a/worker/src/__tests__/llm-base-url-validation.test.ts+++ b/worker/src/__tests__/llm-base-url-validation.test.ts@@ -1,6 +1,7 @@import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import dns from "node:dns/promises";import {+ llmBaseUrlWhitelistFromEnv,validateLlmConnectionBaseURL,type LlmBaseUrlValidationWhitelist,} from "../../../packages/shared/src/server/llm/baseUrlValidation";@@ -11,6 +12,8 @@ const originalAllowedHosts = env.LANGFUSE_LLM_CONNECTION_WHITELISTED_HOST;const originalAllowedIps = env.LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS;const originalAllowedIpSegments =env.LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS;+const originalPositiveAllowlist = env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS;+const originalBlockedIpSegments = env.LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS;describe("LLM base URL validation", () => {beforeEach(() => {@@ -27,6 +30,8 @@ describe("LLM base URL validation", () => {(env as any).LANGFUSE_LLM_CONNECTION_WHITELISTED_IPS = originalAllowedIps;(env as any).LANGFUSE_LLM_CONNECTION_WHITELISTED_IP_SEGMENTS =originalAllowedIpSegments;+ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = originalPositiveAllowlist;+ env.LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS = originalBlockedIpSegments;});it("should reject localhost by default for self-hosted instances", async () => {@@ -172,6 +177,77 @@ describe("LLM base URL validation", () => {).rejects.toThrow(/DNS lookup failed/);});+ it("rejects hosts that are not on the positive allowlist", async () => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;++ await expect(+ validateLlmConnectionBaseURL("https://1.1.1.1/v1", {+ hosts: [],+ ips: [],+ ip_ranges: [],+ allowedHosts: ["llm.internal"],+ }),+ ).rejects.toMatchObject({ code: "host-not-allowed" });+ });++ it("allows a private host that is on the positive allowlist", async () => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;++ await expect(+ validateLlmConnectionBaseURL("http://localhost:11434/v1", {+ hosts: [],+ ips: [],+ ip_ranges: [],+ allowedHosts: ["localhost"],+ }),+ ).resolves.not.toThrow();+ });++ it("still allows other public hosts when only an exemption host is set", async () => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;+ vi.spyOn(dns, "resolve4").mockResolvedValue(["1.1.1.1"]);+ vi.spyOn(dns, "lookup").mockResolvedValue([+ { address: "1.1.1.1", family: 4 },+ ]);++ await expect(+ validateLlmConnectionBaseURL("https://api.openai.com/v1", {+ hosts: ["localhost"],+ ips: [],+ ip_ranges: [],+ }),+ ).resolves.not.toThrow();+ });++ it("ignores a positive host allowlist on Langfuse Cloud", async () => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "US";++ await expect(+ validateLlmConnectionBaseURL("https://1.1.1.1/v1", {+ hosts: [],+ ips: [],+ ip_ranges: [],+ allowedHosts: ["llm.internal"],+ }),+ ).resolves.not.toThrow();+ });++ it("blocks public addresses listed in LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS", async () => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;+ env.LANGFUSE_OUTBOUND_BLOCKED_IP_SEGMENTS = ["1.1.1.0/24"];++ await expect(+ validateLlmConnectionBaseURL("https://1.1.1.1/v1"),+ ).rejects.toMatchObject({ code: "blocked-ip" });+ });++ it("reads the positive allowlist from the environment on self-hosted", () => {+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;+ env.LANGFUSE_LLM_CONNECTION_ALLOWED_HOSTS = ["llm.internal"];++ expect(llmBaseUrlWhitelistFromEnv().allowedHosts).toEqual(["llm.internal"]);+ });+it("should reject non-HTTPS URLs on Langfuse Cloud", async () => {(env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "US";diff --git a/worker/src/__tests__/secureLlmFetch.test.ts b/worker/src/__tests__/secureLlmFetch.test.tsindex c608e8d2cc..7d7e0f09f3 100644--- a/worker/src/__tests__/secureLlmFetch.test.ts+++ b/worker/src/__tests__/secureLlmFetch.test.ts@@ -333,14 +333,13 @@ describe("secure LLM fetch", () => {await expect(fetchSecureLlmUrl(- "https://example.com/v1/chat/completions",+ "https://1.1.1.1/v1/chat/completions",{method: "POST",dispatcher: leakedDispatcher,} as RequestInit & { dispatcher: unknown },{logContext: "Test LLM endpoint",- whitelist: { hosts: ["example.com"], ips: [], ip_ranges: [] },},),).resolves.toBeInstanceOf(Response);diff --git a/worker/src/__tests__/webhook-validation.test.ts b/worker/src/__tests__/webhook-validation.test.tsindex 32ddb0c833..13c7084592 100644--- a/worker/src/__tests__/webhook-validation.test.ts+++ b/worker/src/__tests__/webhook-validation.test.ts@@ -1,6 +1,11 @@import { afterEach, beforeEach, describe, it, expect, vi } from "vitest";import dns from "node:dns/promises";-import { validateWebhookURL } from "../../../packages/shared/src/server/webhooks/validation";+import { env } from "../../../packages/shared/src/env";+import {+ validateWebhookURL,+ webhookWhitelistFromEnv,+ whitelistFromEnv,+} from "../../../packages/shared/src/server/webhooks/validation";const nonexistentDomain = "this-domain-definitely-does-not-exist-12345.com";@@ -32,6 +37,23 @@ afterEach(() => {});describe("Webhook URL Validation", () => {+ describe("webhook host allowlist", () => {+ const original = env.LANGFUSE_WEBHOOK_ALLOWED_HOSTS;++ afterEach(() => {+ env.LANGFUSE_WEBHOOK_ALLOWED_HOSTS = original;+ });++ it("keeps the positive list off the shared exemption builder", () => {+ env.LANGFUSE_WEBHOOK_ALLOWED_HOSTS = ["hooks.example.com"];++ expect(whitelistFromEnv().allowedHosts).toBeUndefined();+ expect(webhookWhitelistFromEnv().allowedHosts).toEqual([+ "hooks.example.com",+ ]);+ });+ });+describe("validateWebhookURL", () => {it("should accept valid public HTTPS URLs", async () => {await expect(diff --git a/worker/src/__tests__/webhooks.test.ts b/worker/src/__tests__/webhooks.test.tsindex ebbcbde6a9..5356a78319 100644--- a/worker/src/__tests__/webhooks.test.ts+++ b/worker/src/__tests__/webhooks.test.ts@@ -9,6 +9,7 @@ import {vi,} from "vitest";import { v4 } from "uuid";+import dns from "node:dns/promises";import { backOff } from "exponential-backoff";import {ActionExecutionStatus,@@ -34,6 +35,42 @@ import { setupServer } from "msw/node";import { http, HttpResponse } from "msw";import { executeWebhook } from "../queues/webhooks";+const outboundWhitelistBuilders = vi.hoisted(() => ({+ webhookWhitelistFromEnv: vi.fn(),+ whitelistFromEnv: vi.fn(),+ realWebhookWhitelistFromEnv: (): {+ hosts: string[];+ ips: string[];+ ip_ranges: string[];+ allowedHosts?: string[];+ } => ({ hosts: [], ips: [], ip_ranges: [] }),+ realWhitelistFromEnv: (): {+ hosts: string[];+ ips: string[];+ ip_ranges: string[];+ allowedHosts?: string[];+ } => ({ hosts: [], ips: [], ip_ranges: [] }),+}));++vi.mock("@langfuse/shared/src/server", async (importOriginal) => {+ const actual =+ await importOriginal<typeof import("@langfuse/shared/src/server")>();+ outboundWhitelistBuilders.realWebhookWhitelistFromEnv =+ actual.webhookWhitelistFromEnv;+ outboundWhitelistBuilders.realWhitelistFromEnv = actual.whitelistFromEnv;+ outboundWhitelistBuilders.webhookWhitelistFromEnv.mockImplementation(+ actual.webhookWhitelistFromEnv,+ );+ outboundWhitelistBuilders.whitelistFromEnv.mockImplementation(+ actual.whitelistFromEnv,+ );+ return {+ ...actual,+ webhookWhitelistFromEnv: outboundWhitelistBuilders.webhookWhitelistFromEnv,+ whitelistFromEnv: outboundWhitelistBuilders.whitelistFromEnv,+ };+});+vi.mock("exponential-backoff", async (importOriginal) => {const actual = await importOriginal<typeof import("exponential-backoff")>();return {@@ -1873,6 +1910,134 @@ describe("Webhook Integration Tests", () => {expect(clientPayloadKeys[clientPayloadKeys.length - 1]).toBe("prompt");});+ it("applies the webhook host allowlist to webhook delivery and not GitHub dispatch", async () => {+ const resolve4 = vi+ .spyOn(dns, "resolve4")+ .mockResolvedValue(["93.184.216.34"]);+ const resolve6 = vi+ .spyOn(dns, "resolve6")+ .mockRejectedValue(+ Object.assign(new Error("ENODATA"), { code: "ENODATA" }),+ );+ const lookup = vi+ .spyOn(dns, "lookup")+ .mockResolvedValue([{ address: "93.184.216.34", family: 4 }] as never);+ outboundWhitelistBuilders.webhookWhitelistFromEnv.mockReturnValue({+ hosts: [],+ ips: [],+ ip_ranges: [],+ allowedHosts: ["hooks.example.com"],+ });+ outboundWhitelistBuilders.whitelistFromEnv.mockReturnValue({+ hosts: [],+ ips: [],+ ip_ranges: [],+ });++ try {+ const fullPrompt = await prisma.prompt.findUnique({+ where: { id: promptId },+ });+ const promptPayload = {+ prompt: PromptDomainSchema.parse(fullPrompt),+ action: "created" as const,+ type: "prompt-version" as const,+ };++ const webhookInput: WebhookInput = {+ projectId,+ automationId,+ executionId,+ payload: promptPayload,+ };+ await prisma.automationExecution.create({+ data: {+ id: executionId,+ projectId,+ triggerId,+ automationId,+ actionId,+ status: ActionExecutionStatus.PENDING,+ sourceId: promptId,+ input: webhookInput,+ },+ });++ await executeWebhook(webhookInput);++ const webhookExecution = await prisma.automationExecution.findUnique({+ where: { id: executionId },+ });+ expect(webhookExecution?.status).toBe(ActionExecutionStatus.ERROR);+ expect(webhookExecution?.error).toContain(+ "Host is not in the outbound URL allowlist",+ );+ expect(webhookServer.getReceivedRequests()).toHaveLength(0);++ const ghActionId = v4();+ await prisma.action.create({+ data: {+ id: ghActionId,+ projectId,+ type: "GITHUB_DISPATCH",+ config: {+ type: "GITHUB_DISPATCH",+ url: "https://webhook.example.com/dispatches",+ eventType: "prompt-update",+ githubToken: encrypt("ghp_test_token"),+ displayGitHubToken: "ghp_...n",+ },+ },+ });+ const ghAutomationId = v4();+ await prisma.automation.create({+ data: {+ id: ghAutomationId,+ projectId,+ triggerId,+ actionId: ghActionId,+ name: "GitHub Dispatch Automation",+ },+ });+ const ghExecutionId = v4();+ await prisma.automationExecution.create({+ data: {+ id: ghExecutionId,+ projectId,+ triggerId,+ automationId: ghAutomationId,+ actionId: ghActionId,+ status: ActionExecutionStatus.PENDING,+ sourceId: promptId,+ input: {},+ },+ });++ await executeWebhook({+ projectId,+ automationId: ghAutomationId,+ executionId: ghExecutionId,+ payload: promptPayload,+ });++ expect(webhookServer.getReceivedRequests()).toHaveLength(1);+ const dispatchExecution = await prisma.automationExecution.findUnique({+ where: { id: ghExecutionId },+ });+ expect(dispatchExecution?.status).toBe(ActionExecutionStatus.COMPLETED);+ } finally {+ outboundWhitelistBuilders.webhookWhitelistFromEnv.mockImplementation(+ outboundWhitelistBuilders.realWebhookWhitelistFromEnv,+ );+ outboundWhitelistBuilders.whitelistFromEnv.mockImplementation(+ outboundWhitelistBuilders.realWhitelistFromEnv,+ );+ resolve4.mockRestore();+ resolve6.mockRestore();+ lookup.mockRestore();+ }+ });+it("should truncate oversized GitHub dispatch prompt fields", async () => {const fullPrompt = await prisma.prompt.findUnique({where: { id: promptId },diff --git a/worker/src/features/integrations/customerFaultClassification.test.ts b/worker/src/features/integrations/customerFaultClassification.test.tsindex 339d7fe5df..5586b72008 100644--- a/worker/src/features/integrations/customerFaultClassification.test.ts+++ b/worker/src/features/integrations/customerFaultClassification.test.ts@@ -55,6 +55,7 @@ const gcsError = (const OUTBOUND_URL_FAULTS = [["blocked-ip", "ssrf_blocked_endpoint"],["blocked-hostname", "ssrf_blocked_endpoint"],+ ["host-not-allowed", "ssrf_blocked_endpoint"],["invalid-syntax", "invalid_endpoint_url"],["invalid-encoding", "invalid_endpoint_url"],["https-required", "invalid_endpoint_url"],diff --git a/worker/src/features/integrations/customerFaultClassification.ts b/worker/src/features/integrations/customerFaultClassification.tsindex 769e0cc262..d0983d71de 100644--- a/worker/src/features/integrations/customerFaultClassification.ts+++ b/worker/src/features/integrations/customerFaultClassification.ts@@ -60,6 +60,7 @@ const BUCKET_FAULT_CODES = new Set<string>([const SSRF_BLOCKED_OUTBOUND_URL_CODES = new Set<string>(["blocked-hostname","blocked-ip",+ "host-not-allowed",]);const INVALID_URL_OUTBOUND_URL_CODES = new Set<string>([diff --git a/worker/src/queues/webhooks.ts b/worker/src/queues/webhooks.tsindex a30561277b..10adda6bb7 100644--- a/worker/src/queues/webhooks.ts+++ b/worker/src/queues/webhooks.ts@@ -15,9 +15,11 @@ import { decrypt, createSignatureHeader } from "@langfuse/shared/encryption";import { Prisma, prisma } from "@langfuse/shared/src/db";import {validateWebhookURL,+ webhookWhitelistFromEnv,whitelistFromEnv,fetchWithSecureRedirects,urlForLog,+ type WebhookValidationWhitelist,WEBHOOK_URL_VALIDATION_LOG_CONTEXT,buildWebhookRequestHeaders,} from "@langfuse/shared/src/server";@@ -212,6 +214,7 @@ async function executeHttpAction({actionConfig,additionalSensitiveHeaders,payloadType,+ whitelist,addressAttemptTimeoutMs,}: {url: string;@@ -225,6 +228,7 @@ async function executeHttpAction({actionConfig: ActionDomainWithSecrets;additionalSensitiveHeaders?: string[];payloadType: WebhookInput["payload"]["type"];+ whitelist: WebhookValidationWhitelist;addressAttemptTimeoutMs?: number;}): Promise<{ httpStatus: number; responseBody: string }> {// monitor-alert does not write AutomationExecution rows; auto-disable rides a Redis counter instead.@@ -247,8 +251,6 @@ async function executeHttpAction({}, env.LANGFUSE_WEBHOOK_TIMEOUT_MS);try {- const whitelist = whitelistFromEnv();-// Skip validation when flag is set (for tests with MSW mocking)if (!skipValidation) {await validateWebhookURL(url, whitelist);@@ -567,6 +569,7 @@ async function executeWebhookAction({actionConfig,additionalSensitiveHeaders,payloadType: input.payload.type,+ whitelist: webhookWhitelistFromEnv(),addressAttemptTimeoutMs: WEBHOOK_ADDRESS_ATTEMPT_TIMEOUT_MS,});}@@ -709,6 +712,7 @@ async function executeGitHubDispatchAction({executionStart,actionConfig,payloadType: input.payload.type,+ whitelist: whitelistFromEnv(),});}