CVE-2023-48310 (Python, published 2023-11-20T23:15Z)
Description: value: TestingPlatform is a testing platform for Internet Security Standards. Prior to version 2.1.1, user input is not filtered correctly. Nmap options are accepted. In this particular case, the option to create log files is accepted in addition to a host name (and even without). A log file is created at the location specified. These files are created as root. If the file exists, the existing file is being rendered useless. This can result in denial of service. Additionally, input for scanning can be any CIDR blocks passed to nmap. An attacker can scan 0.0.0.0/0 or even local networks. Version 2.1.1 contains a patch for this issue.
Severity: nan (CVSS v3 base score: 7.5)
Weakness: CWE-20 \u2014 Improper Input Validation
The product receives input or data, but it does
        not validate or incorrectly validates that the input has the
        properties that are required to process the data safely and
        correctly.
Repository: https://github.com/NC3-LU/TestingPlatform
Files changed:
api/serializers.py
api/views.py
pyproject.toml
testing/helpers.py
testing/validators.py
Commit message: chg: [security] Added new domain name validator used in the file testing/helpers.py (GHSA-9fhc-f3mr-w6h6).


Vulnerable code:
        max_length=200, required=True, help_text="Domain name."
import re

from rest_framework.serializers import ValidationError

pattern = re.compile(
    r"^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|"
    r"([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|"
    r"([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\."
    r"([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$"
)


# class DomainName:
#     def __init__(self, fields):
#         self.fields = fields

#     def __call__(self, value):
#         for field_name in self.fields:
#             res = pattern.match(value)
#         if res:
#             return True
#         else:
#             raise ValidationError("This field must be a domain name.")


def domain_name(value):
    """
    Return whether or not given value is a valid domain.
    See:
    https://validators.readthedocs.io/en/latest/_modules/validators/domain.html#domain
    """
    res = pattern.match(value)
    if res:
        return True
    else:
        raise ValidationError("This field must be a domain name.")
version = "0.1.0"
def full_domain_validator(hostname):
    """
    Fully validates a domain name as compilant with the standard rules:
        - Composed of series of labels concatenated with dots, as are all domain names.
        - Each label must be between 1 and 63 characters long.
        - The entire hostname (including the delimiting dots) has a maximum of 255 characters.
        - Only characters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9'.
        - Labels can't start or end with a hyphen.
    """
    HOSTNAME_LABEL_PATTERN = re.compile(r"(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
    if not hostname:
        return
    if len(hostname) > 255:
        raise Exception(
            "The domain name cannot be composed of more than 255 characters."
        )
    if hostname[-1:] == ".":
        hostname = hostname[:-1]  # strip exactly one dot from the right, if present
    for label in hostname.split("."):
        if len(label) > 63:
            raise Exception(
                "The label '%(label)s' is too long (maximum is 63 characters)."
                % {"label": label}
            )
        if not HOSTNAME_LABEL_PATTERN.match(label):
            raise Exception(f"Unallowed characters in label '{label}'.")
    return hostname


def check_soa_record(target: str) -> bool:

Fixed code:
from testing.validators import domain_name
        max_length=200,
        required=True,
        help_text="Domain name.",
        validators=[domain_name],
    def validate_domain_name(self, value):
        """
        Check that value is a valid domain name.
        """
        return domain_name(value)

        DomainNameSerializer(domain_name)
version = "0.1.1"
from testing.validators import domain_name, full_domain_validator
    try:
        domain_name(target)
    except Exception:
        return {"error": "You entered an invalid hostname!"}
def check_soa_record(target: str) -> Union[bool, Dict]:
    try:
        domain_name(target)
    except Exception:
        return {"status": False, "statusmessage": "The given domain is invalid!"}
    try:
        domain_name(target)
    except Exception:
        return {"status": False, "statusmessage": "The given domain is invalid!"}
    try:
        domain_name(domain)
    except Exception:
        return {"status": False, "statusmessage": "The given domain is invalid!"}
    try:
        domain_name(hostname)
    except Exception:
        return {"status": False, "statusmessage": "The given domain is invalid!"}
    try:
        domain_name(domain)
    except Exception:
        return {"status": False, "statusmessage": "The given domain is invalid!"}
    try:
        domain_name(domain)
    except Exception:
        return {"status": False, "statusmessage": "The given domain is invalid!"}
import re

from rest_framework.serializers import ValidationError

pattern = re.compile(
    r"^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|"
    r"([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|"
    r"([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9]))\."
    r"([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$"
)


def domain_name(value):
    """
    Return whether or not given value is a valid domain.
    See:
    https://validators.readthedocs.io/en/latest/_modules/validators/domain.html#domain
    """
    res = pattern.match(value)
    if res:
        return True
    else:
        raise ValidationError("This field must be a domain name.")


def full_domain_validator(hostname):
    """
    Fully validates a domain name as compilant with the standard rules:
        - Composed of series of labels concatenated with dots, as are all domain names.
        - Each label must be between 1 and 63 characters long.
        - The entire hostname (including the delimiting dots) has a maximum of 255 characters.
        - Only characters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9'.
        - Labels can't start or end with a hyphen.
    """
    HOSTNAME_LABEL_PATTERN = re.compile(r"(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
    if not hostname:
        return
    if len(hostname) > 255:
        raise Exception(
            "The domain name cannot be composed of more than 255 characters."
        )
    if hostname[-1:] == ".":
        hostname = hostname[:-1]  # strip exactly one dot from the right, if present
    for label in hostname.split("."):
        if len(label) > 63:
            raise Exception(
                "The label '%(label)s' is too long (maximum is 63 characters)."
                % {"label": label}
            )
        if not HOSTNAME_LABEL_PATTERN.match(label):
            raise Exception(f"Unallowed characters in label '{label}'.")
    return hostname