Skip to content

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

fix(slack): force project auth on slack install endpoint (#11566)

Steffen Schmitz3adc89e4d727
diff --git a/web/src/__tests__/async/slack-integration.servertest.ts b/web/src/__tests__/async/slack-integration.servertest.ts
index cc04051a7b..8ae3aa1518 100644
--- a/web/src/__tests__/async/slack-integration.servertest.ts
+++ b/web/src/__tests__/async/slack-integration.servertest.ts
@@ -423,6 +423,50 @@ describe("Slack Integration", () => {
});
});
+ describe("Slack Install Endpoint Authentication", () => {
+ it("should reject unauthenticated requests with 401", async () => {
+ const { project } = await prepare();
+
+ // Make request without any session/auth
+ const response = await fetch(
+ `http://localhost:3000/api/public/slack/install?projectId=${project.id}`,
+ {
+ method: "GET",
+ headers: {
+ Accept: "application/json",
+ },
+ // No cookies/session - simulating unauthenticated request
+ },
+ );
+
+ expect(response.status).toBe(401);
+
+ const body = await response.json();
+ expect(body).toMatchObject({
+ error: "Authentication required",
+ });
+ });
+
+ it("should reject requests without projectId with 400", async () => {
+ const response = await fetch(
+ `http://localhost:3000/api/public/slack/install`,
+ {
+ method: "GET",
+ headers: {
+ Accept: "application/json",
+ },
+ },
+ );
+
+ expect(response.status).toBe(400);
+
+ const body = await response.json();
+ expect(body).toMatchObject({
+ error: "Missing projectId parameter",
+ });
+ });
+ });
+
describe("Slack Security", () => {
it("should encrypt bot tokens in database", async () => {
const { project } = await prepare();
diff --git a/web/src/features/slack/server/oauth-handlers.ts b/web/src/features/slack/server/oauth-handlers.ts
index 8a41e088ff..a392d475c4 100644
--- a/web/src/features/slack/server/oauth-handlers.ts
+++ b/web/src/features/slack/server/oauth-handlers.ts
@@ -5,6 +5,9 @@ import {
} from "@langfuse/shared/src/server";
import { logger } from "@langfuse/shared/src/server";
import { env } from "@/src/env.mjs";
+import { getServerAuthSession } from "@/src/server/auth";
+import { auditLog } from "@/src/features/audit-logs/auditLog";
+import { prisma } from "@langfuse/shared/src/db";
/**
* SlackOAuthHandlers
@@ -71,6 +74,44 @@ export async function handleCallback(
teamName: installation.team?.name,
});
+ // Create audit log for the Slack integration
+ // The session should still be valid from when the user initiated the install
+ try {
+ const session = await getServerAuthSession({ req, res });
+ if (session?.user?.id) {
+ // Find the integration that was just created
+ const integration = await prisma.slackIntegration.findUnique({
+ where: { projectId },
+ select: {
+ id: true,
+ projectId: true,
+ project: { select: { orgId: true } },
+ },
+ });
+
+ if (integration) {
+ await auditLog({
+ userId: session.user.id,
+ orgId: integration.project.orgId,
+ projectId,
+ resourceType: "slackIntegration",
+ resourceId: integration.id,
+ action: "create",
+ after: {
+ teamId: installation.team?.id,
+ teamName: installation.team?.name,
+ },
+ });
+ }
+ }
+ } catch (auditError) {
+ // Don't fail the callback if audit logging fails
+ logger.warn("Failed to create audit log for Slack installation", {
+ error: auditError,
+ projectId,
+ });
+ }
+
// Redirect to project-specific Slack settings page
const redirectUrl = `/project/${projectId}/settings/integrations/slack?success=true&team_name=${encodeURIComponent(installation.team?.name || "")}`;
res.redirect(redirectUrl);
diff --git a/web/src/pages/api/public/slack/install/index.ts b/web/src/pages/api/public/slack/install/index.ts
index 4d0f1091cd..75763ef1dc 100644
--- a/web/src/pages/api/public/slack/install/index.ts
+++ b/web/src/pages/api/public/slack/install/index.ts
@@ -2,6 +2,8 @@ import { type NextApiRequest, type NextApiResponse } from "next";
import { handleInstallPath } from "@/src/features/slack/server/oauth-handlers";
import { logger } from "@langfuse/shared/src/server";
import { cors, runMiddleware } from "@/src/features/public-api/server/cors";
+import { getServerAuthSession } from "@/src/server/auth";
+import { hasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
export default async function handler(
req: NextApiRequest,
@@ -21,7 +23,34 @@ export default async function handler(
return res.status(400).json({ error: "Missing projectId parameter" });
}
- logger.info("Slack install request received", { projectId });
+ // Verify user is authenticated
+ const session = await getServerAuthSession({ req, res });
+ if (!session) {
+ logger.warn("Unauthenticated Slack install attempt", { projectId });
+ return res.status(401).json({ error: "Authentication required" });
+ }
+
+ // Verify user has permission to modify automations for this project
+ const hasAccess = hasProjectAccess({
+ session,
+ projectId,
+ scope: "automations:CUD",
+ });
+
+ if (!hasAccess) {
+ logger.warn("Unauthorized Slack install attempt", {
+ projectId,
+ userId: session.user?.id,
+ });
+ return res.status(403).json({
+ error: "You do not have permission to configure Slack for this project",
+ });
+ }
+
+ logger.info("Slack install request received", {
+ projectId,
+ userId: session.user?.id,
+ });
// Let SlackOAuthHandlers handle the installation page rendering
// This includes: