Am I affected?
Description
Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') vulnerability in hexpm hex allows a malicious package publisher to delete a directory outside the project's deps directory and extract package files into it when a developer fetches dependencies.
A package release's requirements carry an app label that names the dependency's OTP application. Hex takes this label from the repository's signed registry and uses it as the dependency's application name without validating it (Hex.RemoteConverger.deps/2, Hex.Mix.to_lock/1). Mix derives the dependency's checkout directory as deps/<app>, and Hex.SCM.update/1 runs File.rm_rf!/1 on that directory and unpacks the package into it. hex.pm only checks the label's length, so a publisher can set it to a value such as ../../target. When a project depends on such a release directly or transitively, mix deps.get or mix deps.update deletes the directory the label resolves to and writes the labelled package's files there. This happens before any dependency code is compiled.
This issue affects hex: from 0.6.0 before 2.5.2.
Technical analysis
- hex.pm stores each requirement's
appfield with only a length check (255 codepoints) and copies it into the signed package registry resource when it differs from the dependency's package name. Hex.RemoteConverger.deps/2returns the dependency's children withString.to_atom/1of that label as the application name, andHex.Mix.to_lock/1writes it as themix.lockkey.- Mix computes the dependency destination as
Path.join(Mix.Project.deps_path(), Atom.to_string(app))without validating the name. Hex.SCM.update/1callsFile.rm_rf!(dest)and thenHex.Tar.unpack!(path, dest), so a label containing..segments deletes and repopulates a directory outsidedeps.- Mix doesn't evaluate a Hex dependency's
mix.exsduringmix deps.get, so this is reachable before any package code runs.
Proof of concept
- Publish package
beta. - Publish package
alphawith a requirement onbetawhoseappis../../escape_marker. - In a project that depends on
alpha, create../escape_marker/canary.txtnext to the project directory. - Run
mix deps.get. Hex prints* Updating ../../escape_marker (Hex package), deletescanary.txtand writesbeta's files into../escape_marker.
Weaknesses & attack patterns
Weakness
CWE-22
·
Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
in catalog →
MITRE ↗
Attack patterns
CAPEC-126
·
Path Traversal
MITRE ↗
A publisher of any package in a project's dependency tree can make mix deps.get delete a directory that the developer's account can write and that .. segments relative to deps reach, such as the project directory or its parent, and fill it with the labelled package's files. Writing attacker-chosen files into a directory whose contents are later run or loaded can lead to code execution without the developer compiling the dependency. Developers who fetch dependencies to review them before compiling, CI jobs and dependency update bots that only run mix deps.get or mix deps.update are affected.
Affected — Hex Mix Integration Repository ↗
modules · source files · routines
Affected — GitHub / hexpm/hex Repository ↗
modules · source files · routines
Workarounds
Before running mix deps.get on a mix.lock from an untrusted change, check that every key in mix.lock is a plain application name without /, \ or ... There's no workaround for new dependency resolution with mix deps.update or mix deps.get without a lock entry.
Configurations
Affects projects whose dependency tree, directly or transitively, contains a package release whose registry metadata carries a crafted dependency app label, when dependencies are fetched with mix deps.get or mix deps.update. Packages on hex.pm are affected while hex.pm accepts such labels; packages on self-hosted repositories are affected when the repository operator signs such metadata.
Solutions
Upgrade Hex to 2.5.2 or later, for example with mix local.hex. Hex then skips package releases that give a dependency an application name other than a letter followed by letters, digits, _ and -, and warns about them. Resolution fails if no other release of the package fits the requirements.
References
Credits
CVSS breakdown
CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:A/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N