Summary
A path traversal vulnerability in pnpm allows an attacker-controlled package name from a malicious lockfile to influence where package contents are written during installation. If a user installs from the crafted lockfile, files may be written outside the intended node_modules directory, and permitted lifecycle scripts could execute with the user's privileges.
Affected context
The issue occurs in pnpm installations that process attacker-controlled pnpm-lock.yaml package keys and use the parsed package name in filesystem paths. Risk is higher when a project permits lifecycle scripts through broad build-script policies or matching build allowlists, because an escaped package may both write files outside the dependency directory and run code during installation.
Exploit preconditions
- Attack surface: Dependency installation through pnpm
- Required access: Ability to provide or influence a project's lockfile or dependency input
- Trigger point: Lockfile-to-dependency-graph processing and package installation
- Why exploitation is possible: The parsed package name is used in path construction and package import operations without sufficient path validation, allowing filesystem locations to escape the intended dependency directory
- Additional condition for code execution: Lifecycle scripts must be permitted by the project's build-script policy
Why this happens
The vulnerability results from treating a package name extracted from an untrusted lockfile as a trusted filesystem path component. Without canonicalization, containment checks, and validation against an expected package-name format, path construction can resolve outside the intended node_modules location. This maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory, commonly known as path traversal. If installation lifecycle scripts are also enabled, the unintended file placement may create an additional code-execution path.
Potential impact
- Malicious package contents may be written outside the intended dependency directory
- Project files or other user-accessible files could be overwritten
- Installation-time code may execute with the installing user's privileges
- Build environments, developer workstations, or CI runners could be compromised
- Attackers may modify project behavior or persistence-related files
General mitigation guidance
Mitigation generally focuses on treating lockfiles and package metadata as untrusted input and enforcing filesystem containment.
Common defensive patterns include:
- Validate package names against the package-manager's expected syntax before using them in paths
- Resolve target paths canonically and verify they remain within the intended dependency directory
- Reject absolute paths, traversal segments, and unexpected separators
- Keep package installation and lifecycle scripts separated by default
- Require narrowly scoped, explicit approval for trusted build scripts
- Run dependency installation with least-privilege accounts and isolated workspaces
- Use read-only or disposable environments for untrusted dependency installation where practical
- Treat lockfiles from external sources as reviewed supply-chain artifacts
How teams detect issues like this
Organizations can detect related risks through:
- Static analysis rules that flag unvalidated metadata flowing into path construction APIs such as
path.join - Code review checks for package names or archive entries used as filesystem paths
- Tests that verify canonicalized installation paths remain inside the intended directory
- Dependency-installation policies that flag broad lifecycle-script permissions
- File-integrity monitoring for unexpected writes outside dependency directories
- CI checks that reject malformed or untrusted lockfile changes
Automated security review tools may help identify tainted-data flows from lockfiles into filesystem and process-execution operations.

