summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/tarfile.py2
-rw-r--r--Lib/test/recursion.tarbin0 -> 516 bytes
-rw-r--r--Lib/test/test_tarfile.py7
3 files changed, 9 insertions, 0 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 999a99b978..8015fae7a6 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1221,6 +1221,8 @@ class TarInfo(object):
length, keyword = match.groups()
length = int(length)
+ if length == 0:
+ raise InvalidHeaderError("invalid header")
value = buf[match.end(2) + 1:match.start(1) + length - 1]
# Normally, we could just use "utf-8" as the encoding and "strict"
diff --git a/Lib/test/recursion.tar b/Lib/test/recursion.tar
new file mode 100644
index 0000000000..b823725196
--- /dev/null
+++ b/Lib/test/recursion.tar
Binary files differ
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 1efb841bb2..2438fd3abf 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -394,6 +394,13 @@ class CommonReadTest(ReadTest):
with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
tar.extractfile(t).read()
+ def test_length_zero_header(self):
+ # bpo-39017 (CVE-2019-20907): reading a zero-length header should fail
+ # with an exception
+ with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
+ with tarfile.open(support.findfile('recursion.tar')) as tar:
+ pass
+
class MiscReadTestBase(CommonReadTest):
def requires_name_attribute(self):
pass