Summary
A path traversal issue in Black's cache file handling allows the value of the --python-cell-magics option to be written directly into cache filenames without sanitization.
When an attacker can control this argument, they may cause Black to create or overwrite cache files in arbitrary locations on the filesystem, potentially impacting system integrity and other applications.
Affected context
This vulnerability affects environments where Black is used as a formatter and the --python-cell-magics option can be influenced by untrusted input.
It is most relevant in:
- Shared development platforms or CI/CD pipelines that invoke Black with user-provided options
- Editor/IDE integrations that derive formatter arguments from project or workspace configuration
- Automation scripts or hooks (e.g., pre-commit, Git hooks) that allow contributors to affect Black's CLI options
Exploit preconditions
- Attack surface: Command-line invocation of Black, particularly the
--python-cell-magicsoption. - Required access: Ability to control or influence the arguments passed to Black (e.g., via configuration files, environment variables, or wrapper scripts).
- Trigger point: Execution of Black with a specially crafted
--python-cell-magicsvalue, causing the cache filename computation to include attacker-controlled path elements. - Why exploitation is possible: The value of
--python-cell-magicswas used directly in constructing the cache file path without validation or normalization, enabling traversal outside the intended cache directory.
Why this happens
The core issue is the lack of input validation on a user-controllable parameter that is incorporated into filesystem paths.
By embedding the value of --python-cell-magics directly into the cache filename, Black allowed path components such as directory separators or traversal sequences to influence where cache files were written.
This maps to CWE-22: Path Traversal, where unvalidated external input is used in file and directory paths, allowing access to file system locations outside the intended scope.
Potential impact
- Creation of arbitrary files in locations writable by the Black process
- Overwriting of existing cache or temporary files used by other tools, potentially disrupting builds or tests
- Assistance in multi-step attacks where crafted files (e.g., configuration, lock, or state files) can modify the behavior of other tools or services
- Interference with CI/CD pipelines by corrupting or polluting cache directories and artifacts
General mitigation guidance
Effective mitigation centers on constraining how user-controlled values influence filesystem paths.
Common defensive patterns include:
- Normalizing and validating all inputs that may appear in filenames, rejecting path separators and traversal sequences
- Restricting cache and temporary files to a fixed, internal directory and using generated, non-user-derived identifiers for filenames
- Applying allowlists for parameters that influence paths, or mapping user options to safe, canonical tokens
- Running formatting tools in environments with least-privilege filesystem access, limiting the damage of any unintended file writes
How teams detect issues like this
Organizations can detect similar issues by:
- Using static analysis or linters to flag concatenation of user-controllable inputs into filesystem paths
- Reviewing CLI argument handling and configuration parsing logic for any direct use of options in filenames
- Implementing policy checks in CI that scan for unsafe path construction patterns (e.g., use of raw user input with OS path APIs)
- Instrumenting logging around cache or temporary file creation to highlight unexpected directories or filenames for further investigation

