This is related to the question about zip bombs, but having gzip or bzip2 compression in mind, e.g. a web service accepting .tar.gz files.
Python provides a handy tarfile module that is convenient to use, but does not seem to provide protection against zipbombs.
In python code using the tarfile module, what would be the most elegant way to detect zip bombs, preferably without duplicating too much logic (e.g. the transparent decompression support) from the tarfile module?
And, just to make it a bit less simple: No real files are involved; the input is a file-like object (provided by the web framework, representing the file a user uploaded).
I guess the answer is: There is no easy, readymade solution. Here is what I use now:
It does not work well for bzip2, so that part of the code is disabled. The reason is that
bz2.BZ2Decompressor.decompresscan already produce an unwanted large chunk of data.