Skip to content

Commit ac8a988

Browse files
author
christopherholland-workday
committed
Add protections for loop-bound injections
1 parent 95a26ae commit ac8a988

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

packages/components/evaluation/EvaluationRunner.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ export class EvaluationRunner {
8888

8989
public async runEvaluations(data: ICommonObject) {
9090
const chatflowIds = JSON.parse(data.chatflowId)
91+
92+
// Validate chatflowIds is an actual array to prevent DoS attacks
93+
if (!Array.isArray(chatflowIds)) {
94+
throw new Error('chatflowId must be a valid array')
95+
}
96+
97+
// Validate dataset.rows is an actual array to prevent DoS attacks
98+
if (!data.dataset || !Array.isArray(data.dataset.rows)) {
99+
throw new Error('dataset.rows must be a valid array')
100+
}
101+
91102
const returnData: ICommonObject = {}
92103
returnData.evaluationId = data.evaluationId
93104
returnData.runDate = new Date()

packages/server/src/services/evaluations/EvaluatorRunner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export const runAdditionalEvaluators = async (
1717
selectedEvaluators: string[],
1818
workspaceId: string
1919
) => {
20+
// Validate inputs are arrays and enforce size limits
21+
if (!Array.isArray(actualOutputArray) || !Array.isArray(selectedEvaluators)) {
22+
throw new Error('Invalid input: expected arrays')
23+
}
24+
2025
const evaluationResults: any[] = []
2126
const evaluatorDict: any = {}
2227

packages/server/src/services/evaluations/index.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,31 @@ const createEvaluation = async (body: ICommonObject, baseURL: string, orgId: str
7272
const row = appServer.AppDataSource.getRepository(Evaluation).create(newEval)
7373
row.average_metrics = JSON.stringify({})
7474

75+
// Parse and validate evaluator arrays to prevent DoS attacks
76+
const chatflowTypes = body.chatflowType ? JSON.parse(body.chatflowType) : []
77+
if (!Array.isArray(chatflowTypes)) {
78+
throw new Error('chatflowType must be a valid array')
79+
}
80+
81+
const simpleEvaluators = body.selectedSimpleEvaluators.length > 0 ? JSON.parse(body.selectedSimpleEvaluators) : []
82+
if (!Array.isArray(simpleEvaluators)) {
83+
throw new Error('selectedSimpleEvaluators must be a valid array')
84+
}
85+
7586
const additionalConfig: ICommonObject = {
76-
chatflowTypes: body.chatflowType ? JSON.parse(body.chatflowType) : [],
87+
chatflowTypes: chatflowTypes,
7788
datasetAsOneConversation: body.datasetAsOneConversation,
78-
simpleEvaluators: body.selectedSimpleEvaluators.length > 0 ? JSON.parse(body.selectedSimpleEvaluators) : []
89+
simpleEvaluators: simpleEvaluators
7990
}
8091

8192
if (body.evaluationType === 'llm') {
82-
additionalConfig.lLMEvaluators = body.selectedLLMEvaluators.length > 0 ? JSON.parse(body.selectedLLMEvaluators) : []
93+
const lLMEvaluators = body.selectedLLMEvaluators.length > 0 ? JSON.parse(body.selectedLLMEvaluators) : []
94+
95+
if (!Array.isArray(lLMEvaluators)) {
96+
throw new Error('selectedLLMEvaluators must be a valid array')
97+
}
98+
99+
additionalConfig.lLMEvaluators = lLMEvaluators
83100
additionalConfig.llmConfig = {
84101
credentialId: body.credentialId,
85102
llm: body.llm,
@@ -123,6 +140,12 @@ const createEvaluation = async (body: ICommonObject, baseURL: string, orgId: str
123140
// When chatflow has an APIKey
124141
const apiKeys: { chatflowId: string; apiKey: string }[] = []
125142
const chatflowIds = JSON.parse(body.chatflowId)
143+
144+
// Validate chatflowIds is an actual array to prevent DoS attacks
145+
if (!Array.isArray(chatflowIds)) {
146+
throw new Error('chatflowId must be a valid array')
147+
}
148+
126149
for (let i = 0; i < chatflowIds.length; i++) {
127150
const chatflowId = chatflowIds[i]
128151
const cFlow = await appServer.AppDataSource.getRepository(ChatFlow).findOneBy({
@@ -246,7 +269,7 @@ const createEvaluation = async (body: ICommonObject, baseURL: string, orgId: str
246269
metricsArray,
247270
actualOutputArray,
248271
errorArray,
249-
body.selectedSimpleEvaluators.length > 0 ? JSON.parse(body.selectedSimpleEvaluators) : [],
272+
additionalConfig.simpleEvaluators,
250273
workspaceId
251274
)
252275

@@ -257,7 +280,7 @@ const createEvaluation = async (body: ICommonObject, baseURL: string, orgId: str
257280

258281
if (body.evaluationType === 'llm') {
259282
resultRow.llmConfig = additionalConfig.llmConfig
260-
resultRow.LLMEvaluators = body.selectedLLMEvaluators.length > 0 ? JSON.parse(body.selectedLLMEvaluators) : []
283+
resultRow.LLMEvaluators = additionalConfig.lLMEvaluators
261284
const llmEvaluatorMap: { evaluatorId: string; evaluator: any }[] = []
262285
for (let i = 0; i < resultRow.LLMEvaluators.length; i++) {
263286
const evaluatorId = resultRow.LLMEvaluators[i]

0 commit comments

Comments
 (0)