GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146581: Use ZipFile.extractall() in shutil for secure ZIP extraction by Shrey-N · Pull Request #146588 · python/cpython

Conversation

@Shrey-N

@Shrey-N Shrey-N commented Mar 29, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This PR refactors shutil._unpack_zipfile to use zipfile.ZipFile.extractall() instead of a manual extraction loop. This resolves a directory traversal vulnerability on Windows where archives containing drive prefixed paths for exampleD:/file.txt could write files outside the intended destination directory.

Changes Made

  • Replaced manual path joining and validation with ZipFile.extractall(), which leverages the zipfile module
  • Updated the _unpack_zipfile signature to accept **kwargs. This ensures compatibility with the filter argument, preventing TypeError while allowing future filter support for ZIP files.
  • This change brings ZIP extraction in line with how shutil handles TAR files, which already uses extractall().

Regression Test

Added a new test case test_unpack_zipfile_traversal_windows_drive to Lib/test/test_shutil.py. This test specifically verifies that a ZIP file containing a drive prefixed path is safely sanitized and extracted within the intended extract_dir on Windows.

Linked Issue

#146581

  • Issue: shutil.unpack_archive() on Windows writes outside extract_dir for ZIP entries with drive-prefixed names #146581

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Shrey-N added 3 commits March 29, 2026 13:31
@Shrey-N
Update shutil.py
b3d178a
@Shrey-N
Implement Windows ZIP traversal test in test_shutil
494d2b1
Add a test case for ZIP file traversal on Windows.
@Shrey-N
Clarify comments in test_unpack_zipfile_traversal_windows_drive
d185905
Updated comments for clarity and removed redundant lines.
@Shrey-N
Shrey-N requested a review from giampaolo as a code owner March 29, 2026 08:12
@bedevere-app bedevere-app Bot added the awaiting review label Mar 29, 2026
blurb-it Bot and others added 4 commits March 29, 2026 08:14
@blurb-it
📜🤖 Added by blurb_it.
958ec29
@Shrey-N
Remove unnecessary blank lines in test_shutil.py
b0f0dc8
@Shrey-N
Clean up whitespace in _unpack_zipfile function
8ce8bc2
Removed unnecessary blank lines in _unpack_zipfile function.
@Shrey-N
Update shutil.py
cfb3d0f
@hugovk hugovk mentioned this pull request Mar 29, 2026
shutil.unpack_archive() on Windows writes outside extract_dir for ZIP entries with drive-prefixed names #146581
Closed
@Shrey-N
Update shutil.py
678fc3d
@StanFromIreland

StanFromIreland commented Mar 29, 2026

Copy link
Copy Markdown
Member

Closing in favour of #146591

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@StanFromIreland StanFromIreland closed this Mar 29, 2026
@serhiy-storchaka

serhiy-storchaka commented Mar 29, 2026

Copy link
Copy Markdown
Member

Thank you for your PR, @Shrey-N, but we already discussed this issue and created a solution. #146591 is in general similar to your PR, but includes results of that discussion.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Shrey-N

Shrey-N commented Mar 29, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the update @serhiy-storchaka. I am glad to see the fix landing via #146591 :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@giampaolo giampaolo Awaiting requested review from giampaolo giampaolo is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@Shrey-N @StanFromIreland @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.