Skip to content

tar.py: Always use extraction filter - #2192

Merged
juergbi merged 5 commits into
masterfrom
jbilleter/tar
Sep 15, 2026
Merged

juergbi merged 5 commits into
masterfrom
jbilleter/tar

Conversation

@juergbi

@juergbi juergbi commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Extraction in the tar plugin currently has two code paths. Using extraction filters on Python 3.12+ and relying on custom safety checks on older Python versions. This PR switches to using an extraction filter on all supported Python versions by importing a copy of Python's tarfile.py for use on older Python versions. This allows replacing the custom safety checks with a combination of upstream Python filters that are more widely tested.

The `tarfile` extraction filter support from Python 3.12 was backported
to older Python releases. However, distro packages of Python < 3.12 may
not include that backport. Import the latest upstream version to be able
to use extraction filters on all Python versions.
This was introduced in Python 3.14. While it was also backported to
older versions, the backport may not be available everywhere.
This aligns the behavior across Python versions, offering some
protection from unusual and possibly malicious tar files. On Python
versions lower than 3.12, this uses the imported copy of `tarfile.py`.
The `tar` filter catches attempts to extract files outside the
destination directory but does not catch hardlink targets outside the
destination directory. The `data` filter catches both but doesn't
preserve symlinks, which may break, e.g., rootfs tarball extraction.

Define a custom extraction filter that uses the `data` filter except for
symlinks where the `tar` filter is used.

This filter replaces the incomplete checks of `_assert_safe()`, which
was invoked before extraction, which meant that the checks may already
have been outdated during actual extraction.
Reported-by: Gjoko Krstic <gjoko@zeroscience.mk>
@juergbi
juergbi merged commit 342f216 into master Sep 15, 2026
17 checks passed
@juergbi
juergbi deleted the jbilleter/tar branch September 15, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants