4
0

reader.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package detect
  2. import (
  3. "context"
  4. "io"
  5. "github.com/zricethezav/gitleaks/v8/report"
  6. "github.com/zricethezav/gitleaks/v8/sources"
  7. )
  8. // DetectReader accepts an io.Reader and a buffer size for the reader in KB
  9. //
  10. // Deprecated: Use sources.File with no path defined and Detector.DetectSource instead
  11. func (d *Detector) DetectReader(r io.Reader, bufSize int) ([]report.Finding, error) {
  12. var findings []report.Finding
  13. file := sources.File{
  14. Content: r,
  15. Buffer: make([]byte, 1000*bufSize),
  16. MaxArchiveDepth: d.MaxArchiveDepth,
  17. }
  18. ctx := context.Background()
  19. err := file.Fragments(ctx, func(fragment sources.Fragment, err error) error {
  20. if err != nil {
  21. return err
  22. }
  23. for _, finding := range d.Detect(Fragment(fragment)) {
  24. findings = append(findings, finding)
  25. if d.Verbose {
  26. printFinding(finding, d.NoColor)
  27. }
  28. }
  29. return nil
  30. })
  31. return findings, err
  32. }
  33. // StreamDetectReader streams the detection results from the provided io.Reader.
  34. // It reads data using the specified buffer size (in KB) and processes each chunk through
  35. // the existing detection logic. Findings are sent down the returned findings channel as soon as
  36. // they are detected, while a separate error channel signals a terminal error (or nil upon successful completion).
  37. // The function returns two channels:
  38. // - findingsCh: a receive-only channel that emits report.Finding objects as they are found.
  39. // - errCh: a receive-only channel that emits a single final error (or nil if no error occurred)
  40. // once the stream ends.
  41. //
  42. // Recommended Usage:
  43. //
  44. // Since there will only ever be a single value on the errCh, it is recommended to consume the findingsCh
  45. // first. Once findingsCh is closed, the consumer should then read from errCh to determine
  46. // if the stream completed successfully or if an error occurred.
  47. //
  48. // This design avoids the need for a select loop, keeping client code simple.
  49. //
  50. // Example:
  51. //
  52. // // Assume detector is an instance of *Detector and myReader implements io.Reader.
  53. // findingsCh, errCh := detector.StreamDetectReader(myReader, 64) // using 64 KB buffer size
  54. //
  55. // // Process findings as they arrive.
  56. // for finding := range findingsCh {
  57. // fmt.Printf("Found secret: %+v\n", finding)
  58. // }
  59. //
  60. // // After the findings channel is closed, check the final error.
  61. // if err := <-errCh; err != nil {
  62. // log.Fatalf("StreamDetectReader encountered an error: %v", err)
  63. // } else {
  64. // fmt.Println("Scanning completed successfully.")
  65. // }
  66. //
  67. // Deprecated: Use sources.File.Fragments(context.Context, FragmentsFunc) instead
  68. func (d *Detector) StreamDetectReader(r io.Reader, bufSize int) (<-chan report.Finding, <-chan error) {
  69. findingsCh := make(chan report.Finding, 1)
  70. errCh := make(chan error, 1)
  71. file := sources.File{
  72. Content: r,
  73. Buffer: make([]byte, 1000*bufSize),
  74. MaxArchiveDepth: d.MaxArchiveDepth,
  75. }
  76. go func() {
  77. defer close(findingsCh)
  78. defer close(errCh)
  79. ctx := context.Background()
  80. errCh <- file.Fragments(ctx, func(fragment sources.Fragment, err error) error {
  81. if err != nil {
  82. return err
  83. }
  84. for _, finding := range d.Detect(Fragment(fragment)) {
  85. findingsCh <- finding
  86. if d.Verbose {
  87. printFinding(finding, d.NoColor)
  88. }
  89. }
  90. return nil
  91. })
  92. }()
  93. return findingsCh, errCh
  94. }