🪢 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 `LANGFUSE_WHITELISTED_WEBHOOK_HOSTNAMES` option (#9205)
Carsten Csikyd8a91f15b9fe
diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.tsindex c68dd52c12..8e8177fd77 100644--- a/packages/shared/src/env.ts+++ b/packages/shared/src/env.ts@@ -175,6 +175,12 @@ const EnvSchema = z.object({.transform((s) =>s ? s.split(",").map((s) => s.toLowerCase().trim()) : [],),+ LANGFUSE_WEBHOOK_WHITELISTED_HOST: z+ .string()+ .optional()+ .transform((s) =>+ s ? s.split(",").map((s) => s.toLowerCase().trim()) : [],+ ),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/webhooks/validation.ts b/packages/shared/src/server/webhooks/validation.tsindex 8557e6dd89..a4243b9901 100644--- a/packages/shared/src/server/webhooks/validation.ts+++ b/packages/shared/src/server/webhooks/validation.ts@@ -27,6 +27,20 @@ export async function resolveHost(hostname: string): Promise<string[]> {return ips;}+export interface WebhookValidationWhitelist {+ hosts: string[];+ ips: string[];+ ip_ranges: string[];+}++function whitelistFromEnv(): WebhookValidationWhitelist {+ return {+ hosts: env.LANGFUSE_WEBHOOK_WHITELISTED_HOST || [],+ ips: env.LANGFUSE_WEBHOOK_WHITELISTED_IPS || [],+ ip_ranges: env.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS || [],+ };+}+/*** Validates a webhook URL to prevent SSRF attacks by blocking internal/private IP addresses* Should be called when saving webhook URLs and before sending webhooks@@ -35,7 +49,10 @@ export async function resolveHost(hostname: string): Promise<string[]> {* where DNS can change between validation and actual HTTP request. For maximum security,* the HTTP client should also implement IP blocking at connection time.*/-export async function validateWebhookURL(urlString: string): Promise<void> {+export async function validateWebhookURL(+ urlString: string,+ whitelist: WebhookValidationWhitelist = whitelistFromEnv(),+): Promise<void> {// Step 1: Basic URL parsing and normalizationlet url: URL;try {@@ -58,6 +75,11 @@ export async function validateWebhookURL(urlString: string): Promise<void> {// Step 3: Hostname normalization and validationconst hostname = normalizeHostname(url.hostname);+ if (whitelist.hosts.includes(hostname)) {+ // skip further checks if hostname is whitelisted+ return;+ }+// Block obviously dangerous hostnamesif (isHostnameBlocked(hostname)) {throw new Error("Blocked hostname detected");@@ -65,13 +87,7 @@ export async function validateWebhookURL(urlString: string): Promise<void> {// Step 4: Check for IP address literals in hostnameif (isIPAddress(hostname)) {- if (- isIPBlocked(- hostname,- env.LANGFUSE_WEBHOOK_WHITELISTED_IPS,- env.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS,- )- ) {+ if (isIPBlocked(hostname, whitelist.ips, whitelist.ip_ranges)) {throw new Error(`Blocked IP address detected: ${hostname}`);}}@@ -79,13 +95,7 @@ export async function validateWebhookURL(urlString: string): Promise<void> {// Step 5: DNS resolution and validationconst ips = await resolveHost(hostname);for (const ip of ips) {- if (- isIPBlocked(- ip,- env.LANGFUSE_WEBHOOK_WHITELISTED_IPS,- env.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS,- )- ) {+ if (isIPBlocked(ip, whitelist.ips, whitelist.ip_ranges)) {throw new Error(`Blocked IP address detected: ${ip}`);}}diff --git a/worker/src/__tests__/webhook-validation.test.ts b/worker/src/__tests__/webhook-validation.test.tsindex e02533b454..86038143bd 100644--- a/worker/src/__tests__/webhook-validation.test.ts+++ b/worker/src/__tests__/webhook-validation.test.ts@@ -149,5 +149,29 @@ describe("Webhook URL Validation", () => {validateWebhookURL("http://exam%ple.com/hook"),).rejects.toThrow(/Invalid URL encoding|Invalid URL syntax/);});++ it("should allow local hostname, if it is included in the whitelist", async () => {+ await expect(+ validateWebhookURL("http://internal.company.com/hook", {+ hosts: ["internal.company.com"],+ ips: [],+ ip_ranges: [],+ }),+ ).resolves.not.toThrow();+ await expect(+ validateWebhookURL("http://app.internal/hook", {+ hosts: ["app.internal"],+ ips: [],+ ip_ranges: [],+ }),+ ).resolves.not.toThrow();+ await expect(+ validateWebhookURL("http://intranet/hook", {+ hosts: ["intranet"],+ ips: [],+ ip_ranges: [],+ }),+ ).resolves.not.toThrow();+ });});});