]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-45507: EOFErrors should be thrown for truncated gzip members (GH-29029)
authorRuben Vorderman <r.h.p.vorderman@lumc.nl>
Fri, 19 Nov 2021 18:07:05 +0000 (19:07 +0100)
committerGitHub <noreply@github.com>
Fri, 19 Nov 2021 18:07:05 +0000 (19:07 +0100)
Lib/gzip.py
Lib/test/test_gzip.py
Misc/NEWS.d/next/Library/2021-10-18-14-00-01.bpo-45507.lDotNV.rst [new file with mode: 0644]

index ac1781042b26405e85b4935e013c4d8160a29e01..6773ea3eef0971df7aee1277346a9ebadaa8b5e3 100644 (file)
@@ -603,6 +603,9 @@ def decompress(data):
         do = zlib.decompressobj(wbits=-zlib.MAX_WBITS)
         # Read all the data except the header
         decompressed = do.decompress(data[fp.tell():])
+        if not do.eof or len(do.unused_data) < 8:
+            raise EOFError("Compressed file ended before the end-of-stream "
+                           "marker was reached")
         crc, length = struct.unpack("<II", do.unused_data[:8])
         if crc != zlib.crc32(decompressed):
             raise BadGzipFile("CRC check failed")
index f86e767ac0e59cbe7e04088a8e02730bc02666fb..aa66d2f07f508cbb82437b97fa4b7648fb7ecdc6 100644 (file)
@@ -562,6 +562,14 @@ class TestGzip(BaseTest):
             datac = gzip.compress(data)
             self.assertEqual(gzip.decompress(datac), data)
 
+    def test_decompress_truncated_trailer(self):
+        compressed_data = gzip.compress(data1)
+        self.assertRaises(EOFError, gzip.decompress, compressed_data[:-4])
+
+    def test_decompress_missing_trailer(self):
+        compressed_data = gzip.compress(data1)
+        self.assertRaises(EOFError, gzip.decompress, compressed_data[:-8])
+
     def test_read_truncated(self):
         data = data1*50
         # Drop the CRC (4 bytes) and file size (4 bytes).
diff --git a/Misc/NEWS.d/next/Library/2021-10-18-14-00-01.bpo-45507.lDotNV.rst b/Misc/NEWS.d/next/Library/2021-10-18-14-00-01.bpo-45507.lDotNV.rst
new file mode 100644 (file)
index 0000000..a69ccda
--- /dev/null
@@ -0,0 +1 @@
+Add tests for truncated/missing trailers in gzip.decompress implementation.