Skip to content

Commit b3f056b

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 this public repository, some of these workflows are copies of "templates" which are intended to be applicable in public and private repositories both. A small excess in permissions was chosen instead of the alternative of having to maintain separate variants of each workflow template for use in public or private repos.
1 parent b348867 commit b3f056b

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

.github/workflows/check-taskfiles.yml

Lines changed: 3 additions & 0 deletions
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/libraries_report-size-deltas.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on:
1414
jobs:
1515
test:
1616
runs-on: ubuntu-latest
17+
permissions: {}
1718

1819
env:
1920
PYTHON_PROJECT_PATH: ${GITHUB_WORKSPACE}/reportsizedeltas

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

Lines changed: 6 additions & 0 deletions
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

.github/workflows/test-integration.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525
local-source:
2626
if: github.event_name == 'pull_request'
2727
runs-on: ubuntu-latest
28+
permissions:
29+
pull-requests: write
2830

2931
steps:
3032
- name: Checkout repository

0 commit comments

Comments
 (0)