🪢 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
chore: ip allowlist for webhooks (#8977)
Matthew Lapointe9907c3986184
diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.tsindex e936fb74dc..83e9a7da67 100644--- a/packages/shared/src/env.ts+++ b/packages/shared/src/env.ts@@ -197,6 +197,12 @@ const EnvSchema = z.object({return new Map<string, number>();}}),+ LANGFUSE_WEBHOOK_WHITELISTED_IPS: 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/ipBlocking.ts b/packages/shared/src/server/webhooks/ipBlocking.tsindex 077306ead0..1ccacaac35 100644--- a/packages/shared/src/server/webhooks/ipBlocking.ts+++ b/packages/shared/src/server/webhooks/ipBlocking.ts@@ -44,8 +44,16 @@ const blockedNetworks = BLOCKED_CIDRS.map((cidr) => {/*** Check if an IP address is blocked based on CIDR ranges*/-export function isIPBlocked(ipString: string): boolean {+export function isIPBlocked(+ ipString: string,+ whitelistedIPs: string[],+): boolean {try {+ // Check if IP is in whitelist first+ if (whitelistedIPs.includes(ipString.toLowerCase().trim())) {+ return false;+ }+const ip = ipaddr.parse(ipString);for (const { network, mask, kind } of blockedNetworks) {diff --git a/packages/shared/src/server/webhooks/validation.ts b/packages/shared/src/server/webhooks/validation.tsindex cae131662a..5ffb0ebbb2 100644--- a/packages/shared/src/server/webhooks/validation.ts+++ b/packages/shared/src/server/webhooks/validation.ts@@ -1,6 +1,7 @@import dns from "node:dns/promises";import { URL } from "node:url";import { isIPBlocked, isIPAddress, isHostnameBlocked } from "./ipBlocking";+import { env } from "../../env";export async function resolveHost(hostname: string): Promise<string[]> {// Returns every A + AAAA address@@ -64,7 +65,7 @@ export async function validateWebhookURL(urlString: string): Promise<void> {// Step 4: Check for IP address literals in hostnameif (isIPAddress(hostname)) {- if (isIPBlocked(hostname)) {+ if (isIPBlocked(hostname, env.LANGFUSE_WEBHOOK_WHITELISTED_IPS)) {throw new Error(`Blocked IP address detected: ${hostname}`);}}@@ -72,7 +73,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)) {+ if (isIPBlocked(ip, env.LANGFUSE_WEBHOOK_WHITELISTED_IPS)) {throw new Error(`Blocked IP address detected: ${ip}`);}}diff --git a/worker/src/__tests__/ip-blocking.test.ts b/worker/src/__tests__/ip-blocking.test.tsindex 954e0ac500..19736a237f 100644--- a/worker/src/__tests__/ip-blocking.test.ts+++ b/worker/src/__tests__/ip-blocking.test.ts@@ -9,74 +9,74 @@ describe("IP Blocking Module", () => {describe("isIPBlocked", () => {describe("IPv4 blocked addresses", () => {it("should block localhost addresses", () => {- expect(isIPBlocked("127.0.0.1")).toBe(true);- expect(isIPBlocked("127.1.1.1")).toBe(true);- expect(isIPBlocked("127.255.255.255")).toBe(true);+ expect(isIPBlocked("127.0.0.1", [])).toBe(true);+ expect(isIPBlocked("127.1.1.1", [])).toBe(true);+ expect(isIPBlocked("127.255.255.255", [])).toBe(true);});it("should block private network addresses", () => {- expect(isIPBlocked("10.0.0.1")).toBe(true);- expect(isIPBlocked("10.255.255.255")).toBe(true);- expect(isIPBlocked("172.16.0.1")).toBe(true);- expect(isIPBlocked("172.31.255.255")).toBe(true);- expect(isIPBlocked("192.168.0.1")).toBe(true);- expect(isIPBlocked("192.168.255.255")).toBe(true);+ expect(isIPBlocked("10.0.0.1", [])).toBe(true);+ expect(isIPBlocked("10.255.255.255", [])).toBe(true);+ expect(isIPBlocked("172.16.0.1", [])).toBe(true);+ expect(isIPBlocked("172.31.255.255", [])).toBe(true);+ expect(isIPBlocked("192.168.0.1", [])).toBe(true);+ expect(isIPBlocked("192.168.255.255", [])).toBe(true);});it("should block link-local addresses", () => {- expect(isIPBlocked("169.254.0.1")).toBe(true);- expect(isIPBlocked("169.254.169.254")).toBe(true); // AWS metadata- expect(isIPBlocked("169.254.255.255")).toBe(true);+ expect(isIPBlocked("169.254.0.1", [])).toBe(true);+ expect(isIPBlocked("169.254.169.254", [])).toBe(true); // AWS metadata+ expect(isIPBlocked("169.254.255.255", [])).toBe(true);});it("should block multicast addresses", () => {- expect(isIPBlocked("224.0.0.1")).toBe(true);- expect(isIPBlocked("239.255.255.255")).toBe(true);+ expect(isIPBlocked("224.0.0.1", [])).toBe(true);+ expect(isIPBlocked("239.255.255.255", [])).toBe(true);});it("should block broadcast and reserved addresses", () => {- expect(isIPBlocked("255.255.255.255")).toBe(true);- expect(isIPBlocked("240.0.0.1")).toBe(true);- expect(isIPBlocked("0.0.0.1")).toBe(true);+ expect(isIPBlocked("255.255.255.255", [])).toBe(true);+ expect(isIPBlocked("240.0.0.1", [])).toBe(true);+ expect(isIPBlocked("0.0.0.1", [])).toBe(true);});it("should block test networks", () => {- expect(isIPBlocked("192.0.2.1")).toBe(true); // TEST-NET-1- expect(isIPBlocked("198.51.100.1")).toBe(true); // TEST-NET-2- expect(isIPBlocked("203.0.113.1")).toBe(true); // TEST-NET-3+ expect(isIPBlocked("192.0.2.1", [])).toBe(true); // TEST-NET-1+ expect(isIPBlocked("198.51.100.1", [])).toBe(true); // TEST-NET-2+ expect(isIPBlocked("203.0.113.1", [])).toBe(true); // TEST-NET-3});it("should block CG-NAT addresses", () => {- expect(isIPBlocked("100.64.0.1")).toBe(true);- expect(isIPBlocked("100.127.255.255")).toBe(true);+ expect(isIPBlocked("100.64.0.1", [])).toBe(true);+ expect(isIPBlocked("100.127.255.255", [])).toBe(true);});});describe("IPv6 blocked addresses", () => {it("should block IPv6 localhost", () => {- expect(isIPBlocked("::1")).toBe(true);- expect(isIPBlocked("::")).toBe(true);+ expect(isIPBlocked("::1", [])).toBe(true);+ expect(isIPBlocked("::", [])).toBe(true);});it("should block IPv6 private addresses", () => {- expect(isIPBlocked("fc00::1")).toBe(true);- expect(isIPBlocked("fd00::1")).toBe(true);- expect(isIPBlocked("fe80::1")).toBe(true);+ expect(isIPBlocked("fc00::1", [])).toBe(true);+ expect(isIPBlocked("fd00::1", [])).toBe(true);+ expect(isIPBlocked("fe80::1", [])).toBe(true);});it("should block IPv6 multicast", () => {- expect(isIPBlocked("ff00::1")).toBe(true);- expect(isIPBlocked("ff02::1")).toBe(true);+ expect(isIPBlocked("ff00::1", [])).toBe(true);+ expect(isIPBlocked("ff02::1", [])).toBe(true);});it("should block IPv4-mapped IPv6 addresses", () => {- expect(isIPBlocked("::ffff:192.168.1.1")).toBe(true);- expect(isIPBlocked("::ffff:127.0.0.1")).toBe(true);+ expect(isIPBlocked("::ffff:192.168.1.1", [])).toBe(true);+ expect(isIPBlocked("::ffff:127.0.0.1", [])).toBe(true);});it("should block Teredo addresses", () => {- expect(isIPBlocked("2001::1")).toBe(true);- expect(isIPBlocked("2001:0000:4136:e378:8000:63bf:3fff:fdd2")).toBe(+ expect(isIPBlocked("2001::1", [])).toBe(true);+ expect(isIPBlocked("2001:0000:4136:e378:8000:63bf:3fff:fdd2", [])).toBe(true,);});@@ -84,24 +84,61 @@ describe("IP Blocking Module", () => {describe("allowed public addresses", () => {it("should allow public IPv4 addresses", () => {- expect(isIPBlocked("8.8.8.8")).toBe(false); // Google DNS- expect(isIPBlocked("1.1.1.1")).toBe(false); // Cloudflare DNS- expect(isIPBlocked("208.67.222.222")).toBe(false); // OpenDNS- expect(isIPBlocked("74.125.224.72")).toBe(false); // Google+ expect(isIPBlocked("8.8.8.8", [])).toBe(false); // Google DNS+ expect(isIPBlocked("1.1.1.1", [])).toBe(false); // Cloudflare DNS+ expect(isIPBlocked("208.67.222.222", [])).toBe(false); // OpenDNS+ expect(isIPBlocked("74.125.224.72", [])).toBe(false); // Google});it("should allow public IPv6 addresses", () => {- expect(isIPBlocked("2001:4860:4860::8888")).toBe(false); // Google DNS- expect(isIPBlocked("2606:4700:4700::1111")).toBe(false); // Cloudflare DNS+ expect(isIPBlocked("2001:4860:4860::8888", [])).toBe(false); // Google DNS+ expect(isIPBlocked("2606:4700:4700::1111", [])).toBe(false); // Cloudflare DNS});});describe("invalid IP addresses", () => {it("should block invalid IP strings", () => {- expect(isIPBlocked("not-an-ip")).toBe(true);- expect(isIPBlocked("256.256.256.256")).toBe(true);- expect(isIPBlocked("")).toBe(true);- expect(isIPBlocked("invalid")).toBe(true);+ expect(isIPBlocked("not-an-ip", [])).toBe(true);+ expect(isIPBlocked("256.256.256.256", [])).toBe(true);+ expect(isIPBlocked("", [])).toBe(true);+ expect(isIPBlocked("invalid", [])).toBe(true);+ });+ });++ describe("whitelisted IPs", () => {+ it("should allow whitelisted IPv4 addresses that would normally be blocked", () => {+ // These IPs would normally be blocked but should be allowed due to whitelist+ expect(isIPBlocked("127.0.0.1", ["127.0.0.1", "192.168.1.100"])).toBe(+ false,+ );+ expect(+ isIPBlocked("192.168.1.100", ["127.0.0.1", "192.168.1.100"]),+ ).toBe(false);++ // Non-whitelisted IPs should still be blocked normally+ expect(isIPBlocked("192.168.1.1", ["127.0.0.1", "192.168.1.100"])).toBe(+ true,+ );+ expect(isIPBlocked("10.0.0.1", ["127.0.0.1", "192.168.1.100"])).toBe(+ true,+ );+ });++ it("should allow whitelisted IPv6 addresses that would normally be blocked", () => {+ expect(isIPBlocked("::1", ["::1", "fe80::1"])).toBe(false);+ expect(isIPBlocked("fe80::1", ["::1", "fe80::1"])).toBe(false);++ // Non-whitelisted IPv6 should still be blocked+ expect(isIPBlocked("fc00::1", ["::1", "fe80::1"])).toBe(true);+ });++ it("should still block non-whitelisted IPs normally", () => {+ // Private IPs not in whitelist should still be blocked+ expect(isIPBlocked("192.168.1.1", ["127.0.0.1"])).toBe(true);+ expect(isIPBlocked("172.16.0.1", ["127.0.0.1"])).toBe(true);++ // Public IPs should not be blocked+ expect(isIPBlocked("8.8.8.8", ["127.0.0.1"])).toBe(false);});});});