Summary
The pnpm patch application pipeline trusts file paths embedded in .patch files without validating whether they stay within the intended package directory.
When a malicious contributor adds a crafted patch to a project, running pnpm install can overwrite or delete arbitrary files on the filesystem as the installing user, making code review alone insufficient to catch the issue.
Affected context
This vulnerability appears in development and CI environments that use pnpm's patch-package functionality to apply .patch files during pnpm install.
It becomes relevant when projects accept patches from contributors, store them in version control, and treat them as trusted input.
The risk is higher in repositories where reviewers do not closely inspect patch file headers, and where installs run with broad filesystem access on developer machines or CI runners.
Exploit preconditions
- Attack surface: The patch application pipeline invoked as part of
pnpm install. - Required access: Ability to submit or modify .patch files in the project (e.g., via pull requests or direct repository access).
- Trigger point: Execution of
pnpm installon a codebase that includes the malicious patch. - Why exploitation is possible: The implementation does not validate or constrain paths in the
diff --githeaders, allowing../../segments to escape the package directory and target arbitrary locations.
Why this happens
The underlying issue is a path traversal bug consistent with CWE-22: Improper Limitation of a Pathname to a Restricted Directory.
pnpm's patch application logic uses file paths extracted from .patch files as-is, assuming they remain within the package directory.
Without normalization, canonicalization, and enforcement of a strict base directory, ../../ sequences in patch headers allow attackers to navigate outside the intended scope and perform file operations on arbitrary paths.
Potential impact
- Attacker-controlled content may overwrite files outside the package directory.
- Local configuration or source files could be deleted, disrupting builds or development environments.
- Sensitive project assets (scripts, hooks, configs) can be modified to introduce further compromise.
- CI/CD environments can be destabilized or prepared for follow-on attacks via tampered files.
General mitigation guidance
Effective mitigation focuses on constraining patch file handling to a well-defined directory and treating patch paths as untrusted input.
Common patterns include:
- Normalizing and canonicalizing patch paths, then enforcing that they resolve under a single trusted base directory.
- Rejecting or ignoring any file path containing directory traversal segments or absolute paths.
- Reducing the trust level of contributed patch files and minimizing automatic execution of file modifications from third-party contributions.
- Running dependency and patch processing with least-privilege filesystem access so that unintended writes are limited in scope.
How teams detect issues like this
High-level detection approaches may include:
- Static analysis or custom linters that scan .patch files for suspicious
../../segments or absolute paths indiff --githeaders. - Code review guidelines that explicitly call out patch files as high-risk inputs and require checking path scopes.
- Repository policies that restrict who can add or modify patch files, combined with automated checks on pull requests.
- File-system activity monitoring in CI environments to flag unexpected writes outside designated project directories during
pnpm install.

