🪢 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
security: PostHog SSRF validation (#11311)
Max Deichmannf1d6c2627610
diff --git a/web/src/__tests__/posthog-integration.servertest.ts b/web/src/__tests__/posthog-integration.servertest.tsnew file mode 100644index 0000000000..3bf78c9007--- /dev/null+++ b/web/src/__tests__/posthog-integration.servertest.ts@@ -0,0 +1,75 @@+/** @jest-environment node */+/* eslint-disable @typescript-eslint/no-unsafe-member-access */++import type { Session } from "next-auth";+import { pruneDatabase } from "@/src/__tests__/test-utils";+import { prisma } from "@langfuse/shared/src/db";+import { appRouter } from "@/src/server/api/root";+import { createInnerTRPCContext } from "@/src/server/api/trpc";++describe("PostHog Integration SSRF Protection", () => {+ const projectId = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";+ const originalEncryptionKey = process.env.ENCRYPTION_KEY;++ beforeAll(() => {+ // Set a test encryption key (64 hex characters = 32 bytes)+ process.env.ENCRYPTION_KEY =+ "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";+ });++ afterAll(() => {+ // Restore original environment+ process.env.ENCRYPTION_KEY = originalEncryptionKey;+ });++ beforeEach(async () => await pruneDatabase());++ const session: Session = {+ expires: "1",+ user: {+ id: "user-1",+ name: "Demo User",+ canCreateOrganizations: true,+ organizations: [+ {+ id: "seed-org-id",+ role: "OWNER",+ plan: "cloud:hobby",+ cloudConfig: undefined,+ name: "Test Organization",+ metadata: {},+ projects: [+ {+ id: projectId,+ role: "ADMIN",+ name: "Test Project",+ deletedAt: null,+ retentionDays: null,+ metadata: {},+ },+ ],+ },+ ],+ featureFlags: {+ templateFlag: true,+ excludeClickhouseRead: false,+ },+ admin: true,+ },+ environment: {} as any,+ };++ const ctx = createInnerTRPCContext({ session, headers: {} });+ const caller = appRouter.createCaller({ ...ctx, prisma });++ it("should reject private IPs and localhost in PostHog hostname", async () => {+ await expect(+ caller.posthogIntegration.update({+ projectId,+ posthogHostname: "http://localhost",+ posthogProjectApiKey: "phc_test_key_12345",+ enabled: true,+ }),+ ).rejects.toThrow(/Invalid PostHog hostname.*Blocked/);+ });+});diff --git a/web/src/features/posthog-integration/posthog-integration-router.ts b/web/src/features/posthog-integration/posthog-integration-router.tsindex 905ce6859c..bf7419d894 100644--- a/web/src/features/posthog-integration/posthog-integration-router.ts+++ b/web/src/features/posthog-integration/posthog-integration-router.ts@@ -10,6 +10,7 @@ import { decrypt, encrypt } from "@langfuse/shared/encryption";import { posthogIntegrationFormSchema } from "@/src/features/posthog-integration/types";import { TRPCError } from "@trpc/server";import { env } from "@/src/env.mjs";+import { validateWebhookURL } from "@langfuse/shared/src/server";export const posthogIntegrationRouter = createTRPCRouter({get: protectedProjectProcedure@@ -67,6 +68,20 @@ export const posthogIntegrationRouter = createTRPCRouter({});}}++ // Validate PostHog hostname to prevent SSRF attacks+ try {+ await validateWebhookURL(input.posthogHostname);+ } catch (error) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message:+ error instanceof Error+ ? `Invalid PostHog hostname: ${error.message}`+ : "Invalid PostHog hostname",+ });+ }+await auditLog({session: ctx.session,action: "update",diff --git a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.tsindex 08f0e20650..fc489cb35a 100644--- a/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts+++ b/worker/src/features/posthog/handlePostHogIntegrationProjectJob.ts@@ -8,6 +8,7 @@ import {getGenerationsForAnalyticsIntegrations,getScoresForAnalyticsIntegrations,getCurrentSpan,+ validateWebhookURL,} from "@langfuse/shared/src/server";import {transformTraceForPostHog,@@ -182,6 +183,18 @@ export const handlePostHogIntegrationProjectJob = async (return;}+ // Validate PostHog hostname to prevent SSRF attacks before sending data+ try {+ await validateWebhookURL(postHogIntegration.posthogHostName);+ } catch (error) {+ logger.error(+ `PostHog integration for project ${projectId} has invalid hostname: ${postHogIntegration.posthogHostName}. Error: ${error instanceof Error ? error.message : String(error)}`,+ );+ throw new Error(+ `Invalid PostHog hostname for project ${projectId}: ${error instanceof Error ? error.message : "Unknown error"}`,+ );+ }+// Fetch relevant data and send it to PostHogconst executionConfig: PostHogExecutionConfig = {projectId,