Skip to content

Commit eb7a31e

Browse files
committed
when file urls have hash fragments, check it
1 parent e41bf02 commit eb7a31e

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

pip/download.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,6 @@ def unpack_http_url(link, location, download_cache, download_dir=None,
625625
def unpack_file_url(link, location, download_dir=None):
626626

627627
link_path = url_to_path(link.url_without_fragment)
628-
from_path = None
629628
already_downloaded = False
630629

631630
# If it's a url to a local directory
@@ -635,6 +634,11 @@ def unpack_file_url(link, location, download_dir=None):
635634
shutil.copytree(link_path, location, symlinks=True)
636635
return
637636

637+
# if link has a hash, let's confirm it matches
638+
if link.hash:
639+
link_path_hash = _get_hash_from_file(link_path, link)
640+
_check_hash(link_path_hash, link)
641+
638642
# If a download dir is specified, is the file already there and valid?
639643
if download_dir:
640644
download_path = os.path.join(download_dir, link.filename)
@@ -655,16 +659,17 @@ def unpack_file_url(link, location, download_dir=None):
655659
else:
656660
already_downloaded = True
657661

658-
# a download dir is specified and not already downloaded
659-
if download_dir and not already_downloaded:
660-
content_type = mimetypes.guess_type(link_path)[0]
661-
_copy_file(link_path, download_dir, content_type, link)
662-
663-
# unpack the archive to the build dir location. even when only downloading
664-
# archives, they have to be unpacked to parse dependencies
665662
if already_downloaded:
666663
from_path = download_path
667664
else:
668665
from_path = link_path
666+
669667
content_type = mimetypes.guess_type(from_path)[0]
668+
669+
# unpack the archive to the build dir location. even when only downloading
670+
# archives, they have to be unpacked to parse dependencies
670671
unpack_file(from_path, location, content_type, link)
672+
673+
# a download dir is specified and not already downloaded
674+
if download_dir and not already_downloaded:
675+
_copy_file(from_path, download_dir, content_type, link)

tests/unit/test_download.py

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pip
1010
from pip.backwardcompat import urllib, BytesIO, b, pathname2url
11+
from pip.exceptions import HashMismatch
1112
from pip.download import (PipSession, path_to_url, unpack_http_url,
1213
url_to_path, unpack_file_url)
1314
from pip.index import Link
@@ -229,6 +230,16 @@ def test_unpack_file_url_download_already_exists(self, tmpdir,
229230
assert dist_path2_md5 == hashlib.md5(
230231
open(dest_file, 'rb').read()).hexdigest()
231232

233+
def test_unpack_file_url_bad_hash(self, tmpdir, data,
234+
monkeypatch):
235+
"""
236+
Test when the file url hash fragment is wrong
237+
"""
238+
self.prep(tmpdir, data)
239+
self.dist_url.url = "%s#md5=bogus" % self.dist_url.url
240+
with pytest.raises(HashMismatch):
241+
unpack_file_url(self.dist_url, self.build_dir)
242+
232243
def test_unpack_file_url_download_bad_hash(self, tmpdir, data,
233244
monkeypatch):
234245
"""

0 commit comments

Comments
 (0)