Skip to content

Commit 1401cb4

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 GitHub 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. Configuration Granularity ------------------------- 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. Security Implications --------------------- 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. API Request Implications ------------------------ 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). Excess Permissions ------------------ 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 this public repositories, these workflows are "templates", intended to be applicable in public and private repositories both. 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. For the sake of maintainability, it is best to avoid any unnecessary differences between the files in this repository and the contents of the upstream "templates".
1 parent 8d82172 commit 1401cb4

13 files changed

+56
-3
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ on:
1515
jobs:
1616
run-determination:
1717
runs-on: ubuntu-latest
18+
permissions: {}
1819
outputs:
1920
result: ${{ steps.determination.outputs.result }}
2021
steps:
@@ -40,6 +41,8 @@ jobs:
4041
needs: run-determination
4142
if: needs.run-determination.outputs.result == 'true'
4243
runs-on: ubuntu-latest
44+
permissions:
45+
contents: read
4346

4447
steps:
4548
- name: Set environment variables

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

+5
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
@@ -119,6 +122,8 @@ jobs:
119122
needs: run-determination
120123
if: needs.run-determination.outputs.result == 'true'
121124
runs-on: ubuntu-latest
125+
permissions:
126+
contents: read
122127

123128
steps:
124129
- name: Checkout repository

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

+11
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

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ on:
3535
jobs:
3636
run-determination:
3737
runs-on: ubuntu-latest
38+
permissions: {}
3839
outputs:
3940
result: ${{ steps.determination.outputs.result }}
4041
steps:
@@ -60,6 +61,8 @@ jobs:
6061
needs: run-determination
6162
if: needs.run-determination.outputs.result == 'true'
6263
runs-on: ubuntu-latest
64+
permissions:
65+
contents: read
6366

6467
steps:
6568
- name: Checkout repository

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

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ on:
3939
jobs:
4040
run-determination:
4141
runs-on: ubuntu-latest
42+
permissions: {}
4243
outputs:
4344
result: ${{ steps.determination.outputs.result }}
4445
steps:
@@ -64,6 +65,8 @@ jobs:
6465
needs: run-determination
6566
if: needs.run-determination.outputs.result == 'true'
6667
runs-on: ubuntu-latest
68+
permissions:
69+
contents: read
6770

6871
steps:
6972
- name: Checkout repository
@@ -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
steps:
95100
- name: Checkout repository

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ on:
2424
workflow_dispatch:
2525
repository_dispatch:
2626

27-
permissions:
28-
contents: read
29-
3027
jobs:
3128
run-determination:
3229
runs-on: ubuntu-latest
30+
permissions: {}
3331
outputs:
3432
result: ${{ steps.determination.outputs.result }}
3533
steps:
@@ -56,6 +54,8 @@ jobs:
5654
needs: run-determination
5755
if: needs.run-determination.outputs.result == 'true'
5856
runs-on: ubuntu-latest
57+
permissions:
58+
contents: read
5959

6060
strategy:
6161
fail-fast: false
@@ -90,6 +90,8 @@ jobs:
9090
needs: run-determination
9191
if: needs.run-determination.outputs.result == 'true'
9292
runs-on: ubuntu-latest
93+
permissions:
94+
contents: read
9395

9496
strategy:
9597
fail-fast: false

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

+2
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ jobs:
233233
needs: run-determination
234234
if: needs.run-determination.outputs.result == 'true'
235235
runs-on: ubuntu-latest
236+
permissions:
237+
contents: read
236238

237239
steps:
238240
- name: Checkout repository

.github/workflows/check-taskfiles.yml

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ on:
2929
jobs:
3030
run-determination:
3131
runs-on: ubuntu-latest
32+
permissions: {}
3233
outputs:
3334
result: ${{ steps.determination.outputs.result }}
3435
steps:
@@ -55,6 +56,8 @@ jobs:
5556
needs: run-determination
5657
if: needs.run-determination.outputs.result == 'true'
5758
runs-on: ubuntu-latest
59+
permissions:
60+
contents: read
5861

5962
strategy:
6063
fail-fast: false

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

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ on:
2626
jobs:
2727
validate:
2828
runs-on: ubuntu-latest
29+
permissions:
30+
contents: read
2931

3032
steps:
3133
- name: Checkout repository

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

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ on:
4949
jobs:
5050
run-determination:
5151
runs-on: ubuntu-latest
52+
permissions: {}
5253
outputs:
5354
result: ${{ steps.determination.outputs.result }}
5455
steps:
@@ -75,6 +76,8 @@ jobs:
7576
needs: run-determination
7677
if: needs.run-determination.outputs.result == 'true'
7778
runs-on: ubuntu-latest
79+
permissions:
80+
contents: read
7881

7982
strategy:
8083
fail-fast: false

.github/workflows/release-go-crosscompile-task.yml

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ on:
2020
jobs:
2121
create-release-artifacts:
2222
runs-on: ubuntu-latest
23+
permissions:
24+
contents: read
2325

2426
strategy:
2527
matrix:
@@ -87,6 +89,8 @@ jobs:
8789
outputs:
8890
checksum-darwin_amd64: ${{ steps.re-package.outputs.checksum-darwin_amd64 }}
8991
checksum-darwin_arm64: ${{ steps.re-package.outputs.checksum-darwin_arm64 }}
92+
permissions:
93+
contents: read
9094

9195
env:
9296
GON_CONFIG_PATH: gon.config.hcl
@@ -198,6 +202,8 @@ jobs:
198202
create-release:
199203
runs-on: ubuntu-latest
200204
needs: notarize-macos
205+
permissions:
206+
contents: write
201207

202208
steps:
203209
- name: Download artifact

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

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ jobs:
4545
needs: run-determination
4646
if: needs.run-determination.outputs.result == 'true'
4747
runs-on: ubuntu-latest
48+
permissions:
49+
contents: read
4850

4951
steps:
5052
- name: Checkout repository

.github/workflows/sync-labels-npm.yml

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ on:
3030
jobs:
3131
check:
3232
runs-on: ubuntu-latest
33+
permissions:
34+
contents: read
3335

3436
steps:
3537
- name: Checkout repository
@@ -65,6 +67,7 @@ jobs:
6567
download:
6668
needs: check
6769
runs-on: ubuntu-latest
70+
permissions: {}
6871

6972
strategy:
7073
matrix:
@@ -92,6 +95,9 @@ jobs:
9295
sync:
9396
needs: download
9497
runs-on: ubuntu-latest
98+
permissions:
99+
contents: read
100+
issues: write
95101

96102
steps:
97103
- name: Set environment variables

0 commit comments

Comments
 (0)