From 95b503503e9f472a31157adf4b831155de9c4bf5 Mon Sep 17 00:00:00 2001 From: per1234 Date: Sun, 25 Jun 2023 22:24:24 -0700 Subject: [PATCH] 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, the templates are intended to be applicable in public and private repositories both and so a small excess in permissions was chosen in order to use the upstream templates unmodified. --- .github/workflows/check-certificates.yml | 1 + .github/workflows/check-code-generation-task.yml | 2 ++ .github/workflows/check-general-formatting-task.yml | 2 ++ .github/workflows/check-go-dependencies-task.yml | 5 +++++ .github/workflows/check-go-task.yml | 11 +++++++++++ .github/workflows/check-license.yml | 2 ++ .github/workflows/check-markdown-task.yml | 4 ++++ .github/workflows/check-mkdocs-task.yml | 2 ++ .github/workflows/check-prettier-formatting-task.yml | 2 ++ .github/workflows/check-python-task.yml | 4 ++++ .github/workflows/check-shell-task.yml | 6 ++++++ .github/workflows/check-workflows-task.yml | 2 ++ .../deploy-cobra-mkdocs-versioned-poetry.yml | 3 +++ .github/workflows/publish-go-nightly-task.yml | 7 +++++++ .github/workflows/publish-go-tester-task.yml | 6 ++++++ .github/workflows/release-go-task.yml | 6 ++++++ .github/workflows/spell-check-task.yml | 2 ++ .github/workflows/sync-labels.yml | 6 ++++++ .github/workflows/test-go-integration-task.yml | 3 +++ .github/workflows/test-go-task.yml | 3 +++ .github/workflows/test-install.yml | 4 ++++ 21 files changed, 83 insertions(+) diff --git a/.github/workflows/check-certificates.yml b/.github/workflows/check-certificates.yml index f6f6feb5..94e4d966 100644 --- a/.github/workflows/check-certificates.yml +++ b/.github/workflows/check-certificates.yml @@ -27,6 +27,7 @@ jobs: (github.event_name != 'pull_request' && github.repository == 'arduino/arduino-lint') || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == 'arduino/arduino-lint') runs-on: ubuntu-latest + permissions: {} strategy: fail-fast: false diff --git a/.github/workflows/check-code-generation-task.yml b/.github/workflows/check-code-generation-task.yml index ce58df7f..cc5a1892 100644 --- a/.github/workflows/check-code-generation-task.yml +++ b/.github/workflows/check-code-generation-task.yml @@ -28,6 +28,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 @@ -51,6 +52,7 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: {} steps: - name: Checkout local repository diff --git a/.github/workflows/check-general-formatting-task.yml b/.github/workflows/check-general-formatting-task.yml index e1c202b6..fdac3733 100644 --- a/.github/workflows/check-general-formatting-task.yml +++ b/.github/workflows/check-general-formatting-task.yml @@ -14,6 +14,8 @@ on: jobs: check: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Set environment variables diff --git a/.github/workflows/check-go-dependencies-task.yml b/.github/workflows/check-go-dependencies-task.yml index f502f907..c62a2eb8 100644 --- a/.github/workflows/check-go-dependencies-task.yml +++ b/.github/workflows/check-go-dependencies-task.yml @@ -37,6 +37,7 @@ on: jobs: run-determination: runs-on: ubuntu-latest + permissions: {} outputs: result: ${{ steps.determination.outputs.result }} steps: @@ -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 @@ -118,6 +121,8 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/check-go-task.yml b/.github/workflows/check-go-task.yml index 8f0c8986..b99650ef 100644 --- a/.github/workflows/check-go-task.yml +++ b/.github/workflows/check-go-task.yml @@ -28,6 +28,7 @@ on: jobs: run-determination: runs-on: ubuntu-latest + permissions: {} outputs: result: ${{ steps.determination.outputs.result }} steps: @@ -54,6 +55,8 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: + contents: read strategy: fail-fast: false @@ -89,6 +92,8 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: + contents: read strategy: fail-fast: false @@ -127,6 +132,8 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: + contents: read strategy: fail-fast: false @@ -165,6 +172,8 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: + contents: read strategy: fail-fast: false @@ -203,6 +212,8 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: + contents: read strategy: fail-fast: false diff --git a/.github/workflows/check-license.yml b/.github/workflows/check-license.yml index 7fc08d06..9bb2d339 100644 --- a/.github/workflows/check-license.yml +++ b/.github/workflows/check-license.yml @@ -31,6 +31,8 @@ on: jobs: check-license: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/check-markdown-task.yml b/.github/workflows/check-markdown-task.yml index 5a17b249..747fcbb0 100644 --- a/.github/workflows/check-markdown-task.yml +++ b/.github/workflows/check-markdown-task.yml @@ -36,6 +36,8 @@ on: jobs: lint: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository @@ -55,6 +57,8 @@ jobs: links: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/check-mkdocs-task.yml b/.github/workflows/check-mkdocs-task.yml index 66945663..cbac829b 100644 --- a/.github/workflows/check-mkdocs-task.yml +++ b/.github/workflows/check-mkdocs-task.yml @@ -37,6 +37,8 @@ on: jobs: check: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/check-prettier-formatting-task.yml b/.github/workflows/check-prettier-formatting-task.yml index 68247c87..d89bb09a 100644 --- a/.github/workflows/check-prettier-formatting-task.yml +++ b/.github/workflows/check-prettier-formatting-task.yml @@ -201,6 +201,8 @@ on: jobs: check: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/check-python-task.yml b/.github/workflows/check-python-task.yml index 81762a78..946c1014 100644 --- a/.github/workflows/check-python-task.yml +++ b/.github/workflows/check-python-task.yml @@ -33,6 +33,8 @@ on: jobs: lint: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository @@ -60,6 +62,8 @@ jobs: formatting: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/check-shell-task.yml b/.github/workflows/check-shell-task.yml index 837585cc..dce1620a 100644 --- a/.github/workflows/check-shell-task.yml +++ b/.github/workflows/check-shell-task.yml @@ -27,6 +27,8 @@ jobs: lint: name: ${{ matrix.configuration.name }} runs-on: ubuntu-latest + permissions: + contents: read env: # See: https://github.com/koalaman/shellcheck/releases/latest @@ -89,6 +91,8 @@ jobs: formatting: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Set environment variables @@ -132,6 +136,8 @@ jobs: executable: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/check-workflows-task.yml b/.github/workflows/check-workflows-task.yml index 5b5287c5..60a44e04 100644 --- a/.github/workflows/check-workflows-task.yml +++ b/.github/workflows/check-workflows-task.yml @@ -20,6 +20,8 @@ on: jobs: validate: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/deploy-cobra-mkdocs-versioned-poetry.yml b/.github/workflows/deploy-cobra-mkdocs-versioned-poetry.yml index 472011e8..f2a45685 100644 --- a/.github/workflows/deploy-cobra-mkdocs-versioned-poetry.yml +++ b/.github/workflows/deploy-cobra-mkdocs-versioned-poetry.yml @@ -32,6 +32,7 @@ on: jobs: publish-determination: runs-on: ubuntu-latest + permissions: {} outputs: result: ${{ steps.determination.outputs.result }} steps: @@ -51,6 +52,8 @@ jobs: runs-on: ubuntu-latest needs: publish-determination if: needs.publish-determination.outputs.result == 'true' + permissions: + contents: write steps: - name: Checkout repository diff --git a/.github/workflows/publish-go-nightly-task.yml b/.github/workflows/publish-go-nightly-task.yml index db94b4a6..1b074b48 100644 --- a/.github/workflows/publish-go-nightly-task.yml +++ b/.github/workflows/publish-go-nightly-task.yml @@ -21,6 +21,8 @@ on: jobs: create-nightly-artifacts: runs-on: ubuntu-latest + permissions: + contents: read strategy: matrix: @@ -66,6 +68,9 @@ jobs: checksum-darwin_amd64: ${{ steps.re-package.outputs.checksum-darwin_amd64 }} checksum-darwin_arm64: ${{ steps.re-package.outputs.checksum-darwin_arm64 }} + permissions: + contents: read + env: GON_CONFIG_PATH: gon.config.hcl @@ -166,6 +171,7 @@ jobs: publish-nightly: runs-on: ubuntu-latest needs: notarize-macos + permissions: {} steps: - name: Download artifact @@ -194,6 +200,7 @@ jobs: runs-on: ubuntu-latest needs: publish-nightly if: failure() # Run if publish-nightly or any of its job dependencies failed + permissions: {} steps: - name: Report failure diff --git a/.github/workflows/publish-go-tester-task.yml b/.github/workflows/publish-go-tester-task.yml index b97a385c..8f2094b4 100644 --- a/.github/workflows/publish-go-tester-task.yml +++ b/.github/workflows/publish-go-tester-task.yml @@ -34,6 +34,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 @@ -57,6 +58,7 @@ jobs: needs: run-determination if: needs.run-determination.outputs.result == 'true' runs-on: ubuntu-latest + permissions: {} outputs: prefix: ${{ steps.calculation.outputs.prefix }} steps: @@ -75,6 +77,8 @@ jobs: needs: package-name-prefix name: Build ${{ matrix.os.name }} runs-on: ubuntu-latest + permissions: + contents: read strategy: matrix: @@ -135,6 +139,8 @@ jobs: - build - package-name-prefix runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Download build artifacts diff --git a/.github/workflows/release-go-task.yml b/.github/workflows/release-go-task.yml index 9f01394c..da62d95d 100644 --- a/.github/workflows/release-go-task.yml +++ b/.github/workflows/release-go-task.yml @@ -18,6 +18,8 @@ on: jobs: create-release-artifacts: runs-on: ubuntu-latest + permissions: + contents: read strategy: matrix: @@ -71,6 +73,8 @@ jobs: outputs: checksum-darwin_amd64: ${{ steps.re-package.outputs.checksum-darwin_amd64 }} checksum-darwin_arm64: ${{ steps.re-package.outputs.checksum-darwin_arm64 }} + permissions: + contents: read env: GON_CONFIG_PATH: gon.config.hcl @@ -172,6 +176,8 @@ jobs: create-release: runs-on: ubuntu-latest needs: notarize-macos + permissions: + contents: write steps: - name: Download artifact diff --git a/.github/workflows/spell-check-task.yml b/.github/workflows/spell-check-task.yml index bebccd62..80693309 100644 --- a/.github/workflows/spell-check-task.yml +++ b/.github/workflows/spell-check-task.yml @@ -18,6 +18,8 @@ on: jobs: spellcheck: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository diff --git a/.github/workflows/sync-labels.yml b/.github/workflows/sync-labels.yml index 70085bde..467bb5b7 100644 --- a/.github/workflows/sync-labels.yml +++ b/.github/workflows/sync-labels.yml @@ -24,6 +24,8 @@ env: jobs: check: runs-on: ubuntu-latest + permissions: + contents: read steps: - name: Checkout repository @@ -55,6 +57,7 @@ jobs: download: needs: check runs-on: ubuntu-latest + permissions: {} strategy: matrix: @@ -82,6 +85,9 @@ jobs: sync: needs: download runs-on: ubuntu-latest + permissions: + contents: read + issues: write steps: - name: Set environment variables diff --git a/.github/workflows/test-go-integration-task.yml b/.github/workflows/test-go-integration-task.yml index d78c7e39..31419f75 100644 --- a/.github/workflows/test-go-integration-task.yml +++ b/.github/workflows/test-go-integration-task.yml @@ -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 @@ -60,6 +61,8 @@ jobs: test: needs: run-determination if: needs.run-determination.outputs.result == 'true' + permissions: + contents: read strategy: matrix: diff --git a/.github/workflows/test-go-task.yml b/.github/workflows/test-go-task.yml index c561e2f3..e78ec447 100644 --- a/.github/workflows/test-go-task.yml +++ b/.github/workflows/test-go-task.yml @@ -34,6 +34,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 @@ -57,6 +58,8 @@ jobs: name: test (${{ matrix.module.path }} - ${{ matrix.operating-system }}) needs: run-determination if: needs.run-determination.outputs.result == 'true' + permissions: + contents: read strategy: fail-fast: false diff --git a/.github/workflows/test-install.yml b/.github/workflows/test-install.yml index 738f8b92..9ab87d36 100644 --- a/.github/workflows/test-install.yml +++ b/.github/workflows/test-install.yml @@ -21,6 +21,7 @@ env: jobs: default: + permissions: {} strategy: fail-fast: false @@ -47,6 +48,7 @@ jobs: "${PWD}/bin/${{ env.TOOL_NAME }}" --version bindir: + permissions: {} strategy: fail-fast: false @@ -80,6 +82,7 @@ jobs: "${{ env.BINDIR }}/${{ env.TOOL_NAME }}" --version version: + permissions: {} strategy: fail-fast: false @@ -109,6 +112,7 @@ jobs: "${PWD}/bin/${{ env.TOOL_NAME }}" --version | grep --fixed-strings "${{ env.VERSION }}" nightly: + permissions: {} strategy: fail-fast: false