Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extract_zipped_paths infinite loop when provided invalid unc path #5851

Merged
merged 1 commit into from Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions requests/utils.py
Expand Up @@ -245,6 +245,10 @@ def extract_zipped_paths(path):
archive, member = os.path.split(path)
while archive and not os.path.exists(archive):
archive, prefix = os.path.split(archive)
if not prefix:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explains why, but the code isn't clear as to what's happening or why this fixes the infinite loop. For example, a far superior comment might be:

# If we don't check for an empty prefix after the split (in other words, archive = / before and after the split), we _can_ end up in an infinite loop on a rare corner case affecting a small number of users

This comment explains the conditions, why we care (it affects users), and the risk to making a change (if we break this again, we may not find out about it quickly)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I've updated the comment.

# If we don't check for an empty prefix after the split (in other words, archive remains unchanged after the split),
# we _can_ end up in an infinite loop on a rare corner case affecting a small number of users
break
member = '/'.join([prefix, member])

if not zipfile.is_zipfile(archive):
Expand Down
4 changes: 4 additions & 0 deletions tests/test_utils.py
Expand Up @@ -285,6 +285,10 @@ def test_zipped_paths_extracted(self, tmpdir):
assert os.path.exists(extracted_path)
assert filecmp.cmp(extracted_path, __file__)

def test_invalid_unc_path(self):
path = r"\\localhost\invalid\location"
assert extract_zipped_paths(path) == path


class TestContentEncodingDetection:

Expand Down