From 3496c717a9dea4b3dd5ae61807856f6b1407208b Mon Sep 17 00:00:00 2001 From: Shreya Date: Fri, 2 Jul 2021 16:36:54 +0530 Subject: [PATCH] cc, common: Split ransomware dir path validator regex expressions and rename related stuff to accurately describe it --- .../common_consts/validation_formats.py | 4 +-- .../cc/services/config_schema/ransomware.py | 9 +++-- .../ValidationErrorMessages.js | 6 ++-- .../ValidationFormats.js | 36 ++++++++++++------- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/monkey/common/common_consts/validation_formats.py b/monkey/common/common_consts/validation_formats.py index c7f92e5e5..41a460a8a 100644 --- a/monkey/common/common_consts/validation_formats.py +++ b/monkey/common/common_consts/validation_formats.py @@ -1,5 +1,5 @@ # Defined in UI on ValidationFormats.js IP_RANGE = "ip-range" IP = "ip" -VALID_DIR_LINUX = "valid-directory-linux" -VALID_DIR_WINDOWS = "valid-directory-windows" +VALID_RANSOMWARE_TARGET_PATH_LINUX = "valid-ransomware-target-path-linux" +VALID_RANSOMWARE_TARGET_PATH_WINDOWS = "valid-ransomware-target-path-windows" diff --git a/monkey/monkey_island/cc/services/config_schema/ransomware.py b/monkey/monkey_island/cc/services/config_schema/ransomware.py index 4e03af168..be4403c6f 100644 --- a/monkey/monkey_island/cc/services/config_schema/ransomware.py +++ b/monkey/monkey_island/cc/services/config_schema/ransomware.py @@ -1,4 +1,7 @@ -from common.common_consts.validation_formats import VALID_DIR_LINUX, VALID_DIR_WINDOWS +from common.common_consts.validation_formats import ( + VALID_RANSOMWARE_TARGET_PATH_LINUX, + VALID_RANSOMWARE_TARGET_PATH_WINDOWS, +) RANSOMWARE = { "title": "Ransomware", @@ -22,7 +25,7 @@ RANSOMWARE = { "linux_target_dir": { "title": "Linux target directory", "type": "string", - "format": VALID_DIR_LINUX, + "format": VALID_RANSOMWARE_TARGET_PATH_LINUX, "default": "", "description": "A path to a directory on Linux systems that contains " "files that you will allow Infection Monkey to encrypt. If no " @@ -31,7 +34,7 @@ RANSOMWARE = { "windows_target_dir": { "title": "Windows target directory", "type": "string", - "format": VALID_DIR_WINDOWS, + "format": VALID_RANSOMWARE_TARGET_PATH_WINDOWS, "default": "", "description": "A path to a directory on Windows systems that contains " "files that you will allow Infection Monkey to encrypt. If no " 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 index 803e4e7e7..3c7280f97 100644 --- a/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationErrorMessages.js +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/ValidationErrorMessages.js @@ -1,4 +1,4 @@ -import {IP, IP_RANGE, VALID_DIR_LINUX, VALID_DIR_WINDOWS} from './ValidationFormats'; +import {IP, IP_RANGE, VALID_RANSOMWARE_TARGET_PATH_LINUX, VALID_RANSOMWARE_TARGET_PATH_WINDOWS} from './ValidationFormats'; let invalidDirMessage = 'Invalid directory. Path should be absolute or begin with an environment variable.'; @@ -10,9 +10,9 @@ export default function transformErrors(errors) { error.message = 'Invalid IP range, refer to description for valid examples.' } else if (error.name === 'format' && error.params.format === IP) { error.message = 'Invalid IP.' - } else if (error.name === 'format' && error.params.format === VALID_DIR_LINUX) { + } else if (error.name === 'format' && error.params.format === VALID_RANSOMWARE_TARGET_PATH_LINUX) { error.message = invalidDirMessage - } else if (error.name === 'format' && error.params.format === VALID_DIR_WINDOWS) { + } else if (error.name === 'format' && error.params.format === VALID_RANSOMWARE_TARGET_PATH_WINDOWS) { error.message = invalidDirMessage } 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 e03519c5d..1038c45a2 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 @@ -1,22 +1,26 @@ const ipRegex = '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' 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]*)$' -// path is empty, or starts with `/` OR `$` -const linuxDirRegex = '(^\\s*$)|^/|^\\$' -// path is empty, or starts like `C:\` OR `C:/` OR `$` OR `%abc%` -const windowsDirRegex = '(^\\s*$)|^([A-Za-z]:(\\\\|\\/))|^\\$|^(%\\w*\\d*\\s*%)' + +const linuxAbsolutePathRegex = '^/' // path starts with `/` +const linuxPathStartsWithEnvVariableRegex = '^\\$' // path starts with `$` + +const windowsAbsolutePathRegex = '^([A-Za-z]:(\\\\|\\/))' // path starts like `C:\` OR `C:/` +const windowsPathStartsWithEnvVariableRegex = '^\\$|^(%\\w*\\d*\\s*%)' // path starts like `$` OR `%abc%` + +const whitespacesOnlyRegex = '^\\s*$' export const IP_RANGE = 'ip-range'; export const IP = 'ip'; -export const VALID_DIR_LINUX = 'valid-directory-linux' -export const VALID_DIR_WINDOWS = 'valid-directory-windows' +export const VALID_RANSOMWARE_TARGET_PATH_LINUX = 'valid-ransomware-target-path-linux' +export const VALID_RANSOMWARE_TARGET_PATH_WINDOWS = 'valid-ransomware-target-path-windows' export const formValidationFormats = { [IP_RANGE]: buildIpRangeRegex(), [IP]: buildIpRegex(), - [VALID_DIR_LINUX]: buildValidDirLinuxRegex(), - [VALID_DIR_WINDOWS]: buildValidDirWindowsRegex() + [VALID_RANSOMWARE_TARGET_PATH_LINUX]: buildValidRansomwarePathLinuxRegex(), + [VALID_RANSOMWARE_TARGET_PATH_WINDOWS]: buildValidRansomwarePathWindowsRegex() }; function buildIpRangeRegex(){ @@ -32,10 +36,18 @@ function buildIpRegex(){ return new RegExp('^'+ipRegex+'$') } -function buildValidDirLinuxRegex() { - return new RegExp(linuxDirRegex) +function buildValidRansomwarePathLinuxRegex() { + return new RegExp([ + whitespacesOnlyRegex, + linuxAbsolutePathRegex, + linuxPathStartsWithEnvVariableRegex + ].join('|')) } -function buildValidDirWindowsRegex() { - return new RegExp(windowsDirRegex) +function buildValidRansomwarePathWindowsRegex() { + return new RegExp([ + whitespacesOnlyRegex, + windowsAbsolutePathRegex, + windowsPathStartsWithEnvVariableRegex + ].join('|')) }