Skip to content

bpo-39039: tarfile raises descriptive exception from zlib.error #27766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,15 @@ def next(self):
raise ReadError(str(e)) from None
except SubsequentHeaderError as e:
raise ReadError(str(e)) from None
except Exception as e:
try:
import zlib
if isinstance(e, zlib.error):
raise ReadError(f'zlib error: {e}') from None
else:
raise e
except ImportError:
raise e
Comment on lines +2359 to +2360
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Observation: this particular raise will be pretty noisy since it will be a chained failure. But this is good, it's an unexpected issue for us and we want all the context we can get so that somebody might report a good bug to us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I figured. I could even do raise Exception('please submit a bug report!') from e but maybe that is too much.

break

if tarinfo is not None:
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import gzip
except ImportError:
gzip = None
try:
import zlib
except ImportError:
zlib = None
try:
import bz2
except ImportError:
Expand Down Expand Up @@ -687,6 +691,16 @@ def test_parallel_iteration(self):
self.assertEqual(m1.offset, m2.offset)
self.assertEqual(m1.get_info(), m2.get_info())

@unittest.skipIf(zlib is None, "requires zlib")
def test_zlib_error_does_not_leak(self):
# bpo-39039: tarfile.open allowed zlib exceptions to bubble up when
# parsing certain types of invalid data
with unittest.mock.patch("tarfile.TarInfo.fromtarfile") as mock:
mock.side_effect = zlib.error
with self.assertRaises(tarfile.ReadError):
tarfile.open(self.tarname)


class MiscReadTest(MiscReadTestBase, unittest.TestCase):
test_fail_comp = None

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tarfile.open raises :exc:`~tarfile.ReadError` when a zlib error occurs
during file extraction.