From 14f8c2b33b74475382b1304981534c1b8f25606a Mon Sep 17 00:00:00 2001 From: Ryan Bright Date: Sun, 29 Nov 2026 05:25:50 -0700 Subject: [PATCH] ci(github): add full-history gitleaks scans - Move the gitleaks pin to the setup-gitleaks action and add the gitleaks-history workflow. - A weekly cron, workflow_dispatch, and a change to .gitleaks.toml or the pin run the full scan. - The range scan of Repository checks also falls back to the full history on a pin change. --- .github/actions/setup-gitleaks/action.yml | 25 ++++++++++++ .github/workflows/gitleaks-history.yml | 50 +++++++++++++++++++++++ .github/workflows/repository-checks.yml | 44 +++++++++++++------- 3 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 .github/actions/setup-gitleaks/action.yml create mode 100644 .github/workflows/gitleaks-history.yml diff --git a/.github/actions/setup-gitleaks/action.yml b/.github/actions/setup-gitleaks/action.yml new file mode 100644 index 0000000..d80202c --- /dev/null +++ b/.github/actions/setup-gitleaks/action.yml @@ -0,0 +1,25 @@ +# Installs the pinned gitleaks release and adds it to the PATH. This file is +# the only pin of the gitleaks version, so a change to it makes the Gitleaks +# step of repository-checks.yml scan the full history. +name: Set up gitleaks +description: Install the pinned gitleaks release after a SHA-256 check. + +runs: + using: composite + steps: + - name: Set up gitleaks + shell: bash + env: + GITLEAKS_VERSION: 8.30.1 + # SHA-256 of gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz. Update it with GITLEAKS_VERSION. + GITLEAKS_SHA256: 551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb + # The callers run on pull_request, push, schedule, and workflow_dispatch, + # and the PATH entry is a fixed runner directory, so the write is safe. + run: | # zizmor: ignore[github-env] + archive="gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" + curl --fail --silent --show-error --location --output "$RUNNER_TEMP/$archive" \ + "https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/$archive" + echo "$GITLEAKS_SHA256 $RUNNER_TEMP/$archive" | sha256sum --check --strict + mkdir -p "$RUNNER_TEMP/bin" + tar -xzf "$RUNNER_TEMP/$archive" -C "$RUNNER_TEMP/bin" gitleaks + echo "$RUNNER_TEMP/bin" >>"$GITHUB_PATH" diff --git a/.github/workflows/gitleaks-history.yml b/.github/workflows/gitleaks-history.yml new file mode 100644 index 0000000..ca96163 --- /dev/null +++ b/.github/workflows/gitleaks-history.yml @@ -0,0 +1,50 @@ +# Scans the full Git history for secrets once a week, on demand, and when a +# pull request or a push to main changes .gitleaks.toml or the gitleaks pin +# in .github/actions/setup-gitleaks/action.yml. The Gitleaks step of +# repository-checks.yml scans a commit range in most runs, so this scan checks +# each commit that HEAD reaches with the pinned rules. +name: gitleaks-history + +on: + schedule: + # Each Monday at 06:17 UTC. + - cron: "17 6 * * 1" + workflow_dispatch: + pull_request: + branches: + - main + paths: + - .gitleaks.toml + - .github/actions/setup-gitleaks/action.yml + push: + branches: + - main + paths: + - .gitleaks.toml + - .github/actions/setup-gitleaks/action.yml + +permissions: + contents: read + +jobs: + gitleaks-history: + name: gitleaks-history + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + # gitleaks scans every commit that HEAD reaches, so the job needs the full history. + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - uses: ./.github/actions/setup-gitleaks + + # gitleaks reads .gitleaks.toml when the file exists, and its default + # rules when not. The repository is public, so --redact keeps each + # secret out of the log. + - name: Gitleaks + run: gitleaks git --no-banner --redact --verbose --log-opts=HEAD . diff --git a/.github/workflows/repository-checks.yml b/.github/workflows/repository-checks.yml index 61faf7c..b1e46cc 100644 --- a/.github/workflows/repository-checks.yml +++ b/.github/workflows/repository-checks.yml @@ -25,11 +25,11 @@ jobs: fetch-depth: 0 persist-credentials: false - # Each tool is a release binary, pinned by its version and SHA-256. - - name: Install Gitleaks and actionlint + - uses: ./.github/actions/setup-gitleaks + + # actionlint is a release binary, pinned by its version and SHA-256. + - name: Install actionlint env: - GITLEAKS_VERSION: 8.30.1 - GITLEAKS_SHA256: 551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb ACTIONLINT_VERSION: 1.7.12 ACTIONLINT_SHA256: 8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8 run: | @@ -37,11 +37,6 @@ jobs: mkdir -p "$bin" cd "$RUNNER_TEMP" - gitleaks_archive="gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" - curl -fsSLO "https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/${gitleaks_archive}" - echo "${GITLEAKS_SHA256} ${gitleaks_archive}" | sha256sum --check - tar -xzf "$gitleaks_archive" -C "$bin" gitleaks - actionlint_archive="actionlint_${ACTIONLINT_VERSION}_linux_amd64.tar.gz" curl -fsSLO "https://github.com/rhysd/actionlint/releases/download/v${ACTIONLINT_VERSION}/${actionlint_archive}" echo "${ACTIONLINT_SHA256} ${actionlint_archive}" | sha256sum --check @@ -50,21 +45,40 @@ jobs: echo "$bin" >> "$GITHUB_PATH" # A pull request scans its own commits (base..head), and a push to main - # scans the pushed commits (before..after). When the base commit is not - # in the clone (a zero SHA for a new branch, or a force push), the scan - # reads the full history that the head reaches. + # scans the pushed commits (before..after). Earlier runs scanned the + # older commits, and the weekly workflow gitleaks-history scans the full + # history. The step scans the full history that HEAD reaches when the + # base commit is not in the clone (a zero SHA for a new branch, or a + # force push), or when the range changes .gitleaks.toml or + # .github/actions/setup-gitleaks/action.yml. - name: Gitleaks env: + EVENT_NAME: ${{ github.event_name }} GITLEAKS_BASE: ${{ github.event.pull_request.base.sha || github.event.before }} GITLEAKS_HEAD: ${{ github.event.pull_request.head.sha || github.event.after }} run: | - log_opts="$GITLEAKS_BASE..$GITLEAKS_HEAD" + # A pull request counts only its own changes, from the merge base. + changes="$GITLEAKS_BASE..$GITLEAKS_HEAD" + if [ "$EVENT_NAME" = pull_request ]; then + changes="$GITLEAKS_BASE...$GITLEAKS_HEAD" + fi + + log_opts=HEAD if ! git cat-file -e "$GITLEAKS_BASE^{commit}" 2>/dev/null; then - log_opts="$GITLEAKS_HEAD" + echo "The clone does not hold the base commit: scan the full history." + else + changed="$(git diff --name-only "$changes" -- .gitleaks.toml .github/actions/setup-gitleaks/action.yml)" + if [ -n "$changed" ]; then + echo "$changes changes the gitleaks rules or version: scan the full history." + else + log_opts="$GITLEAKS_BASE..$GITLEAKS_HEAD" + fi fi echo "Gitleaks: --log-opts=$log_opts" - # The repository is public, so --redact keeps each secret out of the log. + # gitleaks reads .gitleaks.toml when the file exists, and its default + # rules when not. The repository is public, so --redact keeps each + # secret out of the log. gitleaks git --no-banner --redact --verbose --log-opts="$log_opts" . - name: Markdown lint