Skip to content

Commit

Permalink
Configure permissions of GITHUB_TOKEN in workflows
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
per1234 committed Jun 26, 2023
1 parent 94d73e5 commit 2992f13
Show file tree
Hide file tree
Showing 58 changed files with 216 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check-ci-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ on:
jobs:
check-sync:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/check-clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand All @@ -60,6 +61,7 @@ jobs:

check-config:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down Expand Up @@ -136,6 +138,7 @@ jobs:

check-output:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down Expand Up @@ -195,6 +198,7 @@ jobs:

check-testdata:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand All @@ -211,6 +215,7 @@ jobs:

convert:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Set environment variables
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check-community-health-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ on:
jobs:
check-sync:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check-config-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ on:
jobs:
check-sync:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check-dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check-eslint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/check-files-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
runs-on: ubuntu-latest
outputs:
result: ${{ steps.determination.outputs.result }}
permissions: {}
steps:
- name: Determine if the rest of the workflow should run
id: determination
Expand All @@ -40,6 +41,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand All @@ -58,6 +61,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check-general-formatting-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -40,6 +41,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Set environment variables
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check-issue-templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/check-javascript-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ on:
workflow_dispatch:
repository_dispatch:

permissions:
contents: read

jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -60,6 +58,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check-license.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
runs-on: ubuntu-latest
outputs:
result: ${{ steps.determination.outputs.result }}
permissions: {}
steps:
- name: Determine if the rest of the workflow should run
id: determination
Expand All @@ -61,6 +62,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/check-markdown-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -66,6 +67,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand All @@ -92,6 +95,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/check-markdownlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
permissions: {}

steps:
- name: Checkout repository
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/check-npm-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ on:
workflow_dispatch:
repository_dispatch:

permissions:
contents: read

jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -57,6 +55,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand All @@ -80,6 +80,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check-prettier-formatting-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -234,6 +235,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/check-python-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -62,6 +63,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down Expand Up @@ -91,6 +94,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/check-shell-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -53,6 +54,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

env:
# See: https://github.com/koalaman/shellcheck/releases/latest
Expand Down Expand Up @@ -121,6 +124,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Set environment variables
Expand Down Expand Up @@ -166,6 +171,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check-taskfiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -55,6 +56,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

strategy:
fail-fast: false
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/check-workflows-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ on:
jobs:
validate:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/check-yaml-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -75,6 +76,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

strategy:
fail-fast: false
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/spell-check-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ on:
jobs:
run-determination:
runs-on: ubuntu-latest
permissions: {}
outputs:
result: ${{ steps.determination.outputs.result }}
steps:
Expand All @@ -44,6 +45,8 @@ jobs:
needs: run-determination
if: needs.run-determination.outputs.result == 'true'
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- name: Checkout repository
Expand Down
Loading

0 comments on commit 2992f13

Please sign in to comment.