Learn about secrets in software development and how you can manage them safely.
What is a secret?In software development, a secret is a piece of sensitive information that is used to authenticate or authorize access to systems, services, data, and APIs. Examples include:
Since secrets provide so much access, including to critical systems, we can understand why it's so important to keep your secrets secure.
What can happen when a secret is exposed?Consider all the access and abilities a secret grants you and what a hacker could do with it. For example, if a personal access token for your GitHub account was exposed, a hacker could post and make changes on GitHub as you.
Best practices for managing your secretsTo avoid these types of issues, follow best practices to prevent leaks and limit damage if a secret is ever exposed.
Follow the Principle of Least Privilege (PoLP)Whenever possible, restrict what a secret can do and can access to only what is necessary. For example:
There's a lot that you can do to keep your secrets safe, but there's also a lot that GitHub does to help keep your secrets secret. Everyone makes mistakes, and we're here to help with features that will catch any secrets you accidentally expose:
In this exercise, we'll create a personal access token and store it safely so we can use it with GitHub Actions. The action we'll create is a straightforward workflow that responds to an issue.
1. Creating a practice repositoryWe'll start by creating a repository to work from. The new2code
account has a template repository we can use to quickly get started.
new2code
account.secret-action
.Everyone makes mistakes, and it's possible that you'll accidentally commit a secret at some point in your coding journey. In this exercise, we'll intentionally commit a fake token so that we can become familiar and comfortable with the alert that gets triggered.
Navigate to the repository you just created.
Navigate to the YAML workflow file by clicking .github/workflows
in the list of files.
Open the workflow file by clicking comment.yml
in the list of files.
To edit the workflow file, at the top-right, click .
On line 13, GH_TOKEN: ""
, insert this dummy token between the quotes:
secret_scanning_ab85fc6f8d7638cf1c11da812da308d43_abcde
The end result should look like this:
GH_TOKEN: "secret_scanning_ab85fc6f8d7638cf1c11da812da308d43_abcde"
To attempt to commit the change, at the top right, click Commit changes... and then click Commit changes again in the dialog.
You should now see the push protection alert, telling you that "Secret scanning found a GitHub Secret Scanning secret on line 13".
If we weren't experimenting with a dummy token, this would alert us that we were one step away from exposing a token. Review the options you can select on the alert.
To stop your commit and avoid exposing the secret, click Cancel. In the top right, click Cancel changes, then discard your unsaved changes if prompted.
Now, let's try following our best practices. First, we'll create a personal access token which will allow the action to act on your behalf (the comment it creates will appear to come from your user account).
Note
Notice how we follow the Principle of Least Privilege for each configuration step. Your token will have the shortest expiration necessary, only have access to the repository it needs, and have the minimum permissions needed to work.
It's crucial to handle the resulting token securely from this moment forward. As we'll be using the token shortly, you can copy it to your clipboard briefly.
4. Storing the token safelyWe can now store our new token safely in our repository.
Navigate to the repository you created at the beginning of the exercise.
Under your repository name, click Settings. If you cannot see the "Settings" tab, select the dropdown menu, then click Settings.
In the "Security" section of the sidebar, select Secrets and variables, then click Actions.
Under "Repository secrets," click New repository secret.
In the Name field, type the name for your secret. For this exercise, we'll use MY_TOKEN
.
In the Secret field, paste the personal access token you generated previously.
Click Add secret.
Your secret is now safely encrypted and ready to use!
5. Referencing the token in our actionNow we can update the YAML workflow file to use the token and test it works.
Navigate back to your repository. If you're in your repository's settings, you can click Code under the repository name.
Navigate to the YAML workflow file by clicking .github/workflows
in the list of files.
Open the workflow file by clicking comment.yml
in the list of files.
To start editing the workflow file, at the top-right, click .
On line 13, GH_TOKEN: ""
, replace the empty quotes with ${{ secrets.MY_TOKEN }}
. This will reference the repository secret we added previously.
GH_TOKEN: ${{ secrets.MY_TOKEN }}
To commit the change, at the top-right, click Commit changes...
In the "Commit changes" dialog, edit "Commit message" to reflect the change we're making. For example, you could enter "Updating workflow to use repository secret".
Make sure "Commit directly to the main
branch" is selected.
Click Commit changes.
We should be all set now! Let's go ahead and test the workflow.
Under your repository name, click Issues.
Click New issue.
Under "Add a title", you can type any title you like.
Under "Add a description", in the text area, type Hello
.
Beneath the text area, click Create.
Once the workflow has had time to complete, you should see a new comment appear. The comment will be authored by yourself, as we're using your token, and contain a greeting in return.
Next stepsFor a more in-depth dive into secret scanning and push protection, you can complete the Introduction to secret scanning course in GitHub Skills.
Another important part of code security is learning how to identify and patch code vulnerabilities in your projects. See Finding and fixing your first code vulnerability.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4