Python shutil and Safe Archive Extraction Prompt
Create and extract tar/zip archives in Python with shutil, tarfile, and zipfile — defending against path-traversal (zip slip), symlink escapes, and decompression bombs while preserving permissions where intended.
- Target user
- Engineers packaging or unpacking archives in build, backup, and deploy scripts
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Python engineer who has fixed real "zip slip" and tar-extraction CVEs in deployment tooling. I will provide: - The code that creates or extracts archives (`shutil.make_archive`, `tarfile`, `zipfile`) - Whether archives come from a trusted build step or untrusted/external sources - The Python version (so we know if `tarfile`'s `filter='data'` from 3.12 is available) Your job: 1. **Classify trust** — make explicit whether the archive is attacker-influenced. If untrusted, every extraction is a security boundary and the defaults are NOT safe. 2. **Extract defensively**: - For tar: never use bare `extractall()`. On Python 3.12+ pass `filter='data'`; on older versions, validate each member — reject absolute paths, `..` components, symlinks/hardlinks pointing outside the target, and device/special files — and confirm the resolved destination stays under the extraction root. - For zip: guard against zip slip by resolving each member path against the target dir and rejecting escapes; be aware `extractall` does not preserve Unix permissions. 3. **Defuse decompression bombs** — cap total uncompressed bytes and member count, stream-extract rather than loading whole files into memory, and abort when the ratio or size limit is exceeded. 4. **Create archives cleanly** — use `shutil.make_archive` or `tarfile`/`zipfile` with deterministic ordering, controlled `arcname` (strip leading paths), and intentional handling of symlinks and permissions for reproducible output. 5. **Preserve or drop metadata deliberately** — state when permissions/ownership/mtime should be kept (backups) vs normalized (distribution), and how. 6. **Test the attacks** — pytest cases that attempt `../escape`, an absolute path member, a symlink escape, and an oversized member, asserting each is rejected. Output as: (a) a `safe_extract(archive, dest)` function covering tar and zip, (b) a `make_archive` helper with deterministic options, (c) the adversarial pytest suite. Bias toward: failing closed on any member that resolves outside the target, explicit size/count caps, and never trusting archive metadata from external sources.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python Safe Subprocess Wrapper Prompt
Build a hardened Python wrapper around subprocess that runs external commands safely — no shell=True, list args, timeouts, captured output, non-zero handling, and streaming logs — replacing fragile os.system and shell-string calls.
-
Bash GPG Secrets Encrypt/Decrypt Workflow Prompt
Build a Bash workflow that encrypts and decrypts secret files with GPG for git-committable storage, with strict permissions, no plaintext left on disk, and safe handling in automation
-
Bash printf Safe String Formatting and %q Quoting Prompt
Replace fragile echo calls with printf for locale-stable, injection-resistant string building and %q shell-quoting
-
Secure umask & File-Write Hardening for Secret-Handling Scripts Prompt
Harden a Bash automation script that writes tokens, keys, or credentials so every file it creates is restrictive by default, written atomically, never leaks via temp files or world-readable defaults, and never echoes secrets to logs or process lists.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.