shareSameDomainSuffix function

bool shareSameDomainSuffix(
  1. String hostname,
  2. String vhost
)

Check if vhost is a valid suffix of hostname (top-domain)

It means that vhost needs to be a suffix of hostname and we then need to make sure that: either they are equal, or the character preceding vhost in hostname is a '.' (it should not be a partial label).

  • hostname = 'not.evil.com' and vhost = 'vil.com' => not ok
  • hostname = 'not.evil.com' and vhost = 'evil.com' => ok
  • hostname = 'not.evil.com' and vhost = 'not.evil.com' => ok

Implementation

bool shareSameDomainSuffix(String hostname, String vhost) {
  if (hostname.endsWith(vhost)) {
    return (hostname.length == vhost.length ||
        hostname[hostname.length - vhost.length - 1] == '.');
  }

  return false;
}