Class DomainNameUtils

java.lang.Object
com.digicert.validation.utils.DomainNameUtils

public class DomainNameUtils extends Object
Utility class for domain name validation and manipulation.

This class provides various utility methods for validating and manipulating domain names. It includes methods for checking the validity of domain names and labels, removing wildcard prefixes, and determining the base domain and parent domains. The class also handles special cases such as reserved LDH labels and IDNA (Internationalized Domain Names in Applications) processing.

  • Field Details

    • DOMAIN_LABEL_REGEX

      private static final String DOMAIN_LABEL_REGEX
      Domain name labels consist of up to 63 letters, numbers, and hyphens, but cannot start or end with a hyphen.
      See Also:
    • DOMAIN_LABEL_PATTERN

      private static final Pattern DOMAIN_LABEL_PATTERN
      Pattern for a domain label.
    • DOMAIN_REGEX

      private static final String DOMAIN_REGEX
      Regex pattern for domains. Domains are two or more "."-separated labels.
      See Also:
    • DOMAIN_PATTERN

      private static final Pattern DOMAIN_PATTERN
      Compiled pattern for a domain.
    • XN_LABEL

      public static final String XN_LABEL
      Per https://datatracker.ietf.org/doc/rfc5890/, labels that start with "??--" are "tagged domain names". Currently, the only allowed start for such labels is "xn--". This is a regex pattern for the "xn--" label.
      See Also:
    • R_LDH_LABEL_SEPARATOR

      public static final String R_LDH_LABEL_SEPARATOR
      Separator for reserved LDH labels.
      See Also:
    • MAX_DOMAIN_LENGTH

      private static final int MAX_DOMAIN_LENGTH
      Maximum length of a domain name.
      See Also:
    • EMAIL_CHAR_REGEX

      private static final String EMAIL_CHAR_REGEX
      Regex for one or more of the allowed char set for the local-part of a valid email address.
      See Also:
    • EMAIL_LOCAL_PART_REGEX

      private static final String EMAIL_LOCAL_PART_REGEX
      Regex for the local-part of an email address

      The local-part of an email address can contain '.'s, but they cannot be the first or last character, and they cannot appear consecutively.

      See Also:
    • EMAIL_PATTERN

      private static final Pattern EMAIL_PATTERN
      Compiled pattern for validating email addresses.

      Email address consist of a local-part and a domain, with an @ separating them.

    • UTS_46_INSTANCE

      private static final com.ibm.icu.text.IDNA UTS_46_INSTANCE
      IDNA instance for UTS 46.

      This instance of the IDNA class is configured for UTS 46 (Unicode Technical Standard #46) processing. It is used to handle internationalized domain names, converting them between Unicode and ASCII representations.

    • pslOverrideSupplier

      private final PslOverrideSupplier pslOverrideSupplier
      Supplier for Public Suffix List overrides.

      This supplier provides overrides for the Public Suffix List (PSL). It is used to handle special cases where the default PSL does not apply, allowing for custom domain suffix rules.

  • Constructor Details

    • DomainNameUtils

      public DomainNameUtils(DcvContext dcvContext)
      Constructs a new DomainNameUtils with the specified DcvContext.
      Parameters:
      dcvContext - context where we can find the needed dependencies and configuration
  • Method Details

    • validateDomainName

      public void validateDomainName(String domainName) throws InputException
      Validates the given domain name.

      This method validates the provided domain name by checking its length, format, and reserved LDH labels. It also ensures that the domain name is under a valid public suffix.

      Parameters:
      domainName - the domain name to validate
      Throws:
      InputException - if the domain name is invalid
    • truncateDomainName

      private String truncateDomainName(String domainName)
      Utility function to prevent logging a domain name that is excessively long.
      Parameters:
      domainName - the domain name to truncate
      Returns:
      up to MAX_DOMAIN_LENGTH+10 chars of the domain name followed by "..." if the domain name was truncated
    • isValidDomainLabel

      public static boolean isValidDomainLabel(String domainLabel)
      Checks if the given domain label is matches the DOMAIN_LABEL_PATTERN.
      Parameters:
      domainLabel - the domain label to check
      Returns:
      true if the domain label is valid, false otherwise
    • domainMatchesRegex

      public static boolean domainMatchesRegex(String domain)
      Checks if the given domain matches the domain regex pattern.
      Parameters:
      domain - the domain to check
      Returns:
      true if the domain matches the regex pattern, false otherwise
    • removeWildCard

      static String removeWildCard(String domainName)
      Removes the wildcard prefix from the given domain name and ensures it is lowercase.
      Parameters:
      domainName - the domain name to process
      Returns:
      the domain name without the wildcard prefix
    • domainContainsInvalidReservedLDHLabel

      static boolean domainContainsInvalidReservedLDHLabel(String domain)
      Determines if the domain contains invalid reserved LDH labels, including verifying any punycode present is valid.
      Parameters:
      domain - the domain to check
      Returns:
      true if the domain contains invalid reserved LDH labels, false otherwise
    • getDomainAndParents

      public List<String> getDomainAndParents(String domain) throws InputException
      Gets the domain and its parent domains.

      This method returns a list of the provided domain name and all parent domains up to the base domain. For example, "a.b.c.d.e.com" would return the list ["a.b.c.d.e.com", "b.c.d.e.com", "c.d.e.com", "d.e.com", "e.com"]

      Parameters:
      domain - the domain to process
      Returns:
      a list of the domain and its parent domains
      Throws:
      InputException - if the domain is invalid
    • getBaseDomain

      public String getBaseDomain(String domainName) throws InputException
      Gets the base domain of the given domain name.

      This method returns the base domain of the provided domain name. It uses the Public Suffix List (PSL) and any overrides to determine the base domain.

      Parameters:
      domainName - the domain name to process
      Returns:
      the base domain
      Throws:
      InputException - if the domain is invalid
    • isValidEmailAddress

      public static boolean isValidEmailAddress(String email)
      Checks if the given email address is valid.

      This method checks if the provided email address matches the EMAIL_PATTERN.

      Parameters:
      email - the email address to check
      Returns:
      true if the email address is valid, false otherwise