Skip to content

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

fix(llm-connections): enforce write permissions on LLM connection test endpoints (#13027)

Hassieb Pakzade12386f9d436
diff --git a/web/src/__tests__/server/llm-api-key.servertest.ts b/web/src/__tests__/server/llm-api-key.servertest.ts
index 064cceefc0..0e8e4f027b 100644
--- a/web/src/__tests__/server/llm-api-key.servertest.ts
+++ b/web/src/__tests__/server/llm-api-key.servertest.ts
@@ -11,14 +11,46 @@ import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
describe("llmApiKey.all RPC", () => {
let projectId: string;
let orgId: string;
+ let session: Session;
let caller: ReturnType<typeof appRouter.createCaller>;
+ const createCallerForProjectRole = (
+ projectRole: "ADMIN" | "MEMBER" | "VIEWER",
+ ) => {
+ const limitedSession: Session = {
+ ...session,
+ user: {
+ ...session.user!,
+ admin: false,
+ organizations: [
+ {
+ ...session.user!.organizations[0],
+ role: "MEMBER",
+ projects: [
+ {
+ ...session.user!.organizations[0].projects[0],
+ role: projectRole,
+ },
+ ],
+ },
+ ],
+ },
+ };
+
+ const limitedCtx = createInnerTRPCContext({
+ session: limitedSession,
+ headers: {},
+ });
+
+ return appRouter.createCaller({ ...limitedCtx, prisma });
+ };
+
beforeEach(async () => {
const setup = await createOrgProjectAndApiKey();
projectId = setup.projectId;
orgId = setup.orgId;
- const session: Session = {
+ session = {
expires: "1",
user: {
id: "user-1",
@@ -141,6 +173,49 @@ describe("llmApiKey.all RPC", () => {
expect(secretKey).toBeUndefined();
});
+ it("should require llmApiKeys:create access for testing a new llm api key", async () => {
+ const memberCaller = createCallerForProjectRole("MEMBER");
+
+ await expect(
+ memberCaller.llmApiKey.test({
+ projectId,
+ provider: "openai",
+ adapter: LLMAdapter.OpenAI,
+ secretKey: "sk-test",
+ baseURL: "https://attacker.example.com/v1",
+ }),
+ ).rejects.toThrow("User does not have access to this resource or action");
+ });
+
+ it("should require llmApiKeys:update access for testing an existing llm api key", async () => {
+ await caller.llmApiKey.create({
+ projectId,
+ provider: "openai",
+ adapter: LLMAdapter.OpenAI,
+ secretKey: "sk-test",
+ baseURL: "https://api.openai.com/v1",
+ });
+
+ const existingKey = await prisma.llmApiKeys.findFirstOrThrow({
+ where: {
+ projectId,
+ provider: "openai",
+ },
+ });
+
+ const memberCaller = createCallerForProjectRole("MEMBER");
+
+ await expect(
+ memberCaller.llmApiKey.testUpdate({
+ id: existingKey.id,
+ projectId,
+ provider: "openai",
+ adapter: LLMAdapter.OpenAI,
+ baseURL: "https://attacker.example.com/v1",
+ }),
+ ).rejects.toThrow("User does not have access to this resource or action");
+ });
+
it("should create and update an llm api key", async () => {
const secret = "test-secret";
const provider = "openai";
diff --git a/web/src/features/llm-api-key/server/router.ts b/web/src/features/llm-api-key/server/router.ts
index bddccef389..fa81773e09 100644
--- a/web/src/features/llm-api-key/server/router.ts
+++ b/web/src/features/llm-api-key/server/router.ts
@@ -399,7 +399,13 @@ export const llmApiKeyRouter = createTRPCRouter({
test: protectedProjectProcedureWithoutTracing
.input(CreateLlmApiKey)
- .mutation(async ({ input }) => {
+ .mutation(async ({ input, ctx }) => {
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "llmApiKeys:create",
+ });
+
return testLLMConnection({
adapter: input.adapter,
provider: input.provider,
@@ -414,13 +420,13 @@ export const llmApiKeyRouter = createTRPCRouter({
testUpdate: protectedProjectProcedureWithoutTracing
.input(UpdateLlmApiKey)
.mutation(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "llmApiKeys:read",
- });
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "llmApiKeys:update",
+ });
+ try {
// Get the existing key from the database
const existingKey = await ctx.prisma.llmApiKeys.findUnique({
where: {