Skip to content

Commit 06f2db7

Browse files
committed
Add tests to show that CVE-2024-6232 is okay
1 parent 6ad96ea commit 06f2db7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Lib/test/test_tarfile.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,43 @@ def test_pax_number_fields(self):
785785
finally:
786786
tar.close()
787787

788+
def test_pax_header_bad_formats(self):
789+
# The fields from the pax header have priority over the
790+
# TarInfo.
791+
pax_header_replacements = (
792+
b" foo=bar\n",
793+
b"0 \n",
794+
b"1 \n",
795+
b"2 \n",
796+
b"3 =\n",
797+
b"4 =a\n",
798+
b"1000000 foo=bar\n",
799+
b"0 foo=bar\n",
800+
b"-12 foo=bar\n",
801+
b"000000000000000000000000036 foo=bar\n",
802+
)
803+
pax_headers = {"foo": "bar"}
804+
for replacement in pax_header_replacements:
805+
tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT,
806+
encoding="iso8859-1")
807+
try:
808+
t = tarfile.TarInfo()
809+
t.name = "pax" # non-ASCII
810+
t.uid = 1
811+
t.pax_headers = pax_headers
812+
tar.addfile(t)
813+
finally:
814+
tar.close()
815+
with open(tmpname, "rb") as f:
816+
data = f.read()
817+
self.assertIn(b"11 foo=bar\n", data)
818+
data = data.replace(b"11 foo=bar\n", replacement)
819+
with open(tmpname, "wb") as f:
820+
f.truncate()
821+
f.write(data)
822+
with self.assertRaisesRegexp(tarfile.ReadError, r"file could not be opened successfully"):
823+
tarfile.open(tmpname, encoding="iso8859-1")
824+
788825

789826
class WriteTestBase(unittest.TestCase):
790827
# Put all write tests in here that are supposed to be tested

0 commit comments

Comments
 (0)