baseline.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package detect
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/zricethezav/gitleaks/v8/report"
  7. )
  8. func IsNew(finding report.Finding, baseline []report.Finding) bool {
  9. // Explicitly testing each property as it gives significantly better performance in comparison to cmp.Equal(). Drawback is that
  10. // the code requires maintenance if/when the Finding struct changes
  11. for _, b := range baseline {
  12. if finding.Author == b.Author &&
  13. finding.Commit == b.Commit &&
  14. finding.Date == b.Date &&
  15. finding.Description == b.Description &&
  16. finding.Email == b.Email &&
  17. finding.EndColumn == b.EndColumn &&
  18. finding.EndLine == b.EndLine &&
  19. finding.Entropy == b.Entropy &&
  20. finding.File == b.File &&
  21. // Omit checking finding.Fingerprint - if the format of the fingerprint changes, the users will see unexpected behaviour
  22. finding.Match == b.Match &&
  23. finding.Message == b.Message &&
  24. finding.RuleID == b.RuleID &&
  25. finding.Secret == b.Secret &&
  26. finding.StartColumn == b.StartColumn &&
  27. finding.StartLine == b.StartLine {
  28. return false
  29. }
  30. }
  31. return true
  32. }
  33. func LoadBaseline(baselinePath string) ([]report.Finding, error) {
  34. bytes, err := os.ReadFile(baselinePath)
  35. if err != nil {
  36. return nil, fmt.Errorf("could not open %s", baselinePath)
  37. }
  38. var previousFindings []report.Finding
  39. err = json.Unmarshal(bytes, &previousFindings)
  40. if err != nil {
  41. return nil, fmt.Errorf("the format of the file %s is not supported", baselinePath)
  42. }
  43. return previousFindings, nil
  44. }