fix: apply tar "data" extraction filter whenever available (path traversal in Untar) - #544
Open
gaoflow wants to merge 1 commit into
Open
fix: apply tar "data" extraction filter whenever available (path traversal in Untar)#544gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
Untar gated the "data" tarfile extraction filter on Python >= 3.12, so on 3.9-3.11 it extracted with no filter and no member validation. Since the filter was backported to 3.9.17, 3.10.12 and 3.11.4, a malicious archive (a symlink escaping the destination followed by a file written through it) could write outside the .untar directory on those interpreters. Detect the filter by feature (hasattr(tarfile, "data_filter")) instead of minor version so the protection applies on every interpreter that supports it. Closes fatiando#543
Author
|
Heads-up: the red CI here isn't from this change — every job fails in the "Collect requirements" step with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
Untarenables the tarfiledataextraction filter only on Python ≥ 3.12:But the
datafilter (PEP 706) was backported to 3.9.17, 3.10.12 and 3.11.4. On those interpreters Pooch skips the filter even though it is available, sotarfile.extractall()runs with no filter and no per-member validation.A malicious archive served from a caller-influenceable URL can then escape the destination: a symlink member pointing outside
.untar, followed by a regular file written through that symlink, writes the file outside the extraction directory when an application callspooch.retrieve(url, known_hash=None, processor=Untar()). Reported in #543.I verified this on Python 3.11.11 (which does have
tarfile.data_filter): the two-entry PoC writes outside the destination under the current< (3, 12)gate, and is blocked (AbsoluteLinkError) once the filter is applied.Fix
Detect the filter by feature instead of minor version:
The protection now applies on every interpreter that ships the filter, while interpreters without it keep the previous behavior (no regression). Scope is limited to
Untar;Unziprelies onZipFile.extractall, which already sanitizes../ absolute member names.Test
Adds
test_untar_blocks_symlink_path_traversal: it builds the symlink-escape tar in memory, then asserts extraction raisestarfile.FilterErrorand that no file escapes the destination. The test isskipif-guarded on the rare interpreter withoutdata_filter. It fails on the old< (3, 12)gate (the escape succeeds) and passes with the fix.Closes #543