You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2024-12-13-python-packaging-security.md
+30-13Lines changed: 30 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ last_modified: 2024-12-19
18
18
19
19
## Is your PyPI publication workflow secure?
20
20
21
-
We can learn a lot from the recent Python package breach [involving Ultralytics](https://blog.pypi.org/posts/2024-12-11-ultralytics-attack-analysis/). This breach highlighted our need to use more secure PyPI publishing workflows for Python packages.
21
+
We can learn a lot from the Python package breach [involving Ultralytics](https://blog.pypi.org/posts/2024-12-11-ultralytics-attack-analysis/). This breach highlighted the importance of making our PyPI publishing workflows for Python packages more secure.
22
22
23
23
In this breach, hackers exploited a GitHub action workflow to inject malicious code into a Python package. This package was then published to PyPI. The outcome: Users who downloaded the package unknowingly allowed their machines to be hijacked for Bitcoin mining.
24
24
@@ -28,11 +28,10 @@ While unsettling, there’s a silver lining: the PyPI security team had already
28
28
29
29
{% include pyos-blockquote.html quote="Because the Ultralytics project was using Trusted Publishing and the PyPA’s publishing GitHub Action: PyPI staff, volunteers, and security researchers were able to dig into how maliciously injected software was able to make its way into the package." author="Seth Larson, PSF Security Expert" class="highlight magenta" %}
30
30
31
-
This incident underscores the importance of understanding Python packaging security best practices, and this includes understanding how to lock things down on GitHub & GitLab!
31
+
This means that the important thing for us, as maintainers, is that we all should know how to lock down our publishing workflows.
32
+
Here, I'll cover the lessons learned that you can apply TODAY to your Python packaging workflows!
32
33
33
-
But never fear, here, I'll cover the lessons learned that you can apply TODAY to your Python packaging workflows!
34
-
35
-
*Special thanks to [Sviatoslav Sydorenko](https://github.com/webknjaz) for reviewing this blog post!!*
34
+
*Special thanks to [Sviatoslav Sydorenko](https://github.com/webknjaz) for reviewing and providing significant input on this blog post!!*
The Ultralytics breach highlights the need for us all to follow and understand secure PyPI publishing practices and carefully monitor workflows. Below are actionable steps you can take to enhance security when publishing Python packages to PyPI using GitHub actions.
60
59
61
-
[PyPA provides a great overview of using actions to publish your Python package.](https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/)
60
+
<iclass="fa-solid fa-circle-info"></i> [PyPA provides a great overview of using actions to publish your Python package.](https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/)
62
61
{: .notice }
63
62
64
63
## 1. Create a dedicated GitHub environment for publishing actions
@@ -83,16 +82,21 @@ If you look at the workflow example below, notice that we have an [environment c
83
82
84
83
To lockdown a GitHub environment:
85
84
86
-
* First, go to the **settings** in your repository where the workflow is run
85
+
* First, go to the <kbd>Settings</kbd> in your repository where the workflow is run
87
86
* Within settings, select **environments** from the left-hand sidebar
88
-
* Add a new environment. Use `pypi` as your environment name; this is what PyPA (the Python Packaging Authority) recommends.
89
-
* Ensure **Required reviewers** is enabled. This setting allows you to designate specific individuals who can approve and manually run the workflow on GitHub. Any reviewers you add must have the appropriate permissions to authorize the workflow by clicking a button. This adds a human verification step to the process.
90
-
* Once the required reviewers are checked, add maintainers who you want to be able to enable the action to run.
87
+
* Add a new environment. Use <kbd>pypi</kbd> as your environment name; this is what PyPA (the Python Packaging Authority) recommends.
88
+
* Ensure <kbd>Required reviewers</kbd> is enabled. This setting allows you to designate specific individuals who can approve and manually run the workflow on GitHub. Any reviewers you add must have the appropriate permissions to authorize the workflow by clicking a button. This adds a human verification step to the process.
89
+
* Once the <kbd>Required reviewers</kbd> button is checked, add maintainers who you want to be able to enable the action to run.
91
90
92
-
*Optionally, you can prevent self-review, preventing someone from triggering a release or a build and then running it!*
91
+
*Optionally, you can click <kbd>prevent self-review</kbd>, preventing someone from triggering a release or a build and then running it!*
92
+
93
+
<figure>
94
+
<img src="/images/python-packaging/create-github-environment.gif" alt="Animated gif file that shows the github interface where you can click on settings and go to the environment setting to create or edit a GitHub environment">
95
+
<figcaption>
96
+
To create a new environment to use in a GitHub action, 1) go to your repo's settings; 2) click <kbd>environment</kbd>; 3) add a new environment. In this screenshot, we already have a <kbd>pypi</kbd> environment created. Note that you can name your environment whatever you want, however, PyPI suggests that you use the name <kbd>pypi</kbd> for a Trusted Publisher workflow.
97
+
</figcaption>
98
+
</figure>
93
99
94
-
TODO: add an animated gif that shows the process on GitHub of creating the environment in setting and adding users. Or add screenshots.
95
-
{: .notice }
96
100
97
101
<figure>
98
102
<img src="/images/python-packaging/github-action-environment-pypi.png" alt="Screenshot of the GitHub settings interface showing the ‘Environments’ section with configuration options for ‘pypi.’ The ‘Deployment protection rules’ section is visible, with ‘Required reviewers’ enabled and two reviewers listed: ‘lwasser’ and ‘willingc.’ Other options such as ‘Prevent self-review’ and ‘Wait timer’ are present but not enabled.">
@@ -135,6 +139,7 @@ The steps for setting up Trusted Publisher are:
135
139
7. Fill out a form that looks like the one below in the add a new pending publisher section. Notice that you can select GitHub, GitLab, Google and Active State as platforms.
136
140
10. Notice that the form asks for your project name, owner, repo name, workflow's file name, and environment (**STRONGLY recommended**).
@@ -147,6 +152,18 @@ The steps for setting up Trusted Publisher are:
147
152
148
153
For an example of a GitHub workflow that uses trusted publishing, check out our active pyOpenSci [PyPI publishing GitHub workflow](https://github.com/pyOpenSci/pyosMeta/blob/main/.github/workflows/publish-pypi.yml), which follows the Trusted Publisher approach.
<img src="trusted-publisher-form.webp" alt="PyPI Trusted Publisher manage settings showing what the Trusted Publisher setup looks like after you've created it in PyPI. It shows all of the items that you filled out in the form and has a remove button if you want to remove it from PyPI. " loading="lazy">
160
+
</picture>
161
+
<figcaption>
162
+
Example of the PyPI Trusted Publisher setup in PyPI once you've created the Trusted PuUblisher link by filling the form out above.
163
+
</figcaption>
164
+
</figure>
165
+
166
+
150
167
**Note:** Read more here about [support for publishing to GitLab](https://docs.pypi.org/trusted-publishers/adding-a-publisher/#gitlab-cicd) using trusted publishing.
0 commit comments