Common: Improve hostname pattern matching in scan target configuration

This commit is contained in:
vakarisz 2022-07-28 16:35:04 +03:00
parent ce33a7b2f4
commit a1760a8701
2 changed files with 19 additions and 10 deletions

View File

@ -3,9 +3,6 @@ from ipaddress import AddressValueError, IPv4Address, IPv4Network, NetmaskValueE
from marshmallow import ValidationError
hostname_pattern = r"([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*.?)*([A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*)"
hostname_regex = re.compile(hostname_pattern)
def validate_subnet_range(subnet_range: str):
try:
@ -30,11 +27,21 @@ def validate_subnet_range(subnet_range: str):
def validate_hostname(hostname: str):
match = re.match(hostname_regex, hostname)
if match and match.group() == hostname:
return
else:
raise ValidationError(f"Invalid hostname {hostname}")
# Based on hostname syntax: https://www.rfc-editor.org/rfc/rfc1123#page-13
hostname_segments = hostname.split(".")
if any((part.endswith("-") or part.startswith("-") for part in hostname_segments)):
raise ValidationError(f"Hostname segment can't start or end with a hyphen: {hostname}")
if not any((char.isalpha() for char in hostname_segments[-1])):
raise ValidationError(f"Last segment of a hostname must contain a letter: {hostname}")
valid_characters_pattern = r"^[A-Za-z0-9\-]+$"
valid_characters_regex = re.compile(valid_characters_pattern)
matches = (
re.match(valid_characters_regex, hostname_segment) for hostname_segment in hostname_segments
)
if not all(matches):
raise ValidationError(f"Hostname contains invalid characters: {hostname}")
def validate_ip_network(ip_network: str):

View File

@ -46,12 +46,14 @@ def test_validate_subnet_range__ip_range_invalid(ip_range):
validate_subnet_range(ip_range)
@pytest.mark.parametrize("hostname", ["infection.monkey", "1nfection-Monkey"])
@pytest.mark.parametrize("hostname", ["infection.monkey", "1nfection-Monkey", "1.1.1.1a"])
def test_validate_subnet_range__hostname_valid(hostname):
validate_subnet_range(hostname)
@pytest.mark.parametrize("hostname", ["hy&!he.host", "čili-peppers.are-hot"])
@pytest.mark.parametrize(
"hostname", ["hy&!he.host", "čili-peppers.are-hot", "one.two-", "one-.two", "one@two", ""]
)
def test_validate_subnet_range__hostname_invalid(hostname):
with pytest.raises(ValidationError):
validate_subnet_range(hostname)