From 8b8920d4c3fe5baf37dfe5675b73eef898555010 Mon Sep 17 00:00:00 2001 From: eyalatox <96191699+eyalatox@users.noreply.github.com> Date: Thu, 25 Jan 2024 00:08:05 +0200 Subject: [PATCH] --max-target-megabytes flag now supported for --no-git flag as well (#1330) --- detect/directory.go | 16 ++++++++++++++++ go.mod | 3 +-- go.sum | 2 -- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/detect/directory.go b/detect/directory.go index bdfa61a0..9e39f6a2 100644 --- a/detect/directory.go +++ b/detect/directory.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/h2non/filetype" + "github.com/rs/zerolog/log" "github.com/zricethezav/gitleaks/v8/report" "github.com/zricethezav/gitleaks/v8/sources" ) @@ -14,12 +15,27 @@ func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Findin for pa := range paths { p := pa d.Sema.Go(func() error { + f, err := os.Open(p.Path) if err != nil { return err } defer f.Close() + // Get file size + fileInfo, err := f.Stat() + if err != nil { + return err + } + fileSize := fileInfo.Size() + if d.MaxTargetMegaBytes > 0 { + rawLength := fileSize / 1000000 + if rawLength > int64(d.MaxTargetMegaBytes) { + log.Debug().Msgf("skipping file: %s scan due to size: %d", p.Path, rawLength) + return nil + } + } + // Buffer to hold file chunks buf := make([]byte, chunkSize) totalLines := 0 diff --git a/go.mod b/go.mod index f6ab470a..702b70a9 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/zricethezav/gitleaks/v8 go 1.19 require ( + github.com/BobuSumisu/aho-corasick v1.0.3 github.com/charmbracelet/lipgloss v0.5.0 github.com/fatih/semgroup v1.2.0 github.com/gitleaks/go-gitdiff v0.9.0 @@ -14,7 +15,6 @@ require ( ) require ( - github.com/BobuSumisu/aho-corasick v1.0.3 // indirect github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.17 // indirect @@ -33,7 +33,6 @@ require ( github.com/magiconair/properties v1.8.5 // indirect github.com/mitchellh/mapstructure v1.4.1 // indirect github.com/pelletier/go-toml v1.9.3 // indirect - github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/afero v1.6.0 // indirect github.com/spf13/cast v1.3.1 // indirect diff --git a/go.sum b/go.sum index 43dbd190..9c071657 100644 --- a/go.sum +++ b/go.sum @@ -225,8 +225,6 @@ github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4AN github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ= github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 h1:lL+y4Xv20pVlCGyLzNHRC0I0rIHhIL1lTvHizoS/dU8= -github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9/go.mod h1:EHPiTAKtiFmrMldLUNswFwfZ2eJIYBHktdaUTZxYWRw= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=