From 343bd1b5b631e6c66afba3f529ba51af5a6ab36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Rodr=C3=ADguez?= <127134616+armando-rodriguez-cko@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:22:13 +0100 Subject: [PATCH] fix: security hardening Security hardening of the CI workflows. The release is split so that the job that publishes to npm only checks and publishes the tarball built and tested by the earlier jobs. Actions are pinned by commit SHA on current versions and kept up to date by Dependabot, and the integration test secrets reach only the test steps. --- .github/dependabot.yml | 7 + .github/workflows/build-master.yml | 18 ++- .github/workflows/build-pull-request.yml | 18 ++- .github/workflows/build-release.yml | 176 +++++++++++++++++++---- .github/workflows/codeql-analysis.yml | 62 -------- 5 files changed, 175 insertions(+), 106 deletions(-) create mode 100644 .github/dependabot.yml delete mode 100644 .github/workflows/codeql-analysis.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 00000000..81061f4b --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +version: 2 +updates: + # Keeps the actions pinned by commit SHA in .github/workflows up to date. + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly diff --git a/.github/workflows/build-master.yml b/.github/workflows/build-master.yml index a619112d..9b67648b 100644 --- a/.github/workflows/build-master.yml +++ b/.github/workflows/build-master.yml @@ -13,13 +13,18 @@ jobs: if: "!contains(github.event.commits[0].message, 'Release')" runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - id: setup-node - uses: actions/setup-node@v2.5.1 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 18 + package-manager-cache: false registry-url: https://registry.npmjs.org/ - - id: build-and-analyse + - id: install + run: npm ci --ignore-scripts + - id: test env: CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }} CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }} @@ -34,7 +39,6 @@ jobs: CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET }} CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID }} CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET }} - run: | - npm ci --ignore-scripts - npm test - npm run build \ No newline at end of file + run: npm test + - id: build + run: npm run build \ No newline at end of file diff --git a/.github/workflows/build-pull-request.yml b/.github/workflows/build-pull-request.yml index 64513680..09135010 100644 --- a/.github/workflows/build-pull-request.yml +++ b/.github/workflows/build-pull-request.yml @@ -12,13 +12,18 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - id: setup-node - uses: actions/setup-node@v2.5.1 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 18 + package-manager-cache: false registry-url: https://registry.npmjs.org/ - - id: build-and-analyse + - id: install + run: npm ci --ignore-scripts + - id: test env: CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }} CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }} @@ -33,8 +38,7 @@ jobs: CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET }} CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID }} CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET }} - run: | - npm ci --ignore-scripts - npm test - npm run build + run: npm test + - id: build + run: npm run build diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 94d2dc41..8f8a7608 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -7,26 +7,30 @@ on: paths: - package.json +permissions: + contents: read + +# One release at a time; a second run waits instead of starting in parallel. +concurrency: + group: build-release + cancel-in-progress: false + jobs: - deploy: + # Unit and integration tests; the only job with the integration test secrets. + test: runs-on: ubuntu-latest - # Must match the environment configured on the npm trusted publisher - environment: production - permissions: - contents: write # create the GitHub release - id-token: write # npm trusted publishing (OIDC) steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - id: setup-node - uses: actions/setup-node@v4 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 22 - registry-url: https://registry.npmjs.org/ - - id: upgrade-npm - # Trusted publishing needs npm >= 11.5.1; Node 22 bundles npm 10. - # Pinned so the release job is reproducible. - run: npm install -g npm@11.21.0 --ignore-scripts && npm --version - - id: build-and-analyse + package-manager-cache: false + - id: install + run: npm ci --ignore-scripts + - id: test env: CHECKOUT_PROCESSING_CHANNEL_ID: ${{ secrets.IT_CHECKOUT_PROCESSING_CHANNEL_ID }} CHECKOUT_PREVIOUS_SECRET_KEY: ${{ secrets.IT_CHECKOUT_PREVIOUS_SECRET_KEY }} @@ -41,27 +45,139 @@ jobs: CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ACCOUNTS_CLIENT_SECRET }} CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_ID }} CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET: ${{ secrets.IT_CHECKOUT_DEFAULT_OAUTH_ISSUING_CLIENT_SECRET }} + run: npm test + + # Builds and packs the package after the tests pass; no secrets. + build: + needs: test + runs-on: ubuntu-latest + outputs: + version: ${{ steps.version.outputs.version }} + tarball: ${{ steps.pack.outputs.tarball }} + sha256: ${{ steps.pack.outputs.sha256 }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + # Read before the install, as checked out. + - id: version run: | - npm ci - npm test - npm run build + set -euo pipefail + version=$(jq -r .version package.json) + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "::error::package.json version is not a release version (x.y.z)" + exit 1 + fi + echo "version=$version" >> "$GITHUB_OUTPUT" + - id: setup-node + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 22 + package-manager-cache: false + - id: install + run: npm ci --ignore-scripts + - id: build + run: npm run build - id: rsync run: rsync -avh --progress ./types/ ./ - - id: read-version - run: echo "CURRENT_VERSION=$( awk -F'"' '/version/ {print $4}' package.json )" >> $GITHUB_ENV - - id: print-version - run: echo "Releasing $CURRENT_VERSION" + - id: pack + env: + VERSION: ${{ steps.version.outputs.version }} + run: | + set -euo pipefail + version="$VERSION" + mkdir -p "$RUNNER_TEMP/package" + npm pack --ignore-scripts --pack-destination "$RUNNER_TEMP/package" + tarball="checkout-sdk-node-$version.tgz" + test -f "$RUNNER_TEMP/package/$tarball" || { echo "::error::npm pack did not produce $tarball"; exit 1; } + sha256=$(sha256sum "$RUNNER_TEMP/package/$tarball" | cut -d' ' -f1) + { + echo "tarball=$tarball" + echo "sha256=$sha256" + } >> "$GITHUB_OUTPUT" + echo "Releasing $version ($tarball, sha256 $sha256)" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: package + path: ${{ runner.temp }}/package/${{ steps.pack.outputs.tarball }} + if-no-files-found: error + retention-days: 7 + + # Publishes the tarball built above. + publish: + needs: build + runs-on: ubuntu-latest + # Must match the environment configured on the npm trusted publisher + environment: production + permissions: + id-token: write # npm trusted publishing (OIDC) + steps: + - id: setup-node + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 22 + package-manager-cache: false + registry-url: https://registry.npmjs.org/ + - id: upgrade-npm + # Trusted publishing needs npm >= 11.5.1; Node 22 bundles npm 10. + # Pinned by version and by the registry's sha512 integrity of that version. + env: + NPM_INTEGRITY: sha512-Zov8KhamNneiLdELtj5YALtNmJW4L4fCLTzjfpzXG2w6MSHcf0UxgdlK5uloCuksWT+7mGUU7wi79cO6RqivPg== + run: | + set -euo pipefail + cd "$(mktemp -d)" + curl -fsSL --proto '=https' -o npm-11.21.0.tgz https://registry.npmjs.org/npm/-/npm-11.21.0.tgz + test "sha512-$(openssl dgst -sha512 -binary npm-11.21.0.tgz | base64 -w0)" = "$NPM_INTEGRITY" \ + || { echo "::error::npm 11.21.0 download does not match its integrity"; exit 1; } + npm install -g ./npm-11.21.0.tgz --ignore-scripts + npm --version + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: package + path: ${{ runner.temp }}/package + - id: verify + env: + VERSION: ${{ needs.build.outputs.version }} + TARBALL: ${{ needs.build.outputs.tarball }} + SHA256: ${{ needs.build.outputs.sha256 }} + run: | + set -euo pipefail + fail() { echo "::error::Package check failed: $1"; exit 1; } + cd "$RUNNER_TEMP/package" + [ "$TARBALL" = "checkout-sdk-node-$VERSION.tgz" ] || fail "file name" + [ "$(find . -type f | wc -l)" -eq 1 ] || fail "file count" + echo "$SHA256 $TARBALL" | sha256sum -c - || fail "sha256" + # Every entry a regular file under package/ with a plain path, one manifest. + tar --ignore-zeros -tzf "$TARBALL" > "$RUNNER_TEMP/entries" 2> "$RUNNER_TEMP/tar-errors" || fail "archive" + tar --ignore-zeros -tvzf "$TARBALL" > "$RUNNER_TEMP/entries-long" 2>> "$RUNNER_TEMP/tar-errors" || fail "archive" + [ ! -s "$RUNNER_TEMP/tar-errors" ] || fail "archive warnings" + # Counts read from the files, no pipe: anything but 0 (an error included) refuses. + [ "$(grep -c -v -E '^package/[^/]' "$RUNNER_TEMP/entries" || true)" = 0 ] || fail "entry outside package/" + [ "$(grep -c -E '(^|/)\.{1,2}(/|$)|//' "$RUNNER_TEMP/entries" || true)" = 0 ] || fail "entry path" + [ "$(grep -c -v '^-' "$RUNNER_TEMP/entries-long" || true)" = 0 ] || fail "entry type" + [ "$(grep -c -x 'package/package.json' "$RUNNER_TEMP/entries")" -eq 1 ] || fail "manifest count" + tar --ignore-zeros -xzOf "$TARBALL" package/package.json | jq -e 'has("publishConfig") | not' > /dev/null || fail "publishConfig" + # Name, version, registry and tag as npm reads the file. + npm publish "$TARBALL" --dry-run --json --access public > "$RUNNER_TEMP/dry-run.json" 2> "$RUNNER_TEMP/dry-run.log" || fail "npm dry run" + [ "$(jq -r 'keys | join(",")' "$RUNNER_TEMP/dry-run.json")" = "checkout-sdk-node" ] || fail "package name" + [ "$(jq -r '.["checkout-sdk-node"].id' "$RUNNER_TEMP/dry-run.json")" = "checkout-sdk-node@$VERSION" ] || fail "package version" + grep -q -x 'npm notice Publishing to https://registry.npmjs.org/ with tag latest and public access (dry-run)' "$RUNNER_TEMP/dry-run.log" || fail "registry or tag" - id: publish-to-npm # No token: npm exchanges the GitHub OIDC token with the registry # and publishes with provenance. - run: npm publish --provenance --access public + env: + VERSION: ${{ needs.build.outputs.version }} + run: npm publish "$RUNNER_TEMP/package/checkout-sdk-node-$VERSION.tgz" --provenance --access public + + github-release: + needs: [build, publish] + runs-on: ubuntu-latest + permissions: + contents: write # create the GitHub release + steps: - id: create-release - uses: actions/create-release@v1 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ env.CURRENT_VERSION }} - release_name: ${{ env.CURRENT_VERSION }} - body: ${{ github.event.head_commit.message }} - draft: false - prerelease: false \ No newline at end of file + GH_TOKEN: ${{ github.token }} + VERSION: ${{ needs.build.outputs.version }} + NOTES: ${{ github.event.head_commit.message }} + run: gh release create "$VERSION" --repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --title "$VERSION" --notes "$NOTES" diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml deleted file mode 100644 index 51689dc7..00000000 --- a/.github/workflows/codeql-analysis.yml +++ /dev/null @@ -1,62 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL" - -on: - push: - branches: [ master ] - pull_request: - # The branches below must be a subset of the branches above - branches: [ master ] - schedule: - - cron: '34 2 * * 0' - -jobs: - analyze: - name: Analyze - runs-on: ubuntu-latest - permissions: - actions: read - contents: read - security-events: write - - strategy: - fail-fast: false - matrix: - language: [ 'javascript' ] - # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] - # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support - - steps: - - name: Checkout repository - uses: actions/checkout@v3 - with: - submodules: recursive - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v3 - with: - languages: ${{ matrix.language }} - queries: security-and-quality - - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - - # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs - # queries: security-extended,security-and-quality - - name: Autobuild - uses: github/codeql-action/autobuild@v3 - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3