Skip to content

gh-160446: replace instead of writing through in tarfile/zipfile - #160449

Open
haampie wants to merge 1 commit into
python:mainfrom
haampie-llms:tarfile-replace-links
Open

haampie wants to merge 1 commit into
python:mainfrom
haampie-llms:tarfile-replace-links

Conversation

@haampie

@haampie haampie commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

When extracting a regular file, open the output with O_CREAT|O_EXCL and, if something already exists at the path, unlink it and retry once, instead of opening with O_TRUNC. Previously an existing symlink or hardlink at that path was written through, which could modify a file outside the extraction directory, also under tarfile's 'data' and 'tar' filters.

Windows and Emscripten follow a dangling symlink even with O_EXCL, so there the path is unlinked before opening.

The helper lives in a new private module, _archive, shared by tarfile and zipfile.

…it in tarfile and zipfile

When extracting a regular file, open the output with O_CREAT|O_EXCL and,
if something already exists at the path, unlink it and retry once,
instead of opening with O_TRUNC. Previously an existing symlink or
hardlink at that path was written through, which could modify a file
outside the extraction directory, also under tarfile's 'data' and 'tar'
filters.

Windows and Emscripten follow a dangling symlink even with O_EXCL, so
there the path is unlinked before opening.

The helper lives in a new private module, _archive, shared by tarfile
and zipfile.

Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
@haampie

haampie commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Possibly of interest to @woodruffw.

The mkdir side is not part of this PR, cause unfortunately it doesn't exactly mirror the file case.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant