Skip to content

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

test(automations): reject internal monitor webhook destinations at create time (#19614)

Tobias Wochingerfc34518a9f47
diff --git a/web/src/__tests__/server/automations-trpc.servertest.ts b/web/src/__tests__/server/automations-trpc.servertest.ts
index 746dc2a1e1..28eb87b9ba 100644
--- a/web/src/__tests__/server/automations-trpc.servertest.ts
+++ b/web/src/__tests__/server/automations-trpc.servertest.ts
@@ -1,8 +1,10 @@
+import dns from "node:dns/promises";
import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";
import type * as SharedServer from "@langfuse/shared/src/server";
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
import { type Prisma, prisma } from "@langfuse/shared/src/db";
+import { env as sharedEnv } from "@langfuse/shared/src/env";
import {
createOrgProjectAndApiKey,
SLACK_CHANNEL_NOT_FOUND_MESSAGE,
@@ -1167,6 +1169,115 @@ describe("automations trpc", () => {
).rejects.toThrow("Name is required");
});
+ describe("monitor webhook destination validation", () => {
+ const original = {
+ hosts: sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_HOST,
+ ips: sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_IPS,
+ ipSegments: sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS,
+ allowedHosts: sharedEnv.LANGFUSE_WEBHOOK_ALLOWED_HOSTS,
+ };
+
+ const monitorWebhookInput = (projectId: string, url: string) => ({
+ projectId,
+ name: "Monitor webhook",
+ eventSource: TriggerEventSource.Monitor,
+ eventAction: [],
+ filter: null,
+ status: JobConfigState.ACTIVE,
+ actionType: "WEBHOOK" as const,
+ actionConfig: {
+ type: "WEBHOOK" as const,
+ url,
+ requestHeaders: {},
+ apiVersion: { monitor: "v1" as const },
+ },
+ });
+
+ async function expectNothingPersisted(projectId: string) {
+ const [automations, triggers, actions] = await Promise.all([
+ prisma.automation.count({ where: { projectId } }),
+ prisma.trigger.count({ where: { projectId } }),
+ prisma.action.count({ where: { projectId } }),
+ ]);
+ expect({ automations, triggers, actions }).toEqual({
+ automations: 0,
+ triggers: 0,
+ actions: 0,
+ });
+ }
+
+ beforeEach(() => {
+ sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_HOST = [];
+ sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_IPS = [];
+ sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS = [];
+ sharedEnv.LANGFUSE_WEBHOOK_ALLOWED_HOSTS = [];
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_HOST = original.hosts;
+ sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_IPS = original.ips;
+ sharedEnv.LANGFUSE_WEBHOOK_WHITELISTED_IP_SEGMENTS =
+ original.ipSegments;
+ sharedEnv.LANGFUSE_WEBHOOK_ALLOWED_HOSTS = original.allowedHosts;
+ });
+
+ it.each([
+ ["loopback IPv6", "http://[::1]/hook", "Blocked IP address"],
+ [
+ "cloud metadata",
+ "http://169.254.169.254/latest/meta-data",
+ "Blocked hostname",
+ ],
+ ["RFC1918", "http://10.0.0.1/hook", "Blocked IP address"],
+ [
+ "embedded credentials",
+ "https://user:[email protected]/hook",
+ "URL credentials are not allowed",
+ ],
+ ])(
+ "rejects a %s destination without persisting it",
+ async (_case, url, message) => {
+ const { project, caller } = await prepare();
+
+ await expect(
+ caller.automations.createAutomation(
+ monitorWebhookInput(project.id, url),
+ ),
+ ).rejects.toMatchObject({
+ code: "BAD_REQUEST",
+ message: expect.stringContaining(`Invalid webhook URL: ${message}`),
+ });
+ await expectNothingPersisted(project.id);
+ },
+ );
+
+ it("rejects a hostname that resolves to a private address", async () => {
+ vi.spyOn(dns, "resolve4").mockResolvedValue(["10.0.0.5"]);
+ vi.spyOn(dns, "resolve6").mockRejectedValue(new Error("ENODATA"));
+ vi.spyOn(dns, "lookup").mockResolvedValue([
+ { address: "10.0.0.5", family: 4 },
+ ] as never);
+ const { project, caller } = await prepare();
+
+ await expect(
+ caller.automations.createAutomation(
+ monitorWebhookInput(
+ project.id,
+ "https://hooks.internal.example.com",
+ ),
+ ),
+ ).rejects.toMatchObject({
+ code: "BAD_REQUEST",
+ message: expect.stringContaining(
+ "Invalid webhook URL: Blocked IP address",
+ ),
+ });
+ expect(dns.resolve4).toHaveBeenCalledWith("hooks.internal.example.com");
+ await expectNothingPersisted(project.id);
+ });
+ });
+
it("should create a new Slack automation", async () => {
const { project, caller } = await prepare();