@@ -45,11 +45,51 @@ jobs:
4545
4646 - name : Fetch base and head commits
4747 if : github.event_name == 'pull_request'
48- run : git fetch --depth=1 origin ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}
48+ env :
49+ BASE_SHA : ${{ github.event.pull_request.base.sha }}
50+ HEAD_SHA : ${{ github.event.pull_request.head.sha }}
51+ run : git fetch --depth=1 origin "${BASE_SHA}" "${HEAD_SHA}"
4952
5053 - name : Remove persisted credentials
5154 run : git config --unset-all http.https://github.com/.extraheader
5255
56+ - name : Fetch org-wide TruffleHog exclude patterns
57+ env :
58+ GH_TOKEN : ${{ github.token }}
59+ WORKFLOW_REF : ${{ github.workflow_ref }}
60+ run : |
61+ DEST=/tmp/trufflehog-exclude.txt
62+ REPO=grafana/security-github-actions
63+ FILE_PATH=trufflehog/exclude-paths.txt
64+
65+ # Resolve the ref the calling workflow used (e.g. @main, @feature/branch, @v1).
66+ CALLER_REF="main"
67+ if [[ -n "${WORKFLOW_REF}" ]]; then
68+ CALLER_REF=$(echo "${WORKFLOW_REF}" | sed 's|.*@||; s|^refs/heads/||; s|^refs/tags/||')
69+ fi
70+
71+ LOADED=false
72+ for REF in main "${CALLER_REF}"; do
73+ [[ "$LOADED" == "true" ]] && break
74+ if gh api "repos/${REPO}/contents/${FILE_PATH}?ref=${REF}" \
75+ -H "Accept: application/vnd.github.v3.raw" -o "${DEST}" 2>/dev/null && [[ -s "${DEST}" ]]; then
76+ echo "Loaded exclude patterns from ${REPO}@${REF} (GitHub API)"
77+ LOADED=true
78+ elif curl -fsSL "https://raw.githubusercontent.com/${REPO}/${REF}/${FILE_PATH}" \
79+ -o "${DEST}" 2>/dev/null && [[ -s "${DEST}" ]]; then
80+ echo "Loaded exclude patterns from raw.githubusercontent.com@${REF}"
81+ LOADED=true
82+ fi
83+ done
84+
85+ if [[ "$LOADED" != "true" ]]; then
86+ echo "::warning::Could not fetch TruffleHog exclude patterns from ${REPO} (tried main and ${CALLER_REF}). Scanning without exclusions."
87+ touch "${DEST}"
88+ fi
89+
90+ echo "--- effective exclude patterns ---"
91+ cat "${DEST}"
92+
5393 - name : Install TruffleHog
5494 run : |
5595 # Download binary directly from GitHub releases for supply chain security
@@ -67,53 +107,29 @@ jobs:
67107 sudo chmod +x /usr/local/bin/trufflehog
68108 trufflehog --version
69109
70- - name : Create global exclusions
71- run : |
72- # Create centralized exclusion patterns for common false positives
73- cat > /tmp/trufflehog-exclude.txt <<'EOF'
74- # Lock files and checksums (contain hashes, not secrets)
75- path:go\.sum$
76- path:go\.mod$
77- # Dependency manifests (contain URLs that trigger false positives)
78- path:package\.json$
79- path:package-lock\.json$
80- path:pnpm-lock\.yaml$
81- path:yarn\.lock$
82- path:poetry\.lock$
83- path:Pipfile\.lock$
84- path:uv\.lock$
85- path:Cargo\.lock$
86- path:Gemfile\.lock$
87- # Grafana plugin metadata
88- path:grafana\.json$
89- EOF
90-
91- echo "Created global exclusion patterns:"
92- cat /tmp/trufflehog-exclude.txt
93-
94110 - name : Scan for secrets
95111 id : scan
112+ env :
113+ EVENT_NAME : ${{ github.event_name }}
114+ BASE_SHA : ${{ github.event.pull_request.base.sha }}
115+ HEAD_SHA : ${{ github.event.pull_request.head.sha }}
96116 run : |
97117 set +e
98118 echo "[]" > results.json
99119
100- if [[ "${{ github.event_name }}" == "pull_request" ]]; then
101- # PR: Scan only changed files (using two-dot diff with explicit base SHA)
120+ # Extract non-comment, non-blank patterns for the shell pre-filter.
121+ grep -vE '^\s*#|^\s*$' /tmp/trufflehog-exclude.txt > /tmp/exclude-regexes.txt 2>/dev/null || true
122+
123+ if [[ "${EVENT_NAME}" == "pull_request" ]]; then
102124 echo "Scanning changed files in PR..."
103- git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} > changed-files.txt
125+ git diff --name-only "${BASE_SHA}" "${HEAD_SHA}" > changed-files.txt
104126
105127 if [[ -s changed-files.txt ]]; then
106128 while IFS= read -r file; do
107- # Get just the filename
108- filename=$(basename "$file")
109-
110- # Skip excluded files (use case statement for cleaner matching)
111- case "$filename" in
112- go.sum|go.mod|package.json|package-lock.json|pnpm-lock.yaml|yarn.lock|poetry.lock|Pipfile.lock|uv.lock|Cargo.lock|Gemfile.lock|grafana.json)
113- echo "Skipping: ${file} (excluded manifest/lock file)"
114- continue
115- ;;
116- esac
129+ if [[ -s /tmp/exclude-regexes.txt ]] && echo "$file" | grep -qEf /tmp/exclude-regexes.txt 2>/dev/null; then
130+ echo "Skipping: ${file} (matches exclude pattern)"
131+ continue
132+ fi
117133
118134 if [[ -f "${file}" ]]; then
119135 echo "Scanning: ${file}"
@@ -124,7 +140,6 @@ jobs:
124140 echo "No files changed"
125141 fi
126142 else
127- # Push to main: Scan current filesystem
128143 echo "Scanning current filesystem..."
129144 trufflehog filesystem . --exclude-paths /tmp/trufflehog-exclude.txt --concurrency 16 --json --no-update --results=verified,unverified > results.ndjson || true
130145 fi
@@ -141,8 +156,12 @@ jobs:
141156 # Filter out CHANGELOG git hashes if we have results
142157 if jq -e 'length > 0' results.json >/dev/null 2>&1; then
143158 jq '[.[] | select(
144- ((.SourceMetadata?.Data?.Filesystem?.file // .SourceMetadata?.Data?.Git?.file) // "") as $file |
145- !(($file | test("CHANGELOG|HISTORY\\.md|NEWS\\.md"; "i")) and (.Raw | test("^[0-9a-f]{7,40}$"; "i")))
159+ (
160+ (try .SourceMetadata.Data.Filesystem.file catch null) //
161+ (try .SourceMetadata.Data.Git.file catch null) //
162+ ""
163+ ) as $file |
164+ ((($file | test("CHANGELOG|HISTORY\\.md|NEWS\\.md"; "i")) and (.Raw | test("^[0-9a-f]{7,40}$"; "i"))) | not)
146165 )]' results.json > results.json.tmp && mv results.json.tmp results.json
147166 fi
148167 else
@@ -176,26 +195,29 @@ jobs:
176195
177196 - name : Delete resolved comment
178197 if : ${{ !cancelled() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && steps.scan.outputs.total == '0' }}
198+ env :
199+ GH_TOKEN : ${{ github.token }}
200+ GH_REPOSITORY : ${{ github.repository }}
201+ PR_NUMBER : ${{ github.event.pull_request.number }}
179202 run : |
180203 # Find and delete TruffleHog comment if secrets have been resolved
181- COMMENT_ID=$(gh api repos/${{ github.repository }} /issues/${{ github.event.pull_request.number }} /comments --jq '.[] | select(.body | contains("<!-- trufflehog-secret-scan-comment -->")) | .id' | head -1 || echo "")
204+ COMMENT_ID=$(gh api " repos/${GH_REPOSITORY} /issues/${PR_NUMBER} /comments" --jq '.[] | select(.body | contains("<!-- trufflehog-secret-scan-comment -->")) | .id' | head -1 || echo "")
182205
183206 if [[ -n "$COMMENT_ID" ]]; then
184207 echo "Secrets resolved - deleting previous warning comment (ID: ${COMMENT_ID})"
185- gh api -X DELETE /repos/${{ github.repository }} /issues/comments/${COMMENT_ID}
208+ gh api -X DELETE " /repos/${GH_REPOSITORY} /issues/comments/${COMMENT_ID}"
186209 echo "Comment deleted successfully"
187210 else
188211 echo "No existing TruffleHog comment to delete"
189212 fi
190- env :
191- GH_TOKEN : ${{ github.token }}
192213
193214 - name : Generate PR comment
194215 if : ${{ !cancelled() && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && steps.scan.outputs.total > 0 }}
195216 id : comment-body
217+ env :
218+ VERIFIED : ${{ steps.scan.outputs.verified }}
219+ UNVERIFIED : ${{ steps.scan.outputs.unverified }}
196220 run : |
197- VERIFIED=${{ steps.scan.outputs.verified }}
198- UNVERIFIED=${{ steps.scan.outputs.unverified }}
199221 TOTAL=$((VERIFIED+UNVERIFIED))
200222
201223 if [[ $TOTAL -eq 0 ]]; then
@@ -212,9 +234,9 @@ jobs:
212234 "- " +
213235 (if .Verified then "**VERIFIED SECRET**" else "**Possible secret**" end) +
214236 " (" + .DetectorName + ") at `" +
215- ((.SourceMetadata? .Data? .Filesystem? .file // .SourceMetadata? .Data? .Git? .file) // "unknown") +
237+ (((try .SourceMetadata.Data.Filesystem.file catch null) // (try .SourceMetadata.Data.Git.file catch null) ) // "unknown") +
216238 ":" +
217- ((.SourceMetadata? .Data? .Filesystem? .line // .SourceMetadata? .Data? .Git? .line) | tostring) +
239+ (((try .SourceMetadata.Data.Filesystem.line catch null) // (try .SourceMetadata.Data.Git.line catch null) ) | tostring) +
218240 "` → `" +
219241 (if (.Raw | length) > 8 then (.Raw[:4] + "***" + .Raw[-4:]) else "***" end) +
220242 "`"' results.json 2>/dev/null || echo "- Error processing results")
@@ -261,23 +283,28 @@ jobs:
261283 - name : Create scan report
262284 env :
263285 GITHUB_REF_NAME : ${{ github.ref_name }}
286+ GH_REPOSITORY : ${{ github.repository }}
287+ GH_SHA : ${{ github.sha }}
288+ TOTAL_SECRETS : ${{ steps.scan.outputs.total }}
289+ VERIFIED_SECRETS : ${{ steps.scan.outputs.verified }}
290+ UNVERIFIED_SECRETS : ${{ steps.scan.outputs.unverified }}
264291 run : |
265292 {
266293 echo "TruffleHog Scan Report"
267294 echo "====================="
268295 echo "Date: $(date)"
269- echo "Repository: ${{ github.repository } }"
296+ echo "Repository: ${GH_REPOSITORY }"
270297 echo "Branch: ${GITHUB_REF_NAME}"
271- echo "Commit: ${{ github.sha } }"
298+ echo "Commit: ${GH_SHA }"
272299 echo ""
273300 echo "Summary:"
274- echo "- Total secrets: ${{ steps.scan.outputs.total } }"
275- echo "- Verified: ${{ steps.scan.outputs.verified } }"
276- echo "- Unverified: ${{ steps.scan.outputs.unverified } }"
301+ echo "- Total secrets: ${TOTAL_SECRETS }"
302+ echo "- Verified: ${VERIFIED_SECRETS }"
303+ echo "- Unverified: ${UNVERIFIED_SECRETS }"
277304 echo ""
278305 echo "Detailed Results:"
279306 if [[ -f "results.json" && -s "results.json" ]] && jq empty results.json 2>/dev/null; then
280- jq -r '.[] | "- " + (if .Verified then "VERIFIED" else "Unverified" end) + " " + .DetectorName + " at " + ((.SourceMetadata? .Data? .Filesystem? .file // .SourceMetadata? .Data? .Git? .file) // "unknown") + ":" + ((.SourceMetadata? .Data? .Filesystem? .line // .SourceMetadata? .Data? .Git? .line) | tostring) + " → " + (if (.Raw | length) > 8 then (.Raw[:4] + "***" + .Raw[-4:]) else "***" end)' results.json 2>/dev/null || echo "Error processing results"
307+ jq -r '.[] | "- " + (if .Verified then "VERIFIED" else "Unverified" end) + " " + .DetectorName + " at " + (((try .SourceMetadata.Data.Filesystem.file catch null) // (try .SourceMetadata.Data.Git.file catch null)) // "unknown") + ":" + (((try .SourceMetadata.Data.Filesystem.line catch null) // (try .SourceMetadata.Data.Git.line catch null) ) | tostring) + " → " + (if (.Raw | length) > 8 then (.Raw[:4] + "***" + .Raw[-4:]) else "***" end)' results.json 2>/dev/null || echo "Error processing results"
281308 else
282309 echo "No secrets detected"
283310 fi
0 commit comments