How can I protect myself from a zip bomb?

JavaPythonSecurityCompressionZip

Java Problem Overview


I just read about zip bombs, i.e. zip files that contain very large amount of highly compressible data (00000000000000000...).

When opened they fill the server's disk.

How can I detect a zip file is a zip bomb before unzipping it?

UPDATE Can you tell me how is this done in Python or Java?

Java Solutions


Solution 1 - Java

Try this in Python:

import zipfile

with zipfile.ZipFile('a_file.zip') as z
    print(f'total files size={sum(e.file_size for e in z.infolist())}')

Solution 2 - Java

Zip is, erm, an "interesting" format. A robust solution is to stream the data out, and stop when you have had enough. In Java, use ZipInputStream rather than ZipFile. The latter also requires you to store the data in a temporary file, which is also not the greatest of ideas.

Solution 3 - Java

Reading over the description on Wikipedia -

Deny any compressed files that contain compressed files.
     Use ZipFile.entries() to retrieve a list of files, then ZipEntry.getName() to find the file extension.
Deny any compressed files that contain files over a set size, or the size can not be determined at startup.
     While iterating over the files use ZipEntry.getSize() to retrieve the file size.

Solution 4 - Java

Don't allow the upload process to write enough data to fill up the disk, ie solve the problem, not just one possible cause of the problem.

Solution 5 - Java

Check a zip header first :)

Solution 6 - Java

If the ZIP decompressor you use can provide the data on original and compressed size you can use that data. Otherwise start unzipping and monitor the output size - if it grows too much cut it loose.

Solution 7 - Java

Make sure you are not using your system drive for temp storage. I am not sure if a virusscanner will check it if it encounters it.

Also you can look at the information inside the zip file and retrieve a list of the content. How to do this depends on the utility used to extract the file, so you need to provide more information here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionflybywireView Question on Stackoverflow
Solution 1 - JavaNick DandoulakisView Answer on Stackoverflow
Solution 2 - JavaTom Hawtin - tacklineView Answer on Stackoverflow
Solution 3 - JavaMichael Lloyd Lee mlkView Answer on Stackoverflow
Solution 4 - JavaPete KirkhamView Answer on Stackoverflow
Solution 5 - JavaVladislav RastrusnyView Answer on Stackoverflow
Solution 6 - JavasharptoothView Answer on Stackoverflow
Solution 7 - JavaHeiko HatzfeldView Answer on Stackoverflow