From 0be9b19186eb461ae43dc762805eff8d811893a1 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 27 Jul 2020 15:31:25 +0300 Subject: [PATCH] Added custom config validation errors --- .../ValidationErrorMessages.js | 14 ++++++++++++++ .../configuration-components/ValidationFormats.js | 8 ++++++-- .../cc/ui/src/components/pages/ConfigurePage.js | 14 +++++++------- 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationErrorMessages.js diff --git a/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationErrorMessages.js b/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationErrorMessages.js new file mode 100644 index 000000000..3d20998ab --- /dev/null +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationErrorMessages.js @@ -0,0 +1,14 @@ +import {IP, IP_RANGE} from "./ValidationFormats"; + +export default function transformErrors(errors) { + return errors.map(error => { + if (error.name === "type") { + error.message = "Field can't be empty." + } else if (error.name === "format" && error.params.format === IP_RANGE) { + error.message = "Invalid IP range, refer to description for valid examples." + } else if (error.name === "format" && error.params.format === IP) { + error.message = "Invalid IP." + } + return error; + }); +} diff --git a/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationFormats.js b/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationFormats.js index eb9c5ecc9..18705660d 100644 --- a/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationFormats.js +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationFormats.js @@ -2,12 +2,16 @@ const ipRegex = '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0 const cidrNotationRegex = '([0-9]|1[0-9]|2[0-9]|3[0-2])' const hostnameRegex = '^([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*.?)*([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*)$' +export const IP_RANGE = 'ip-range'; +export const IP = 'ip'; + export const formValidationFormats = { - 'ip-range': buildIpRangeRegex(), - 'ip': buildIpRegex(), + [IP_RANGE]: buildIpRangeRegex(), + [IP]: buildIpRegex() }; function buildIpRangeRegex(){ + console.log(formValidationFormats); return new RegExp([ '^'+ipRegex+'$|', // Single: IP '^'+ipRegex+'-'+ipRegex+'$|', // IP range: IP-IP diff --git a/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js b/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js index e7ef099e5..dd38a3cf0 100644 --- a/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js +++ b/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js @@ -10,6 +10,7 @@ import {faInfoCircle} from '@fortawesome/free-solid-svg-icons/faInfoCircle'; import {faCheck} from '@fortawesome/free-solid-svg-icons/faCheck'; import {faExclamationCircle} from '@fortawesome/free-solid-svg-icons/faExclamationCircle'; import {formValidationFormats} from '../configuration-components/ValidationFormats'; +import transformErrors from '../configuration-components/ValidationErrorMessages'; import InternalConfig from '../configuration-components/InternalConfig'; const ATTACK_URL = '/api/attack'; @@ -258,8 +259,6 @@ class ConfigurePageComponent extends AuthComponent { this.setState({attackConfig: res.configuration}); this.setInitialAttackConfig(res.configuration); }) - - }; removePBAfile(apiEndpoint, setFilenameFnc) { @@ -341,11 +340,12 @@ class ConfigurePageComponent extends AuthComponent { setPbaFilenameLinux: this.setPbaFilenameLinux, selectedSection: this.state.selectedSection }) - formProperties['formData'] = this.state.configuration[this.state.selectedSection] - formProperties['onChange'] = this.onChange - formProperties['customFormats'] = formValidationFormats - formProperties['className'] = 'config-form' - formProperties['liveValidate'] = true + formProperties['formData'] = this.state.configuration[this.state.selectedSection]; + formProperties['onChange'] = this.onChange; + formProperties['customFormats'] = formValidationFormats; + formProperties['transformErrors'] = transformErrors; + formProperties['className'] = 'config-form'; + formProperties['liveValidate'] = true; if (this.state.selectedSection === 'internal') { return ()