Skip to content

Commit bd5792b

Browse files
committed
Configure permissions of GITHUB_TOKEN in workflows
`GITHUB_TOKEN` is an access token that is automatically generated and made accessible for use in GitHub Actions workflow runs. The global default permissions of this token for workflow runs in a trusted context (i.e., not triggered by a `pull_request` event from a fork) are set in the GiHub enterprise/organization/repository's administrative settings, giving it either read-only or write permissions in all scopes. In the case of a read-only default configuration, any workflow operations that require write permissions would fail with an error like: > 403: Resource not accessible by integration In the case of a write default configuration, workflows have unnecessary permissions, which violates the security principle of least privilege. For this reason, GitHub Actions now allows fine grained control at a per-workflow or per-workflow job scope of the permissions provided to the token. This is done using the `permissions` workflow key, which is used here to configure the workflows for only the permissions require by each individual job. I chose to always configure permissions at the job level even though in some cases the same permissions configuration could be used for all jobs in a workflow. Even if functionally equivalent, I think it is semantically more appropriate to always set the permissions at the job scope since the intention is to make the most granular possible permissions configuration. Hopefully this approach will increase the likelihood that appropriate permissions configurations will be made in any additional jobs that are added to the workflows in the future. The automatic permissions downgrade from write to read for workflow runs in an untrusted context (e.g., triggered by a `pull_request` event from a fork) is unaffected by this change. Even when all permissions are withheld (`permissions: {}`), the token still provides the authenticated API request rate limiting allowance (authenticating API requests to avoid rate limiting is a one of the uses of the token in these workflows). Read permissions are required in the "contents" scope in order to checkout private repositories. Even though those permissions are not required when the workflows are installed in public repositories, the templates are intended to be applicable in public and private repositories both and so a small excess in permissions was chosen instead of the alternative of having to maintain separate variants of each workflow for use in public or private repos.
1 parent 5b64eaa commit bd5792b

16 files changed

+56
-0
lines changed

.github/workflows/check-general-formatting-task.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ on:
1414
jobs:
1515
check:
1616
runs-on: ubuntu-latest
17+
permissions:
18+
contents: read
1719

1820
steps:
1921
- name: Set environment variables

.github/workflows/check-go-dependencies-task.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ on:
3737
jobs:
3838
run-determination:
3939
runs-on: ubuntu-latest
40+
permissions: {}
4041
outputs:
4142
result: ${{ steps.determination.outputs.result }}
4243
steps:
@@ -62,6 +63,8 @@ jobs:
6263
needs: run-determination
6364
if: needs.run-determination.outputs.result == 'true'
6465
runs-on: ubuntu-latest
66+
permissions:
67+
contents: read
6568

6669
steps:
6770
- name: Checkout repository
@@ -112,6 +115,8 @@ jobs:
112115
needs: run-determination
113116
if: needs.run-determination.outputs.result == 'true'
114117
runs-on: ubuntu-latest
118+
permissions:
119+
contents: read
115120

116121
steps:
117122
- name: Checkout repository

.github/workflows/check-go-task.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ on:
3131
jobs:
3232
run-determination:
3333
runs-on: ubuntu-latest
34+
permissions: {}
3435
outputs:
3536
result: ${{ steps.determination.outputs.result }}
3637
steps:
@@ -57,6 +58,8 @@ jobs:
5758
needs: run-determination
5859
if: needs.run-determination.outputs.result == 'true'
5960
runs-on: ubuntu-latest
61+
permissions:
62+
contents: read
6063

6164
strategy:
6265
fail-fast: false
@@ -90,6 +93,8 @@ jobs:
9093
needs: run-determination
9194
if: needs.run-determination.outputs.result == 'true'
9295
runs-on: ubuntu-latest
96+
permissions:
97+
contents: read
9398

9499
strategy:
95100
fail-fast: false
@@ -126,6 +131,8 @@ jobs:
126131
needs: run-determination
127132
if: needs.run-determination.outputs.result == 'true'
128133
runs-on: ubuntu-latest
134+
permissions:
135+
contents: read
129136

130137
strategy:
131138
fail-fast: false
@@ -162,6 +169,8 @@ jobs:
162169
needs: run-determination
163170
if: needs.run-determination.outputs.result == 'true'
164171
runs-on: ubuntu-latest
172+
permissions:
173+
contents: read
165174

166175
strategy:
167176
fail-fast: false
@@ -198,6 +207,8 @@ jobs:
198207
needs: run-determination
199208
if: needs.run-determination.outputs.result == 'true'
200209
runs-on: ubuntu-latest
210+
permissions:
211+
contents: read
201212

202213
strategy:
203214
fail-fast: false

.github/workflows/check-license.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ on:
3131
jobs:
3232
check-license:
3333
runs-on: ubuntu-latest
34+
permissions:
35+
contents: read
3436

3537
steps:
3638
- name: Checkout repository

.github/workflows/check-markdown-task.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ on:
3232
jobs:
3333
lint:
3434
runs-on: ubuntu-latest
35+
permissions:
36+
contents: read
3537

3638
steps:
3739
- name: Checkout repository
@@ -51,6 +53,8 @@ jobs:
5153

5254
links:
5355
runs-on: ubuntu-latest
56+
permissions:
57+
contents: read
5458

5559
steps:
5660
- name: Checkout repository

.github/workflows/check-prettier-formatting-task.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ on:
201201
jobs:
202202
check:
203203
runs-on: ubuntu-latest
204+
permissions:
205+
contents: read
204206

205207
steps:
206208
- name: Checkout repository

.github/workflows/check-python-task.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ on:
3333
jobs:
3434
lint:
3535
runs-on: ubuntu-latest
36+
permissions:
37+
contents: read
3638

3739
steps:
3840
- name: Checkout repository
@@ -60,6 +62,8 @@ jobs:
6062

6163
formatting:
6264
runs-on: ubuntu-latest
65+
permissions:
66+
contents: read
6367

6468
steps:
6569
- name: Checkout repository

.github/workflows/check-taskfiles.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121
validate:
2222
name: Validate ${{ matrix.file }}
2323
runs-on: ubuntu-latest
24+
permissions:
25+
contents: read
2426

2527
strategy:
2628
fail-fast: false

.github/workflows/check-yaml-task.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ jobs:
4646
check:
4747
name: ${{ matrix.configuration.name }}
4848
runs-on: ubuntu-latest
49+
permissions:
50+
contents: read
4951

5052
strategy:
5153
fail-fast: false

.github/workflows/compare-performance.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ on:
2727
jobs:
2828
init:
2929
runs-on: ubuntu-latest
30+
permissions: {}
3031

3132
outputs:
3233
base-ref: ${{ steps.base-ref.outputs.ref }}
@@ -75,6 +76,7 @@ jobs:
7576
name: Run at ${{ matrix.data.ref }} (${{ matrix.data.description }})
7677
needs: init
7778
runs-on: ubuntu-latest
79+
permissions: {}
7880

7981
strategy:
8082
matrix:
@@ -243,6 +245,7 @@ jobs:
243245
results:
244246
needs: run
245247
runs-on: ubuntu-latest
248+
permissions: {}
246249

247250
env:
248251
REPORTS_PATH: reports

0 commit comments

Comments
 (0)