From 2503e83dde0e0f2ec09cb815d1a8a7cde76f22da Mon Sep 17 00:00:00 2001 From: vakarisz Date: Mon, 1 Aug 2022 13:01:37 +0300 Subject: [PATCH] Common: Separate filename validation out of config schemas --- .../agent_sub_configuration_schemas.py | 44 +++---------------- .../validators/filenames.py | 19 ++++++++ 2 files changed, 25 insertions(+), 38 deletions(-) create mode 100644 monkey/common/agent_configuration/validators/filenames.py diff --git a/monkey/common/agent_configuration/agent_sub_configuration_schemas.py b/monkey/common/agent_configuration/agent_sub_configuration_schemas.py index 30c49c27f..b6a803eb6 100644 --- a/monkey/common/agent_configuration/agent_sub_configuration_schemas.py +++ b/monkey/common/agent_configuration/agent_sub_configuration_schemas.py @@ -1,6 +1,4 @@ -import re - -from marshmallow import Schema, ValidationError, fields, post_load, validate, validates +from marshmallow import Schema, fields, post_load, validate from .agent_sub_configurations import ( CustomPBAConfiguration, @@ -14,11 +12,12 @@ from .agent_sub_configurations import ( TCPScanConfiguration, ) from .utils import freeze_lists +from .validators.filenames import ( + valid_linux_custom_pba_filename_regex, + validate_windows_custom_pba_filename, +) from .validators.ip_ranges import validate_ip, validate_subnet_range -valid_windows_custom_pba_filename_regex = re.compile(r"^[^<>:\"\\\/|?*]*[^<>:\"\\\/|?* \.]+$|^$") -valid_linux_custom_pba_filename_regex = re.compile(r"^[^\0/]*$") - class CustomPBAConfigurationSchema(Schema): linux_command = fields.Str() @@ -26,38 +25,7 @@ class CustomPBAConfigurationSchema(Schema): validate=validate.Regexp(regex=valid_linux_custom_pba_filename_regex) ) windows_command = fields.Str() - windows_filename = fields.Str( - validate=validate.Regexp(regex=valid_windows_custom_pba_filename_regex) - ) - - @validates("windows_filename") - def validate_windows_filename_not_reserved(self, windows_filename): - # filename shouldn't start with any of these and be followed by a period - if windows_filename.split(".")[0].upper() in [ - "CON", - "PRN", - "AUX", - "NUL", - "COM1", - "COM2", - "COM3", - "COM4", - "COM5", - "COM6", - "COM7", - "COM8", - "COM9", - "LPT1", - "LPT2", - "LPT3", - "LPT4", - "LPT5", - "LPT6", - "LPT7", - "LPT8", - "LPT9", - ]: - raise ValidationError("Invalid Windows filename: reserved name used") + windows_filename = fields.Str(validate=validate_windows_custom_pba_filename) @post_load def _make_custom_pba_configuration(self, data, **kwargs): diff --git a/monkey/common/agent_configuration/validators/filenames.py b/monkey/common/agent_configuration/validators/filenames.py new file mode 100644 index 000000000..33ccb8cd9 --- /dev/null +++ b/monkey/common/agent_configuration/validators/filenames.py @@ -0,0 +1,19 @@ +import re +from pathlib import PureWindowsPath + +from marshmallow import ValidationError + +valid_windows_custom_pba_filename_regex = re.compile(r"^[^<>:\"\\\/|?*]*[^<>:\"\\\/|?* \.]+$|^$") +valid_linux_custom_pba_filename_regex = re.compile(r"^[^\0/]*$") + + +def validate_windows_custom_pba_filename(windows_filename: str): + validate_windows_filename_not_reserved(windows_filename) + if not re.match(valid_windows_custom_pba_filename_regex, windows_filename): + raise ValidationError(f"Invalid Windows filename {windows_filename}: illegal characters") + + +def validate_windows_filename_not_reserved(windows_filename: str): + # filename shouldn't start with any of these and be followed by a period + if PureWindowsPath(windows_filename).is_reserved(): + raise ValidationError(f"Invalid Windows filename {windows_filename}: reserved name used")