4
0

directory.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package detect
  2. import (
  3. "bufio"
  4. "bytes"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "github.com/h2non/filetype"
  10. "github.com/zricethezav/gitleaks/v8/logging"
  11. "github.com/zricethezav/gitleaks/v8/report"
  12. "github.com/zricethezav/gitleaks/v8/sources"
  13. )
  14. const maxPeekSize = 25 * 1_000 // 10kb
  15. // DetectFiles schedules each ScanTarget—file or archive—for concurrent scanning.
  16. func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Finding, error) {
  17. for pa := range paths {
  18. d.Sema.Go(func() error {
  19. return d.detectScanTarget(pa)
  20. })
  21. }
  22. if err := d.Sema.Wait(); err != nil {
  23. return d.findings, err
  24. }
  25. return d.findings, nil
  26. }
  27. // detectScanTarget handles one ScanTarget: it unpacks archives recursively
  28. // or scans a regular file, always using VirtualPath for reporting.
  29. func (d *Detector) detectScanTarget(scanTarget sources.ScanTarget) error {
  30. // Choose display path: either VirtualPath (archive chain) or on-disk path.
  31. display := scanTarget.Path
  32. if scanTarget.VirtualPath != "" {
  33. display = scanTarget.VirtualPath
  34. }
  35. logger := logging.With().Str("path", display).Logger()
  36. logger.Trace().Msg("Scanning path")
  37. // skipping windows archives for now
  38. if isArchive(scanTarget.Path) {
  39. logger.Debug().Msg("Found archive")
  40. targets, tmpArchiveDir, err := extractArchive(scanTarget.Path)
  41. if err != nil {
  42. logger.Warn().Err(err).Msg("Failed to extract archive")
  43. return nil
  44. }
  45. // Schedule each extracted file for its own scan, carrying forward VirtualPath.
  46. for _, t := range targets {
  47. t := t
  48. // compute path INSIDE this archive
  49. rel, rerr := filepath.Rel(tmpArchiveDir, t.Path)
  50. if rerr != nil {
  51. rel = filepath.Base(t.Path)
  52. }
  53. rel = filepath.ToSlash(rel)
  54. // prepend existing chain or archive base name
  55. if scanTarget.VirtualPath != "" {
  56. t.VirtualPath = scanTarget.VirtualPath + "/" + rel
  57. } else {
  58. t.VirtualPath = filepath.Base(scanTarget.Path) + "/" + rel
  59. }
  60. d.Sema.Go(func() error {
  61. return d.detectScanTarget(t)
  62. })
  63. }
  64. return nil
  65. }
  66. // --- Regular file branch ---
  67. f, err := os.Open(scanTarget.Path)
  68. if err != nil {
  69. if os.IsPermission(err) {
  70. logger.Warn().Msg("Skipping file: permission denied")
  71. return nil
  72. }
  73. return err
  74. }
  75. defer f.Close()
  76. // Skip binary files by sniffing header
  77. head := make([]byte, 261)
  78. if n, _ := io.ReadFull(f, head); n > 0 {
  79. if kind, _ := filetype.Match(head[:n]); kind != filetype.Unknown {
  80. logger.Debug().Str("kind", kind.Extension).Msg("Skipping binary")
  81. return nil
  82. }
  83. }
  84. if _, err := f.Seek(0, io.SeekStart); err != nil {
  85. return err
  86. }
  87. reader := bufio.NewReader(f)
  88. buf := make([]byte, chunkSize)
  89. totalLines := 0
  90. for {
  91. n, err := reader.Read(buf)
  92. if n > 0 {
  93. peekBuf := bytes.NewBuffer(buf[:n])
  94. if readErr := readUntilSafeBoundary(reader, n, maxPeekSize, peekBuf); readErr != nil {
  95. return readErr
  96. }
  97. chunk := peekBuf.String()
  98. linesInChunk := strings.Count(chunk, "\n")
  99. // build fragment and set FilePath to our display chain
  100. fragment := Fragment{
  101. Raw: chunk,
  102. Bytes: peekBuf.Bytes(),
  103. }
  104. fragment.FilePath = display
  105. // if this file was itself a symlink
  106. if scanTarget.Symlink != "" {
  107. fragment.SymlinkFile = scanTarget.Symlink
  108. }
  109. if isWindows {
  110. fragment.FilePath = filepath.ToSlash(scanTarget.Path)
  111. fragment.SymlinkFile = filepath.ToSlash(fragment.SymlinkFile)
  112. fragment.WindowsFilePath = scanTarget.Path
  113. }
  114. // run detection and adjust line numbers
  115. for _, finding := range d.Detect(fragment) {
  116. finding.StartLine += totalLines + 1
  117. finding.EndLine += totalLines + 1
  118. // We have to augment the finding if the source is coming
  119. // from a archive committed in Git
  120. if scanTarget.Source == "github-archive" {
  121. finding.Author = scanTarget.GitInfo.Author
  122. finding.Commit = scanTarget.GitInfo.Commit
  123. finding.Email = scanTarget.GitInfo.Email
  124. finding.Date = scanTarget.GitInfo.Date
  125. finding.Message = scanTarget.GitInfo.Message
  126. }
  127. d.AddFinding(finding)
  128. }
  129. totalLines += linesInChunk
  130. }
  131. if err != nil {
  132. if err == io.EOF {
  133. return nil
  134. }
  135. return err
  136. }
  137. }
  138. }
  139. // readUntilSafeBoundary consumes |f| until it finds two consecutive `\n` characters, up to |maxPeekSize|.
  140. // This hopefully avoids splitting. (https://github.com/gitleaks/gitleaks/issues/1651)
  141. func readUntilSafeBoundary(r *bufio.Reader, n int, maxPeekSize int, peekBuf *bytes.Buffer) error {
  142. if peekBuf.Len() == 0 {
  143. return nil
  144. }
  145. // Does the buffer end in consecutive newlines?
  146. var (
  147. data = peekBuf.Bytes()
  148. lastChar = data[len(data)-1]
  149. newlineCount = 0 // Tracks consecutive newlines
  150. )
  151. if isWhitespace(lastChar) {
  152. for i := len(data) - 1; i >= 0; i-- {
  153. lastChar = data[i]
  154. if lastChar == '\n' {
  155. newlineCount++
  156. // Stop if two consecutive newlines are found
  157. if newlineCount >= 2 {
  158. return nil
  159. }
  160. } else if lastChar == '\r' || lastChar == ' ' || lastChar == '\t' {
  161. // The presence of other whitespace characters (`\r`, ` `, `\t`) shouldn't reset the count.
  162. // (Intentionally do nothing.)
  163. } else {
  164. break
  165. }
  166. }
  167. }
  168. // If not, read ahead until we (hopefully) find some.
  169. newlineCount = 0
  170. for {
  171. data = peekBuf.Bytes()
  172. // Check if the last character is a newline.
  173. lastChar = data[len(data)-1]
  174. if lastChar == '\n' {
  175. newlineCount++
  176. // Stop if two consecutive newlines are found
  177. if newlineCount >= 2 {
  178. break
  179. }
  180. } else if lastChar == '\r' || lastChar == ' ' || lastChar == '\t' {
  181. // The presence of other whitespace characters (`\r`, ` `, `\t`) shouldn't reset the count.
  182. // (Intentionally do nothing.)
  183. } else {
  184. newlineCount = 0 // Reset if a non-newline character is found
  185. }
  186. // Stop growing the buffer if it reaches maxSize
  187. if (peekBuf.Len() - n) >= maxPeekSize {
  188. break
  189. }
  190. // Read additional data into a temporary buffer
  191. b, err := r.ReadByte()
  192. if err != nil {
  193. if err == io.EOF {
  194. break
  195. }
  196. return err
  197. }
  198. peekBuf.WriteByte(b)
  199. }
  200. return nil
  201. }