name: Secrets Analyzer on: pull_request: types: [opened, synchronize, reopened] push: branches: - master - main workflow_dispatch: jobs: scan: name: gitleaks runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 with: fetch-depth: 0 - name: Run Gitleaks Scan id: gitleaks-scan uses: gitleaks/gitleaks-action@v2 continue-on-error: true env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITLEAKS_CONFIG: .gitleaks.toml GITLEAKS_ENABLE_COMMENTS: true GITLEAKS_ENABLE_UPLOAD_ARTIFACT: false GITLEAKS_ENABLE_SUMMARY: true GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE}} - name: Extract Findings for Slack Notifications if: steps.gitleaks-scan.outcome == 'failure' id: extract-findings run: | # Extract and format findings from SARIF output if [ -f results.sarif ]; then # Generate formatted table for Slack (first 10 findings) FINDINGS_SLACK=$(cat results.sarif | jq -r ' ["File | Line | Secret Type", "------------------------|------|--------------------"] + [.runs[0].results[:10][] | "\(.locations[0].physicalLocation.artifactLocation.uri | .[0:24] | . + " " * (24 - length))| \(.locations[0].physicalLocation.region.startLine | tostring | . + " " * (5 - length))| \(.ruleId)"] | .[] ' | awk '{printf "%s\\n", $0}' | sed 's/\\n$//') if [ -z "$FINDINGS_SLACK" ]; then FINDINGS_SLACK="No findings to display" fi echo "findings_slack=$FINDINGS_SLACK" >> $GITHUB_OUTPUT # Extract fingerprints for copy-paste remediation (all findings) # Format: file:ruleId:line (matches Gitleaks .gitleaksignore format) FINGERPRINTS=$(cat results.sarif | jq -r ' .runs[0].results[] | "\(.locations[0].physicalLocation.artifactLocation.uri):\(.ruleId):\(.locations[0].physicalLocation.region.startLine)" ' | awk '{printf "%s\\n", $0}' | sed 's/\\n$//') if [ -z "$FINGERPRINTS" ]; then FINGERPRINTS="No fingerprints available. Check workflow logs." fi echo "fingerprints=$FINGERPRINTS" >> $GITHUB_OUTPUT # Count total findings COUNT=$(cat results.sarif | jq '.runs[0].results | length') echo "count=$COUNT" >> $GITHUB_OUTPUT else echo "findings_slack=Unable to parse findings. Check workflow logs." >> $GITHUB_OUTPUT echo "fingerprints=No report file found." >> $GITHUB_OUTPUT echo "count=unknown" >> $GITHUB_OUTPUT fi - name: Get PR Author Email if: steps.gitleaks-scan.outcome == 'failure' && github.event_name == 'pull_request' id: get-email run: | # Extract email from HEAD commit (works with fetch-depth: 1) EMAIL=$(git log -1 --format='%ae' HEAD) echo "email=$EMAIL" >> $GITHUB_OUTPUT - name: Lookup Slack User if: steps.gitleaks-scan.outcome == 'failure' && github.event_name == 'pull_request' id: slack-lookup uses: slackapi/slack-github-action@v2.1.1 continue-on-error: true with: method: users.lookupByEmail token: ${{ secrets.SLACK_BOT_TOKEN }} payload: | email: ${{ steps.get-email.outputs.email }} - name: Extract Slack User ID if: steps.gitleaks-scan.outcome == 'failure' && github.event_name == 'pull_request' && steps.slack-lookup.outputs.ok == 'true' run: | SLACK_USER_ID=$(echo '${{ steps.slack-lookup.outputs.response }}' | jq -r '.user.id') echo "SLACK_USER_ID=$SLACK_USER_ID" >> $GITHUB_ENV - name: Send DM to PR Author if: steps.gitleaks-scan.outcome == 'failure' && github.event_name == 'pull_request' && steps.slack-lookup.outputs.ok == 'true' id: send-dm uses: slackapi/slack-github-action@v2.1.1 continue-on-error: true with: method: chat.postMessage token: ${{ secrets.SLACK_BOT_TOKEN }} payload: | { "channel": "${{ env.SLACK_USER_ID }}", "text": "Security Alert: Secrets detected in your PR #${{ github.event.pull_request.number }}", "blocks": [ { "type": "header", "text": { "type": "plain_text", "text": "Security Alert: Secrets Detected in Your PR" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "Your PR contains *${{ steps.extract-findings.outputs.count }} potential secret(s)* and cannot be merged." } }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*PR:* <${{ github.event.pull_request.html_url }}|#${{ github.event.pull_request.number }}>" }, { "type": "mrkdwn", "text": "*Repo:* ${{ github.repository }}" } ] }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Detected Secrets:*\n```${{ steps.extract-findings.outputs.findings_slack }}```" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "*How to Fix (if these are false positives):*\n\n1. Copy the fingerprints below\n2. Add them to `.gitleaksignore` at the repo root\n3. Commit and push\n\n*Fingerprints to add:*\n```\n${{ steps.extract-findings.outputs.fingerprints }}\n```" } }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "If these are REAL secrets, remove them immediately | " } ] }, { "type": "actions", "block_id": "gitleaks_actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "Commit to .gitleaksignore" }, "action_id": "commit_gitleaks_fingerprints", "value": "${{ github.repository }}|${{ github.event.pull_request.number }}|${{ github.event.pull_request.head.ref }}|${{ github.event.pull_request.user.login }}", "style": "primary" }, { "type": "button", "text": { "type": "plain_text", "text": "View PR" }, "url": "${{ github.event.pull_request.html_url }}" }, { "type": "button", "text": { "type": "plain_text", "text": "View Scan Details" }, "url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" } ] } ] } - name: Notify Channel - DM Failed if: steps.gitleaks-scan.outcome == 'failure' && github.event_name == 'pull_request' && (steps.slack-lookup.outputs.ok != 'true' || steps.send-dm.outputs.ok != 'true') uses: slackapi/slack-github-action@v2.1.1 with: webhook: ${{ secrets.SLACK_WEBHOOK_URL }} webhook-type: incoming-webhook payload: | { "text": "Security Alert: Secrets detected in PR #${{ github.event.pull_request.number }} - Could not DM author", "blocks": [ { "type": "header", "text": { "type": "plain_text", "text": "Security Alert: Could Not Reach PR Author Directly" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "⚠️ *@${{ github.event.pull_request.user.login }}* could not be reached via DM - please check the security alert for PR #${{ github.event.pull_request.number }}\n\n*Reason:* ${{ steps.slack-lookup.outputs.ok != 'true' && 'User not found in Slack workspace' || 'Failed to send direct message' }}" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Pull Request:* <${{ github.event.pull_request.html_url }}|#${{ github.event.pull_request.number }} - ${{ github.event.pull_request.title }}>" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Detected Secrets:*\n```${{ steps.extract-findings.outputs.findings_slack }}```" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "📢 *Action Required by @${{ github.event.pull_request.user.login }}:*\n• Remove any exposed secrets from the code\n• Or add false positives to `.gitleaksignore` (file:line:commit) or `.gitleaks.toml` (stopwords)\n• PR is blocked until resolved" } } ] } # Keep existing channel notification as additional backup - name: Notify Slack - Secrets Detected (Pull Request) if: steps.gitleaks-scan.outcome == 'failure' && github.event_name == 'pull_request' uses: slackapi/slack-github-action@v2.1.1 with: webhook: ${{ secrets.SLACK_WEBHOOK_URL }} webhook-type: incoming-webhook payload: | { "text": "Security Alert: Secrets detected in PR #${{ github.event.pull_request.number }}", "blocks": [ { "type": "header", "text": { "type": "plain_text", "text": "Security Alert: Secrets Detected" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Pull Request #${{ github.event.pull_request.number }}:* ${{ github.event.pull_request.title }}\n<${{ github.event.pull_request.html_url }}|View Pull Request>" } }, { "type": "divider" }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*Repository*\n${{ github.repository }}" }, { "type": "mrkdwn", "text": "*Branch*\n`${{ github.event.pull_request.head.ref }}`" }, { "type": "mrkdwn", "text": "*Commit*\n`${{ github.sha }}`" }, { "type": "mrkdwn", "text": "*Author*\n${{ github.actor }}" } ] }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Commit Message*\n```${{ github.event.pull_request.title }}```" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "*DETECTED SECRETS (${{ steps.extract-findings.outputs.count }} findings):*\n```${{ steps.extract-findings.outputs.findings_slack }}```" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": ":warning: *Action Required*\nGitleaks has detected potential secrets in the codebase. Please review the findings immediately and remove any exposed secrets." } }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Pull Request" }, "url": "${{ github.event.pull_request.html_url }}", "style": "primary" }, { "type": "button", "text": { "type": "plain_text", "text": "View Workflow Run" }, "url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}", "style": "danger" } ] }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "Triggered by: `${{ github.event_name }}` | Run ID: `${{ github.run_id }}`" } ] } ] } - name: Notify Slack - Secrets Detected (Push) if: steps.gitleaks-scan.outcome == 'failure' && github.event_name == 'push' uses: slackapi/slack-github-action@v2.1.1 with: webhook: ${{ secrets.SLACK_WEBHOOK_URL }} webhook-type: incoming-webhook payload: | { "text": "Security Alert: Secrets detected in push to ${{ github.ref_name }}", "blocks": [ { "type": "header", "text": { "type": "plain_text", "text": "Security Alert: Secrets Detected" } }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Direct Push to `${{ github.ref_name }}` Branch*" } }, { "type": "divider" }, { "type": "section", "fields": [ { "type": "mrkdwn", "text": "*Repository*\n${{ github.repository }}" }, { "type": "mrkdwn", "text": "*Branch*\n`${{ github.ref_name }}`" }, { "type": "mrkdwn", "text": "*Commit*\n`${{ github.sha }}`" }, { "type": "mrkdwn", "text": "*Author*\n${{ github.actor }}" } ] }, { "type": "section", "text": { "type": "mrkdwn", "text": "*Commit Message*\n```${{ github.event.head_commit.message }}```" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": "*DETECTED SECRETS (${{ steps.extract-findings.outputs.count }} findings):*\n```${{ steps.extract-findings.outputs.findings_slack }}```" } }, { "type": "divider" }, { "type": "section", "text": { "type": "mrkdwn", "text": ":warning: *Action Required*\nGitleaks has detected potential secrets in the codebase. Please review the findings immediately and take corrective action." } }, { "type": "actions", "elements": [ { "type": "button", "text": { "type": "plain_text", "text": "View Commit" }, "url": "${{ github.event.head_commit.url }}", "style": "primary" }, { "type": "button", "text": { "type": "plain_text", "text": "View Workflow Run" }, "url": "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}", "style": "danger" } ] }, { "type": "context", "elements": [ { "type": "mrkdwn", "text": "Triggered by: `${{ github.event_name }}` | Run ID: `${{ github.run_id }}`" } ] } ] } - name: Fail workflow if secrets detected if: steps.gitleaks-scan.outcome == 'failure' run: exit 1