3

My question is simple: how do I make sure (or prevent) a user from uploading an archive that upon extraction fills the entire disc space (a so-called ZipBomb)? I am using PHP.

Tower
  • 98,741
  • 129
  • 357
  • 507

1 Answers1

2

Before extracting your archive, use the PHP Zip library functions to ensure that, when extracted, the contents fall within a total size limit.

For example:

$zip = zip_open('uploaded.zip');
$file = zip_read($zip);
$totalsize = 0;

while ($file) {
    $totalsize += zip_entry_filesize($file);
    $file = zip_read($zip); // read next file
}

zip_close($zip);

if ($totalsize > SIZE_LIMIT) {
    // not allowed!
}
Jon
  • 428,835
  • 81
  • 738
  • 806
  • 1
    [The comment below the manual entry for `zip_entry_filesize` mentions ZipBombs as well](http://de2.php.net/manual/en/function.zip-entry-filesize.php#98949) – Gordon Nov 28 '10 at 12:47