Skip to main content

Hardened ZIP extraction for Python - secure by default.

Project description

SafeZip Logo

Hardened ZIP extraction for Python - secure by default.

PyPI Version Supported Python versions Build Status Documentation Status llms.txt - documentation for LLMs MIT Coverage

safezip is a zero-dependency, production-grade wrapper around Python’s zipfile module that defends against the most common ZIP-based attacks: ZipSlip path traversal, ZIP bombs, and malformed/crafted archives.

Features

  • ZipSlip protection - relative traversal, absolute paths, Windows UNC paths, Unicode lookalike attacks, and null bytes in filenames are all blocked.

  • ZIP bomb protection - per-member and cumulative decompression ratio limits abort extraction before runaway decompression can exhaust disk or memory.

  • File size limits - per-member and total extraction size limits enforced at stream time (not based on untrusted header values).

  • ZIP64 consistency checks - crafted archives with inconsistent ZIP64 extra fields are rejected before decompression begins.

  • Symlink policy - configurable: REJECT (default), IGNORE, or RESOLVE_INTERNAL (full chain verification).

  • Atomic writes - every member is written to a temporary file first; the destination is only created after all checks pass. No partial files are left on disk after a security abort.

  • Secure by default - all limits are active without any configuration.

  • Zero dependencies - standard library only.

  • Environment variable overrides - all numeric limits can be set via SAFEZIP_* environment variables for containerised deployments.

Prerequisites

Python 3.10 or later. No additional packages required.

Installation

With uv:

uv pip install safezip

Or with pip:

pip install safezip

Quick start

Drop-in replacement for the common zipfile extraction pattern:

from safezip import safe_extract

safe_extract("path/to/file.zip", "/var/files/extracted/")

Or use the SafeZipFile context manager for more control:

from safezip import SafeZipFile

with SafeZipFile("path/to/file.zip") as zf:
    print(zf.namelist())
    zf.extractall("/var/files/extracted/")

Custom limits

from safezip import SafeZipFile

with SafeZipFile(
    "path/to/file.zip",
    max_file_size=100 * 1024 * 1024,   # 100 MiB per member
    max_total_size=500 * 1024 * 1024,  # 500 MiB total
    max_files=1_000,
    max_per_member_ratio=50.0,
    max_total_ratio=50.0,
) as zf:
    zf.extractall("/var/files/extracted/")

Security event monitoring

from safezip import SafeZipFile, SecurityEvent

def my_monitor(event: SecurityEvent) -> None:
    print(f"[safezip] {event.event_type} archive={event.archive_hash}")

with SafeZipFile("path/to/file.zip", on_security_event=my_monitor) as zf:
    zf.extractall("/var/files/extracted/")

Environment variable overrides

All numeric limits can be overridden without changing code:

export SAFEZIP_MAX_FILE_SIZE=104857600    # 100 MiB
export SAFEZIP_MAX_TOTAL_SIZE=524288000   # 500 MiB
export SAFEZIP_MAX_FILES=1000
export SAFEZIP_MAX_PER_MEMBER_RATIO=50
export SAFEZIP_MAX_TOTAL_RATIO=50

Default limits

Parameter

Default

max_file_size

1 GiB

max_total_size

5 GiB

max_files

10 000

max_per_member_ratio

200

max_total_ratio

200

max_nesting_depth

3

symlink_policy

REJECT

Testing

All tests run inside Docker to prevent accidental pollution of the host system:

make test

To test a specific Python version:

make test-env ENV=py312

Writing documentation

Keep the following hierarchy:

=====
title
=====

header
======

sub-header
----------

sub-sub-header
~~~~~~~~~~~~~~

sub-sub-sub-header
^^^^^^^^^^^^^^^^^^

sub-sub-sub-sub-header
++++++++++++++++++++++

sub-sub-sub-sub-sub-header
**************************

License

MIT

Support

For security issues contact me at the e-mail given in the Author section.

For overall issues, go to GitHub.

Author

Artur Barseghyan <artur.barseghyan@gmail.com>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safezip-0.1.1.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safezip-0.1.1-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

Details for the file safezip-0.1.1.tar.gz.

File metadata

  • Download URL: safezip-0.1.1.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for safezip-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1cb0cdd315ab7ed01f3abb88d5ed40a2442d2f6f9c49167f02bf76337fa60c5f
MD5 af5c73f1d35d66708786398f48fb5f77
BLAKE2b-256 f6ac35f59fb2ef350b99fa3d4cb8e8bbbf747e736eb9cc88496a94d6fac75207

See more details on using hashes here.

File details

Details for the file safezip-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: safezip-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 26.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for safezip-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e166ce65d131f16e13886bf5781c3c5ae3a06aa3250f6f5d4eca8e6a37f53c7
MD5 8db283d9f14ed0e5e7940a704f56f35d
BLAKE2b-256 d4e132f3bc674c54200ef41802a1489d47e865e476a43710d7ddb6740407997f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page