forked from p15670423/monkey
Common: Separate filename validation out of config schemas
This commit is contained in:
parent
3a6fcd670c
commit
2503e83dde
|
@ -1,6 +1,4 @@
|
||||||
import re
|
from marshmallow import Schema, fields, post_load, validate
|
||||||
|
|
||||||
from marshmallow import Schema, ValidationError, fields, post_load, validate, validates
|
|
||||||
|
|
||||||
from .agent_sub_configurations import (
|
from .agent_sub_configurations import (
|
||||||
CustomPBAConfiguration,
|
CustomPBAConfiguration,
|
||||||
|
@ -14,11 +12,12 @@ from .agent_sub_configurations import (
|
||||||
TCPScanConfiguration,
|
TCPScanConfiguration,
|
||||||
)
|
)
|
||||||
from .utils import freeze_lists
|
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
|
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):
|
class CustomPBAConfigurationSchema(Schema):
|
||||||
linux_command = fields.Str()
|
linux_command = fields.Str()
|
||||||
|
@ -26,38 +25,7 @@ class CustomPBAConfigurationSchema(Schema):
|
||||||
validate=validate.Regexp(regex=valid_linux_custom_pba_filename_regex)
|
validate=validate.Regexp(regex=valid_linux_custom_pba_filename_regex)
|
||||||
)
|
)
|
||||||
windows_command = fields.Str()
|
windows_command = fields.Str()
|
||||||
windows_filename = fields.Str(
|
windows_filename = fields.Str(validate=validate_windows_custom_pba_filename)
|
||||||
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")
|
|
||||||
|
|
||||||
@post_load
|
@post_load
|
||||||
def _make_custom_pba_configuration(self, data, **kwargs):
|
def _make_custom_pba_configuration(self, data, **kwargs):
|
||||||
|
|
|
@ -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")
|
Loading…
Reference in New Issue