Skip to content

Commit

Permalink
Use rfc3986 to validate repo url
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshks committed May 8, 2020
1 parent e00c9d7 commit ba72c0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ install_requires=
tqdm >= 4.14
importlib_metadata; python_version < "3.8"
keyring >= 15.1
rfc3986 >= 1.4.0
setup_requires =
setuptools_scm >= 1.15

Expand Down
32 changes: 29 additions & 3 deletions twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from urllib.parse import urlunparse

import requests
from rfc3986 import uri_reference
from rfc3986 import validators

from twine import exceptions

Expand Down Expand Up @@ -99,20 +101,44 @@ def get_config(path: str = "~/.pypirc") -> Dict[str, RepositoryConfig]:
return dict(config)


def validate_url(repository_url: Optional[str]) -> Any:
"""Validate the given url for allowed schemes and components"""

if not repository_url:
return None

# Scheme should always be https, and the url should at minimum
# contain scheme and host
validator = (
validators.Validator()
.allow_schemes("https")
.require_presence_of("scheme", "host")
)

# Only return scheme when the url is valid
url = uri_reference(repository_url)
try:
validator.validate(url)
except Exception:
return None

return url.scheme


def get_repository_from_config(
config_file: str, repository: str, repository_url: Optional[str] = None
) -> RepositoryConfig:
# Get our config from, if provided, command-line values for the
# repository name and URL, or the .pypirc file
parsed = urlparse(repository_url)
if repository_url and parsed.scheme:
scheme = validate_url(repository_url)
if repository_url and scheme:
# prefer CLI `repository_url` over `repository` or .pypirc
return {
"repository": repository_url,
"username": None,
"password": None,
}
if repository_url and not parsed.scheme:
if repository_url and not scheme:
raise exceptions.UnreachableRepositoryURLDetected(
"Repository URL {} has no protocol. Please add "
"'https://'. \n".format(repository_url)
Expand Down

0 comments on commit ba72c0c

Please sign in to comment.