Summary
A path traversal flaw in Calibre's extract_pictures function allows ZIP entries to write files outside the intended Pictures directory, anywhere the current user has write permissions.
On Windows, an attacker can leverage this to place a malicious file in the Startup folder, which is executed on the next login, effectively leading to remote code execution under the user's context.
Affected context
This vulnerability affects Calibre installations that handle archives (or similar data) using the extract_pictures functionality.
The issue appears when file paths from an archive are trusted if they merely start with "Pictures" and are then written using manual file handling instead of the hardened ZipFile.extractall() path sanitation routine.
The vulnerability is most impactful on systems where user-writable locations like the Windows Startup folder or other sensitive directories are reachable under the user's permissions.
Exploit preconditions
- Attack surface: Calibre's picture extraction mechanism that processes paths from ZIP (or ZIP-like) archives.
- Required access: Ability to supply or influence the archive content or metadata processed by
extract_pictures(e.g., a crafted ebook or archive file). - Trigger point: User opens, imports, or otherwise processes a crafted file that causes
extract_picturesto run. - Why exploitation is possible: The function only checks that the path starts with
"Pictures"and does not normalize or reject..path components, allowing attacker-controlled paths to escape the intended directory and target arbitrary locations writable by the user.
Why this happens
The underlying bug is an input validation failure where file paths extracted from an archive are insufficiently sanitized before being used for filesystem writes.
Instead of relying on Calibre's hardened _get_targetpath() logic in utils/zipfile.py (which strips or rejects .. sequences), extract_pictures manually reads archive entries and writes them using open() after a simple startswith('Pictures') check.
This maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), where adversary-controlled path elements can break out of the intended directory sandbox and overwrite or create files in arbitrary user-writable locations.
Potential impact
- Creation or overwrite of files in arbitrary directories where the current user has write permissions.
- On Windows, placing executables, scripts, or shortcuts in the user's Startup folder that execute automatically on next login.
- Persistence of attacker code or tooling under the user account.
- Potential tampering with application configuration, user documents, or other local data.
- Indirect escalation to remote code execution scenarios when malicious files are later executed by the system or other applications.
General mitigation guidance
Effective mitigation typically focuses on robust path normalization and confinement:
- Ensure all archive extraction code paths use a centralized, hardened routine for joining and validating paths (e.g., a function that normalizes paths, rejects
..segments and absolute paths, and enforces a fixed base directory). - Avoid manual file writes based on archive entry names unless the paths are canonicalized and explicitly verified to remain within a designated directory.
- Treat all archive metadata (including filenames and paths) as untrusted input; apply strict validation and disallow path constructs that can escape the intended extraction root.
- Run client applications like Calibre with least privilege and avoid granting them access to sensitive directories beyond what is required for normal operation.
- Where possible, isolate processing of untrusted content using separate profiles, accounts, or sandboxing techniques to limit the blast radius of any file-write issues.
How teams detect issues like this
Detection efforts can combine code-level and behavioral approaches:
- Static analysis: Define rules or patterns to flag any use of archive or user-supplied filenames being concatenated directly into filesystem paths, especially when
.., absolute paths, or environment-dependent directories may be reachable. - Code review: Look for custom extraction logic that bypasses standard, hardened helpers (e.g., calls to
read()+open()instead of centralizedextract/extractall()wrappers) and verify that all such paths are normalized and checked against a fixed base directory. - Policy enforcement: Introduce development guidelines requiring all path operations on untrusted filenames to go through utility functions that perform canonicalization and directory containment checks.
- Runtime monitoring (optional): On high-assurance systems, monitor applications for unexpected writes into sensitive directories (Startup folders, configuration directories, or other auto-execution locations) when processing untrusted content.

