From 106bd0d77fbe5f22c9a67f74ca74b3e97513aba9 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Sun, 29 Mar 2020 12:00:14 +0530 Subject: [PATCH 1/3] Raise an exception if revision is empty in git url --- news/7402.bugfix | 1 + src/pip/_internal/vcs/versioncontrol.py | 6 ++++++ tests/unit/test_vcs.py | 15 +++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 news/7402.bugfix diff --git a/news/7402.bugfix b/news/7402.bugfix new file mode 100644 index 000000000..8c8372914 --- /dev/null +++ b/news/7402.bugfix @@ -0,0 +1 @@ +Raise an exception if revision part of URL is empty for URL used in VCS support diff --git a/src/pip/_internal/vcs/versioncontrol.py b/src/pip/_internal/vcs/versioncontrol.py index da53827cd..8bfa1cd57 100644 --- a/src/pip/_internal/vcs/versioncontrol.py +++ b/src/pip/_internal/vcs/versioncontrol.py @@ -436,6 +436,12 @@ class VersionControl(object): rev = None if '@' in path: path, rev = path.rsplit('@', 1) + if not rev: + raise ValueError( + "The URL {!r} has an empty revision (after @) " + "which is not supported. Include a revision after @ " + "or remove @ from the URL.".format(url) + ) url = urllib_parse.urlunsplit((scheme, netloc, path, query, '')) return url, rev, user_pass diff --git a/tests/unit/test_vcs.py b/tests/unit/test_vcs.py index 42fc43d68..92e1c0e34 100644 --- a/tests/unit/test_vcs.py +++ b/tests/unit/test_vcs.py @@ -292,6 +292,21 @@ def test_version_control__get_url_rev_and_auth__missing_plus(url): assert 'malformed VCS url' in str(excinfo.value) +@pytest.mark.parametrize('url', [ + # Test a URL with revision part as empty. + 'git+https://github.com/MyUser/myProject.git@#egg=py_pkg', +]) +def test_version_control__get_url_rev_and_auth__no_revision(url): + """ + Test passing a URL to VersionControl.get_url_rev_and_auth() with + empty revision + """ + with pytest.raises(ValueError) as excinfo: + VersionControl.get_url_rev_and_auth(url) + + assert 'an empty revision (after @)' in str(excinfo.value) + + @pytest.mark.parametrize('url, expected', [ # Test http. ('bzr+http://bzr.myproject.org/MyProject/trunk/#egg=MyProject', From 0d2ca67729344adb5514ad7d1be7e1850c3d6be6 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Mon, 30 Mar 2020 22:29:40 +0530 Subject: [PATCH 2/3] Changed ValueError to InstallationError --- src/pip/_internal/vcs/versioncontrol.py | 4 ++-- tests/unit/test_vcs.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pip/_internal/vcs/versioncontrol.py b/src/pip/_internal/vcs/versioncontrol.py index 8bfa1cd57..71b4650a2 100644 --- a/src/pip/_internal/vcs/versioncontrol.py +++ b/src/pip/_internal/vcs/versioncontrol.py @@ -11,7 +11,7 @@ import sys from pip._vendor import pkg_resources from pip._vendor.six.moves.urllib import parse as urllib_parse -from pip._internal.exceptions import BadCommand +from pip._internal.exceptions import BadCommand, InstallationError from pip._internal.utils.compat import samefile from pip._internal.utils.misc import ( ask_path_exists, @@ -437,7 +437,7 @@ class VersionControl(object): if '@' in path: path, rev = path.rsplit('@', 1) if not rev: - raise ValueError( + raise InstallationError( "The URL {!r} has an empty revision (after @) " "which is not supported. Include a revision after @ " "or remove @ from the URL.".format(url) diff --git a/tests/unit/test_vcs.py b/tests/unit/test_vcs.py index 92e1c0e34..590cb5c0b 100644 --- a/tests/unit/test_vcs.py +++ b/tests/unit/test_vcs.py @@ -5,7 +5,7 @@ import pytest from mock import patch from pip._vendor.packaging.version import parse as parse_version -from pip._internal.exceptions import BadCommand +from pip._internal.exceptions import BadCommand, InstallationError from pip._internal.utils.misc import hide_url, hide_value from pip._internal.vcs import make_vcs_requirement_url from pip._internal.vcs.bazaar import Bazaar @@ -301,7 +301,7 @@ def test_version_control__get_url_rev_and_auth__no_revision(url): Test passing a URL to VersionControl.get_url_rev_and_auth() with empty revision """ - with pytest.raises(ValueError) as excinfo: + with pytest.raises(InstallationError) as excinfo: VersionControl.get_url_rev_and_auth(url) assert 'an empty revision (after @)' in str(excinfo.value) From 59df53690604ebed67f20977d76a9f96ece03918 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Tue, 31 Mar 2020 20:08:47 +0530 Subject: [PATCH 3/3] Update newsfile message --- news/7402.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/7402.bugfix b/news/7402.bugfix index 8c8372914..91eb085f5 100644 --- a/news/7402.bugfix +++ b/news/7402.bugfix @@ -1 +1 @@ -Raise an exception if revision part of URL is empty for URL used in VCS support +Reject VCS URLs with an empty revision.