Skip to content

Commit 548c250

Browse files
walteckterado
andauthored
updating guidance on actions usage (#370)
Co-authored-by: Nick Miles <7bzbaedz8d@snkmail.com>
1 parent a165c4c commit 548c250

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

practices/actions-best-practices.md

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
GitHub Actions is a powerful automation tool that enables CI/CD workflows directly within your GitHub repository. Securing your GitHub Actions workflows is crucial to protect your code, secrets, and infrastructure from potential security threats.
66

7-
This guide outlines best practices for securing your GitHub Actions workflows and minimizing security risks.
7+
This guide outlines best practices for securing your GitHub Actions workflows and minimizing security risks. All actions used in committed workflow definitions must be pinned to a full-length commit SHA.
88

99
## Table of Contents
1010

@@ -40,7 +40,7 @@ jobs:
4040
environment: production
4141
runs-on: ubuntu-latest
4242
steps:
43-
- uses: actions/checkout@v3
43+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
4444
- name: Deploy
4545
env:
4646
API_TOKEN: ${{ secrets.API_TOKEN }}
@@ -57,7 +57,7 @@ jobs:
5757
5858
### Use Least Privilege Principle
5959
60-
Limit the GitHub token permissions to only what's necessary please [see here](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#permissions-for-the-github_token) for details on the default permissions that the github token is given when the permissions block is not used:
60+
Limit the GitHub token permissions to only what's necessary [see here](https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#permissions-for-the-github_token) for details on the default permissions that the github token is given when the permissions block is not used:
6161
6262
```yaml
6363
permissions:
@@ -83,28 +83,53 @@ While third-party actions can significantly enhance the functionality and effici
8383
- *Lack of Maintenance*: Some third-party actions may not be actively maintained, leaving them vulnerable to security issues or compatibility problems with newer GitHub Actions features.
8484
- *Excessive Permissions*: Third-party actions may request more permissions than necessary, potentially exposing sensitive data or allowing unauthorized access to your repository.
8585
86-
To mitigate these risks, always follow best practices, such as pinning actions to specific commit SHAs, reviewing the source code of actions, and using only trusted actions from reputable sources.
86+
To mitigate these risks, all actions must be pinned to specific commit SHAs, reviewed before adoption, and sourced only from trusted publishers. Teams must minimise use of third-party actions and should expect the permitted set of actions to be restricted over time.
8787
88-
### Pin Actions to Specific Versions
88+
### Pin All Actions to a Commit SHA
8989
90-
When including a GitHub Action within your workflow you should perform due diligence checks to ensure that the action achieves the aims you are intending it to, and that it doesn't do anything unintended, this would include performing a code review of the GitHub action code. To prevent the underlying code being changed without your awareness always use specific commit SHAs instead of tags or branches as tags can be modified if the upstream repository is compromised:
90+
When including a GitHub Action within your workflow you should perform due diligence checks to ensure that the action achieves the aims you are intending it to, and that it does not do anything unintended, including reviewing the action code where appropriate. Every action reference must use a full-length commit SHA, including GitHub-authored actions, marketplace actions, and internally maintained actions, and must include an inline comment identifying the corresponding tag or version. Do not use tags or branch references in committed workflow definitions because they can move without review or be modified if the upstream repository is compromised. The tag annotation comment is not optional — without it, a pinned SHA is opaque and cannot be reviewed or updated effectively:
9191
9292
```yaml
9393
# Not secure - can change unexpectedly
94-
- uses: actions/checkout@v3
95-
# Better - using a specific version tag
96-
- uses: actions/checkout@v3.1.0
97-
# Best - using a specific commit SHA
98-
- uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3.1.0
94+
- uses: actions/checkout@v4
95+
# Also not acceptable - tags can be moved
96+
- uses: actions/checkout@v4.1.7
97+
# Required - pin to the full commit SHA and annotate the tag for readability
98+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
99+
```
100+
101+
If you use automation such as Dependabot to keep actions up to date, enable the `github-actions` ecosystem in `dependabot.yml` and keep the release tag comment on the same line as the pinned SHA so updates continue to track tagged releases.
102+
103+
A minimal Dependabot configuration for GitHub Actions is:
104+
105+
```yaml
106+
version: 2
107+
updates:
108+
- package-ecosystem: "github-actions"
109+
directory: "/"
110+
schedule:
111+
interval: "weekly"
99112
```
100113

101114
### Verify Third-Party Actions
102115

103-
When including a GitHub Action within your workflow consider alternatives, is there an existing mechanism you can use? Would this be something that could be reused and you could create your own action within the organisation that other teams could benefit from? If you can only achieve your goal with a third-party action then:
116+
Third-party actions must not be the default choice. Before introducing one, teams should confirm that the requirement cannot be met by:
117+
118+
- Native GitHub Actions features such as `run` steps, reusable workflows, or built-in workflow syntax
119+
- An action already owned and maintained within the organisation
120+
- An action that is already approved for reuse by other teams
121+
122+
If a third-party action is still required, document why it is needed, what alternatives were considered, and why those alternatives were rejected. This should live in `docs/ADRs.md`, or similar, to ensure the decision process is held within the repository. Teams should prefer actions with a clear maintenance history, minimal permissions, and a narrow, well-understood scope.
123+
124+
If you can only achieve your goal with a third-party action then:
104125

105126
- Only use trusted actions from the GitHub Marketplace
106127
- Review the source code of third-party actions before using them
107128
- Consider forking and maintaining your own copy of critical actions
129+
- Keep a record of the approval decision and the version or SHA that was reviewed
130+
- Be prepared to replace the action if organisational policy restricts the allowed set of actions
131+
132+
The long-term direction is to lock down the set of actions that can be used. Teams should therefore avoid introducing new third-party actions unless there is a clear, defensible need.
108133

109134
### Use Actions Security Best Practices
110135

@@ -164,7 +189,7 @@ jobs:
164189
permissions:
165190
contents: read
166191
steps:
167-
- uses: actions/checkout@v3
192+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
168193
- name: Run tests
169194
run: npm test
170195
```
@@ -189,9 +214,9 @@ jobs:
189214
id-token: write
190215
contents: read
191216
steps:
192-
- uses: actions/checkout@v3
217+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
193218
- name: Configure AWS credentials
194-
uses: aws-actions/configure-aws-credentials@v1
219+
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
195220
with:
196221
role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/github-actions
197222
aws-region: eu-west-2

0 commit comments

Comments
 (0)