Skip to content

Commit c57cd50

Browse files
authored
fix: security patches (#192)
1 parent f7507f0 commit c57cd50

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

packages/backend/src/api/v1/evaluations/index.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import Router from "koa-router"
1+
import { runChecksOnRun } from "@/src/checks/runChecks"
2+
import { checkAccess } from "@/src/utils/authorization"
3+
import { calcRunCost } from "@/src/utils/calcCost"
4+
import { getReadableDateTime } from "@/src/utils/date"
25
import sql from "@/src/utils/db"
36
import Context from "@/src/utils/koa"
4-
import { getReadableDateTime } from "@/src/utils/date"
5-
import { runEval } from "./utils"
6-
import { getEvaluation } from "./utils"
7-
import { calcRunCost } from "@/src/utils/calcCost"
8-
import { runChecksOnRun } from "@/src/checks/runChecks"
7+
import Router from "koa-router"
8+
import { RunEvent } from "lunary/types"
99
import PQueue from "p-queue"
1010
import { PassThrough } from "stream"
11-
import { checkAccess } from "@/src/utils/authorization"
12-
import { RunEvent } from "lunary/types"
11+
import { runEval } from "./utils"
1312

1413
const evaluations = new Router({ prefix: "/evaluations" })
1514

@@ -20,7 +19,7 @@ evaluations.post(
2019
checkAccess("evaluations", "create"),
2120
async (ctx: Context) => {
2221
const { name, datasetId, checklistId, providers } = ctx.request.body as any
23-
const { userId, projectId } = ctx.state
22+
const { userId, projectId, orgId } = ctx.state
2423

2524
ctx.request.socket.setTimeout(0)
2625
ctx.request.socket.setNoDelay(true)
@@ -37,6 +36,12 @@ evaluations.post(
3736
timeout: 10000,
3837
})
3938

39+
const [{ plan }] =
40+
await sql`select plan, eval_allowance from org where id = ${orgId}`
41+
if (plan === "free") {
42+
ctx.throw(403, "You can't create evaluations on the free plan.")
43+
}
44+
4045
// TODO: transactions, but not working with because of nesting
4146
const evaluationToInsert = {
4247
name: name ? name : `Evaluation of ${getReadableDateTime()}`,

packages/backend/src/api/v1/projects/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ projects.post(
121121
const { projectId } = ctx.params
122122
const { userId } = ctx.state
123123

124-
// Define the schema for request body validation using Zod
124+
const [hasAccess] =
125+
await sql`select * from account_project where project_id = ${projectId} and account_id = ${userId}`
126+
if (!hasAccess) {
127+
ctx.throw(401, "Not allowed")
128+
}
129+
125130
const requestBodySchema = z.object({
126131
type: z.enum(["private", "public"]),
127132
})

packages/backend/src/api/v1/radars.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ radars.get("/:radarId/chart", async (ctx) => {
218218
})
219219

220220
radars.post("/", checkAccess("radars", "create"), async (ctx) => {
221-
const { projectId, userId } = ctx.state
221+
const { projectId, userId, orgId } = ctx.state
222222
const { description, view, checks, alerts, negative } = ctx.request.body as {
223223
description: string
224224
view: any[]
@@ -227,6 +227,12 @@ radars.post("/", checkAccess("radars", "create"), async (ctx) => {
227227
negative: boolean
228228
}
229229

230+
const [{ plan }] =
231+
await sql`select plan, eval_allowance from org where id = ${orgId}`
232+
if (plan === "free") {
233+
ctx.throw(403, "You can't create evaluations on the free plan.")
234+
}
235+
230236
const [row] = await sql`
231237
insert into radar ${sql({
232238
description,

0 commit comments

Comments
 (0)