Summary
On Windows, Python's shutil.unpack_archive() may extract ZIP entries with absolute drive paths (e.g., C:\...) outside the intended target directory, unlike its behavior on other platforms.
When applications unpack untrusted ZIP archives on Windows using this API, an attacker could place files in arbitrary locations on the filesystem, potentially overwriting or planting malicious files.
Affected context
This vulnerability occurs in Windows environments where Python applications use shutil.unpack_archive() to process ZIP files, especially when those archives come from external or partially trusted sources.
The issue is triggered when ZIP entries contain absolute Windows paths including a drive letter (such as C:\path\to\file), allowing extracted files to bypass the caller's specified extraction directory.
The risk is higher in services or tools that automatically unpack user-supplied archives without additional path validation.
Exploit preconditions
- Attack surface: Any Python-based functionality on Windows that calls
shutil.unpack_archive()on ZIP files. - Required access: Ability to supply or influence the contents of a ZIP archive processed by the application (e.g., file upload, plugin package, backup restore).
- Trigger point: The moment the application invokes
shutil.unpack_archive()to extract the attacker-controlled ZIP into a target directory. - Why exploitation is possible: On Windows,
shutil.unpack_archive()does not sufficiently normalize or confine absolute drive-based paths contained in ZIP entries, allowing extraction to paths outside the designated output directory.
Why this happens
The underlying issue is improper neutralization of file paths during extraction, corresponding to CWE-22: Improper Limitation of a Pathname to a Restricted Directory.
On Windows, shutil.unpack_archive() allows ZIP entries with absolute drive paths to be used as-is, instead of rewriting or rejecting them to ensure they remain within the caller-specified extraction root.
Because paths are not consistently sanitized or restricted, archive authors can embed absolute paths that escape the trusted directory boundary, violating the expectation that extraction is confined to the chosen target directory.
Potential impact
- Attacker-controlled files may be written to arbitrary locations on the Windows filesystem accessible by the running process.
- Existing files could be overwritten, potentially altering configuration, logs, or application resources.
- Malicious executables, scripts, or DLLs could be planted in locations that may later be executed by other components.
- Application integrity and data integrity could be compromised through unauthorized file modification.
- In certain deployment setups, this could contribute to privilege escalation or lateral movement when combined with other weaknesses.
General mitigation guidance
Effective mitigation focuses on enforcing strict directory confinement for extracted archives and treating all archive paths as untrusted input:
- Normalize and validate archive member paths before writing to disk, rejecting entries with absolute drive paths, UNC paths, or segments that escape the intended root directory.
- Implement a safe extraction wrapper that joins each member's path to the target directory, resolves it, and confirms it still lies under the intended root before creating files.
- Treat archive uploads as untrusted data: limit where extracted files can be written, and avoid extracting with elevated privileges or sensitive working directories.
- Use defense-in-depth by running archive processing under least-privileged accounts and restricting filesystem permissions for directories that should never be modified by archive extraction.
- Consider separate sandboxed or temporary locations for unpacking archives, followed by a controlled copy/move of only the validated files to their final destinations.
How teams detect issues like this
Detection typically combines code-level review with behavioral monitoring:
- Static analysis / code review:
- Search for uses of
shutil.unpack_archive()and other archive APIs, especially in code paths handling user-supplied ZIP files on Windows. - Flag extraction code that does not normalize or validate member paths, or that directly trusts paths from archive entries.
- Review for absence of checks against absolute paths, drive letters, or traversal segments (
..), and lack of canonicalization (e.g., usingos.path.realpathor similar). - Policy and tooling:
- Introduce secure coding guidelines for archive handling in the codebase and enforce them via pull request templates and automated linters where possible.
- Use security scanning tools or custom scripts to identify high-risk file extraction patterns across repositories.
- Runtime monitoring:
- Log extraction operations and unexpected writes outside designated directories, especially during archive handling workflows.

