CVE-2024-1738: Incorrect Authorization (CWE-863)
value: An incorrect authorization vulnerability exists in the lunary-ai/lunary repository, specifically within the evaluations.get route in the evaluations API endpoint. This vulnerability allows unauthorized users to retrieve the results of any organization's evaluation by simply knowing the evaluation ID, due to the lack of project ID verification in the SQL query. As a result, attackers can gain access to potentially private data contained within the evaluation results.
CWE description: The product performs an authorization check when an actor attempts to access a resource or perform an action, but it does not correctly perform the check. This allows attackers to bypass intended access restrictions.
Published: 2024-04-16T00:15Z
Repository: https://github.com/lunary-ai/lunary
Language: JSON
Files changed: package.json, packages/backend/src/api/v1/evaluations/index.ts
Commit date: 2024-04-06 11:12:59 -0300
Commit message: fix: get evaluation result (#181)


Diff: diff --git a/package.json b/package.json
index 1ba8a461..c60bb503 100644
--- a/package.json
+++ b/package.json
@@ -8,11 +8,11 @@
     "start:backend": "npm -w packages/backend run start",
     "start:radar": "npm -w packages/backend run start:radar",
     "start:ml": ". ./packages/ml/venv/bin/activate && python3 packages/ml/main.py",
     "build:frontend": "npm -w packages/frontend run build",
     "migrate:db": "npm -w packages/backend run migrate:db",
-    "dev": "concurrently \"npm run dev:frontend\" \"npm run dev:backend\" \"npm run dev:radar\"",
+    "dev": "concurrently \"npm run dev:frontend\" \"npm run dev:backend\"",
     "dev:frontend": "npm -w packages/frontend run dev",
     "dev:backend": "npm -w packages/backend run dev",
     "dev:radar": "npm -w packages/backend run dev:radar",
     "dev:withradar": "concurrently \"npm run dev:frontend\" \"npm run dev:backend\" \"npm run start:ml\" \"npm run dev:radar\"",
     "test": "npx playwright test",
diff --git a/packages/backend/src/api/v1/evaluations/index.ts b/packages/backend/src/api/v1/evaluations/index.ts
index 59d6d292..9b0a5ff4 100644
--- a/packages/backend/src/api/v1/evaluations/index.ts
+++ b/packages/backend/src/api/v1/evaluations/index.ts
@@ -120,21 +120,25 @@ evaluations.get(
 evaluations.get(
   "/result/:evaluationId",
   checkAccess("evaluations", "read"),
   async (ctx: Context) => {
     const { evaluationId } = ctx.params
+    const { projectId } = ctx.state
 
     const results = await sql`
-  select 
-    *,
-    p.id as prompt_id
-  from 
-    evaluation_result er 
-    left join dataset_prompt p on p.id = er.prompt_id
-    left join dataset_prompt_variation pv on pv.id = er.variation_id
-  where 
-    er.evaluation_id = ${evaluationId}`
+      select 
+        *,
+        p.id as prompt_id
+      from 
+        evaluation_result er 
+        left join evaluation e on e.id = er.evaluation_id
+        left join dataset_prompt p on p.id = er.prompt_id
+        left join dataset_prompt_variation pv on pv.id = er.variation_id
+      where 
+        er.evaluation_id = ${evaluationId}
+        and e.project_id = ${projectId}
+      `
 
     ctx.body = results
   },
 )
 

Vulnerable code:     "dev": "concurrently \"npm run dev:frontend\" \"npm run dev:backend\" \"npm run dev:radar\"",
  select 
    *,
    p.id as prompt_id
  from 
    evaluation_result er 
    left join dataset_prompt p on p.id = er.prompt_id
    left join dataset_prompt_variation pv on pv.id = er.variation_id
  where 
    er.evaluation_id = ${evaluationId}`
Fixed code:     "dev": "concurrently \"npm run dev:frontend\" \"npm run dev:backend\"",
    const { projectId } = ctx.state
      select 
        *,
        p.id as prompt_id
      from 
        evaluation_result er 
        left join evaluation e on e.id = er.evaluation_id
        left join dataset_prompt p on p.id = er.prompt_id
        left join dataset_prompt_variation pv on pv.id = er.variation_id
      where 
        er.evaluation_id = ${evaluationId}
        and e.project_id = ${projectId}
      `