mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 05:04:22 +01:00
Archive support (#1872)
* Start Files Source
* Keep cmd the same for now
* Get tests to pass
* s.MaxTargetMegaBytes -> s.MaxFileSize
* Tweak some log messages & use q semgroup for files
* Just call yield inside the semgroup
* Refactor a check and revert some log lines
* Move mg && misc tuning
* Add `make profile`
(Benchmark results are only added if tools are installed)
Example output:
```
========================================================================
generating profile data
------------------------------------------------------------------------
- mode: dir
benchmark:
tool: hyperfine
path: profile/1748019612/dir/benchmark.json
results:
mean: 1.12852930166
stddev: 0.12163775966037399
median: 1.07242928416
user: 3.9699382799999996
system: 0.04382341999999999
min: 1.03207673016
max: 1.3913475181600001
profile:
- mode: cpu
path: profile/1748019612/dir/cpu.pprof
view: go tool pprof -http=localhost: ./gitleaks profile/1748019612/dir/cpu.pprof
- mode: mem
path: profile/1748019612/dir/mem.pprof
view: go tool pprof -http=localhost: ./gitleaks profile/1748019612/dir/mem.pprof
- mode: trace
path: profile/1748019612/dir/trace.out
view: go tool trace profile/1748019612/dir/trace.out
- mode: git
...snip...
```
* Archive support for File based sources
* Move Git to Source interface
* Scan archives in git
* Misc linter fixes
* Exclude certain diffs: --diff-filter=tuxdb
* Add git archives tests
* Log error instead of fatal
* Fix spacing in test config
* Refine source comments/wording
* Make MB conversions more readable
* Format cmd
* Adjust DetectSource logging
* MiB -> MB in logs
* Add testdata/repos/archives/dotGit/refs/.gitkeep
Without it the refs folder gets deleted and the
and then it isn't detected as a git repo ¯\_(ツ)_/¯
So then the test starts scanning the whole gitleaks
repo instead :D
* Handle finding links for archives
* Working on archive depth
* Fixes from failfast test
* cmd max-archive-depth
* added comments, fixed warnings
* removed test print
* Update README.md
* Set archive depth for DetectFiles
* Tweak logs & pass max archive for readers
* Pass ctx to Fragments
* Add make lint target and address some issues
* Fix compat issue
* Report path in fragment errors
* Apply PR suggestions & misc changes
- Update blobReader.Close() to discard the buffer
- Misc logger issues & uses
- Tweak .golangci.yaml to default to none
- Discard remaining blobReader data on close
- Undo a De Morgan's Law suggestion (and disable QF1001)
* Add http mode for diagnostics
gitleaks --diagnostics=http ...
-----
From the net/http/pprof docs:
Use the pprof tool to look at the heap profile:
```
go tool pprof http://localhost:6060/debug/pprof/heap
```
Or to look at a 30-second CPU profile:
```
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
```
Or to look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program:
```
go tool pprof http://localhost:6060/debug/pprof/block
```
Or to look at the holders of contended mutexes, after calling runtime.SetMutexProfileFraction in your program:
```
go tool pprof http://localhost:6060/debug/pprof/mutex
```
(For more info see https://pkg.go.dev/net/http/pprof)
---------
Co-authored-by: Alex Layne <alayne@redhat.com>
This commit is contained in:
1 parent
489d13c14e
commit
782f310478
77 files changed
+2975
-777
No files matched your search
@@ -0,0 +1,180 @@
|
||||
package sources
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/fatih/semgroup"
|
||||
"github.com/zricethezav/gitleaks/v8/config"
|
||||
"github.com/zricethezav/gitleaks/v8/logging"
|
||||
)
|
||||
|
||||
// TODO: remove this in v9 and have scanTargets yield file sources
|
||||
type ScanTarget struct {
|
||||
Path string
|
||||
Symlink string
|
||||
}
|
||||
|
||||
// Deprecated: Use Files and detector.DetectSource instead
|
||||
func DirectoryTargets(sourcePath string, s *semgroup.Group, followSymlinks bool, allowlists []*config.Allowlist) (<-chan ScanTarget, error) {
|
||||
paths := make(chan ScanTarget)
|
||||
|
||||
// create a Files source
|
||||
files := Files{
|
||||
FollowSymlinks: followSymlinks,
|
||||
Path: sourcePath,
|
||||
Sema: s,
|
||||
Config: &config.Config{
|
||||
Allowlists: allowlists,
|
||||
},
|
||||
}
|
||||
|
||||
s.Go(func() error {
|
||||
err := files.scanTargets(func(scanTarget ScanTarget, err error) error {
|
||||
paths <- scanTarget
|
||||
return nil
|
||||
})
|
||||
close(paths)
|
||||
return err
|
||||
})
|
||||
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
// Files is a source for yielding fragments from a collection of files
|
||||
type Files struct {
|
||||
Config *config.Config
|
||||
FollowSymlinks bool
|
||||
MaxFileSize int
|
||||
Path string
|
||||
Sema *semgroup.Group
|
||||
MaxArchiveDepth int
|
||||
}
|
||||
|
||||
// scanTargets yields scan targets to a callback func
|
||||
func (s *Files) scanTargets(yield func(ScanTarget, error) error) error {
|
||||
return filepath.WalkDir(s.Path, func(path string, d fs.DirEntry, err error) error {
|
||||
scanTarget := ScanTarget{Path: path}
|
||||
logger := logging.With().Str("path", path).Logger()
|
||||
|
||||
if err != nil {
|
||||
if os.IsPermission(err) {
|
||||
// This seems to only fail on directories at this stage.
|
||||
logger.Warn().Err(errors.New("permission denied")).Msg("skipping directory")
|
||||
return filepath.SkipDir
|
||||
}
|
||||
logger.Warn().Err(err).Msg("skipping")
|
||||
return nil
|
||||
}
|
||||
|
||||
info, err := d.Info()
|
||||
if err != nil {
|
||||
if d.IsDir() {
|
||||
logger.Error().Err(err).Msg("skipping directory: could not get info")
|
||||
return filepath.SkipDir
|
||||
}
|
||||
logger.Error().Err(err).Msg("skipping file: could not get info")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !d.IsDir() {
|
||||
// Empty; nothing to do here.
|
||||
if info.Size() == 0 {
|
||||
logger.Debug().Msg("skipping empty file")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Too large; nothing to do here.
|
||||
if s.MaxFileSize > 0 && info.Size() > int64(s.MaxFileSize) {
|
||||
logger.Warn().Msgf(
|
||||
"skipping file: too large max_size=%dMB, size=%dMB",
|
||||
s.MaxFileSize/1_000_000, info.Size()/1_000_000,
|
||||
)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// set the initial scan target values
|
||||
if d.Type() == fs.ModeSymlink {
|
||||
if !s.FollowSymlinks {
|
||||
logger.Debug().Msg("skipping symlink: follow symlinks disabled")
|
||||
return nil
|
||||
}
|
||||
realPath, err := filepath.EvalSymlinks(path)
|
||||
if err != nil {
|
||||
logger.Error().Err(err).Msg("skipping symlink: could not evaluate")
|
||||
return nil
|
||||
}
|
||||
if realPathFileInfo, _ := os.Stat(realPath); realPathFileInfo.IsDir() {
|
||||
logger.Debug().Str("target", realPath).Msgf("skipping symlink: target is directory")
|
||||
return nil
|
||||
}
|
||||
scanTarget = ScanTarget{
|
||||
Path: realPath,
|
||||
Symlink: path,
|
||||
}
|
||||
}
|
||||
|
||||
// handle dir cases (mainly just see if it should be skipped
|
||||
if info.IsDir() {
|
||||
if shouldSkipPath(s.Config, path) {
|
||||
logger.Debug().Msg("skipping directory: global allowlist")
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if shouldSkipPath(s.Config, path) {
|
||||
logger.Debug().Msg("skipping file: global allowlist")
|
||||
return nil
|
||||
}
|
||||
|
||||
return yield(scanTarget, nil)
|
||||
})
|
||||
}
|
||||
|
||||
// Fragments yields fragments from files discovered under the path
|
||||
func (s *Files) Fragments(ctx context.Context, yield FragmentsFunc) error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
err := s.scanTargets(func(scanTarget ScanTarget, err error) error {
|
||||
wg.Add(1)
|
||||
s.Sema.Go(func() error {
|
||||
logger := logging.With().Str("path", scanTarget.Path).Logger()
|
||||
logger.Trace().Msg("scanning path")
|
||||
|
||||
f, err := os.Open(scanTarget.Path)
|
||||
if err != nil {
|
||||
if os.IsPermission(err) {
|
||||
logger.Warn().Msg("skipping file: permission denied")
|
||||
}
|
||||
wg.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert this to a file source
|
||||
file := File{
|
||||
Content: f,
|
||||
Path: scanTarget.Path,
|
||||
Symlink: scanTarget.Symlink,
|
||||
Config: s.Config,
|
||||
MaxArchiveDepth: s.MaxArchiveDepth,
|
||||
}
|
||||
|
||||
err = file.Fragments(ctx, yield)
|
||||
// Avoiding a defer in a hot loop
|
||||
_ = f.Close()
|
||||
wg.Done()
|
||||
return err
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user