From f27d090a1e15fd8cc21ccdf00c55cf8ba909616d Mon Sep 17 00:00:00 2001 From: Jake Aitchison Date: Mon, 16 Nov 2026 10:59:15 +0000 Subject: [PATCH 1/3] ci: restrict github token permissions in workflows Default every workflow to contents: read and grant per-job write scopes only where needed: actions: read to fetch artifacts from the triggering run, and checks/pull-requests write to publish unit test results. --- .github/workflows/build-status.yml | 3 +++ .github/workflows/publish-pr-coverage-results.yml | 5 +++++ .github/workflows/publish-pr-test-results.yml | 7 +++++++ .github/workflows/receive-pr.yml | 3 +++ .github/workflows/report-benchmark-results.yml | 5 +++++ 5 files changed, 23 insertions(+) diff --git a/.github/workflows/build-status.yml b/.github/workflows/build-status.yml index 3b24f4845..616512e4e 100644 --- a/.github/workflows/build-status.yml +++ b/.github/workflows/build-status.yml @@ -4,6 +4,9 @@ on: branches: - master +permissions: + contents: read + jobs: build-and-test: name: Build And Test ${{matrix.os}} diff --git a/.github/workflows/publish-pr-coverage-results.yml b/.github/workflows/publish-pr-coverage-results.yml index d6c8138eb..0e7b20e6b 100644 --- a/.github/workflows/publish-pr-coverage-results.yml +++ b/.github/workflows/publish-pr-coverage-results.yml @@ -6,10 +6,15 @@ on: types: - completed +permissions: + contents: read + jobs: publish-test-results: name: Publish Code Coverage Results runs-on: ubuntu-latest + permissions: + actions: read # the build-and-test job might be skipped, we don't need to run this job then if: > ${{ github.event.workflow_run.event == 'pull_request' && diff --git a/.github/workflows/publish-pr-test-results.yml b/.github/workflows/publish-pr-test-results.yml index 0eb5221ef..45fe516a3 100644 --- a/.github/workflows/publish-pr-test-results.yml +++ b/.github/workflows/publish-pr-test-results.yml @@ -6,10 +6,17 @@ on: types: - completed +permissions: + contents: read + jobs: publish-test-results: name: Publish Test Results runs-on: ubuntu-latest + permissions: + actions: read + checks: write + pull-requests: write # the build-and-test job might be skipped, we don't need to run this job then if: > ${{ github.event.workflow_run.event == 'pull_request' && diff --git a/.github/workflows/receive-pr.yml b/.github/workflows/receive-pr.yml index 7d2189d1e..21a387af9 100644 --- a/.github/workflows/receive-pr.yml +++ b/.github/workflows/receive-pr.yml @@ -2,6 +2,9 @@ name: Receive Pull Request on: [pull_request] +permissions: + contents: read + jobs: build-and-test: name: Build And Test ${{matrix.os}} diff --git a/.github/workflows/report-benchmark-results.yml b/.github/workflows/report-benchmark-results.yml index 2149f6469..b9e9b735a 100644 --- a/.github/workflows/report-benchmark-results.yml +++ b/.github/workflows/report-benchmark-results.yml @@ -6,10 +6,15 @@ on: types: - completed +permissions: + contents: read + jobs: publish-test-results: name: Report Benchmark Results runs-on: ubuntu-latest + permissions: + actions: read # the build-and-test job might be skipped, we don't need to run this job then if: > ${{ github.event.workflow_run.event == 'pull_request' && From dcc88f97445d43ccd6486be7d0b002ccee1e0d08 Mon Sep 17 00:00:00 2001 From: Jake Aitchison Date: Mon, 16 Nov 2026 11:00:55 +0000 Subject: [PATCH 2/3] ci: harden artifact handling and verify codacy reporter download Download artifacts from untrusted pull request runs outside the workspace, read coverage files as data only and validate the head SHA before use. Replace the piped Codacy get.sh script with a pinned reporter release verified by SHA-256, and fix workflow_run if conditions that always evaluated to true because of a trailing newline. --- .github/workflows/build-status.yml | 37 +++++++++++---- .../workflows/publish-pr-coverage-results.yml | 46 ++++++++++++++----- .github/workflows/publish-pr-test-results.yml | 5 +- .../workflows/report-benchmark-results.yml | 5 +- 4 files changed, 68 insertions(+), 25 deletions(-) diff --git a/.github/workflows/build-status.yml b/.github/workflows/build-status.yml index 616512e4e..4724401d4 100644 --- a/.github/workflows/build-status.yml +++ b/.github/workflows/build-status.yml @@ -84,21 +84,37 @@ jobs: uses: actions/download-artifact@v8 with: name: coverage-reports + path: ${{ runner.temp }}/artifacts - - name: Set Coverage Report Paths - id: coverage-paths + - name: Download Codacy Coverage Reporter + env: + CODACY_REPORTER_VERSION: 14.1.3 + CODACY_REPORTER_SHA256: 15c5f052207d27b8501ab5d5910c68c206ea70b0157ee1725132780530580875 run: | - readarray -d '' coveragePathArray < <(find . -name "coverage.cobertura.xml" -print0) - coveragePaths=$(printf -- "-r %s " "${coveragePathArray[@]}") - echo "COVERAGE_REPORT_PATHS=$coveragePaths" >> $GITHUB_OUTPUT + reporter="$RUNNER_TEMP/codacy/codacy-coverage-reporter" + mkdir -p "$(dirname "$reporter")" + curl -fsSL --retry 3 -o "$reporter" \ + "https://github.com/codacy/codacy-coverage-reporter/releases/download/${CODACY_REPORTER_VERSION}/codacy-coverage-reporter-linux" + echo "${CODACY_REPORTER_SHA256} ${reporter}" | sha256sum --check --strict + chmod +x "$reporter" - name: Publish Code Coverage Results + env: + CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }} run: | - auth="--project-token ${{ secrets.CODACY_PROJECT_TOKEN }}" + reports=() + while IFS= read -r -d '' file; do + reports+=(-r "$file") + done < <(find "$RUNNER_TEMP/artifacts" -type f -name "coverage.cobertura.xml" -print0) - bash <(curl -Ls https://coverage.codacy.com/get.sh) report $auth \ - ${{ steps.coverage-paths.outputs.COVERAGE_REPORT_PATHS }} --partial &&\ - bash <(curl -Ls https://coverage.codacy.com/get.sh) final $auth + if [ ${#reports[@]} -eq 0 ]; then + echo "::error::No coverage reports found in artifact" + exit 1 + fi + + reporter="$RUNNER_TEMP/codacy/codacy-coverage-reporter" + "$reporter" report "${reports[@]}" --partial &&\ + "$reporter" final report-benchmark-results: needs: build-and-test @@ -111,6 +127,7 @@ jobs: uses: actions/download-artifact@v8 with: name: benchmark-results + path: ${{ runner.temp }}/artifacts - name: 'Report benchmark result' - run: cat ./NHapi.Benchmarks.LargeEmbeddedFileTest-report-github.md >> $GITHUB_STEP_SUMMARY + run: cat "$RUNNER_TEMP/artifacts/NHapi.Benchmarks.LargeEmbeddedFileTest-report-github.md" >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/publish-pr-coverage-results.yml b/.github/workflows/publish-pr-coverage-results.yml index 0e7b20e6b..de1ea7665 100644 --- a/.github/workflows/publish-pr-coverage-results.yml +++ b/.github/workflows/publish-pr-coverage-results.yml @@ -16,31 +16,55 @@ jobs: permissions: actions: read # the build-and-test job might be skipped, we don't need to run this job then - if: > + if: >- ${{ github.event.workflow_run.event == 'pull_request' && ( github.event.workflow_run.conclusion == 'success' || github.event.workflow_run.conclusion == 'failure' ) }} steps: + # The artifact comes from an untrusted pull request build. It is downloaded + # outside the workspace, nothing in it is ever executed, and only regular + # coverage.cobertura.xml files are read from it as data. - name: 'Download artifact' uses: actions/download-artifact@v8 with: name: coverage-reports + path: ${{ runner.temp }}/artifacts github-token: ${{ secrets.GITHUB_TOKEN }} run-id: ${{ github.event.workflow_run.id }} - - name: Set Coverage Report Paths - id: coverage-paths + - name: Download Codacy Coverage Reporter + env: + CODACY_REPORTER_VERSION: 14.1.3 + CODACY_REPORTER_SHA256: 15c5f052207d27b8501ab5d5910c68c206ea70b0157ee1725132780530580875 run: | - readarray -d '' coveragePathArray < <(find . -name "coverage.cobertura.xml" -print0) - coveragePaths=$(printf -- "-r %s " "${coveragePathArray[@]}") - echo "COVERAGE_REPORT_PATHS=$coveragePaths" >> $GITHUB_OUTPUT + reporter="$RUNNER_TEMP/codacy/codacy-coverage-reporter" + mkdir -p "$(dirname "$reporter")" + curl -fsSL --retry 3 -o "$reporter" \ + "https://github.com/codacy/codacy-coverage-reporter/releases/download/${CODACY_REPORTER_VERSION}/codacy-coverage-reporter-linux" + echo "${CODACY_REPORTER_SHA256} ${reporter}" | sha256sum --check --strict + chmod +x "$reporter" - name: Publish Code Coverage Results + env: + CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }} + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} run: | - auth="--project-token ${{ secrets.CODACY_PROJECT_TOKEN }}" + if [[ ! "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then + echo "::error::Unexpected head SHA format" + exit 1 + fi - bash <(curl -Ls https://coverage.codacy.com/get.sh) report $auth \ - --commit-uuid ${{ github.event.workflow_run.head_sha }} \ - ${{ steps.coverage-paths.outputs.COVERAGE_REPORT_PATHS }} --partial &&\ - bash <(curl -Ls https://coverage.codacy.com/get.sh) final $auth + reports=() + while IFS= read -r -d '' file; do + reports+=(-r "$file") + done < <(find "$RUNNER_TEMP/artifacts" -type f -name "coverage.cobertura.xml" -print0) + + if [ ${#reports[@]} -eq 0 ]; then + echo "::error::No coverage reports found in artifact" + exit 1 + fi + + reporter="$RUNNER_TEMP/codacy/codacy-coverage-reporter" + "$reporter" report --commit-uuid "$HEAD_SHA" "${reports[@]}" --partial &&\ + "$reporter" final diff --git a/.github/workflows/publish-pr-test-results.yml b/.github/workflows/publish-pr-test-results.yml index 45fe516a3..79dcec120 100644 --- a/.github/workflows/publish-pr-test-results.yml +++ b/.github/workflows/publish-pr-test-results.yml @@ -18,7 +18,7 @@ jobs: checks: write pull-requests: write # the build-and-test job might be skipped, we don't need to run this job then - if: > + if: >- ${{ github.event.workflow_run.event == 'pull_request' && ( github.event.workflow_run.conclusion == 'success' || github.event.workflow_run.conclusion == 'failure' ) }} @@ -28,6 +28,7 @@ jobs: uses: actions/download-artifact@v8 with: name: unit-test-results + path: artifacts/unit-test-results github-token: ${{ secrets.GITHUB_TOKEN }} run-id: ${{ github.event.workflow_run.id }} @@ -37,6 +38,6 @@ jobs: check_name: Unit Test Results github_token: ${{ secrets.GITHUB_TOKEN }} commit: ${{ github.event.workflow_run.head_sha }} - trx_files: "**/*.trx" + trx_files: "artifacts/unit-test-results/**/*.trx" report_individual_runs: true check_run_annotations: all tests, skipped tests diff --git a/.github/workflows/report-benchmark-results.yml b/.github/workflows/report-benchmark-results.yml index b9e9b735a..a5801a5b6 100644 --- a/.github/workflows/report-benchmark-results.yml +++ b/.github/workflows/report-benchmark-results.yml @@ -16,7 +16,7 @@ jobs: permissions: actions: read # the build-and-test job might be skipped, we don't need to run this job then - if: > + if: >- ${{ github.event.workflow_run.event == 'pull_request' && ( github.event.workflow_run.conclusion == 'success' || github.event.workflow_run.conclusion == 'failure' ) }} @@ -25,8 +25,9 @@ jobs: uses: actions/download-artifact@v8 with: name: benchmark-results + path: ${{ runner.temp }}/artifacts github-token: ${{ secrets.GITHUB_TOKEN }} run-id: ${{ github.event.workflow_run.id }} - name: 'Report benchmark result' - run: cat ./NHapi.Benchmarks.LargeEmbeddedFileTest-report-github.md >> $GITHUB_STEP_SUMMARY + run: cat "$RUNNER_TEMP/artifacts/NHapi.Benchmarks.LargeEmbeddedFileTest-report-github.md" >> "$GITHUB_STEP_SUMMARY" From 542afff4c449190b3c43fb57aa9d542bda9bde4b Mon Sep 17 00:00:00 2001 From: Jake Aitchison Date: Mon, 16 Nov 2026 12:23:43 +0000 Subject: [PATCH 3/3] ci: restrict codacy reporter download to https --- .github/workflows/build-status.yml | 2 +- .github/workflows/publish-pr-coverage-results.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-status.yml b/.github/workflows/build-status.yml index 4724401d4..de9d38b3b 100644 --- a/.github/workflows/build-status.yml +++ b/.github/workflows/build-status.yml @@ -93,7 +93,7 @@ jobs: run: | reporter="$RUNNER_TEMP/codacy/codacy-coverage-reporter" mkdir -p "$(dirname "$reporter")" - curl -fsSL --retry 3 -o "$reporter" \ + curl --proto "=https" --proto-redir "=https" --tlsv1.2 -fsSL --retry 3 -o "$reporter" \ "https://github.com/codacy/codacy-coverage-reporter/releases/download/${CODACY_REPORTER_VERSION}/codacy-coverage-reporter-linux" echo "${CODACY_REPORTER_SHA256} ${reporter}" | sha256sum --check --strict chmod +x "$reporter" diff --git a/.github/workflows/publish-pr-coverage-results.yml b/.github/workflows/publish-pr-coverage-results.yml index de1ea7665..348ecc796 100644 --- a/.github/workflows/publish-pr-coverage-results.yml +++ b/.github/workflows/publish-pr-coverage-results.yml @@ -40,7 +40,7 @@ jobs: run: | reporter="$RUNNER_TEMP/codacy/codacy-coverage-reporter" mkdir -p "$(dirname "$reporter")" - curl -fsSL --retry 3 -o "$reporter" \ + curl --proto "=https" --proto-redir "=https" --tlsv1.2 -fsSL --retry 3 -o "$reporter" \ "https://github.com/codacy/codacy-coverage-reporter/releases/download/${CODACY_REPORTER_VERSION}/codacy-coverage-reporter-linux" echo "${CODACY_REPORTER_SHA256} ${reporter}" | sha256sum --check --strict chmod +x "$reporter"