|
| 1 | +name: trufflehog-secret-scanning |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + secrets: |
| 6 | + GH_TOKEN: |
| 7 | + required: false |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + trufflehog-scan: |
| 12 | + name: TruffleHog Secret Scan |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + permissions: |
| 16 | + contents: read |
| 17 | + id-token: write |
| 18 | + actions: read |
| 19 | + security-events: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Install TruffleHog |
| 26 | + run: | |
| 27 | + pip install trufflehog |
| 28 | +
|
| 29 | + - name: Run TruffleHog and generate JSON report |
| 30 | + run: | |
| 31 | + trufflehog filesystem --directory . --json > trufflehog-findings.json || true |
| 32 | +
|
| 33 | + - name: Convert TruffleHog findings to SARIF format |
| 34 | + if: github.repository_visibility == 'public' && success() |
| 35 | + run: | |
| 36 | + pip install sarif-tools |
| 37 | + python3 -c """ |
| 38 | + import json |
| 39 | +
|
| 40 | +with open('trufflehog-findings.json') as f: |
| 41 | + findings = json.load(f) |
| 42 | + |
| 43 | + sarif = { |
| 44 | +'version': '2.1.0', |
| 45 | + 'runs': [{ |
| 46 | + 'tool': { |
| 47 | + 'driver': { |
| 48 | + 'name': 'TruffleHog', |
| 49 | + 'informationUri': 'https://github.com/trufflesecurity/trufflehog', |
| 50 | + 'rules': [] |
| 51 | + } |
| 52 | + }, |
| 53 | + 'results': [] |
| 54 | + }] |
| 55 | +} |
| 56 | + |
| 57 | +seen_rules = set() |
| 58 | + |
| 59 | +for finding in findings: |
| 60 | + reason = finding.get('reason', 'Secret detected') |
| 61 | + rule_id = f"trufflehog-{reason.replace(' ', '-')[:64]}" |
| 62 | + if rule_id not in seen_rules: |
| 63 | + sarif['runs'][0]['tool']['driver']['rules'].append({ |
| 64 | + 'id': rule_id, |
| 65 | + 'name': reason |
| 66 | + }) |
| 67 | + seen_rules.add(rule_id) |
| 68 | + sarif['runs'][0]['results'].append({ |
| 69 | + 'ruleId': rule_id, |
| 70 | + 'level': 'warning', |
| 71 | + 'message': {'text': reason}, |
| 72 | + 'locations': [{ |
| 73 | + 'physicalLocation': { |
| 74 | + 'artifactLocation': {'uri': finding.get('path', '')}, |
| 75 | + 'region': {'startLine': 1} |
| 76 | + } |
| 77 | + }] |
| 78 | + }) |
| 79 | + |
| 80 | +with open('trufflehog.sarif', 'w') as out: |
| 81 | + json.dump(sarif, out) |
| 82 | +""" |
| 83 | +
|
| 84 | + - name: Upload TruffleHog SARIF to GitHub Code Scanning |
| 85 | + if: github.repository_visibility == 'public' && success() |
| 86 | + run: | |
| 87 | + gzip -c trufflehog.sarif | base64 -w 0 > trufflehog.sarif.base64 |
| 88 | + encoded_sarif=$(cat trufflehog.sarif.base64) |
| 89 | +
|
| 90 | + curl -s -X POST \ |
| 91 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 92 | + -H "Accept: application/vnd.github+json" \ |
| 93 | + -H "Content-Type: application/json" \ |
| 94 | + https://api.github.com/repos/${{ github.repository }}/code-scanning/sarifs \ |
| 95 | + -d @- <<EOF |
| 96 | + { |
| 97 | + "commit_sha": "${{ github.sha }}", |
| 98 | + "ref": "${{ github.ref }}", |
| 99 | + "sarif": "$encoded_sarif", |
| 100 | + "checkout_uri": "https://github.com/${{ github.repository }}", |
| 101 | + "tool_name": "TruffleHog" |
| 102 | + } |
| 103 | +EOF |
0 commit comments