detect.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. package detect
  2. import (
  3. "bufio"
  4. "context"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "sync"
  9. "sync/atomic"
  10. "time"
  11. "github.com/zricethezav/gitleaks/v8/config"
  12. "github.com/zricethezav/gitleaks/v8/detect/codec"
  13. "github.com/zricethezav/gitleaks/v8/logging"
  14. "github.com/zricethezav/gitleaks/v8/regexp"
  15. "github.com/zricethezav/gitleaks/v8/report"
  16. "github.com/zricethezav/gitleaks/v8/sources"
  17. ahocorasick "github.com/BobuSumisu/aho-corasick"
  18. "github.com/fatih/semgroup"
  19. "github.com/rs/zerolog"
  20. "github.com/spf13/viper"
  21. "golang.org/x/exp/maps"
  22. )
  23. const (
  24. gitleaksAllowSignature = "gitleaks:allow"
  25. // SlowWarningThreshold is the amount of time to wait before logging that a file is slow.
  26. // This is useful for identifying problematic files and tuning the allowlist.
  27. SlowWarningThreshold = 5 * time.Second
  28. )
  29. var (
  30. newLineRegexp = regexp.MustCompile("\n")
  31. )
  32. // Detector is the main detector struct
  33. type Detector struct {
  34. // Config is the configuration for the detector
  35. Config config.Config
  36. // Redact is a flag to redact findings. This is exported
  37. // so users using gitleaks as a library can set this flag
  38. // without calling `detector.Start(cmd *cobra.Command)`
  39. Redact uint
  40. // verbose is a flag to print findings
  41. Verbose bool
  42. // MaxDecodeDepths limits how many recursive decoding passes are allowed
  43. MaxDecodeDepth int
  44. // MaxArchiveDepth limits how deep the sources will explore nested archives
  45. MaxArchiveDepth int
  46. // files larger than this will be skipped
  47. MaxTargetMegaBytes int
  48. // followSymlinks is a flag to enable scanning symlink files
  49. FollowSymlinks bool
  50. // NoColor is a flag to disable color output
  51. NoColor bool
  52. // IgnoreGitleaksAllow is a flag to ignore gitleaks:allow comments.
  53. IgnoreGitleaksAllow bool
  54. // commitMutex is to prevent concurrent access to the
  55. // commit map when adding commits
  56. commitMutex *sync.Mutex
  57. // commitMap is used to keep track of commits that have been scanned.
  58. // This is only used for logging purposes and git scans.
  59. commitMap map[string]bool
  60. // findingMutex is to prevent concurrent access to the
  61. // findings slice when adding findings.
  62. findingMutex *sync.Mutex
  63. // findings is a slice of report.Findings. This is the result
  64. // of the detector's scan which can then be used to generate a
  65. // report.
  66. findings []report.Finding
  67. // prefilter is a ahocorasick struct used for doing efficient string
  68. // matching given a set of words (keywords from the rules in the config)
  69. prefilter ahocorasick.Trie
  70. // a list of known findings that should be ignored
  71. baseline []report.Finding
  72. // path to baseline
  73. baselinePath string
  74. // gitleaksIgnore
  75. gitleaksIgnore map[string]struct{}
  76. // Sema (https://github.com/fatih/semgroup) controls the concurrency
  77. Sema *semgroup.Group
  78. // report-related settings.
  79. ReportPath string
  80. Reporter report.Reporter
  81. TotalBytes atomic.Uint64
  82. }
  83. // Fragment is an alias for sources.Fragment for backwards compatibility
  84. //
  85. // Deprecated: This will be replaced with sources.Fragment in v9
  86. type Fragment sources.Fragment
  87. // NewDetector creates a new detector with the given config
  88. func NewDetector(cfg config.Config) *Detector {
  89. return &Detector{
  90. commitMap: make(map[string]bool),
  91. gitleaksIgnore: make(map[string]struct{}),
  92. findingMutex: &sync.Mutex{},
  93. commitMutex: &sync.Mutex{},
  94. findings: make([]report.Finding, 0),
  95. Config: cfg,
  96. prefilter: *ahocorasick.NewTrieBuilder().AddStrings(maps.Keys(cfg.Keywords)).Build(),
  97. Sema: semgroup.NewGroup(context.Background(), 40),
  98. }
  99. }
  100. // NewDetectorDefaultConfig creates a new detector with the default config
  101. func NewDetectorDefaultConfig() (*Detector, error) {
  102. viper.SetConfigType("toml")
  103. err := viper.ReadConfig(strings.NewReader(config.DefaultConfig))
  104. if err != nil {
  105. return nil, err
  106. }
  107. var vc config.ViperConfig
  108. err = viper.Unmarshal(&vc)
  109. if err != nil {
  110. return nil, err
  111. }
  112. cfg, err := vc.Translate()
  113. if err != nil {
  114. return nil, err
  115. }
  116. return NewDetector(cfg), nil
  117. }
  118. func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
  119. logging.Debug().Str("path", gitleaksIgnorePath).Msgf("found .gitleaksignore file")
  120. file, err := os.Open(gitleaksIgnorePath)
  121. if err != nil {
  122. return err
  123. }
  124. defer func() {
  125. // https://github.com/securego/gosec/issues/512
  126. if err := file.Close(); err != nil {
  127. logging.Warn().Err(err).Msgf("Error closing .gitleaksignore file")
  128. }
  129. }()
  130. scanner := bufio.NewScanner(file)
  131. replacer := strings.NewReplacer("\\", "/")
  132. for scanner.Scan() {
  133. line := strings.TrimSpace(scanner.Text())
  134. // Skip lines that start with a comment
  135. if line == "" || strings.HasPrefix(line, "#") {
  136. continue
  137. }
  138. // Normalize the path.
  139. // TODO: Make this a breaking change in v9.
  140. s := strings.Split(line, ":")
  141. switch len(s) {
  142. case 3:
  143. // Global fingerprint.
  144. // `file:rule-id:start-line`
  145. s[0] = replacer.Replace(s[0])
  146. case 4:
  147. // Commit fingerprint.
  148. // `commit:file:rule-id:start-line`
  149. s[1] = replacer.Replace(s[1])
  150. default:
  151. logging.Warn().Str("fingerprint", line).Msg("Invalid .gitleaksignore entry")
  152. }
  153. d.gitleaksIgnore[strings.Join(s, ":")] = struct{}{}
  154. }
  155. return nil
  156. }
  157. // DetectBytes scans the given bytes and returns a list of findings
  158. func (d *Detector) DetectBytes(content []byte) []report.Finding {
  159. return d.DetectString(string(content))
  160. }
  161. // DetectString scans the given string and returns a list of findings
  162. func (d *Detector) DetectString(content string) []report.Finding {
  163. return d.Detect(Fragment{
  164. Raw: content,
  165. })
  166. }
  167. // DetectSource scans the given source and returns a list of findings
  168. func (d *Detector) DetectSource(ctx context.Context, source sources.Source) ([]report.Finding, error) {
  169. err := source.Fragments(ctx, func(fragment sources.Fragment, err error) error {
  170. logContext := logging.With()
  171. if len(fragment.FilePath) > 0 {
  172. logContext = logContext.Str("path", fragment.FilePath)
  173. }
  174. if len(fragment.CommitSHA) > 6 {
  175. logContext = logContext.Str("commit", fragment.CommitSHA[:7])
  176. d.addCommit(fragment.CommitSHA)
  177. } else if len(fragment.CommitSHA) > 0 {
  178. logContext = logContext.Str("commit", fragment.CommitSHA)
  179. d.addCommit(fragment.CommitSHA)
  180. logger := logContext.Logger()
  181. logger.Warn().Msg("commit SHAs should be >= 7 characters long")
  182. }
  183. logger := logContext.Logger()
  184. if err != nil {
  185. // Log the error and move on to the next fragment
  186. logger.Error().Err(err).Send()
  187. return nil
  188. }
  189. // both the fragment's content and path should be empty for it to be
  190. // considered empty at this point because of path based matches
  191. if len(fragment.Raw) == 0 && len(fragment.FilePath) == 0 {
  192. logger.Trace().Msg("skipping empty fragment")
  193. return nil
  194. }
  195. var timer *time.Timer
  196. // Only start the timer in debug mode
  197. if logger.GetLevel() <= zerolog.DebugLevel {
  198. timer = time.AfterFunc(SlowWarningThreshold, func() {
  199. logger.Debug().Msgf("Taking longer than %s to inspect fragment", SlowWarningThreshold.String())
  200. })
  201. }
  202. for _, finding := range d.Detect(Fragment(fragment)) {
  203. d.AddFinding(finding)
  204. }
  205. // Stop the timer if it was created
  206. if timer != nil {
  207. timer.Stop()
  208. }
  209. return nil
  210. })
  211. if _, isGit := source.(*sources.Git); isGit {
  212. logging.Info().Msgf("%d commits scanned.", len(d.commitMap))
  213. logging.Debug().Msg("Note: this number might be smaller than expected due to commits with no additions")
  214. }
  215. return d.Findings(), err
  216. }
  217. // Detect scans the given fragment and returns a list of findings
  218. func (d *Detector) Detect(fragment Fragment) []report.Finding {
  219. if fragment.Bytes == nil {
  220. d.TotalBytes.Add(uint64(len(fragment.Raw)))
  221. }
  222. d.TotalBytes.Add(uint64(len(fragment.Bytes)))
  223. var (
  224. findings []report.Finding
  225. logger = func() zerolog.Logger {
  226. l := logging.With().Str("path", fragment.FilePath)
  227. if fragment.CommitSHA != "" {
  228. l = l.Str("commit", fragment.CommitSHA)
  229. }
  230. return l.Logger()
  231. }()
  232. )
  233. // check if filepath is allowed
  234. if fragment.FilePath != "" {
  235. // is the path our config or baseline file?
  236. if fragment.FilePath == d.Config.Path || (d.baselinePath != "" && fragment.FilePath == d.baselinePath) {
  237. logging.Trace().Msg("skipping file: matches config or baseline path")
  238. return findings
  239. }
  240. }
  241. // check if commit or filepath is allowed.
  242. if isAllowed, event := checkCommitOrPathAllowed(logger, fragment, d.Config.Allowlists); isAllowed {
  243. event.Msg("skipping file: global allowlist")
  244. return findings
  245. }
  246. // setup variables to handle different decoding passes
  247. currentRaw := fragment.Raw
  248. encodedSegments := []*codec.EncodedSegment{}
  249. currentDecodeDepth := 0
  250. decoder := codec.NewDecoder()
  251. for {
  252. // build keyword map for prefiltering rules
  253. keywords := make(map[string]bool)
  254. normalizedRaw := strings.ToLower(currentRaw)
  255. matches := d.prefilter.MatchString(normalizedRaw)
  256. for _, m := range matches {
  257. keywords[normalizedRaw[m.Pos():int(m.Pos())+len(m.Match())]] = true
  258. }
  259. for _, rule := range d.Config.Rules {
  260. if len(rule.Keywords) == 0 {
  261. // if no keywords are associated with the rule always scan the
  262. // fragment using the rule
  263. findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
  264. continue
  265. }
  266. // check if keywords are in the fragment
  267. for _, k := range rule.Keywords {
  268. if _, ok := keywords[strings.ToLower(k)]; ok {
  269. findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
  270. break
  271. }
  272. }
  273. }
  274. // increment the depth by 1 as we start our decoding pass
  275. currentDecodeDepth++
  276. // stop the loop if we've hit our max decoding depth
  277. if currentDecodeDepth > d.MaxDecodeDepth {
  278. break
  279. }
  280. // decode the currentRaw for the next pass
  281. currentRaw, encodedSegments = decoder.Decode(currentRaw, encodedSegments)
  282. // stop the loop when there's nothing else to decode
  283. if len(encodedSegments) == 0 {
  284. break
  285. }
  286. }
  287. return filter(findings, d.Redact)
  288. }
  289. // detectRule scans the given fragment for the given rule and returns a list of findings
  290. func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rule, encodedSegments []*codec.EncodedSegment) []report.Finding {
  291. var (
  292. findings []report.Finding
  293. logger = func() zerolog.Logger {
  294. l := logging.With().Str("rule-id", r.RuleID).Str("path", fragment.FilePath)
  295. if fragment.CommitSHA != "" {
  296. l = l.Str("commit", fragment.CommitSHA)
  297. }
  298. return l.Logger()
  299. }()
  300. )
  301. // check if commit or file is allowed for this rule.
  302. if isAllowed, event := checkCommitOrPathAllowed(logger, fragment, r.Allowlists); isAllowed {
  303. event.Msg("skipping file: rule allowlist")
  304. return findings
  305. }
  306. if r.Path != nil {
  307. if r.Regex == nil && len(encodedSegments) == 0 {
  308. // Path _only_ rule
  309. if r.Path.MatchString(fragment.FilePath) || (fragment.WindowsFilePath != "" && r.Path.MatchString(fragment.WindowsFilePath)) {
  310. finding := report.Finding{
  311. Commit: fragment.CommitSHA,
  312. RuleID: r.RuleID,
  313. Description: r.Description,
  314. File: fragment.FilePath,
  315. SymlinkFile: fragment.SymlinkFile,
  316. Match: "file detected: " + fragment.FilePath,
  317. Tags: r.Tags,
  318. }
  319. if fragment.CommitInfo != nil {
  320. finding.Author = fragment.CommitInfo.AuthorName
  321. finding.Date = fragment.CommitInfo.Date
  322. finding.Email = fragment.CommitInfo.AuthorEmail
  323. finding.Link = createScmLink(fragment.CommitInfo.Remote, finding)
  324. finding.Message = fragment.CommitInfo.Message
  325. }
  326. return append(findings, finding)
  327. }
  328. } else {
  329. // if path is set _and_ a regex is set, then we need to check both
  330. // so if the path does not match, then we should return early and not
  331. // consider the regex
  332. if !(r.Path.MatchString(fragment.FilePath) || (fragment.WindowsFilePath != "" && r.Path.MatchString(fragment.WindowsFilePath))) {
  333. return findings
  334. }
  335. }
  336. }
  337. // if path only rule, skip content checks
  338. if r.Regex == nil {
  339. return findings
  340. }
  341. // if flag configure and raw data size bigger then the flag
  342. if d.MaxTargetMegaBytes > 0 {
  343. rawLength := len(currentRaw) / 1_000_000
  344. if rawLength > d.MaxTargetMegaBytes {
  345. logger.Debug().
  346. Int("size", rawLength).
  347. Int("max-size", d.MaxTargetMegaBytes).
  348. Msg("skipping fragment: size")
  349. return findings
  350. }
  351. }
  352. matches := r.Regex.FindAllStringIndex(currentRaw, -1)
  353. if len(matches) == 0 {
  354. return findings
  355. }
  356. // TODO profile this, probably should replace with something more efficient
  357. newlineIndices := newLineRegexp.FindAllStringIndex(fragment.Raw, -1)
  358. // use currentRaw instead of fragment.Raw since this represents the current
  359. // decoding pass on the text
  360. for _, matchIndex := range r.Regex.FindAllStringIndex(currentRaw, -1) {
  361. // Extract secret from match
  362. secret := strings.Trim(currentRaw[matchIndex[0]:matchIndex[1]], "\n")
  363. // For any meta data from decoding
  364. var metaTags []string
  365. currentLine := ""
  366. // Check if the decoded portions of the segment overlap with the match
  367. // to see if its potentially a new match
  368. if len(encodedSegments) > 0 {
  369. segments := codec.SegmentsWithDecodedOverlap(encodedSegments, matchIndex[0], matchIndex[1])
  370. if len(segments) == 0 {
  371. // This item has already been added to a finding
  372. continue
  373. }
  374. matchIndex = codec.AdjustMatchIndex(segments, matchIndex)
  375. metaTags = append(metaTags, codec.Tags(segments)...)
  376. currentLine = codec.CurrentLine(segments, currentRaw)
  377. } else {
  378. // Fixes: https://github.com/gitleaks/gitleaks/issues/1352
  379. // removes the incorrectly following line that was detected by regex expression '\n'
  380. matchIndex[1] = matchIndex[0] + len(secret)
  381. }
  382. // determine location of match. Note that the location
  383. // in the finding will be the line/column numbers of the _match_
  384. // not the _secret_, which will be different if the secretGroup
  385. // value is set for this rule
  386. loc := location(newlineIndices, fragment.Raw, matchIndex)
  387. if matchIndex[1] > loc.endLineIndex {
  388. loc.endLineIndex = matchIndex[1]
  389. }
  390. finding := report.Finding{
  391. Commit: fragment.CommitSHA,
  392. RuleID: r.RuleID,
  393. Description: r.Description,
  394. StartLine: fragment.StartLine + loc.startLine,
  395. EndLine: fragment.StartLine + loc.endLine,
  396. StartColumn: loc.startColumn,
  397. EndColumn: loc.endColumn,
  398. Line: fragment.Raw[loc.startLineIndex:loc.endLineIndex],
  399. Match: secret,
  400. Secret: secret,
  401. File: fragment.FilePath,
  402. SymlinkFile: fragment.SymlinkFile,
  403. Tags: append(r.Tags, metaTags...),
  404. }
  405. if fragment.CommitInfo != nil {
  406. finding.Author = fragment.CommitInfo.AuthorName
  407. finding.Date = fragment.CommitInfo.Date
  408. finding.Email = fragment.CommitInfo.AuthorEmail
  409. finding.Link = createScmLink(fragment.CommitInfo.Remote, finding)
  410. finding.Message = fragment.CommitInfo.Message
  411. }
  412. if !d.IgnoreGitleaksAllow && strings.Contains(finding.Line, gitleaksAllowSignature) {
  413. logger.Trace().
  414. Str("finding", finding.Secret).
  415. Msg("skipping finding: 'gitleaks:allow' signature")
  416. continue
  417. }
  418. if currentLine == "" {
  419. currentLine = finding.Line
  420. }
  421. // Set the value of |secret|, if the pattern contains at least one capture group.
  422. // (The first element is the full match, hence we check >= 2.)
  423. groups := r.Regex.FindStringSubmatch(finding.Secret)
  424. if len(groups) >= 2 {
  425. if r.SecretGroup > 0 {
  426. if len(groups) <= r.SecretGroup {
  427. // Config validation should prevent this
  428. continue
  429. }
  430. finding.Secret = groups[r.SecretGroup]
  431. } else {
  432. // If |secretGroup| is not set, we will use the first suitable capture group.
  433. for _, s := range groups[1:] {
  434. if len(s) > 0 {
  435. finding.Secret = s
  436. break
  437. }
  438. }
  439. }
  440. }
  441. // check entropy
  442. entropy := shannonEntropy(finding.Secret)
  443. finding.Entropy = float32(entropy)
  444. if r.Entropy != 0.0 {
  445. // entropy is too low, skip this finding
  446. if entropy <= r.Entropy {
  447. logger.Trace().
  448. Str("finding", finding.Secret).
  449. Float32("entropy", finding.Entropy).
  450. Msg("skipping finding: low entropy")
  451. continue
  452. }
  453. }
  454. // check if the result matches any of the global allowlists.
  455. if isAllowed, event := checkFindingAllowed(logger, finding, fragment, currentLine, d.Config.Allowlists); isAllowed {
  456. event.Msg("skipping finding: global allowlist")
  457. continue
  458. }
  459. // check if the result matches any of the rule allowlists.
  460. if isAllowed, event := checkFindingAllowed(logger, finding, fragment, currentLine, r.Allowlists); isAllowed {
  461. event.Msg("skipping finding: rule allowlist")
  462. continue
  463. }
  464. findings = append(findings, finding)
  465. }
  466. return findings
  467. }
  468. // AddFinding synchronously adds a finding to the findings slice
  469. func (d *Detector) AddFinding(finding report.Finding) {
  470. globalFingerprint := fmt.Sprintf("%s:%s:%d", finding.File, finding.RuleID, finding.StartLine)
  471. if finding.Commit != "" {
  472. finding.Fingerprint = fmt.Sprintf("%s:%s:%s:%d", finding.Commit, finding.File, finding.RuleID, finding.StartLine)
  473. } else {
  474. finding.Fingerprint = globalFingerprint
  475. }
  476. // check if we should ignore this finding
  477. logger := logging.With().Str("finding", finding.Secret).Logger()
  478. if _, ok := d.gitleaksIgnore[globalFingerprint]; ok {
  479. logger.Debug().
  480. Str("fingerprint", globalFingerprint).
  481. Msg("skipping finding: global fingerprint")
  482. return
  483. } else if finding.Commit != "" {
  484. // Awkward nested if because I'm not sure how to chain these two conditions.
  485. if _, ok := d.gitleaksIgnore[finding.Fingerprint]; ok {
  486. logger.Debug().
  487. Str("fingerprint", finding.Fingerprint).
  488. Msgf("skipping finding: fingerprint")
  489. return
  490. }
  491. }
  492. if d.baseline != nil && !IsNew(finding, d.Redact, d.baseline) {
  493. logger.Debug().
  494. Str("fingerprint", finding.Fingerprint).
  495. Msgf("skipping finding: baseline")
  496. return
  497. }
  498. d.findingMutex.Lock()
  499. d.findings = append(d.findings, finding)
  500. if d.Verbose {
  501. printFinding(finding, d.NoColor)
  502. }
  503. d.findingMutex.Unlock()
  504. }
  505. // Findings returns the findings added to the detector
  506. func (d *Detector) Findings() []report.Finding {
  507. return d.findings
  508. }
  509. // AddCommit synchronously adds a commit to the commit slice
  510. func (d *Detector) addCommit(commit string) {
  511. d.commitMutex.Lock()
  512. d.commitMap[commit] = true
  513. d.commitMutex.Unlock()
  514. }
  515. // checkCommitOrPathAllowed evaluates |fragment| against all provided |allowlists|.
  516. //
  517. // If the match condition is "OR", only commit and path are checked.
  518. // Otherwise, if regexes or stopwords are defined this will fail.
  519. func checkCommitOrPathAllowed(
  520. logger zerolog.Logger,
  521. fragment Fragment,
  522. allowlists []*config.Allowlist,
  523. ) (bool, *zerolog.Event) {
  524. if fragment.FilePath == "" && fragment.CommitSHA == "" {
  525. return false, nil
  526. }
  527. for _, a := range allowlists {
  528. var (
  529. isAllowed bool
  530. allowlistChecks []bool
  531. commitAllowed, _ = a.CommitAllowed(fragment.CommitSHA)
  532. pathAllowed = a.PathAllowed(fragment.FilePath) || (fragment.WindowsFilePath != "" && a.PathAllowed(fragment.WindowsFilePath))
  533. )
  534. // If the condition is "AND" we need to check all conditions.
  535. if a.MatchCondition == config.AllowlistMatchAnd {
  536. if len(a.Commits) > 0 {
  537. allowlistChecks = append(allowlistChecks, commitAllowed)
  538. }
  539. if len(a.Paths) > 0 {
  540. allowlistChecks = append(allowlistChecks, pathAllowed)
  541. }
  542. // These will be checked later.
  543. if len(a.Regexes) > 0 {
  544. continue
  545. }
  546. if len(a.StopWords) > 0 {
  547. continue
  548. }
  549. isAllowed = allTrue(allowlistChecks)
  550. } else {
  551. isAllowed = commitAllowed || pathAllowed
  552. }
  553. if isAllowed {
  554. event := logger.Trace().Str("condition", a.MatchCondition.String())
  555. if commitAllowed {
  556. event.Bool("allowed-commit", commitAllowed)
  557. }
  558. if pathAllowed {
  559. event.Bool("allowed-path", pathAllowed)
  560. }
  561. return true, event
  562. }
  563. }
  564. return false, nil
  565. }
  566. // checkFindingAllowed evaluates |finding| against all provided |allowlists|.
  567. //
  568. // If the match condition is "OR", only regex and stopwords are run. (Commit and path should be handled separately).
  569. // Otherwise, all conditions are checked.
  570. //
  571. // TODO: The method signature is awkward. I can't think of a better way to log helpful info.
  572. func checkFindingAllowed(
  573. logger zerolog.Logger,
  574. finding report.Finding,
  575. fragment Fragment,
  576. currentLine string,
  577. allowlists []*config.Allowlist,
  578. ) (bool, *zerolog.Event) {
  579. for _, a := range allowlists {
  580. allowlistTarget := finding.Secret
  581. switch a.RegexTarget {
  582. case "match":
  583. allowlistTarget = finding.Match
  584. case "line":
  585. allowlistTarget = currentLine
  586. }
  587. var (
  588. checks []bool
  589. isAllowed bool
  590. commitAllowed bool
  591. commit string
  592. pathAllowed bool
  593. regexAllowed = a.RegexAllowed(allowlistTarget)
  594. containsStopword, word = a.ContainsStopWord(finding.Secret)
  595. )
  596. // If the condition is "AND" we need to check all conditions.
  597. if a.MatchCondition == config.AllowlistMatchAnd {
  598. // Determine applicable checks.
  599. if len(a.Commits) > 0 {
  600. commitAllowed, commit = a.CommitAllowed(fragment.CommitSHA)
  601. checks = append(checks, commitAllowed)
  602. }
  603. if len(a.Paths) > 0 {
  604. pathAllowed = a.PathAllowed(fragment.FilePath) || (fragment.WindowsFilePath != "" && a.PathAllowed(fragment.WindowsFilePath))
  605. checks = append(checks, pathAllowed)
  606. }
  607. if len(a.Regexes) > 0 {
  608. checks = append(checks, regexAllowed)
  609. }
  610. if len(a.StopWords) > 0 {
  611. checks = append(checks, containsStopword)
  612. }
  613. isAllowed = allTrue(checks)
  614. } else {
  615. isAllowed = regexAllowed || containsStopword
  616. }
  617. if isAllowed {
  618. event := logger.Trace().
  619. Str("finding", finding.Secret).
  620. Str("condition", a.MatchCondition.String())
  621. if commitAllowed {
  622. event.Str("allowed-commit", commit)
  623. }
  624. if pathAllowed {
  625. event.Bool("allowed-path", pathAllowed)
  626. }
  627. if regexAllowed {
  628. event.Bool("allowed-regex", regexAllowed)
  629. }
  630. if containsStopword {
  631. event.Str("allowed-stopword", word)
  632. }
  633. return true, event
  634. }
  635. }
  636. return false, nil
  637. }
  638. func allTrue(bools []bool) bool {
  639. for _, check := range bools {
  640. if !check {
  641. return false
  642. }
  643. }
  644. return true
  645. }