4
0

files.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package detect
  2. import (
  3. "context"
  4. "errors"
  5. "os"
  6. "sync"
  7. "github.com/zricethezav/gitleaks/v8/logging"
  8. "github.com/zricethezav/gitleaks/v8/report"
  9. "github.com/zricethezav/gitleaks/v8/sources"
  10. )
  11. // DetectFiles runs detections against a chanel of scan targets
  12. //
  13. // Deprecated: Use sources.Files and Detector.DetectSource instead
  14. func (d *Detector) DetectFiles(scanTargets <-chan sources.ScanTarget) ([]report.Finding, error) {
  15. var wg sync.WaitGroup
  16. for scanTarget := range scanTargets {
  17. wg.Add(1)
  18. d.Sema.Go(func() error {
  19. defer wg.Done()
  20. logger := logging.With().Str("path", scanTarget.Path).Logger()
  21. logger.Trace().Msg("scanning path")
  22. f, err := os.Open(scanTarget.Path)
  23. if err != nil {
  24. if os.IsPermission(err) {
  25. err = errors.New("permission denied")
  26. }
  27. logger.Warn().Err(err).Msg("skipping file")
  28. return nil
  29. }
  30. defer func() {
  31. _ = f.Close()
  32. }()
  33. info, err := f.Stat()
  34. if err != nil {
  35. logger.Error().Err(err).Msg("skipping file: could not get info")
  36. return nil
  37. }
  38. // Empty; nothing to do here.
  39. if info.Size() == 0 {
  40. logger.Debug().Msg("skipping empty file")
  41. return nil
  42. }
  43. // Too large; nothing to do here.
  44. if d.MaxTargetMegaBytes > 0 {
  45. rawLength := info.Size() / 1_000_000
  46. if rawLength > int64(d.MaxTargetMegaBytes) {
  47. logger.Warn().Msgf(
  48. "skipping file: too large max_size=%dMB, size=%dMB",
  49. d.MaxTargetMegaBytes, rawLength,
  50. )
  51. return nil
  52. }
  53. }
  54. // Convert this to a file source
  55. file := sources.File{
  56. Content: f,
  57. Path: scanTarget.Path,
  58. Symlink: scanTarget.Symlink,
  59. Config: &d.Config,
  60. MaxArchiveDepth: d.MaxArchiveDepth,
  61. }
  62. ctx := context.Background()
  63. return file.Fragments(ctx, func(fragment sources.Fragment, err error) error {
  64. if err != nil {
  65. logging.Error().Err(err)
  66. return nil
  67. }
  68. for _, finding := range d.Detect(Fragment(fragment)) {
  69. d.AddFinding(finding)
  70. }
  71. return nil
  72. })
  73. })
  74. }
  75. wg.Wait()
  76. return d.findings, nil
  77. }