security(triage): inspect safe source-context flags for imported Gitleaks findings
davoodmehraban89 committed
Oct 14, 2026 at 22:56 UTC
fd7cf413a5cb4684d3966ab800994cf7fb5e74c6
1 file changed
+30
-1
scripts/security/triage-gitleaks-report.mjs
+30
-1
@@ -25,6 +25,34 @@ const sanitize = (value, limit = 240) =>
25
JSON.stringify(String(value ?? "").slice(0, limit))
26
27
const counts = new Map()
28
+
29
+const cachedLines = new Map()
30
+function contextualEvidence(path, lineNumber) {
31
+ // Inspect only inside CI; return labels, NEVER raw text.
32
+ if (!path || path.startsWith("/") || path.split("/").includes("..")) return "invalid-path"
33
+ if (!fs.existsSync(path)) return "historical-only-path"
34
+ if (path.startsWith(".yarn/releases/")) return "generated-vendor-bundle"
35
+ let lines = cachedLines.get(path)
36
+ if (!lines) {
37
+ lines = fs.readFileSync(path, "utf8").split(/\r?\n/)
38
+ cachedLines.set(path, lines)
39
+ }
40
+ const index = Math.max(0, Math.min(lines.length - 1, Number(lineNumber) - 1 || 0))
41
+ const excerpt = lines.slice(Math.max(0, index - 4), Math.min(lines.length, index + 6)).join("\n")
42
+ const flags = []
43
+ if (/__tests__|\.spec\.|\.test\./i.test(path)) flags.push("test-file")
44
+ if (/www\/|\.mdx$|llms-full\.txt$|\.stories\./i.test(path)) flags.push("documentation-or-demo")
45
+ if (/\b(?:example|for example|your_|replace|placeholder|dummy|fake|sample)\b/i.test(excerpt)) flags.push("example-marker")
46
+ if (/\.\.\./.test(excerpt)) flags.push("ellipsis-placeholder")
47
+ if (/\b(?:idempotency_key|mock|jest|expect\()/.test(excerpt)) flags.push("test-field")
48
+ if (/\b(?:curl|authorization|bearer|x-publishable-api-key)\b/i.test(excerpt)) flags.push("http-example")
49
+ if (/BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY/.test(excerpt)) flags.push("pem-header")
50
+ if (/BEGIN (?:RSA |EC )?PRIVATE KEY-----[\s\S]{50,4000}END (?:RSA |EC )?PRIVATE KEY/.test(excerpt) && !/\.\.\./.test(excerpt)) {
51
+ flags.push("POSSIBLE-COMPLETE-PEM-REVIEW")
52
+ }
53
+ return flags.join("|") || "no-context-marker"
54
+}
55
+
56
console.log(`Gitleaks finding metadata (NO secret values): ${raw.length} detections`)
57
for (const [index, item] of raw.entries()) {
58
// A filename alone does NOT establish false-positive status.
@@ -37,7 +65,8 @@ for (const [index, item] of raw.entries()) {
65
console.log(
66
`Finding ${index + 1}: kind=${kind} rule=${sanitize(rule, 100)} ` +
67
`path=${sanitize(path)} commit_prefix=${sanitize(String(item.Commit ?? "").slice(0, 12), 12)} ` +
40
- `start_line=${Number.isFinite(Number(item.StartLine)) ? Number(item.StartLine) : "unknown"}`
68
+ `start_line=${Number.isFinite(Number(item.StartLine)) ? Number(item.StartLine) : "unknown"} ` +
69
+ `source_evidence=${sanitize(contextualEvidence(path, item.StartLine))}`
70
)
71
}
72
console.log("Counts grouped by path category and rule:")