CVE-2022-21668: Pipenv Pipenv's Requirements.txt Parsing Allows Malicious Vulnerability
HIGHCVE-2022-21668 is a HIGH (CVSS 8.0) pipenv's requirements.txt parsing allows malicious vulnerability (CWE-20: Improper Input Validation) affecting Pipenv libraries in the PyPI ecosystem. The issue involves pipenv's requirements.txt parsing allows malicious index url in comments. Remediation is available by upgrading affected packages to version 2022.1.8.
CVE-2022-21668 at a Glance
Security Impact
CVSS: 8.0
The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:C/C:H/I:H/A:H).
How to Fix CVE-2022-21668
To remediate this vulnerability, upgrade all affected Pipenv dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
pip install pipenv>=2022.1.8
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
pip show pipenv
How to Check if You Are Affected
To determine whether your Python environment is affected by CVE-2022-21668, inspect your installed packages using pip:
pip show pipenv
Analyze the terminal output to locate any version of Pipenv in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 2022.1.8), your application is affected.
Note: The pip show pipenv command checks currently installed packages in your active Python environment.
Technical Details
Vulnerability Mechanism & Technical Analysis
This vulnerability is classified under CWE-20: Improper Input Validation, where the product receives input or data, but it does not validate or incorrectly validates that the input has the properties that are required to process the data safely and correctly. In Pipenv, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.
Source Advisory Statement
CVE-2022-21668 Affected Pipenv Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
pipenv
|
PyPI |
≥ 2018.10.9
< 2022.1.8
|
2022.1.8
|
References
Frequently Asked Questions
What is CVE-2022-21668?
Which Pipenv versions are affected?
What version fixes CVE-2022-21668?
Is pipenv affected?
What is the CVSS score of CVE-2022-21668?
What CWE is associated with CVE-2022-21668?
How can I check PyPI dependencies for CVE-2022-21668?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-qc9x-gjcv-465w",
"modified" : "2024-10-11T21:22:02Z",
"published" : "2022-01-12T22:29:41Z",
"aliases" : [ "CVE-2022-21668" ],
"summary" : "Pipenv's requirements.txt parsing allows malicious index url in comments",
"details" : "## Issue Summary\nDue to a flaw in pipenv's parsing of requirements files, an attacker can insert a specially crafted string inside a comment anywhere within a requirements.txt file, which will cause victims who use pipenv to install the requirements file (e.g. with \"`pipenv install -r requirements.txt`\") to download dependencies from a package index server controlled by the attacker. By embedding malicious code in packages served from their malicious index server, the attacker can trigger arbitrary remote code execution (RCE) on the victims' systems.\n\n### Impact\nThe impact of successful exploitation is **severe/critical**.\n\nIf an attacker is able to hide a malicious `--index-url` option in a requirements file that a victim installs with pipenv, the attacker can embed arbitrary malicious code in packages served from their malicious index server that will be executed on the victim's host during installation (remote code execution/RCE). Exploitation using this technique would be relatively simple to achieve for an attacker with basic knowledge of Python, as the attacker can simply build a source distribution for any of the packages specified in the requirements file, and embed arbitrary malicious code in the setup.py file. When pip installs from a source distribution, any code in the setup.py is executed by the install process.\n\nBasic attacks might use the initial RCE triggered when a victim installs the attacker's malicious package to steal credentials from the victim's host, leach the host's resources to mine cryptocurrency, or install exploit kits or other malware. More sophisticated attackers may use more advanced techniques to persist access to the victim's host, hide or remove evidence of their attack by deleting references to the malicious index server in the Pipfile and Pipfile.lock generated by pipenv or other potential indicators of compromise. Highly sophisticated attackers could attempt to pivot to additional targets from the initial compromised host, and might leverage any exposed credentials in the compromised host environment or implicit authorization granted to the host to gain privileged access to other systems or resources, such as source repositories or package registries.\n\n### Likelihood\nThe overall likelihood of exploitation is **low to moderate** depending on a range of factors.\n\nThe primary hurdle to successful exploitation of this vulnerability depends on an attacker's ability to surreptitiously insert a specially crafted string into a requirements.txt file which will be installed by a victim (or victims). Unfortunately, because the attacker can insert this string into a comment, the attacker's ability to evade suspicion is greatly increased, and they may even be able to hide the initial payload in plain sight if a victim assumes that comments will be ignored by pipenv as expected.\n\nIn many common usage contexts — for example in environments where a requirements file is used to lock or \"freeze\" dependency versions for reproducible builds — requirements files can often become quite large, particularly when leveraging pip's integrity checking, which requires every dependency specified in the requirements file to includes hashes for all of its distribution files. In such cases, a malicious actor might mask an exploitation attempt by opening a pull request ostensibly to update or \"bump\" the project's dependencies to their latest versions, but surreptitiously insert a malicious `—index-url` option amidst the many other changes associated with updating the dependencies in a lock file. As these dependency updates often result in hundreds or even thousands of changes spread across the requirements file and are not easy to review manually, such an attack could be difficult to identify or prevent without tools or other mitigating controls.\n\nMoreover, because the `argparse` module is used to parse the `--index-url`, `--extra-index-url`, and `--trusted-host` options, an attacker's ability to obfuscate their payload and hide their malicious intent is even more greatly enhanced, as the attacker may use abbreviated option names, which are supported by default with `argparse`. For example, an attacker can insert the string, \"`--t pypi.org`\" into a comment anywhere in the requirements file, which will automatically be expanded to \"`--trusted-host pypi.org`\" during processing by pipenv. This \"`--trusted-host pypi.org`\" option will disable SSL/TLS validation when pipenv attempts to connect to the default/official package index server (https://pypi.org/simple), and could allow a malicious index server to pose as the pipi.org index server in a man-in-the-middle attack.\n\nSetting up the malicious index server to serve compromised package versions is relatively simple, even for a non-sophisticated attacker. As `pip` uses a simple directory format for serving packages, the malicious packages simply need to be placed in the correct folder structure and served using an HTTP server with autoindex enabled (e.g. `python3 -m http.server`).\n\nPackaging up the exploit code into the malicious package versions would also be trivial for an attacker with basic knowledge of Python development, as the attacker can simply clone the source code for any of the packages specified in the requirements file, embed their malicious exploit code in the cloned package's setup.py file, and then build a source distribution of the package. When pip installs a package from a source distribution, any code in the setup.py is executed by the install process.\n\n### Additional Context & Details\nAccording to the requirements file format specification (https://pip.pypa.io/en/stable/reference/requirements-file-format/#comments), any lines which begin with a \"#\" character, and/or any text in a line following a whitespace and a \"#\" character, should be interpreted as a comment which will be removed/ignored during processing of the requirements file.\n\nHowever, due to a flaw in pipenv's parsing of requirements files, an attacker can insert a specially crafted string inside a comment anywhere within a requirements.txt file, which will cause victims who use pipenv to install the requirements file (e.g. with \"`pipenv install -r requirements.txt`\") to download dependencies from a package index server controlled by the attacker. By embedding malicious code in packages served from their malicious index server, the attacker is then able to gain arbitrary remote code execution on the victims' systems.\n\nThe vulnerable requirements file parsing code is in the parse_indexes(str: line) function of the pipenv.utils module:\n\nhttps://github.com/pypa/pipenv/blob/cdde3f7bcee6bacba89538f73aba9401337be10c/pipenv/utils.py#L2061-L2078\n\nThis function is called iteratively on each line of a requirements file, and uses the argparse module to find and process `--index-url`, `--extra-index-url`, and `--trusted-host` options (and variations thereof). However, it does not ignore these options when they appear in comments, or validate that these options appear on their own lines as required by the requirements file specification (see: https://pip.pypa.io/en/stable/reference/requirements-file-format/#global-options). The options can also be abbreviated due to default behavior provided by the `argparse.ArgumentParser` object used to parse these options in the requirements file, so that `--trusted-host` and `--t` will be treated as equivalent by pipenv, for example.\n\n### For more information\nIf you have any questions or comments about this advisory:\n* Open an issue in [https://github.com/pypa/pipenv/](https://github.com/pypa/pipenv/)\n* Contact the pipenv maintainers:\n * [Dan Ryan](https://github.com/techalchemy)\n * [Tzu-ping Chung](https://github.com/uranusjr)\n * [Nate Prewitt](https://github.com/nateprewitt)\n* Contact the contributor who discovered the issue and authored this report:\n * [Chris Passarello](https://github.com/milo-minderbinder)",
"severity" : [ {
"type" : "CVSS_V3",
"score" : "CVSS:3.1/AV:N/AC:H/PR:L/UI:R/S:C/C:H/I:H/A:H"
}, {
"type" : "CVSS_V4",
"score" : "CVSS:4.0/AV:N/AC:L/AT:P/PR:L/UI:A/VC:H/VI:H/VA:H/SC:H/SI:H/SA:H"
} ],
"affected" : [ {
"package" : {
"ecosystem" : "PyPI",
"name" : "pipenv"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "2018.10.9"
}, {
"fixed" : "2022.1.8"
} ]
} ]
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/pypa/pipenv/security/advisories/GHSA-qc9x-gjcv-465w"
}, {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2022-21668"
}, {
"type" : "WEB",
"url" : "https://github.com/pypa/pipenv/commit/439782a8ae36c4762c88e43d5f0d8e563371b46f"
}, {
"type" : "WEB",
"url" : "https://github.com/pypa/advisory-database/tree/main/vulns/pipenv/PYSEC-2022-6.yaml"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/pypa/pipenv"
}, {
"type" : "WEB",
"url" : "https://github.com/pypa/pipenv/releases/tag/v2022.1.8"
}, {
"type" : "WEB",
"url" : "https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/56HBA3EOSLEDNCCBJVHE6DO34P56EOUM"
}, {
"type" : "WEB",
"url" : "https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/KCROBYHUS6DKQPCXBRPCZ5CDBNQTYAWT"
}, {
"type" : "WEB",
"url" : "https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/QHQRIWKDP3SVJABAPEXBIQPKDI6UP7G4"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-20", "CWE-77" ],
"severity" : "HIGH",
"github_reviewed" : true,
"github_reviewed_at" : "2022-01-10T21:52:02Z",
"nvd_published_at" : "2022-01-10T21:15:00Z"
}
}