-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1461 lines (1295 loc) · 43.1 KB
/
Copy pathmain.go
File metadata and controls
1461 lines (1295 loc) · 43.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// secscan - Enhanced Go CLI secret scanner
// Version: 2.2.4
// Author: Zayan-Mohamed (itsm.zayan@gmail.com)
// License: MIT
//
// Installation:
//
// make install # build and install to /usr/local/bin (recommended)
//
// Usage examples:
//
// secscan -root . # scan working tree + git history
// secscan -root . -history=false # scan only current files
// secscan -root . -json report.json # output JSON report
// secscan -root . -config .secscan.toml # use custom config
// secscan -root . -entropy 4.5 # adjust entropy threshold
// secscan -root . -min-confidence 0.8 # only report high-confidence findings
// secscan -root . -verbose # show detailed output
// secscan -root . -respect-gitignore=false # disable gitignore support
package main
import (
"bufio"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"math"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
)
// A single diff line can be very long in generated files; the default scanner
// buffer would silently truncate the commit rather than scan it.
const maxDiffLineBytes = 4 * 1024 * 1024
// Version information
const version = "2.2.4"
// Finding represents a detected secret or potential secret
type Finding struct {
File string `json:"file"`
Line int `json:"line"`
Commit string `json:"commit,omitempty"`
Pattern string `json:"pattern"`
Excerpt string `json:"excerpt"`
RawValue string `json:"-"` // Not exported to JSON
Confidence float64 `json:"confidence"`
Verified bool `json:"verified"`
Metadata map[string]string `json:"metadata,omitempty"`
Hash string `json:"hash"` // Identity of the secret, for deduplication
Occurrences int `json:"occurrences"` // Times this same secret was seen
}
// Config holds scanner configuration
type Config struct {
Rules map[string]*Rule
SkipDirs []string
SkipFiles []string
AllowPatterns []*regexp.Regexp
EntropyThreshold float64
MinSecretLength int
MaxSecretLength int
Verbose bool
RespectGitignore bool
GitignorePatterns []GitignorePattern
ExcludeGlobs []string
}
// GitignorePattern represents a pattern from .gitignore with its base directory
type GitignorePattern struct {
Pattern string
Negation bool
Directory bool
BaseDir string
}
// Rule represents a detection rule
type Rule struct {
Name string
Pattern *regexp.Regexp
Keywords []string
Description string
Confidence float64
Enabled bool
}
// Stats tracks scanning statistics
type Stats struct {
FilesScanned int
CommitsScanned int
FindingsTotal int
FindingsUnique int
StartTime time.Time
EndTime time.Time
mu sync.Mutex
}
func (s *Stats) incrementFiles() {
s.mu.Lock()
defer s.mu.Unlock()
s.FilesScanned++
}
func (s *Stats) incrementCommits() {
s.mu.Lock()
defer s.mu.Unlock()
s.CommitsScanned++
}
func (s *Stats) incrementFindings(count int) {
s.mu.Lock()
defer s.mu.Unlock()
s.FindingsTotal += count
}
// Enhanced detection patterns with lower false positive rates
var defaultRegexps = map[string]string{
"aws_access_key": `AKIA[0-9A-Z]{16}`,
"aws_secret_key": `(?i)aws(.{0,20})?(?-i)['\"][0-9a-zA-Z/+]{40}['\"]`,
"rsa_private": `-----BEGIN(?: RSA)? PRIVATE KEY-----`,
"stripe_sk": `sk_live_[0-9a-zA-Z]{24,}`,
"stripe_restricted": `rk_live_[0-9a-zA-Z]{24,}`,
"supabase_jwt": `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9\.[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+`,
"github_pat": `ghp_[0-9a-zA-Z]{36}`,
"github_oauth": `gho_[0-9a-zA-Z]{36}`,
"github_app": `(ghu|ghs)_[0-9a-zA-Z]{36}`,
"slack_token": `xox[baprs]-([0-9a-zA-Z]{10,48})`,
"slack_webhook": `https://hooks\.slack\.com/services/T[a-zA-Z0-9_]+/B[a-zA-Z0-9_]+/[a-zA-Z0-9_]+`,
"google_api": `AIza[0-9A-Za-z_\-]{35}`,
// A Heroku API key is a bare UUID, so the UUID shape alone carries no
// signal -- it matched every seeded uuid in every fixture and migration.
// Require the token to actually be named as a Heroku credential.
"heroku_api": `(?i)heroku[a-z0-9_\-]{0,20}['"\s]*[=:>]['"\s]*[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}`,
"mailgun_api": `key-[0-9a-zA-Z]{32}`,
"paypal_braintree": `access_token\$production\$[0-9a-z]{16}\$[0-9a-f]{32}`,
"picatic_api": `sk_live_[0-9a-z]{32}`,
"sendgrid_api": `SG\.[0-9A-Za-z\-_]{22}\.[0-9A-Za-z\-_]{43}`,
"twilio_api": `SK[0-9a-fA-F]{32}`,
"generic_api_key": `(?i)(?:key|api[_-]?key|apikey)[\s]*[=:>][\s]*['\"]([a-zA-Z0-9_\-]{20,})['\""]`,
"generic_secret": `(?i)(?:secret|password|passwd|pwd)[\s]*[=:>][\s]*['\"]([a-zA-Z0-9_\-!@#$%^&*]{8,})['\""]`,
"db_connection": `(?i)(postgres|mysql|mongodb|redis)://[^\s'"]+:[^\s'"]+@[^\s'"]+`,
}
// Patterns that should be allowed (common false positives)
var defaultAllowPatterns = []string{
`^[A-Z_]+$`, // All caps constants
`^[a-z_]+$`, // All lowercase
`(?i)^(true|false|null|undefined)$`, // Boolean/null values
`^[\d.]+$`, // Pure numbers
`^https?://`, // URLs without credentials
`^[A-Za-z]+\.[A-Za-z]+`, // Class/module names
`(?i)^(test|example|sample|demo|placeholder|your[_-].*|my[_-].*)`, // Test values
`^[*]+$`, // Masked secrets
// Documentation and fixture credentials advertise themselves, but the
// marker is usually a suffix or infix rather than a prefix -- an anchored
// check never saw them. AWS's own canonical example key,
// AKIAIOSFODNN7EXAMPLE, is the case that made secscan flag its own docs
// and test fixtures at confidence 0.9.
`(?i)example`,
`(?i)(dummy|placeholder|redacted|changeme|notreal|fake)`,
`(?i)x{8,}`, // XXXXXXXX-style stand-ins
`(?i)^(foo|bar|baz)`, // Canonical filler
// Fill-me-in placeholders from READMEs and .env templates, e.g.
// JWT_SECRET="your-secret-here". These stay anchored on purpose: an
// unanchored allow pattern is a silent false negative, because a random
// 39-character credential can happily contain the substring "my-" or
// "the_". In a secret scanner an over-broad allowlist is far more dangerous
// than an over-broad rule -- a false positive is merely annoying, a
// suppressed real key is invisible.
`(?i)[_\-]here$`,
`(?i)^(replace|insert|add)[_\-]?(me|this|your)`,
// Connection strings whose password is a well-known default are describing
// a local compose service, not a credential worth reporting.
`(?i)://[^:/@\s]*:(postgres|mysql|root|password|passwd|admin|secret|redis|mongo|dev|test|guest)@`,
}
// Enhanced skip directories with more comprehensive list
var defaultSkipDirs = []string{
"node_modules", ".git", "dist", "build", ".next", "venv", "target",
"__pycache__", ".venv", "env", ".env", "vendor", "coverage",
".pytest_cache", ".mypy_cache", ".tox", "bin", "obj", ".gradle",
".idea", ".vscode", ".terraform", "*.egg-info", ".nuxt",
}
// ignoreFileName is a per-repository list of paths that are deliberately
// credential-shaped: fixture files, documentation with sample keys, and so on.
// One glob per line, # for comments, matched against the basename or the
// repo-relative path.
//
// A per-line `secscan:ignore` directive cannot help once a line is committed,
// because history still holds the version without it, and rewriting published
// history to silence a scanner is a bad trade. A repo-level ignore file covers
// the working tree and history alike, which is why history findings carry the
// file they came from.
const ignoreFileName = ".secscanignore"
// Generated files that are build output rather than source.
var defaultSkipArtifacts = []string{
"search_index.json", // MkDocs search index
"mvnw", "mvnw.cmd", "gradlew", "gradlew.bat", // Build wrappers
}
// File extensions that should be skipped
var defaultSkipExtensions = []string{
".jpg", ".jpeg", ".png", ".gif", ".ico", ".svg", ".webp",
".mp4", ".avi", ".mov", ".mp3", ".wav", ".pdf", ".zip",
".tar", ".gz", ".bz2", ".7z", ".rar", ".exe", ".dll",
".so", ".dylib", ".bin", ".db", ".sqlite", ".lock",
".min.js", ".min.css", ".map", ".woff", ".woff2", ".ttf", ".eot",
}
// loadIgnoreFile reads .secscanignore from the scan root. A missing file is
// not an error; most repositories will not have one.
func loadIgnoreFile(root string) []string {
data, err := os.ReadFile(filepath.Join(root, ignoreFileName))
if err != nil {
return nil
}
var globs []string
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
globs = append(globs, line)
}
return globs
}
// isExcluded reports whether path matches a .secscanignore glob. It is checked
// against both the basename and the path itself, so `main_test.go` covers the
// file wherever it sits and `docs/*` covers a subtree.
func isExcluded(path string, globs []string) bool {
if len(globs) == 0 {
return false
}
clean := filepath.ToSlash(path)
base := filepath.Base(clean)
for _, glob := range globs {
if ok, _ := filepath.Match(glob, base); ok {
return true
}
if ok, _ := filepath.Match(glob, clean); ok {
return true
}
// A directory prefix such as "testdata/" or "docs/".
if strings.HasSuffix(glob, "/") && strings.Contains(clean+"/", glob) {
return true
}
}
return false
}
func shouldSkipDir(d string) bool {
base := filepath.Base(d)
for _, s := range defaultSkipDirs {
if base == s || strings.HasPrefix(base, s) {
return true
}
}
return false
}
func shouldSkipFile(name string) bool {
base := filepath.Base(name)
// A committed .env is exactly what we are looking for, so it is scanned
// despite being a hidden file. Its templates are the opposite: .env.example
// and friends exist to hold placeholders, and reporting them is noise by
// construction.
if strings.HasPrefix(base, ".env.") || base == ".env.template" {
return true
}
// Skip hidden files except specific configs
if strings.HasPrefix(base, ".") && base != ".env" {
return true
}
// Compare against the whole name, not filepath.Ext: Ext("app.min.js")
// returns ".js", so multi-part suffixes like .min.js and .min.css never
// matched and minified bundles were scanned as ordinary source -- a large
// share of the high-entropy false positives came from exactly those files.
lower := strings.ToLower(base)
for _, skipExt := range defaultSkipExtensions {
if strings.HasSuffix(lower, skipExt) {
return true
}
}
// Skip all types of lock files comprehensively
lockFilePatterns := []string{
".lock", // Gemfile.lock, Pipfile.lock, etc.
"-lock.json", // package-lock.json
"-lock.yaml", // pnpm-lock.yaml
"lock.json", // composer.lock.json
"lock.yaml", // yarn.lock (though it's typically yarn.lock)
"yarn.lock", // yarn lock file
"pnpm-lock.yaml", // pnpm lock file
"package-lock.json", // npm lock file
"composer.lock", // PHP composer lock
"Gemfile.lock", // Ruby Gemfile lock
"Pipfile.lock", // Python Pipfile lock
"poetry.lock", // Python poetry lock
"Cargo.lock", // Rust cargo lock
"mix.lock", // Elixir mix lock
"pubspec.lock", // Dart/Flutter pubspec lock
"go.sum", // Go dependencies checksum
"Podfile.lock", // iOS CocoaPods lock
"Cartfile.resolved", // iOS Carthage lock
}
for _, pattern := range lockFilePatterns {
if strings.HasSuffix(base, pattern) || base == pattern {
return true
}
}
// Generated build artifacts. These are compiled from sources that are
// themselves scanned, so reading them again adds no coverage -- only noise.
// A committed docs site is the worst case: `git rev-list --all` reaches the
// gh-pages branch, and documentation about security is saturated with the
// words "key", "token" and "secret", so every long string on every rendered
// page looks like a credential in context.
for _, artifact := range defaultSkipArtifacts {
if base == artifact {
return true
}
}
return false
}
// parseGitignoreLine parses a single line from .gitignore
func parseGitignoreLine(line, baseDir string) *GitignorePattern {
line = strings.TrimSpace(line)
// Skip empty lines and comments
if line == "" || strings.HasPrefix(line, "#") {
return nil
}
pattern := GitignorePattern{
BaseDir: baseDir,
}
// Check for negation
if strings.HasPrefix(line, "!") {
pattern.Negation = true
line = strings.TrimPrefix(line, "!")
}
// Check if pattern is for directories only
if strings.HasSuffix(line, "/") {
pattern.Directory = true
line = strings.TrimSuffix(line, "/")
}
pattern.Pattern = line
return &pattern
}
// loadGitignore loads patterns from a .gitignore file
func loadGitignore(path string) ([]GitignorePattern, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
baseDir := filepath.Dir(path)
var patterns []GitignorePattern
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if pattern := parseGitignoreLine(scanner.Text(), baseDir); pattern != nil {
patterns = append(patterns, *pattern)
}
}
return patterns, scanner.Err()
}
// collectGitignorePatterns finds and loads all .gitignore files in the repository
func collectGitignorePatterns(root string) []GitignorePattern {
var allPatterns []GitignorePattern
// Walk the directory tree to find all .gitignore files
_ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return nil
}
// Skip .git directory
if d.IsDir() && d.Name() == ".git" {
return filepath.SkipDir
}
// Check if this is a .gitignore file
if !d.IsDir() && d.Name() == ".gitignore" {
patterns, err := loadGitignore(path)
if err == nil {
allPatterns = append(allPatterns, patterns...)
}
}
return nil
})
return allPatterns
}
// matchGitignorePattern checks if a path matches a gitignore pattern
func matchGitignorePattern(path string, pattern GitignorePattern) bool {
// Convert absolute path to relative path from pattern's base directory
relPath, err := filepath.Rel(pattern.BaseDir, path)
if err != nil {
return false
}
// If the path is outside the base directory, it doesn't match
if strings.HasPrefix(relPath, "..") {
return false
}
patternStr := pattern.Pattern
// Handle different pattern types
if strings.HasPrefix(patternStr, "/") {
// Anchored to base directory
patternStr = strings.TrimPrefix(patternStr, "/")
return matchPattern(patternStr, relPath)
} else if strings.Contains(patternStr, "/") {
// Contains slash - match anywhere in path
return matchPattern(patternStr, relPath)
} else {
// No slash - match basename anywhere in tree
parts := strings.Split(relPath, string(filepath.Separator))
for _, part := range parts {
if matchPattern(patternStr, part) {
return true
}
}
// Also try full relative path
return matchPattern(patternStr, relPath)
}
}
// matchPattern performs glob-style pattern matching
func matchPattern(pattern, name string) bool {
// Handle ** for matching any number of directories
if strings.Contains(pattern, "**") {
parts := strings.Split(pattern, "**")
if len(parts) == 2 {
prefix := strings.TrimSuffix(parts[0], "/")
suffix := strings.TrimPrefix(parts[1], "/")
if prefix != "" && !strings.HasPrefix(name, prefix) {
return false
}
if suffix != "" && !strings.HasSuffix(name, suffix) {
return false
}
return true
}
}
// Simple glob matching
matched, _ := filepath.Match(pattern, name)
if matched {
return true
}
// Try matching with the full path for patterns with directory separators
matched, _ = filepath.Match(pattern, filepath.Base(name))
return matched
}
// isGitignored checks if a path should be ignored based on gitignore patterns
func isGitignored(path string, patterns []GitignorePattern, isDir bool) bool {
ignored := false
// Process patterns in order (later patterns override earlier ones)
for _, pattern := range patterns {
// Skip directory-only patterns for files
if pattern.Directory && !isDir {
continue
}
if matchGitignorePattern(path, pattern) {
if pattern.Negation {
ignored = false // Negation pattern - don't ignore
} else {
ignored = true // Normal pattern - ignore
}
}
}
return ignored
}
func looksLikeTextFile(name string) bool {
// Skip if should skip file
if shouldSkipFile(name) {
return false
}
ext := strings.ToLower(filepath.Ext(name))
switch ext {
case ".go", ".js", ".ts", ".tsx", ".jsx", ".java", ".py", ".rb", ".php",
".json", ".yaml", ".yml", ".env", ".cfg", ".toml", ".md", ".txt",
".sh", ".bash", ".zsh", ".ps1", ".sql", ".xml", ".html", ".css",
".c", ".cpp", ".h", ".hpp", ".cs", ".rs", ".kt", ".swift", ".scala",
".clj", ".ex", ".exs", ".erl", ".hrl", ".vim", ".lua", ".pl", ".r",
".Dockerfile", ".tf", ".hcl", ".proto", ".graphql", ".vue", ".svelte":
return true
default:
// Check if file has no extension (might be script)
if ext == "" {
return true
}
return false
}
}
func compileRules(rules map[string]string) (map[string]*Rule, error) {
out := make(map[string]*Rule, len(rules))
for k, v := range rules {
r, err := regexp.Compile(v)
if err != nil {
return nil, fmt.Errorf("failed to compile %s: %w", k, err)
}
confidence := 0.9 // Default high confidence for regex patterns
if strings.HasPrefix(k, "generic_") {
confidence = 0.7 // Lower confidence for generic patterns
}
out[k] = &Rule{
Name: k,
Pattern: r,
Description: k,
Confidence: confidence,
Enabled: true,
}
}
return out, nil
}
func compileAllowPatterns(patterns []string) ([]*regexp.Regexp, error) {
var out []*regexp.Regexp
for _, p := range patterns {
r, err := regexp.Compile(p)
if err != nil {
return nil, fmt.Errorf("failed to compile allow pattern %s: %w", p, err)
}
out = append(out, r)
}
return out, nil
}
// isAllowed checks if a value matches any allow pattern
func isAllowed(value string, allowPatterns []*regexp.Regexp) bool {
for _, pattern := range allowPatterns {
if pattern.MatchString(value) {
return true
}
}
return false
}
// generateHash identifies a finding by the secret itself, not by where it was
// seen. Keying on location (file, or commit SHA) meant the same leaked
// credential produced a distinct hash in every commit that touched it, so
// deduplication across commits could never fire.
func generateHash(pattern, value string) string {
h := sha256.New()
h.Write([]byte(pattern + "\x00" + value))
return fmt.Sprintf("%x", h.Sum(nil))[:16]
}
// atoiOr parses a base-10 int, falling back to def.
func atoiOr(s string, def int) int {
n, err := strconv.Atoi(s)
if err != nil {
return def
}
return n
}
func maskSecret(s string) string {
if len(s) <= 8 {
return strings.Repeat("*", len(s))
}
pref := s[:4]
suf := s[len(s)-4:]
return pref + strings.Repeat("*", len(s)-8) + suf
}
func stringExcerpt(line string, a, b int) string {
if a < 0 || b > len(line) || a >= b {
return strings.TrimSpace(line)
}
start := a - 20
if start < 0 {
start = 0
}
end := b + 20
if end > len(line) {
end = len(line)
}
return strings.TrimSpace(line[start:end])
}
// Secrets live in a fairly narrow length band. Below minSecretLen there is not
// enough material to judge; above maxSecretLen we are almost always looking at
// an embedded asset, a bundled blob, or a base64 payload rather than a
// credential -- and those were the dominant source of false positives.
const (
minSecretLen = 20
maxSecretLen = 100
// defaultEntropy sits just above log2(16) = 4.0, the ceiling for hex, so
// git SHAs and integrity hashes fall out by construction while base64
// credentials (which reach ~4.7-5.0 at these lengths) clear it. It must
// stay below log2(minSecretLen) = 4.32 or the shortest detectable secret
// becomes mathematically undetectable; TestEntropyThresholdIsReachable
// enforces that.
defaultEntropy = 4.1
)
// isHighEntropy reports whether s looks like a randomly generated credential.
//
// Shannon entropy over a single token's own character distribution is bounded
// above by log2(len(s)): a 20-character token cannot score above 4.32 no
// matter how random it is. The old default threshold of 5.0 therefore required
// a token of at least 32 characters with almost no repeated characters, which
// no real credential satisfies -- a live AWS key id scores 3.68, a GitHub PAT
// 4.77, a Stripe key 4.75. The detector could only ever fire on long
// high-alphabet blobs, i.e. exactly the things that are not secrets.
//
// The threshold now sits just above the ceiling for hex (log2(16) = 4.0), so
// "more random than hex" is the bar, and the length band excludes blobs.
func isHighEntropy(s string, threshold float64) bool {
length := utf8.RuneCountInString(s)
if length < minSecretLen || length > maxSecretLen {
return false
}
// Must have good character diversity
hasLower := false
hasUpper := false
hasDigit := false
hasSymbol := false
for _, r := range s {
if r >= 'a' && r <= 'z' {
hasLower = true
} else if r >= 'A' && r <= 'Z' {
hasUpper = true
} else if r >= '0' && r <= '9' {
hasDigit = true
} else {
hasSymbol = true
}
}
charClassCount := 0
for _, v := range []bool{hasLower, hasUpper, hasDigit, hasSymbol} {
if v {
charClassCount++
}
}
// Require at least 3 character classes
if charClassCount < 3 {
return false
}
return shannonEntropy(s) > threshold
}
func shannonEntropy(s string) float64 {
if len(s) == 0 {
return 0
}
counts := make(map[rune]int)
for _, r := range s {
counts[r]++
}
e := 0.0
L := float64(len(s))
for _, c := range counts {
p := float64(c) / L
e += -p * math.Log2(p)
}
return e
}
func walkFiles(root string, config *Config, action func(path string) error) error {
return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
// ignore walk errors for robustness
return nil
}
if d.IsDir() {
// Check gitignore first if enabled
if config.RespectGitignore && isGitignored(path, config.GitignorePatterns, true) {
if config.Verbose {
fmt.Printf("Skipping gitignored directory: %s\n", path)
}
return filepath.SkipDir
}
// Then check default skip dirs
if shouldSkipDir(path) && path != root {
return filepath.SkipDir
}
return nil
}
// Check gitignore for files if enabled
if config.RespectGitignore && isGitignored(path, config.GitignorePatterns, false) {
if config.Verbose {
fmt.Printf("Skipping gitignored file: %s\n", path)
}
return nil
}
if !looksLikeTextFile(path) {
return nil
}
if isExcluded(path, config.ExcludeGlobs) {
if config.Verbose {
fmt.Printf("Skipping excluded file: %s\n", path)
}
return nil
}
return action(path)
})
}
// entropyTokens extracts candidate secrets: maximal runs in the base64url
// alphabet, which is what credentials are actually drawn from.
//
// This used to be `\S{20,}` -- any run of non-whitespace. That swallowed
// whole URLs, HTML attributes and code fragments as single "tokens", so
// `href="../user-guide/examples/#share"` was handed to the entropy check as if
// it were a candidate credential. Restricting the alphabet drops separators,
// quotes and punctuation, which splits structured text back into short words
// while leaving real tokens intact.
var entropyTokens = regexp.MustCompile(`[A-Za-z0-9_\-]{20,}`)
// secretContext matches lines that name something as a credential.
//
// Entropy on its own is not a detector at any threshold. Set it high enough to
// reject ordinary source and it rejects real credentials too (Shannon entropy
// over a token's own characters is bounded by log2(len), so a 20-char secret
// tops out at 4.32); set it low enough to catch them and every minified
// fragment, URL and base64 chunk in the tree fires. Measured on a 37-repo
// corpus: a 5.0 threshold found 0 real secrets, and 4.1 produced 15,387
// findings.
//
// What separates a credential from a random-looking token is not its entropy
// but that somebody named it. Consult entropy only where the line says the
// value is a secret, and it becomes a useful last-resort detector for
// credential formats that have no dedicated rule.
var secretContext = regexp.MustCompile(`(?i)(secret|token|password|passwd|` +
`\bpwd\b|api[_\-.]?key|apikey|access[_\-.]?key|private[_\-.]?key|` +
`credential|auth|bearer|client[_\-.]?secret|signing[_\-.]?key|` +
`encryption[_\-.]?key|\bsalt\b|passphrase)`)
// ignoreDirective marks a line that is deliberately credential-shaped.
//
// Some values cannot be allowlisted by their content without weakening the
// allowlist for everyone: a scanner's own test suite has to contain realistic,
// non-allowlisted credentials in order to prove that real ones are not
// suppressed. Widening the allow patterns to cover them would create silent
// false negatives, which is the one failure mode worse than noise. An explicit
// per-line opt-out is the honest way to say "I know what this is".
//
// {"github pat", "ghp_...", true}, // secscan:ignore
var ignoreDirective = regexp.MustCompile(`(?i)secscan:\s*ignore`)
// scanLine applies every rule plus entropy detection to a single line of
// content. Both the working-tree scanner and the git-history scanner go
// through here, so a secret is judged the same way wherever it is found.
func scanLine(line, file string, lineNo int, rules map[string]*Rule, config *Config) []Finding {
trim := strings.TrimSpace(line)
if trim == "" {
return nil
}
if ignoreDirective.MatchString(line) {
return nil
}
// Skip comments (basic detection)
if strings.HasPrefix(trim, "//") || strings.HasPrefix(trim, "#") ||
strings.HasPrefix(trim, "/*") || strings.HasPrefix(trim, "*") {
return nil
}
var findings []Finding
for name, rule := range rules {
if !rule.Enabled {
continue
}
loc := rule.Pattern.FindStringSubmatchIndex(line)
if loc == nil {
continue
}
// Test the allowlist against the captured secret, not the whole match.
// Rules like generic_secret match `password="testpass123"` in full, so
// checking the match meant anchored allow patterns such as ^test or
// ^your[_-] could never fire -- every one of them was dead code, and
// obvious placeholders were reported as credentials.
rawValue := line[loc[0]:loc[1]]
value := rawValue
if len(loc) >= 4 && loc[2] >= 0 {
value = line[loc[2]:loc[3]]
}
if isAllowed(value, config.AllowPatterns) || isAllowed(rawValue, config.AllowPatterns) {
continue
}
findings = append(findings, Finding{
File: file,
Line: lineNo,
Pattern: name,
Excerpt: maskSecret(stringExcerpt(line, loc[0], loc[1])),
RawValue: rawValue,
Confidence: rule.Confidence,
Hash: generateHash(name, rawValue),
})
}
if config.EntropyThreshold > 0 && secretContext.MatchString(line) {
for _, tok := range entropyTokens.FindAllString(line, -1) {
// The token that matched the context keyword is the name, not the
// value; don't report the identifier as its own secret.
if secretContext.MatchString(tok) {
continue
}
if isAllowed(tok, config.AllowPatterns) {
continue
}
if isHighEntropy(tok, config.EntropyThreshold) {
findings = append(findings, Finding{
File: file,
Line: lineNo,
Pattern: "high_entropy",
Excerpt: maskSecret(tok),
RawValue: tok,
Confidence: 0.6,
Hash: generateHash("high_entropy", tok),
})
}
}
}
return findings
}
func scanFileForSecrets(path string, rules map[string]*Rule, config *Config) ([]Finding, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
var findings []Finding
r := bufio.NewReader(f)
lineNo := 0
for {
line, err := r.ReadString('\n')
if err != nil && err != io.EOF {
return findings, err
}
lineNo++
findings = append(findings, scanLine(line, path, lineNo, rules, config)...)
if err == io.EOF {
break
}
}
return findings, nil
}
// git helpers
func gitAvailable() bool {
_, err := exec.LookPath("git")
return err == nil
}
// isGitRepo reports whether root is inside a git working tree.
func isGitRepo(root string) bool {
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
cmd.Dir = root
out, err := cmd.Output()
return err == nil && strings.TrimSpace(string(out)) == "true"
}
func gitAllCommits(root string) ([]string, error) {
cmd := exec.Command("git", "rev-list", "--all")
cmd.Dir = root
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("git rev-list failed: %w (%s)", err, out)
}
lines := strings.Fields(string(out))
return lines, nil
}
func gitShowCommitDiff(root, commit string) (string, error) {
cmd := exec.Command("git", "show", "--pretty=", "--unified=0", commit)
cmd.Dir = root
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("git show %s failed: %w", commit, err)
}
return string(out), nil
}
// hunkHeader matches "@@ -12,3 +14,2 @@", capturing the old and new start lines.
var hunkHeader = regexp.MustCompile(`^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@`)
func scanGitHistory(root string, rules map[string]*Rule, config *Config, stats *Stats) ([]Finding, error) {
if !gitAvailable() {
return nil, errors.New("git not available in PATH")
}
if !isGitRepo(root) {
return nil, fmt.Errorf("%s is not a git repository", root)
}
commits, err := gitAllCommits(root)
if err != nil {
return nil, err
}
var results []Finding
for _, c := range commits {
diff, err := gitShowCommitDiff(root, c)
if err != nil {
// skip commits that fail
continue
}
stats.incrementCommits()
s := bufio.NewScanner(strings.NewReader(diff))
s.Buffer(make([]byte, 0, 64*1024), maxDiffLineBytes)
currentFile := ""
skipCurrentFile := false
oldLine, newLine := 0, 0
for s.Scan() {
line := s.Text()
// Git diff format: "diff --git a/path/to/file b/path/to/file"
if strings.HasPrefix(line, "diff --git") {
parts := strings.Fields(line)
if len(parts) >= 4 {
// parts[3] is b/filename, the new version of the path.
currentFile = strings.TrimPrefix(parts[3], "b/")
skipCurrentFile = shouldSkipFile(currentFile) ||
isExcluded(currentFile, config.ExcludeGlobs)
if config.Verbose && skipCurrentFile {
fmt.Printf("Skipping file in git history: %s (commit: %s)\n", currentFile, c[:8])
}
}
continue
}
// Hunk headers carry the line numbers a finding should point at.
if m := hunkHeader.FindStringSubmatch(line); m != nil {
oldLine = atoiOr(m[1], 0)