Zip bomb attack High

Expanding input archive files without any validation could make your code vulnerable to zip bomb attacks, which could potentially cause denial of service (DoS). We recommend that you sanitize input archive files before extracting them.

Detector ID
python/zip-bomb-attack@v1.0
Category
Common Weakness Enumeration (CWE) external icon
Tags
-

Noncompliant example

1@app.route('/someUrl')
2def zip_bomb_attack_noncompliant():
3    file = request.files['file']
4    filename = file.filename
5    file.save(filename)
6    tfile = tarfile.open(filename)
7    # Noncompliant: Untrusted archive file extracted without any validation.
8    tfile.extractall('./tmp/')
9    tfile.close()

Compliant example

1@app.route('/someUrl')
2def zip_bomb_attack_compliant():
3    file = request.files['file']
4    filename = file.filename
5    file.save(filename)
6    tfile = tarfile.open(filename)
7    threshold_entries = 100  # some threshold value
8    # Compliant: Untrusted archive file is validated before extraction.
9    if len(tfile.getmembers()) < threshold_entries:
10        tfile.extractall('./tmp/')
11    tfile.close()