4
0

detect.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 NewDetectorContext(context.Background(), cfg)
  90. }
  91. // NewDetectorContext is the same as NewDetector but supports passing in a
  92. // context to use for timeouts
  93. func NewDetectorContext(ctx context.Context, cfg config.Config) *Detector {
  94. return &Detector{
  95. commitMap: make(map[string]bool),
  96. gitleaksIgnore: make(map[string]struct{}),
  97. findingMutex: &sync.Mutex{},
  98. commitMutex: &sync.Mutex{},
  99. findings: make([]report.Finding, 0),
  100. Config: cfg,
  101. prefilter: *ahocorasick.NewTrieBuilder().AddStrings(maps.Keys(cfg.Keywords)).Build(),
  102. Sema: semgroup.NewGroup(ctx, 40),
  103. }
  104. }
  105. // NewDetectorDefaultConfig creates a new detector with the default config
  106. func NewDetectorDefaultConfig() (*Detector, error) {
  107. viper.SetConfigType("toml")
  108. err := viper.ReadConfig(strings.NewReader(config.DefaultConfig))
  109. if err != nil {
  110. return nil, err
  111. }
  112. var vc config.ViperConfig
  113. err = viper.Unmarshal(&vc)
  114. if err != nil {
  115. return nil, err
  116. }
  117. cfg, err := vc.Translate()
  118. if err != nil {
  119. return nil, err
  120. }
  121. return NewDetector(cfg), nil
  122. }
  123. func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
  124. logging.Debug().Str("path", gitleaksIgnorePath).Msgf("found .gitleaksignore file")
  125. file, err := os.Open(gitleaksIgnorePath)
  126. if err != nil {
  127. return err
  128. }
  129. defer func() {
  130. // https://github.com/securego/gosec/issues/512
  131. if err := file.Close(); err != nil {
  132. logging.Warn().Err(err).Msgf("Error closing .gitleaksignore file")
  133. }
  134. }()
  135. scanner := bufio.NewScanner(file)
  136. replacer := strings.NewReplacer("\\", "/")
  137. for scanner.Scan() {
  138. line := strings.TrimSpace(scanner.Text())
  139. // Skip lines that start with a comment
  140. if line == "" || strings.HasPrefix(line, "#") {
  141. continue
  142. }
  143. // Normalize the path.
  144. // TODO: Make this a breaking change in v9.
  145. s := strings.Split(line, ":")
  146. switch len(s) {
  147. case 3:
  148. // Global fingerprint.
  149. // `file:rule-id:start-line`
  150. s[0] = replacer.Replace(s[0])
  151. case 4:
  152. // Commit fingerprint.
  153. // `commit:file:rule-id:start-line`
  154. s[1] = replacer.Replace(s[1])
  155. default:
  156. logging.Warn().Str("fingerprint", line).Msg("Invalid .gitleaksignore entry")
  157. }
  158. d.gitleaksIgnore[strings.Join(s, ":")] = struct{}{}
  159. }
  160. return nil
  161. }
  162. // DetectBytes scans the given bytes and returns a list of findings
  163. func (d *Detector) DetectBytes(content []byte) []report.Finding {
  164. return d.DetectString(string(content))
  165. }
  166. // DetectString scans the given string and returns a list of findings
  167. func (d *Detector) DetectString(content string) []report.Finding {
  168. return d.Detect(Fragment{
  169. Raw: content,
  170. })
  171. }
  172. // DetectSource scans the given source and returns a list of findings
  173. func (d *Detector) DetectSource(ctx context.Context, source sources.Source) ([]report.Finding, error) {
  174. err := source.Fragments(ctx, func(fragment sources.Fragment, err error) error {
  175. logContext := logging.With()
  176. if len(fragment.FilePath) > 0 {
  177. logContext = logContext.Str("path", fragment.FilePath)
  178. }
  179. if len(fragment.CommitSHA) > 6 {
  180. logContext = logContext.Str("commit", fragment.CommitSHA[:7])
  181. d.addCommit(fragment.CommitSHA)
  182. } else if len(fragment.CommitSHA) > 0 {
  183. logContext = logContext.Str("commit", fragment.CommitSHA)
  184. d.addCommit(fragment.CommitSHA)
  185. logger := logContext.Logger()
  186. logger.Warn().Msg("commit SHAs should be >= 7 characters long")
  187. }
  188. logger := logContext.Logger()
  189. if err != nil {
  190. // Log the error and move on to the next fragment
  191. logger.Error().Err(err).Send()
  192. return nil
  193. }
  194. // both the fragment's content and path should be empty for it to be
  195. // considered empty at this point because of path based matches
  196. if len(fragment.Raw) == 0 && len(fragment.FilePath) == 0 {
  197. logger.Trace().Msg("skipping empty fragment")
  198. return nil
  199. }
  200. var timer *time.Timer
  201. // Only start the timer in debug mode
  202. if logger.GetLevel() <= zerolog.DebugLevel {
  203. timer = time.AfterFunc(SlowWarningThreshold, func() {
  204. logger.Debug().Msgf("Taking longer than %s to inspect fragment", SlowWarningThreshold.String())
  205. })
  206. }
  207. for _, finding := range d.DetectContext(ctx, Fragment(fragment)) {
  208. d.AddFinding(finding)
  209. }
  210. // Stop the timer if it was created
  211. if timer != nil {
  212. timer.Stop()
  213. }
  214. return nil
  215. })
  216. if _, isGit := source.(*sources.Git); isGit {
  217. logging.Info().Msgf("%d commits scanned.", len(d.commitMap))
  218. logging.Debug().Msg("Note: this number might be smaller than expected due to commits with no additions")
  219. }
  220. return d.Findings(), err
  221. }
  222. // Detect scans the given fragment and returns a list of findings
  223. func (d *Detector) Detect(fragment Fragment) []report.Finding {
  224. return d.DetectContext(context.Background(), fragment)
  225. }
  226. // DetectContext is the same as Detect but supports passing in a
  227. // context to use for timeouts
  228. func (d *Detector) DetectContext(ctx context.Context, fragment Fragment) []report.Finding {
  229. if fragment.Bytes == nil {
  230. d.TotalBytes.Add(uint64(len(fragment.Raw)))
  231. }
  232. d.TotalBytes.Add(uint64(len(fragment.Bytes)))
  233. var (
  234. findings []report.Finding
  235. logger = func() zerolog.Logger {
  236. l := logging.With().Str("path", fragment.FilePath)
  237. if fragment.CommitSHA != "" {
  238. l = l.Str("commit", fragment.CommitSHA)
  239. }
  240. return l.Logger()
  241. }()
  242. )
  243. // check if filepath is allowed
  244. if fragment.FilePath != "" {
  245. // is the path our config or baseline file?
  246. if fragment.FilePath == d.Config.Path || (d.baselinePath != "" && fragment.FilePath == d.baselinePath) {
  247. logging.Trace().Msg("skipping file: matches config or baseline path")
  248. return findings
  249. }
  250. }
  251. // check if commit or filepath is allowed.
  252. if isAllowed, event := checkCommitOrPathAllowed(logger, fragment, d.Config.Allowlists); isAllowed {
  253. event.Msg("skipping file: global allowlist")
  254. return findings
  255. }
  256. // setup variables to handle different decoding passes
  257. currentRaw := fragment.Raw
  258. encodedSegments := []*codec.EncodedSegment{}
  259. currentDecodeDepth := 0
  260. decoder := codec.NewDecoder()
  261. ScanLoop:
  262. for {
  263. select {
  264. case <-ctx.Done():
  265. break ScanLoop
  266. default:
  267. // build keyword map for prefiltering rules
  268. keywords := make(map[string]bool)
  269. normalizedRaw := strings.ToLower(currentRaw)
  270. matches := d.prefilter.MatchString(normalizedRaw)
  271. for _, m := range matches {
  272. keywords[normalizedRaw[m.Pos():int(m.Pos())+len(m.Match())]] = true
  273. }
  274. for _, rule := range d.Config.Rules {
  275. select {
  276. case <-ctx.Done():
  277. break ScanLoop
  278. default:
  279. if len(rule.Keywords) == 0 {
  280. // if no keywords are associated with the rule always scan the
  281. // fragment using the rule
  282. findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
  283. continue
  284. }
  285. // check if keywords are in the fragment
  286. for _, k := range rule.Keywords {
  287. if _, ok := keywords[strings.ToLower(k)]; ok {
  288. findings = append(findings, d.detectRule(fragment, currentRaw, rule, encodedSegments)...)
  289. break
  290. }
  291. }
  292. }
  293. }
  294. // increment the depth by 1 as we start our decoding pass
  295. currentDecodeDepth++
  296. // stop the loop if we've hit our max decoding depth
  297. if currentDecodeDepth > d.MaxDecodeDepth {
  298. break ScanLoop
  299. }
  300. // decode the currentRaw for the next pass
  301. currentRaw, encodedSegments = decoder.Decode(currentRaw, encodedSegments)
  302. // stop the loop when there's nothing else to decode
  303. if len(encodedSegments) == 0 {
  304. break ScanLoop
  305. }
  306. }
  307. }
  308. return filter(findings, d.Redact)
  309. }
  310. // detectRule scans the given fragment for the given rule and returns a list of findings
  311. func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rule, encodedSegments []*codec.EncodedSegment) []report.Finding {
  312. var (
  313. findings []report.Finding
  314. logger = func() zerolog.Logger {
  315. l := logging.With().Str("rule-id", r.RuleID).Str("path", fragment.FilePath)
  316. if fragment.CommitSHA != "" {
  317. l = l.Str("commit", fragment.CommitSHA)
  318. }
  319. return l.Logger()
  320. }()
  321. )
  322. if r.SkipReport && !fragment.InheritedFromFinding {
  323. return findings
  324. }
  325. // check if commit or file is allowed for this rule.
  326. if isAllowed, event := checkCommitOrPathAllowed(logger, fragment, r.Allowlists); isAllowed {
  327. event.Msg("skipping file: rule allowlist")
  328. return findings
  329. }
  330. if r.Path != nil {
  331. if r.Regex == nil && len(encodedSegments) == 0 {
  332. // Path _only_ rule
  333. if r.Path.MatchString(fragment.FilePath) || (fragment.WindowsFilePath != "" && r.Path.MatchString(fragment.WindowsFilePath)) {
  334. finding := report.Finding{
  335. Commit: fragment.CommitSHA,
  336. RuleID: r.RuleID,
  337. Description: r.Description,
  338. File: fragment.FilePath,
  339. SymlinkFile: fragment.SymlinkFile,
  340. Match: "file detected: " + fragment.FilePath,
  341. Tags: r.Tags,
  342. }
  343. if fragment.CommitInfo != nil {
  344. finding.Author = fragment.CommitInfo.AuthorName
  345. finding.Date = fragment.CommitInfo.Date
  346. finding.Email = fragment.CommitInfo.AuthorEmail
  347. finding.Link = createScmLink(fragment.CommitInfo.Remote, finding)
  348. finding.Message = fragment.CommitInfo.Message
  349. }
  350. return append(findings, finding)
  351. }
  352. } else {
  353. // if path is set _and_ a regex is set, then we need to check both
  354. // so if the path does not match, then we should return early and not
  355. // consider the regex
  356. if !(r.Path.MatchString(fragment.FilePath) || (fragment.WindowsFilePath != "" && r.Path.MatchString(fragment.WindowsFilePath))) {
  357. return findings
  358. }
  359. }
  360. }
  361. // if path only rule, skip content checks
  362. if r.Regex == nil {
  363. return findings
  364. }
  365. // if flag configure and raw data size bigger then the flag
  366. if d.MaxTargetMegaBytes > 0 {
  367. rawLength := len(currentRaw) / 1_000_000
  368. if rawLength > d.MaxTargetMegaBytes {
  369. logger.Debug().
  370. Int("size", rawLength).
  371. Int("max-size", d.MaxTargetMegaBytes).
  372. Msg("skipping fragment: size")
  373. return findings
  374. }
  375. }
  376. matches := r.Regex.FindAllStringIndex(currentRaw, -1)
  377. if len(matches) == 0 {
  378. return findings
  379. }
  380. // TODO profile this, probably should replace with something more efficient
  381. newlineIndices := newLineRegexp.FindAllStringIndex(fragment.Raw, -1)
  382. // use currentRaw instead of fragment.Raw since this represents the current
  383. // decoding pass on the text
  384. for _, matchIndex := range r.Regex.FindAllStringIndex(currentRaw, -1) {
  385. // Extract secret from match
  386. secret := strings.Trim(currentRaw[matchIndex[0]:matchIndex[1]], "\n")
  387. // For any meta data from decoding
  388. var metaTags []string
  389. currentLine := ""
  390. // Check if the decoded portions of the segment overlap with the match
  391. // to see if its potentially a new match
  392. if len(encodedSegments) > 0 {
  393. segments := codec.SegmentsWithDecodedOverlap(encodedSegments, matchIndex[0], matchIndex[1])
  394. if len(segments) == 0 {
  395. // This item has already been added to a finding
  396. continue
  397. }
  398. matchIndex = codec.AdjustMatchIndex(segments, matchIndex)
  399. metaTags = append(metaTags, codec.Tags(segments)...)
  400. currentLine = codec.CurrentLine(segments, currentRaw)
  401. } else {
  402. // Fixes: https://github.com/gitleaks/gitleaks/issues/1352
  403. // removes the incorrectly following line that was detected by regex expression '\n'
  404. matchIndex[1] = matchIndex[0] + len(secret)
  405. }
  406. // determine location of match. Note that the location
  407. // in the finding will be the line/column numbers of the _match_
  408. // not the _secret_, which will be different if the secretGroup
  409. // value is set for this rule
  410. loc := location(newlineIndices, fragment.Raw, matchIndex)
  411. if matchIndex[1] > loc.endLineIndex {
  412. loc.endLineIndex = matchIndex[1]
  413. }
  414. finding := report.Finding{
  415. Commit: fragment.CommitSHA,
  416. RuleID: r.RuleID,
  417. Description: r.Description,
  418. StartLine: fragment.StartLine + loc.startLine,
  419. EndLine: fragment.StartLine + loc.endLine,
  420. StartColumn: loc.startColumn,
  421. EndColumn: loc.endColumn,
  422. Line: fragment.Raw[loc.startLineIndex:loc.endLineIndex],
  423. Match: secret,
  424. Secret: secret,
  425. File: fragment.FilePath,
  426. SymlinkFile: fragment.SymlinkFile,
  427. Tags: append(r.Tags, metaTags...),
  428. }
  429. if fragment.CommitInfo != nil {
  430. finding.Author = fragment.CommitInfo.AuthorName
  431. finding.Date = fragment.CommitInfo.Date
  432. finding.Email = fragment.CommitInfo.AuthorEmail
  433. finding.Link = createScmLink(fragment.CommitInfo.Remote, finding)
  434. finding.Message = fragment.CommitInfo.Message
  435. }
  436. if !d.IgnoreGitleaksAllow && strings.Contains(finding.Line, gitleaksAllowSignature) {
  437. logger.Trace().
  438. Str("finding", finding.Secret).
  439. Msg("skipping finding: 'gitleaks:allow' signature")
  440. continue
  441. }
  442. if currentLine == "" {
  443. currentLine = finding.Line
  444. }
  445. // Set the value of |secret|, if the pattern contains at least one capture group.
  446. // (The first element is the full match, hence we check >= 2.)
  447. groups := r.Regex.FindStringSubmatch(finding.Secret)
  448. if len(groups) >= 2 {
  449. if r.SecretGroup > 0 {
  450. if len(groups) <= r.SecretGroup {
  451. // Config validation should prevent this
  452. continue
  453. }
  454. finding.Secret = groups[r.SecretGroup]
  455. } else {
  456. // If |secretGroup| is not set, we will use the first suitable capture group.
  457. for _, s := range groups[1:] {
  458. if len(s) > 0 {
  459. finding.Secret = s
  460. break
  461. }
  462. }
  463. }
  464. }
  465. // check entropy
  466. entropy := shannonEntropy(finding.Secret)
  467. finding.Entropy = float32(entropy)
  468. if r.Entropy != 0.0 {
  469. // entropy is too low, skip this finding
  470. if entropy <= r.Entropy {
  471. logger.Trace().
  472. Str("finding", finding.Secret).
  473. Float32("entropy", finding.Entropy).
  474. Msg("skipping finding: low entropy")
  475. continue
  476. }
  477. }
  478. // check if the result matches any of the global allowlists.
  479. if isAllowed, event := checkFindingAllowed(logger, finding, fragment, currentLine, d.Config.Allowlists); isAllowed {
  480. event.Msg("skipping finding: global allowlist")
  481. continue
  482. }
  483. // check if the result matches any of the rule allowlists.
  484. if isAllowed, event := checkFindingAllowed(logger, finding, fragment, currentLine, r.Allowlists); isAllowed {
  485. event.Msg("skipping finding: rule allowlist")
  486. continue
  487. }
  488. findings = append(findings, finding)
  489. }
  490. // Handle required rules (multi-part rules)
  491. if fragment.InheritedFromFinding || len(r.RequiredRules) == 0 {
  492. return findings
  493. }
  494. // Process required rules and create findings with auxiliary findings
  495. return d.processRequiredRules(fragment, currentRaw, r, encodedSegments, findings, logger)
  496. }
  497. // processRequiredRules handles the logic for multi-part rules with auxiliary findings
  498. func (d *Detector) processRequiredRules(fragment Fragment, currentRaw string, r config.Rule, encodedSegments []*codec.EncodedSegment, primaryFindings []report.Finding, logger zerolog.Logger) []report.Finding {
  499. if len(primaryFindings) == 0 {
  500. logger.Debug().Msg("no primary findings to process for required rules")
  501. return primaryFindings
  502. }
  503. // Pre-collect all required rule findings once
  504. allRequiredFindings := make(map[string][]report.Finding)
  505. for _, requiredRule := range r.RequiredRules {
  506. rule, ok := d.Config.Rules[requiredRule.RuleID]
  507. if !ok {
  508. logger.Error().Str("rule-id", requiredRule.RuleID).Msg("required rule not found in config")
  509. continue
  510. }
  511. // Mark fragment as inherited to prevent infinite recursion
  512. inheritedFragment := fragment
  513. inheritedFragment.InheritedFromFinding = true
  514. // Call detectRule once for each required rule
  515. requiredFindings := d.detectRule(inheritedFragment, currentRaw, rule, encodedSegments)
  516. allRequiredFindings[requiredRule.RuleID] = requiredFindings
  517. logger.Debug().
  518. Str("rule-id", requiredRule.RuleID).
  519. Int("findings", len(requiredFindings)).
  520. Msg("collected required rule findings")
  521. }
  522. var finalFindings []report.Finding
  523. // Now process each primary finding against the pre-collected required findings
  524. for _, primaryFinding := range primaryFindings {
  525. var requiredFindings []*report.RequiredFinding
  526. for _, requiredRule := range r.RequiredRules {
  527. foundRequiredFindings, exists := allRequiredFindings[requiredRule.RuleID]
  528. if !exists {
  529. continue // Rule wasn't found earlier, skip
  530. }
  531. // Filter findings that are within proximity of the primary finding
  532. for _, requiredFinding := range foundRequiredFindings {
  533. if d.withinProximity(primaryFinding, requiredFinding, requiredRule) {
  534. req := &report.RequiredFinding{
  535. RuleID: requiredFinding.RuleID,
  536. StartLine: requiredFinding.StartLine,
  537. EndLine: requiredFinding.EndLine,
  538. StartColumn: requiredFinding.StartColumn,
  539. EndColumn: requiredFinding.EndColumn,
  540. Line: requiredFinding.Line,
  541. Match: requiredFinding.Match,
  542. Secret: requiredFinding.Secret,
  543. }
  544. requiredFindings = append(requiredFindings, req)
  545. }
  546. }
  547. }
  548. // Check if we have at least one auxiliary finding for each required rule
  549. if len(requiredFindings) > 0 && d.hasAllRequiredRules(requiredFindings, r.RequiredRules) {
  550. // Create a finding with auxiliary findings
  551. newFinding := primaryFinding // Copy the primary finding
  552. newFinding.AddRequiredFindings(requiredFindings)
  553. finalFindings = append(finalFindings, newFinding)
  554. logger.Debug().
  555. Str("primary-rule", r.RuleID).
  556. Int("primary-line", primaryFinding.StartLine).
  557. Int("auxiliary-count", len(requiredFindings)).
  558. Msg("multi-part rule satisfied")
  559. }
  560. }
  561. return finalFindings
  562. }
  563. // hasAllRequiredRules checks if we have at least one auxiliary finding for each required rule
  564. func (d *Detector) hasAllRequiredRules(auxiliaryFindings []*report.RequiredFinding, requiredRules []*config.Required) bool {
  565. foundRules := make(map[string]bool)
  566. // AuxiliaryFinding
  567. for _, aux := range auxiliaryFindings {
  568. foundRules[aux.RuleID] = true
  569. }
  570. for _, required := range requiredRules {
  571. if !foundRules[required.RuleID] {
  572. return false
  573. }
  574. }
  575. return true
  576. }
  577. func (d *Detector) withinProximity(primary, required report.Finding, requiredRule *config.Required) bool {
  578. // fmt.Println(requiredRule.WithinLines)
  579. // If neither within_lines nor within_columns is set, findings just need to be in the same fragment
  580. if requiredRule.WithinLines == nil && requiredRule.WithinColumns == nil {
  581. return true
  582. }
  583. // Check line proximity (vertical distance)
  584. if requiredRule.WithinLines != nil {
  585. lineDiff := abs(primary.StartLine - required.StartLine)
  586. if lineDiff > *requiredRule.WithinLines {
  587. return false
  588. }
  589. }
  590. // Check column proximity (horizontal distance)
  591. if requiredRule.WithinColumns != nil {
  592. // Use the start column of each finding for proximity calculation
  593. colDiff := abs(primary.StartColumn - required.StartColumn)
  594. if colDiff > *requiredRule.WithinColumns {
  595. return false
  596. }
  597. }
  598. return true
  599. }
  600. // abs returns the absolute value of an integer
  601. func abs(x int) int {
  602. if x < 0 {
  603. return -x
  604. }
  605. return x
  606. }
  607. // AddFinding synchronously adds a finding to the findings slice
  608. func (d *Detector) AddFinding(finding report.Finding) {
  609. globalFingerprint := fmt.Sprintf("%s:%s:%d", finding.File, finding.RuleID, finding.StartLine)
  610. if finding.Commit != "" {
  611. finding.Fingerprint = fmt.Sprintf("%s:%s:%s:%d", finding.Commit, finding.File, finding.RuleID, finding.StartLine)
  612. } else {
  613. finding.Fingerprint = globalFingerprint
  614. }
  615. // check if we should ignore this finding
  616. logger := logging.With().Str("finding", finding.Secret).Logger()
  617. if _, ok := d.gitleaksIgnore[globalFingerprint]; ok {
  618. logger.Debug().
  619. Str("fingerprint", globalFingerprint).
  620. Msg("skipping finding: global fingerprint")
  621. return
  622. } else if finding.Commit != "" {
  623. // Awkward nested if because I'm not sure how to chain these two conditions.
  624. if _, ok := d.gitleaksIgnore[finding.Fingerprint]; ok {
  625. logger.Debug().
  626. Str("fingerprint", finding.Fingerprint).
  627. Msgf("skipping finding: fingerprint")
  628. return
  629. }
  630. }
  631. if d.baseline != nil && !IsNew(finding, d.Redact, d.baseline) {
  632. logger.Debug().
  633. Str("fingerprint", finding.Fingerprint).
  634. Msgf("skipping finding: baseline")
  635. return
  636. }
  637. d.findingMutex.Lock()
  638. d.findings = append(d.findings, finding)
  639. if d.Verbose {
  640. printFinding(finding, d.NoColor)
  641. }
  642. d.findingMutex.Unlock()
  643. }
  644. // Findings returns the findings added to the detector
  645. func (d *Detector) Findings() []report.Finding {
  646. return d.findings
  647. }
  648. // AddCommit synchronously adds a commit to the commit slice
  649. func (d *Detector) addCommit(commit string) {
  650. d.commitMutex.Lock()
  651. d.commitMap[commit] = true
  652. d.commitMutex.Unlock()
  653. }
  654. // checkCommitOrPathAllowed evaluates |fragment| against all provided |allowlists|.
  655. //
  656. // If the match condition is "OR", only commit and path are checked.
  657. // Otherwise, if regexes or stopwords are defined this will fail.
  658. func checkCommitOrPathAllowed(
  659. logger zerolog.Logger,
  660. fragment Fragment,
  661. allowlists []*config.Allowlist,
  662. ) (bool, *zerolog.Event) {
  663. if fragment.FilePath == "" && fragment.CommitSHA == "" {
  664. return false, nil
  665. }
  666. for _, a := range allowlists {
  667. var (
  668. isAllowed bool
  669. allowlistChecks []bool
  670. commitAllowed, _ = a.CommitAllowed(fragment.CommitSHA)
  671. pathAllowed = a.PathAllowed(fragment.FilePath) || (fragment.WindowsFilePath != "" && a.PathAllowed(fragment.WindowsFilePath))
  672. )
  673. // If the condition is "AND" we need to check all conditions.
  674. if a.MatchCondition == config.AllowlistMatchAnd {
  675. if len(a.Commits) > 0 {
  676. allowlistChecks = append(allowlistChecks, commitAllowed)
  677. }
  678. if len(a.Paths) > 0 {
  679. allowlistChecks = append(allowlistChecks, pathAllowed)
  680. }
  681. // These will be checked later.
  682. if len(a.Regexes) > 0 {
  683. continue
  684. }
  685. if len(a.StopWords) > 0 {
  686. continue
  687. }
  688. isAllowed = allTrue(allowlistChecks)
  689. } else {
  690. isAllowed = commitAllowed || pathAllowed
  691. }
  692. if isAllowed {
  693. event := logger.Trace().Str("condition", a.MatchCondition.String())
  694. if commitAllowed {
  695. event.Bool("allowed-commit", commitAllowed)
  696. }
  697. if pathAllowed {
  698. event.Bool("allowed-path", pathAllowed)
  699. }
  700. return true, event
  701. }
  702. }
  703. return false, nil
  704. }
  705. // checkFindingAllowed evaluates |finding| against all provided |allowlists|.
  706. //
  707. // If the match condition is "OR", only regex and stopwords are run. (Commit and path should be handled separately).
  708. // Otherwise, all conditions are checked.
  709. //
  710. // TODO: The method signature is awkward. I can't think of a better way to log helpful info.
  711. func checkFindingAllowed(
  712. logger zerolog.Logger,
  713. finding report.Finding,
  714. fragment Fragment,
  715. currentLine string,
  716. allowlists []*config.Allowlist,
  717. ) (bool, *zerolog.Event) {
  718. for _, a := range allowlists {
  719. allowlistTarget := finding.Secret
  720. switch a.RegexTarget {
  721. case "match":
  722. allowlistTarget = finding.Match
  723. case "line":
  724. allowlistTarget = currentLine
  725. }
  726. var (
  727. checks []bool
  728. isAllowed bool
  729. commitAllowed bool
  730. commit string
  731. pathAllowed bool
  732. regexAllowed = a.RegexAllowed(allowlistTarget)
  733. containsStopword, word = a.ContainsStopWord(finding.Secret)
  734. )
  735. // If the condition is "AND" we need to check all conditions.
  736. if a.MatchCondition == config.AllowlistMatchAnd {
  737. // Determine applicable checks.
  738. if len(a.Commits) > 0 {
  739. commitAllowed, commit = a.CommitAllowed(fragment.CommitSHA)
  740. checks = append(checks, commitAllowed)
  741. }
  742. if len(a.Paths) > 0 {
  743. pathAllowed = a.PathAllowed(fragment.FilePath) || (fragment.WindowsFilePath != "" && a.PathAllowed(fragment.WindowsFilePath))
  744. checks = append(checks, pathAllowed)
  745. }
  746. if len(a.Regexes) > 0 {
  747. checks = append(checks, regexAllowed)
  748. }
  749. if len(a.StopWords) > 0 {
  750. checks = append(checks, containsStopword)
  751. }
  752. isAllowed = allTrue(checks)
  753. } else {
  754. isAllowed = regexAllowed || containsStopword
  755. }
  756. if isAllowed {
  757. event := logger.Trace().
  758. Str("finding", finding.Secret).
  759. Str("condition", a.MatchCondition.String())
  760. if commitAllowed {
  761. event.Str("allowed-commit", commit)
  762. }
  763. if pathAllowed {
  764. event.Bool("allowed-path", pathAllowed)
  765. }
  766. if regexAllowed {
  767. event.Bool("allowed-regex", regexAllowed)
  768. }
  769. if containsStopword {
  770. event.Str("allowed-stopword", word)
  771. }
  772. return true, event
  773. }
  774. }
  775. return false, nil
  776. }
  777. func allTrue(bools []bool) bool {
  778. for _, check := range bools {
  779. if !check {
  780. return false
  781. }
  782. }
  783. return true
  784. }