|
| 1 | +# .github/workflows/secret-scan.yml |
| 2 | +# |
| 3 | +# Copyright © 2025 Network Pro Strategies (Network Pro™) |
| 4 | +# SPDX-License-Identifier: CC-BY-4.0 OR GPL-3.0-or-later |
| 5 | +# This file is part of Network Pro |
| 6 | + |
| 7 | +name: Secret Scan (Gitleaks) |
| 8 | + |
| 9 | +on: |
| 10 | + pull_request: |
| 11 | + schedule: |
| 12 | + - cron: '0 8 * * *' # nightly scan at 8 AM UTC |
| 13 | + workflow_dispatch: |
| 14 | + |
| 15 | +jobs: |
| 16 | + gitleaks-scan: |
| 17 | + runs-on: ubuntu-24.04 |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + security-events: write |
| 21 | + steps: |
| 22 | + # --------------------------------------------------------------------- |
| 23 | + # Checkout the full repo history (needed for Gitleaks to scan all commits) |
| 24 | + # --------------------------------------------------------------------- |
| 25 | + - uses: actions/checkout@v5 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + # --------------------------------------------------------------------- |
| 30 | + # Run Gitleaks scan |
| 31 | + # Uses org license key (safe because GitHub never passes secrets to forks) |
| 32 | + # --------------------------------------------------------------------- |
| 33 | + - name: Run Gitleaks scan |
| 34 | + id: gitleaks |
| 35 | + uses: gitleaks/gitleaks-action@v2 |
| 36 | + env: |
| 37 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }} |
| 39 | + GITLEAKS_REPORT_PATH: gitleaks-report.json |
| 40 | + |
| 41 | + # --------------------------------------------------------------------- |
| 42 | + # LAYER 2: Secret-handling / fork guard |
| 43 | + # Upload artifact only if workflow runs in trusted context |
| 44 | + # (either not a PR, or a PR from the same repo) |
| 45 | + # --------------------------------------------------------------------- |
| 46 | + - name: Upload Gitleaks Report |
| 47 | + if: always() && (github.event.pull_request.head.repo.full_name == github.repository || github.event_name != 'pull_request') |
| 48 | + uses: actions/upload-artifact@v4 |
| 49 | + with: |
| 50 | + name: gitleaks-report |
| 51 | + path: gitleaks-report.json |
| 52 | + |
| 53 | + # --------------------------------------------------------------------- |
| 54 | + # LAYER 1: Output redaction |
| 55 | + # Public-safe summary – shows only secret descriptions, hides file paths. |
| 56 | + # --------------------------------------------------------------------- |
| 57 | + - name: Post Gitleaks summary |
| 58 | + if: always() |
| 59 | + run: | |
| 60 | + echo "### 🧩 Gitleaks Scan Summary" >> $GITHUB_STEP_SUMMARY |
| 61 | + if [ -s gitleaks-report.json ]; then |
| 62 | + count=$(jq '.findings | length' gitleaks-report.json) |
| 63 | + if [ "$count" -gt 0 ]; then |
| 64 | + echo "🚨 **$count potential secret$( [ "$count" -ne 1 ] && echo "s" ) detected!**" >> $GITHUB_STEP_SUMMARY |
| 65 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 66 | + # 🔒 Redacted output: no file paths or secret values |
| 67 | + jq -r '.findings[] | "- \(.Description) (at undisclosed path)"' gitleaks-report.json | head -n 10 >> $GITHUB_STEP_SUMMARY |
| 68 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 69 | + echo "_Full report available in Artifacts section (maintainers only)._ " >> $GITHUB_STEP_SUMMARY |
| 70 | + else |
| 71 | + echo "✅ No secrets detected." >> $GITHUB_STEP_SUMMARY |
| 72 | + fi |
| 73 | + else |
| 74 | + echo "⚠️ No report file found." >> $GITHUB_STEP_SUMMARY |
| 75 | + fi |
| 76 | +
|
| 77 | + # --------------------------------------------------------------------- |
| 78 | + # LAYER 2: Secret-handling / fork guard |
| 79 | + # Create issue only in trusted repo context (avoids using tokens on forks) |
| 80 | + # --------------------------------------------------------------------- |
| 81 | + - name: Create issue for detected secrets |
| 82 | + if: failure() && (github.event.pull_request.head.repo.full_name == github.repository || github.event_name != 'pull_request') |
| 83 | + uses: actions/github-script@v7 |
| 84 | + with: |
| 85 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 86 | + script: | |
| 87 | + const issueTitle = `🚨 Secret(s) detected in ${context.repo.repo}`; |
| 88 | + const issueBody = `Gitleaks found potential secrets in commit ${context.sha}.\n\nCheck workflow logs and artifacts for details.`; |
| 89 | + await github.rest.issues.create({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + title: issueTitle, |
| 93 | + body: issueBody, |
| 94 | + labels: ['security', 'gitleaks'] |
| 95 | + }); |
| 96 | +
|
| 97 | + # --------------------------------------------------------------------- |
| 98 | + # LAYER 2: Secret-handling / fork guard |
| 99 | + # Send ntfy alert only for trusted repo context. |
| 100 | + # --------------------------------------------------------------------- |
| 101 | + - name: Send ntfy notification |
| 102 | + if: failure() && (github.event.pull_request.head.repo.full_name == github.repository || github.event_name != 'pull_request') |
| 103 | + run: | |
| 104 | + curl -d "🚨 Gitleaks found secrets in repo: $GITHUB_REPOSITORY on commit $GITHUB_SHA" \ |
| 105 | + https://ntfy.neteng.pro/${{ secrets.NTFY_TOPIC }} |
0 commit comments