in tools/tags_validator.py [0:0]
def _assert_valid_url(self, possible_url: str) -> None:
"""Checks that the given string is a valid and allowed URL.
The HTTPS URL should be parseable, not contain spaces and start with an
allowed prefix.
Args:
possible_url: String that should be checked for being an allowed URL.
Raises:
TagDefinitionError:
- if the string is not a valid URL.
- if the URL contains spaces.
- if the URL is not an HTTPS URL.
- if the URL does not specify both a domain and a path.
- if the URL does not start with one allowed URL prefix.
"""
try:
result = urllib.parse.urlparse(possible_url)
except ValueError:
raise TagDefinitionError(f"{possible_url} is not a valid URL.")
if " " in possible_url:
raise TagDefinitionError(f"{possible_url} must not contain spaces.")
if result.scheme != HTTPS_SCHEME:
raise TagDefinitionError(f"{possible_url} is not a HTTPS URL.")
if not all([result.netloc, result.path]):
raise TagDefinitionError(
f"{possible_url} must specify a domain and a path.")
# Keep the two preceding tests even if the next one is stricter. This should
# allow to be future proof in case e.g. an http prefix is accidentially
# added to ALLOWED_URL_PREFIXES.
if not possible_url.startswith(tuple(ALLOWED_URL_PREFIXES)):
raise TagDefinitionError(
f"URL needs to start with any of {sorted(ALLOWED_URL_PREFIXES)}"
f" but was {possible_url}.")