From c4472615ab384820525d78eb8cff0bc570993631 Mon Sep 17 00:00:00 2001 From: Eric Minish Date: Wed, 18 Nov 2026 10:05:17 -0500 Subject: [PATCH] ci: add security scan (gitleaks, osv-scanner, zizmor); fix workflow findings --- .github/workflows/release.yml | 16 +++-- .github/workflows/security.yml | 105 +++++++++++++++++++++++++++++++++ .github/workflows/test.yml | 8 +++ 3 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/security.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e9069a5..9257edd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -33,17 +33,19 @@ on: push: tags: ["v*"] +# Write access is granted per job, only where it is used. permissions: - contents: write # create the tag and the GitHub Release - id-token: write # PyPI trusted publishing (OIDC) + contents: read jobs: version: runs-on: ubuntu-latest + permissions: + contents: write # create and push the tag outputs: tag: ${{ steps.pick.outputs.tag }} steps: - - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 # zizmor: ignore[artipacked] -- this job pushes a tag with the checkout's token with: fetch-depth: 0 # tag-derived versioning needs full history @@ -111,6 +113,9 @@ jobs: release: needs: version + permissions: + contents: write # create the GitHub Release + id-token: write # PyPI trusted publishing (OIDC) if: needs.version.outputs.tag != '' runs-on: ubuntu-latest steps: @@ -118,8 +123,11 @@ jobs: with: ref: ${{ needs.version.outputs.tag }} fetch-depth: 0 + persist-credentials: false - uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2 + with: + enable-cache: false - run: uv build - name: Assert built version matches the tag @@ -141,7 +149,7 @@ jobs: # owner/repo/workflow. Runs BEFORE the GitHub Release: a PyPI upload # cannot be undone (a version may be yanked, never re-uploaded), so if it # is going to fail, fail before announcing anything. - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2 + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 - name: GitHub Release env: diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml new file mode 100644 index 0000000..c427a58 --- /dev/null +++ b/.github/workflows/security.yml @@ -0,0 +1,105 @@ +# Security checks: gitleaks (secrets in the commits this event added), +# osv-scanner (known-vulnerable dependencies) and zizmor (workflow security). +# The scanners are sha256-verified release binaries, not marketplace actions. +# Per-repo tuning goes in each tool's own config: .gitleaks.toml, +# osv-scanner.toml (next to the lockfile), .github/zizmor.yml. + +name: security + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +concurrency: + # Cancel superseded PR runs only: a cancelled push run leaves its commits unscanned. + group: security-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + scan: + name: gitleaks / osv-scanner / zizmor + runs-on: ubuntu-24.04 + timeout-minutes: 15 + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Install scanners (sha256-verified) + run: | + set -euo pipefail + bin="$RUNNER_TEMP/secbin"; mkdir -p "$bin"; cd "$RUNNER_TEMP" + fetch() { curl -fsSL --retry 3 -o "$3" "$1"; echo "$2 $3" | sha256sum -c --quiet -; } + fetch https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz \ + 551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb gitleaks.tgz + fetch https://github.com/google/osv-scanner/releases/download/v2.7.0/osv-scanner_linux_amd64 \ + 160c6253e2922e9e0a6a9771c5c4c40a4b3c6090b34a3cdd5e80c96cc2e4218b osv-scanner + fetch https://github.com/zizmorcore/zizmor/releases/download/v1.32.0/zizmor-x86_64-unknown-linux-gnu.tar.gz \ + bb110cc765eca9cd7efb2e284a956be5974c60027b0970f16397d86208a9957b zizmor.tgz + tar -xzf gitleaks.tgz -C "$bin" gitleaks + tar -xzf zizmor.tgz -C "$bin" zizmor + install -m 0755 osv-scanner "$bin/osv-scanner" + rm -f gitleaks.tgz zizmor.tgz osv-scanner + echo "$bin" >> "$GITHUB_PATH" + + - name: gitleaks (commits added by this event) + if: ${{ !cancelled() }} + env: + EVENT: ${{ github.event_name }} + PR_BASE: ${{ github.event.pull_request.base.sha }} + PR_HEAD: ${{ github.event.pull_request.head.sha }} + PR_BASE_REF: ${{ github.base_ref }} + BEFORE: ${{ github.event.before }} + AFTER: ${{ github.sha }} + # gitleaks reports "0 commits scanned, no leaks" (exit 0) on an unknown + # revision, so every range endpoint is checked to exist first. + run: | + set -euo pipefail + have() { git cat-file -e "$1^{commit}" 2>/dev/null; } + range="" + if [ "$EVENT" = pull_request ] && have "$PR_HEAD"; then + if have "$PR_BASE"; then + range="$PR_BASE..$PR_HEAD" + elif mb=$(git merge-base "$PR_HEAD" "origin/$PR_BASE_REF" 2>/dev/null); then + range="$mb..$PR_HEAD" # base branch was force-pushed after the PR opened + fi + elif [ -n "$BEFORE" ] && [ "$BEFORE" != 0000000000000000000000000000000000000000 ] \ + && git cat-file -e "$BEFORE^{commit}" 2>/dev/null; then + range="$BEFORE..$AFTER" + fi + if [ -n "$range" ]; then + echo "scanning commits $range" + gitleaks git --no-banner --redact --exit-code 1 --log-opts="$range" . + else + echo "no usable commit range (new branch or force-push); scanning the working tree" + gitleaks dir --no-banner --redact --exit-code 1 . + fi + + - name: osv-scanner (dependencies) + if: ${{ !cancelled() }} + run: | + set +e + osv-scanner scan source -r . + rc=$? + # 127 = general error, typically transitive resolution of a manifest + # failing (e.g. a `torch==X+cpu` local-version pin that deps.dev cannot + # resolve). Retry with only the declared/locked packages, and say so. + if [ "$rc" -eq 127 ]; then + echo "::warning title=osv-scanner::transitive resolution failed; rescanning with --no-resolve (manifest-only deps are not expanded)" + osv-scanner scan source -r --no-resolve . + rc=$? + fi + # 128 = no lockfiles/manifests found: nothing to scan is not a failure. + if [ "$rc" -eq 128 ]; then echo "osv-scanner: no packages found"; exit 0; fi + exit "$rc" + + - name: zizmor (workflow security) + if: ${{ !cancelled() }} + env: + GH_TOKEN: ${{ github.token }} + run: zizmor --format=github .github/workflows diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 427c40a..85de270 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,6 +12,9 @@ on: pull_request: branches: [main] +permissions: + contents: read + jobs: unittest: runs-on: ubuntu-latest @@ -28,6 +31,8 @@ jobs: # below were each verified to resolve to a COMMIT and not an annotated # tag object, which is its own object and does not work as a pin. - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 + with: + persist-credentials: false - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: ${{ matrix.python-version }} @@ -40,6 +45,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 + with: + persist-credentials: false - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0 with: python-version: "3.12" @@ -60,6 +67,7 @@ jobs: steps: - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 with: + persist-credentials: false fetch-depth: 0 # hatch-vcs derives the version from tags - uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2 - run: uv build