From: "Miss Islington (bot)"
 <31488909+miss-islington@users.noreply.github.com>
Date: Sun, 25 Oct 2026 06:44:56 -0700
Subject: gh-160097: Reject links replacing the destination in
 `tarfile.tar_filter` (GH-160098) (#160342)

gh-160097: Reject links replacing the destination in `tarfile.tar_filter` (GH-160098)
(cherry picked from commit e7e0b3523ad6ba8472873dda594cf4021a16f8b5)

Co-authored-by: Stan Ulbrych <stan@python.org>
Origin: cPython, https://github.com/python/cpython/commit/f4401da82e2baec5dab06cf070fbca77d53220f0
Bug-Debian: https://bugs.debian.org/1155967
---
 .../Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst   |  5 +++++
 lib-python/3/tarfile.py                                       | 11 ++++++-----
 lib-python/3/test/test_tarfile.py                             |  6 ++++--
 3 files changed, 15 insertions(+), 7 deletions(-)
 create mode 100644 Misc/NEWS.d/next/Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst

diff --git a/Misc/NEWS.d/next/Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst b/Misc/NEWS.d/next/Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst
new file mode 100644
index 0000000..b48c8f7
--- /dev/null
+++ b/Misc/NEWS.d/next/Security/2026-10-21-19-58-53.gh-issue-160097.-opfAL.rst
@@ -0,0 +1,5 @@
+.. cve: CVE-2026-114583
+
+:func:`tarfile.tar_filter` now rejects link members whose path resolves to
+the destination directory itself, as :func:`tarfile.data_filter` already
+did.
diff --git a/lib-python/3/tarfile.py b/lib-python/3/tarfile.py
index ec0f317..7a8e826 100755
--- a/lib-python/3/tarfile.py
+++ b/lib-python/3/tarfile.py
@@ -790,6 +790,12 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
                                    strict=os.path.ALLOW_MISSING)
     if os.path.commonpath([target_path, dest_path]) != dest_path:
         raise OutsideDestinationError(member, target_path)
+    # A link member named after the destination directory itself
+    # would replace it with a (sym)link, redirecting the destination
+    # for all subsequent members.
+    if ((member.islnk() or member.issym())
+            and os.path.normpath(name) == os.curdir):
+        raise OutsideDestinationError(member, target_path)
     # Limit permissions (no high bits, and go-w)
     mode = member.mode
     if mode is not None:
@@ -825,11 +831,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
         if member.islnk() or member.issym():
             if os.path.isabs(member.linkname):
                 raise AbsoluteLinkError(member)
-            # A link member that resolves to the destination directory itself
-            # would replace it with a (sym)link, redirecting the destination
-            # for all subsequent members.
-            if target_path == dest_path:
-                raise OutsideDestinationError(member, target_path)
             normalized = os.path.normpath(member.linkname)
             if normalized != member.linkname:
                 new_attrs['linkname'] = normalized
diff --git a/lib-python/3/test/test_tarfile.py b/lib-python/3/test/test_tarfile.py
index 1f189d9..2993268 100644
--- a/lib-python/3/test/test_tarfile.py
+++ b/lib-python/3/test/test_tarfile.py
@@ -4072,8 +4072,10 @@ class TestExtractionFilters(unittest.TestCase):
                 t.linkname = '.'
                 arc.tar_w.addfile(t)
 
-            with self.check_context(arc.open(), 'data'):
-                self.expect_exception(tarfile.OutsideDestinationError)
+            for filter in 'tar', 'data':
+                with self.subTest(name=name, filter=filter):
+                    with self.check_context(arc.open(), filter):
+                        self.expect_exception(tarfile.OutsideDestinationError)
 
     @symlink_test
     @os_helper.skip_unless_symlink
