@@ -72,14 +72,44 @@ 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 MAX_CHATFLOW_TYPES = 1000
82+ if ( chatflowTypes . length > MAX_CHATFLOW_TYPES ) {
83+ throw new Error ( `Cannot evaluate more than ${ MAX_CHATFLOW_TYPES } chatflow types at once` )
84+ }
85+
86+ const simpleEvaluators = body . selectedSimpleEvaluators . length > 0 ? JSON . parse ( body . selectedSimpleEvaluators ) : [ ]
87+ if ( ! Array . isArray ( simpleEvaluators ) ) {
88+ throw new Error ( 'selectedSimpleEvaluators must be a valid array' )
89+ }
90+
91+ const MAX_EVALUATORS = 1000
92+ if ( simpleEvaluators . length > MAX_EVALUATORS ) {
93+ throw new Error ( `Cannot use more than ${ MAX_EVALUATORS } simple evaluators at once` )
94+ }
95+
7596 const additionalConfig : ICommonObject = {
76- chatflowTypes : body . chatflowType ? JSON . parse ( body . chatflowType ) : [ ] ,
97+ chatflowTypes : chatflowTypes ,
7798 datasetAsOneConversation : body . datasetAsOneConversation ,
78- simpleEvaluators : body . selectedSimpleEvaluators . length > 0 ? JSON . parse ( body . selectedSimpleEvaluators ) : [ ]
99+ simpleEvaluators : simpleEvaluators
79100 }
80101
81102 if ( body . evaluationType === 'llm' ) {
82- additionalConfig . lLMEvaluators = body . selectedLLMEvaluators . length > 0 ? JSON . parse ( body . selectedLLMEvaluators ) : [ ]
103+ const lLMEvaluators = body . selectedLLMEvaluators . length > 0 ? JSON . parse ( body . selectedLLMEvaluators ) : [ ]
104+ if ( ! Array . isArray ( lLMEvaluators ) ) {
105+ throw new Error ( 'selectedLLMEvaluators must be a valid array' )
106+ }
107+
108+ if ( lLMEvaluators . length > MAX_EVALUATORS ) {
109+ throw new Error ( `Cannot use more than ${ MAX_EVALUATORS } LLM evaluators at once` )
110+ }
111+
112+ additionalConfig . lLMEvaluators = lLMEvaluators
83113 additionalConfig . llmConfig = {
84114 credentialId : body . credentialId ,
85115 llm : body . llm ,
@@ -123,6 +153,17 @@ const createEvaluation = async (body: ICommonObject, baseURL: string, orgId: str
123153 // When chatflow has an APIKey
124154 const apiKeys : { chatflowId : string ; apiKey : string } [ ] = [ ]
125155 const chatflowIds = JSON . parse ( body . chatflowId )
156+
157+ // Validate chatflowIds is an actual array to prevent DoS attacks
158+ if ( ! Array . isArray ( chatflowIds ) ) {
159+ throw new Error ( 'chatflowId must be a valid array' )
160+ }
161+
162+ const MAX_CHATFLOWS_EVAL = 100
163+ if ( chatflowIds . length > MAX_CHATFLOWS_EVAL ) {
164+ throw new Error ( `Cannot evaluate more than ${ MAX_CHATFLOWS_EVAL } chatflows at once` )
165+ }
166+
126167 for ( let i = 0 ; i < chatflowIds . length ; i ++ ) {
127168 const chatflowId = chatflowIds [ i ]
128169 const cFlow = await appServer . AppDataSource . getRepository ( ChatFlow ) . findOneBy ( {
@@ -246,7 +287,7 @@ const createEvaluation = async (body: ICommonObject, baseURL: string, orgId: str
246287 metricsArray ,
247288 actualOutputArray ,
248289 errorArray ,
249- body . selectedSimpleEvaluators . length > 0 ? JSON . parse ( body . selectedSimpleEvaluators ) : [ ] ,
290+ additionalConfig . simpleEvaluators ,
250291 workspaceId
251292 )
252293
@@ -257,7 +298,7 @@ const createEvaluation = async (body: ICommonObject, baseURL: string, orgId: str
257298
258299 if ( body . evaluationType === 'llm' ) {
259300 resultRow . llmConfig = additionalConfig . llmConfig
260- resultRow . LLMEvaluators = body . selectedLLMEvaluators . length > 0 ? JSON . parse ( body . selectedLLMEvaluators ) : [ ]
301+ resultRow . LLMEvaluators = additionalConfig . lLMEvaluators
261302 const llmEvaluatorMap : { evaluatorId : string ; evaluator : any } [ ] = [ ]
262303 for ( let i = 0 ; i < resultRow . LLMEvaluators . length ; i ++ ) {
263304 const evaluatorId = resultRow . LLMEvaluators [ i ]
0 commit comments