Summary
A path traversal vulnerability in PHP's Composer allows a maliciously crafted package from an untrusted repository to write attacker-controlled files outside the vendor directory and project root during install or update.
If developers rely on untrusted repositories and Composer fails to correctly validate invalid package names, this flaw can be abused to plant arbitrary files on the filesystem, enabling further compromise.
Affected context
This vulnerability occurs in environments where Composer is used to install or update dependencies from repositories other than Packagist.org or Private Packagist.
It particularly affects workflows that incorporate custom or third-party repositories whose package metadata and naming conventions are not tightly controlled.
Under these conditions, a crafted package with an invalid or malicious package name can cause Composer to place files outside the intended vendor directory or even outside the project path, depending on filesystem layout and permissions.
Exploit preconditions
- Attack surface: Composer dependency installation and update process (CLI usage in developer machines, CI systems, or build servers).
- Required access: Ability to publish or control a package on an untrusted repository consumed by Composer, or to influence repository configuration in a project.
- Trigger point: Running
composer installorcomposer updatewhen the malicious package is part of the resolved dependency graph. - Why exploitation is possible: Composer does not correctly validate or sanitize certain invalid package names before using them in dependency-resolution and path computation, allowing traversal sequences or unexpected paths to be written to the filesystem.
Why this happens
The underlying issue maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory and is related to unsafe use of path components derived from untrusted package metadata.
Composer uses package names and associated data to construct filesystem paths for dependency installation. When invalid or specially crafted names are not rigorously validated, normalized, and constrained to a known safe base directory, they can influence where files are written.
Abstractly, this represents an insecure pattern where input that should be purely descriptive (package name) is implicitly treated as part of a path, without enforcing a strict canonicalization and containment check against a trusted root.
Potential impact
- Attacker-controlled files may be written outside the
vendordirectory. - Files can be created or overwritten elsewhere in the project structure.
- Critical configuration or script files could be modified, enabling code execution.
- Build or deployment artifacts may be poisoned, leading to downstream compromise.
- Local developer environments or CI agents can be destabilized or backdoored.
General mitigation guidance
Effective mitigation focuses on constraining where Composer can write files and reducing trust in external package metadata.
Recommended defensive patterns include:
- Enforcing strict validation and normalization of all path components derived from package metadata, ensuring they cannot escape the intended base directory.
- Applying canonicalization and "stay inside base directory" checks before writing any files, regardless of repository source.
- Treating non-Packagist repositories as untrusted by default, with explicit approval and governance over what packages they can serve.
- Running dependency installation under accounts with minimal filesystem privileges so that, even if a write escapes the project directory, its impact is limited.
- Using layered controls (such as filesystem permissions, sandboxing, or containerization) to restrict Composer's write capabilities to designated directories.
How teams detect issues like this
Detection commonly involves both code-centric and operational techniques:
- Static analysis of dependency management code paths to flag any use of untrusted metadata for path construction without containment checks.
- Security-focused code reviews of Composer-related scripts, custom installers, and repository configuration to identify unvalidated or abnormal package naming assumptions.
- Policy-based enforcement in CI/CD pipelines requiring dependencies to originate from vetted repositories and scanning for unexpected path traversal sequences in package metadata.
- Runtime monitoring or audit logging of Composer operations to detect writes outside expected directories (e.g., vendor or project root), which may indicate exploitation attempts.
- Periodic inspection of build and deployment environments for anomalous files or directories created during dependency installation.

