Skip to content

Commit bb77eae

Browse files
feat(trufflehog): add org-wide path exclusions via exclude-paths.txt
Replace the inline hardcoded exclusion list with a centralized `trufflehog/exclude-paths.txt` file containing Go regexes that are passed directly to `trufflehog --exclude-paths`. Adding a new exclusion is now: add a regex line, merge to main. Changes: - Add trufflehog/exclude-paths.txt with patterns for vendor/, lock files, dependency manifests, grafana.json, and dashboard content - Add fetch step that loads the exclude file from security-github-actions at runtime (GitHub API → raw fallback → workflow ref fallback) - Simplify PR scan loop: pre-filter changed files with grep against exclude patterns instead of per-file case statements - Fix code injection: move all ${{ }} expressions from run blocks into env blocks to prevent shell injection - Fix jq CHANGELOG filter to use try/catch syntax - Clean up org-required-trufflehog.yml comments Made-with: Cursor
1 parent bd0a4d1 commit bb77eae

File tree

3 files changed

+116
-58
lines changed

3 files changed

+116
-58
lines changed

.github/workflows/org-required-trufflehog.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ jobs:
2323
name: TruffleHog Secret Scan
2424
uses: grafana/security-github-actions/.github/workflows/reusable-trufflehog.yml@main
2525
with:
26-
# Fail on verified secrets - blocking mode
27-
fail-on-verified: "true" # Block on verified secrets
28-
fail-on-unverified: "false" # Don't block on unverified secrets
26+
# Non-blocking: job succeeds; PR still gets comments/artifacts when findings exist
27+
fail-on-verified: "false" # Set "true" to fail on verified secrets
28+
fail-on-unverified: "false" # Set "true" to fail on unverified secrets
2929
runs-on: ${{ !github.event.repository.private && 'ubuntu-latest' || 'ubuntu-arm64-small' }} # Use same runner pattern as zizmor
3030
secrets: inherit

.github/workflows/reusable-trufflehog.yml

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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

trufflehog/exclude-paths.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# TruffleHog path exclusions — one Go regex per line.
2+
# Edit this file and merge to main. CI fetches it at runtime and passes
3+
# it directly to `trufflehog --exclude-paths`.
4+
#
5+
# Syntax: Go regexp (https://pkg.go.dev/regexp/syntax).
6+
# A file is excluded when ANY pattern matches its path.
7+
# Lines starting with # and blank lines are ignored.
8+
9+
# Go vendor directory (third-party code, not repo secrets)
10+
vendor/
11+
12+
# Lock files and checksums (contain hashes, not secrets)
13+
go\.sum$
14+
go\.mod$
15+
16+
# Dependency manifests (contain URLs / hashes that trigger false positives)
17+
package\.json$
18+
package-lock\.json$
19+
pnpm-lock\.yaml$
20+
yarn\.lock$
21+
poetry\.lock$
22+
Pipfile\.lock$
23+
uv\.lock$
24+
Cargo\.lock$
25+
Gemfile\.lock$
26+
27+
# Grafana plugin metadata
28+
grafana\.json$
29+
30+
# Grafana dashboards (user-supplied site content, full of base64/hashes)
31+
content/grafana/dashboards

0 commit comments

Comments
 (0)