From 068a7e3b1ff45b99bcd37f20276459145faa100f Mon Sep 17 00:00:00 2001 From: vakarisz Date: Mon, 1 Aug 2022 14:33:19 +0300 Subject: [PATCH] Common: Improve the style of filename validation --- .../agent_sub_configuration_schemas.py | 11 +++-------- .../agent_configuration/validators/filenames.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/monkey/common/agent_configuration/agent_sub_configuration_schemas.py b/monkey/common/agent_configuration/agent_sub_configuration_schemas.py index b6a803eb6..bf5f31c61 100644 --- a/monkey/common/agent_configuration/agent_sub_configuration_schemas.py +++ b/monkey/common/agent_configuration/agent_sub_configuration_schemas.py @@ -12,20 +12,15 @@ 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.filenames import validate_linux_filename, validate_windows_filename from .validators.ip_ranges import validate_ip, validate_subnet_range class CustomPBAConfigurationSchema(Schema): linux_command = fields.Str() - linux_filename = fields.Str( - validate=validate.Regexp(regex=valid_linux_custom_pba_filename_regex) - ) + linux_filename = fields.Str(validate=validate_linux_filename) windows_command = fields.Str() - windows_filename = fields.Str(validate=validate_windows_custom_pba_filename) + windows_filename = fields.Str(validate=validate_windows_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 index 33ccb8cd9..897fa2f13 100644 --- a/monkey/common/agent_configuration/validators/filenames.py +++ b/monkey/common/agent_configuration/validators/filenames.py @@ -3,13 +3,18 @@ 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/]*$") +_valid_windows_filename_regex = re.compile(r"^[^<>:\"\\\/|?*]*[^<>:\"\\\/|?* \.]+$|^$") +_valid_linux_filename_regex = re.compile(r"^[^\0/]*$") -def validate_windows_custom_pba_filename(windows_filename: str): +def validate_linux_filename(linux_filename: str): + if not re.match(_valid_linux_filename_regex, linux_filename): + raise ValidationError(f"Invalid Unix filename {linux_filename}: illegal characters") + + +def validate_windows_filename(windows_filename: str): validate_windows_filename_not_reserved(windows_filename) - if not re.match(valid_windows_custom_pba_filename_regex, windows_filename): + if not re.match(_valid_windows_filename_regex, windows_filename): raise ValidationError(f"Invalid Windows filename {windows_filename}: illegal characters")