Skip to content

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

feat: add webhook ip segment whitelist (#9207)

0x0034c9e6065ea34d
diff --git a/packages/shared/src/env.ts b/packages/shared/src/env.ts
index 8e56d13475..c68dd52c12 100644
--- a/packages/shared/src/env.ts
+++ b/packages/shared/src/env.ts
@@ -169,6 +169,12 @@ const EnvSchema = z.object({
.transform((s) =>
s ? s.split(",").map((s) => s.toLowerCase().trim()) : [],
),
+ LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS: 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.ts
index 1ccacaac35..dfa07b2bb2 100644
--- a/packages/shared/src/server/webhooks/ipBlocking.ts
+++ b/packages/shared/src/server/webhooks/ipBlocking.ts
@@ -47,6 +47,7 @@ const blockedNetworks = BLOCKED_CIDRS.map((cidr) => {
export function isIPBlocked(
ipString: string,
whitelistedIPs: string[],
+ whiteListedIpSegments: string[],
): boolean {
try {
// Check if IP is in whitelist first
@@ -56,6 +57,21 @@ export function isIPBlocked(
const ip = ipaddr.parse(ipString);
+ const whitelistedSegments = whiteListedIpSegments.map((cidr) => {
+ const [addr, bits] = cidr.split("/");
+ const parsed = ipaddr.parse(addr);
+ return {
+ network: parsed,
+ kind: parsed.kind(),
+ mask: parseInt(bits, 10),
+ };
+ });
+
+ for (const { network, mask, kind } of whitelistedSegments) {
+ if (ip.kind() !== kind) continue;
+ if (ip.match(network, mask)) return false;
+ }
+
for (const { network, mask, kind } of blockedNetworks) {
if (ip.kind() !== kind) continue;
if (ip.match(network, mask)) return true;
diff --git a/packages/shared/src/server/webhooks/validation.ts b/packages/shared/src/server/webhooks/validation.ts
index 5ffb0ebbb2..8557e6dd89 100644
--- a/packages/shared/src/server/webhooks/validation.ts
+++ b/packages/shared/src/server/webhooks/validation.ts
@@ -65,7 +65,13 @@ export async function validateWebhookURL(urlString: string): Promise<void> {
// Step 4: Check for IP address literals in hostname
if (isIPAddress(hostname)) {
- if (isIPBlocked(hostname, env.LANGFUSE_WEBHOOK_WHITELISTED_IPS)) {
+ if (
+ isIPBlocked(
+ hostname,
+ env.LANGFUSE_WEBHOOK_WHITELISTED_IPS,
+ env.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS,
+ )
+ ) {
throw new Error(`Blocked IP address detected: ${hostname}`);
}
}
@@ -73,7 +79,13 @@ export async function validateWebhookURL(urlString: string): Promise<void> {
// Step 5: DNS resolution and validation
const ips = await resolveHost(hostname);
for (const ip of ips) {
- if (isIPBlocked(ip, env.LANGFUSE_WEBHOOK_WHITELISTED_IPS)) {
+ if (
+ isIPBlocked(
+ ip,
+ env.LANGFUSE_WEBHOOK_WHITELISTED_IPS,
+ env.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS,
+ )
+ ) {
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.ts
index 19736a237f..101216acaf 100644
--- a/worker/src/__tests__/ip-blocking.test.ts
+++ b/worker/src/__tests__/ip-blocking.test.ts
@@ -9,136 +9,168 @@ 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(
- true,
- );
+ expect(isIPBlocked("2001::1", [], [])).toBe(true);
+ expect(
+ isIPBlocked("2001:0000:4136:e378:8000:63bf:3fff:fdd2", [], []),
+ ).toBe(true);
});
});
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"]),
+ 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,
- );
+ 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);
+ 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);
+ 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);
+ 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);
+ expect(isIPBlocked("8.8.8.8", ["127.0.0.1"], [])).toBe(false);
+ });
+ });
+ describe("whitelisted IP Segments", () => {
+ it("should allow whitelisted IPv4 Segment 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/32"])).toBe(false);
+ expect(isIPBlocked("192.168.1.100", [], ["192.168.1.0/24"])).toBe(
+ false,
+ );
+
+ // Non-whitelisted IPs should still be blocked normally
+ expect(isIPBlocked("192.168.1.1", [], ["10.0.0.0/8"])).toBe(true);
+ expect(isIPBlocked("10.0.0.1", [], ["192.168.1.0/24"])).toBe(true);
+ });
+
+ it("should allow whitelisted IPv6 Segment that would normally be blocked", () => {
+ expect(isIPBlocked("::1", [], ["::1/128"])).toBe(false);
+ expect(isIPBlocked("fe80::1", [], ["fe80::/10"])).toBe(false);
+
+ // Non-whitelisted IPv6 should still be blocked
+ expect(isIPBlocked("fc00::1", [], ["::1/128"])).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", [], [])).toBe(true);
+ expect(isIPBlocked("172.16.0.1", [], [])).toBe(true);
+
+ // Public IPs should not be blocked
+ expect(isIPBlocked("8.8.8.8", ["127.0.0.1"], ["192.168.1.0/24"])).toBe(
+ false,
+ );
});
});
});