A vulnerability in the GitHub Actions workflow for PyPI’s source repository could be exploited to perform a malicious pull request and eventually execute arbitrary code on pypi.org, according to a warning from a Japanese security researcher.
On Friday, security researcher RyotaK published information on three vulnerabilities in PyPI, one of which could potentially lead to the compromise of the entire PyPI ecosystem.
Python Package Index (PyPI) is the official third-party software repository for the Python programming language, with some package managers using it as the default source for packages and dependencies.
The issue was related to the combine-prs.yml workflow in pypa/warehouse, which was designed to collect and merge pull requests with branch names that start with dependabot (Dependabot itself doesn’t have a feature to merge pull requests).
The workflow did not verify the pull request author, anyone could create a pull request with a specific name and have the workflow to process it. Code execution, however, wouldn’t be possible, given that the workflow combines pull requests and the result is reviewed by a human, who will discard any malicious code.
What the researcher discovered was that the code responsible for printing branch lists of pull requests contained a vulnerability that could be exploited to execute commands and “leak GitHub Access Token with write permission against the pypa/warehouse repository.”
Because any code that is pushed to the main pypa/warehouse branch is automatically deployed to pypi.org, an attacker who obtains write permission to the repository can then execute arbitrary code on pypi.org.
For a successful attack, a threat actor would have to fork the pypa/warehouse repository, create in it a branch that that has a name that starts with dependabot, add a modification to the branch and create a benign pull request, wait for combine-prs.yml to execute, capture the leaked GitHub Access Token with write permissions, and then add a modification to the main branch to have it deployed to pypi.org.
As the PyPI security team points out, an attack would be difficult to identify, because the attacker can use a pull request that is not malicious. Thus, even if a PyPI administrator inspects the attacker’s pull request, they would approve it, as it would not attempt to exploit any vulnerability.
The vulnerable workflow was added to the repository in October 2020. The PyPI security team patched the security hole last week, the same day that RyotaK reported it.