baseline.go 1.8 KB

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