Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_defaults():
"""Verify the default Validator settings."""
validator = validators.Validator()
assert validator.required_components == {
c: False for c in validator.COMPONENT_NAMES
}
assert validator.allow_password is True
assert validator.allowed_schemes == set()
assert validator.allowed_hosts == set()
assert validator.allowed_ports == set()
def test_passwordless_uris_pass_validation(uri):
"""Verify password-less URLs validate properly."""
validator = validators.Validator().forbid_use_of_password()
validator.validate(uri)
def __call__(self, value):
uri = rfc3986.uri_reference(value)
validator = rfc3986.validators.Validator().require_presence_of(
'scheme', 'host',
).check_validity_of(
'scheme', 'host', 'path',
)
if self.schemes:
validator = validator.allow_schemes(*self.schemes)
try:
validator.validate(uri)
except rfc3986.exceptions.RFC3986Exception as exc:
raise ValueError(exc)
if self.max_length is not None and len(value) > self.max_length:
raise ValueError("Value '%s' exceeds maximum length %d" %
(value, self.max_length))
# NOTE(dhellmann): self.value is deprecated, and we don't want
def _validate_repository_url(repository_url: str) -> None:
"""Validate the given url for allowed schemes and components."""
# Allowed schemes are http and https, based on whether the repository
# supports TLS or not, and scheme and host must be present in the URL
validator = (
rfc3986.validators.Validator()
.allow_schemes("http", "https")
.require_presence_of("scheme", "host")
)
try:
validator.validate(rfc3986.uri_reference(repository_url))
except rfc3986.exceptions.RFC3986Exception as exc:
raise exceptions.UnreachableRepositoryURLDetected(
f"Invalid repository URL: {exc.args[0]}."
)
def __call__(self, value):
uri = rfc3986.uri_reference(value)
validator = rfc3986.validators.Validator().require_presence_of(
'scheme', 'host',
).check_validity_of(
'scheme', 'host', 'path',
)
if self.schemes:
validator = validator.allow_schemes(*self.schemes)
try:
validator.validate(uri)
except rfc3986.exceptions.RFC3986Exception as exc:
raise ValueError(exc)
if self.max_length is not None and len(value) > self.max_length:
raise ValueError("Value '%s' exceeds maximum length %d" %
(value, self.max_length))
# NOTE(dhellmann): self.value is deprecated, and we don't want
uuid.UUID(value, version=4)
except Exception:
return ERROR
elif format == 'binary':
try:
base64.b64decode(value)
except Exception:
return ERROR
return value
# Internal
_EMAIL_PATTERN = re.compile(r'[^@]+@[^@]+\.[^@]+')
_uri_from_string = rfc3986.uri.URIReference.from_string
_uri_validator = rfc3986.validators.Validator().require_presence_of('scheme')
def is_valid_uri(
uri, require_scheme=True, allowed_schemes={"http", "https"}, require_authority=True
):
uri = uri_reference(uri).normalize()
validator = validators.Validator().allow_schemes(*allowed_schemes)
if require_scheme:
validator.require_presence_of("scheme")
if require_authority:
validator.require_presence_of("host")
validator.check_validity_of("scheme", "host", "port", "path", "query")
try:
validator.validate(uri)
except exceptions.ValidationError:
return False
return True