baseline.go 1.6 KB

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