-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalidateChatBot.ts
More file actions
42 lines (39 loc) · 2.07 KB
/
Copy pathvalidateChatBot.ts
File metadata and controls
42 lines (39 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import config from 'config'
import logger from '../logger'
import colors from 'colors/safe'
import * as utils from '../utils'
export default function validateChatBot (trainingData: any, exitOnFailure = true) {
let success = true
success = checkIntentWithFunctionHandlerExists(trainingData, 'queries.couponCode', 'couponCode') && success
success = checkIntentWithFunctionHandlerExists(trainingData, 'queries.productPrice', 'productPrice') && success
success = checkIntentWithFunctionHandlerExists(trainingData, 'queries.functionTest', 'testFunction') && success
if (success) {
logger.info(`Chatbot training data ${colors.bold(utils.extractFilename(config.get('application.chatBot.trainingData')))} validated (${colors.green('OK')})`)
} else {
logger.warn(`Chatbot training data ${colors.bold(utils.extractFilename(config.get('application.chatBot.trainingData')))} validated (${colors.red('NOT OK')})`)
logger.warn(`Visit ${colors.yellow('https://pwning.owasp-juice.shop/appendix/chatbot.html')} for the training data schema definition.`)
if (exitOnFailure) {
logger.error(colors.red('Exiting due to configuration errors!'))
process.exit(1)
}
}
return success
}
export const checkIntentWithFunctionHandlerExists = (trainingData: any, intent: string, handler: string) => {
let success = true
const intentData = trainingData.data.filter((data: any) => data.intent === intent)
if (intentData.length === 0) {
logger.warn(`Intent ${colors.italic(intent)} is missing in chatbot training data (${colors.red('NOT OK')})`)
success = false
} else {
if (intentData[0].answers.filter((answer: { action: string, handler: string }) => answer.action === 'function' && answer.handler === handler).length === 0) {
logger.warn(`Answer with ${colors.italic('function')} action and handler ${colors.italic(handler)} is missing for intent ${colors.italic(intent)} (${colors.red('NOT OK')})`)
success = false
}
}
return success
}