mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 13:34:23 +01:00
feat: identify slow file (#1479)
This commit is contained in:
1 parent
9609928c02
commit
88f56d3695
3 files changed
+25
No files matched your search
@@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/zricethezav/gitleaks/v8/config"
|
"github.com/zricethezav/gitleaks/v8/config"
|
||||||
"github.com/zricethezav/gitleaks/v8/logging"
|
"github.com/zricethezav/gitleaks/v8/logging"
|
||||||
@@ -25,6 +26,10 @@ import (
|
|||||||
const (
|
const (
|
||||||
gitleaksAllowSignature = "gitleaks:allow"
|
gitleaksAllowSignature = "gitleaks:allow"
|
||||||
chunkSize = 100 * 1_000 // 100kb
|
chunkSize = 100 * 1_000 // 100kb
|
||||||
|
|
||||||
|
// SlowWarningThreshold is the amount of time to wait before logging that a file is slow.
|
||||||
|
// This is useful for identifying problematic files and tuning the allowlist.
|
||||||
|
SlowWarningThreshold = 5 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/h2non/filetype"
|
"github.com/h2non/filetype"
|
||||||
|
|
||||||
@@ -90,6 +91,7 @@ func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Findin
|
|||||||
if pa.Symlink != "" {
|
if pa.Symlink != "" {
|
||||||
fragment.SymlinkFile = pa.Symlink
|
fragment.SymlinkFile = pa.Symlink
|
||||||
}
|
}
|
||||||
|
|
||||||
if isWindows {
|
if isWindows {
|
||||||
fragment.FilePath = filepath.ToSlash(pa.Path)
|
fragment.FilePath = filepath.ToSlash(pa.Path)
|
||||||
fragment.SymlinkFile = filepath.ToSlash(fragment.SymlinkFile)
|
fragment.SymlinkFile = filepath.ToSlash(fragment.SymlinkFile)
|
||||||
@@ -98,12 +100,19 @@ func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Findin
|
|||||||
fragment.FilePath = pa.Path
|
fragment.FilePath = pa.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timer := time.AfterFunc(SlowWarningThreshold, func() {
|
||||||
|
logger.Debug().Msgf("Taking longer than %s to inspect fragment", SlowWarningThreshold.String())
|
||||||
|
})
|
||||||
for _, finding := range d.Detect(fragment) {
|
for _, finding := range d.Detect(fragment) {
|
||||||
// need to add 1 since line counting starts at 1
|
// need to add 1 since line counting starts at 1
|
||||||
finding.StartLine += (totalLines - linesInChunk) + 1
|
finding.StartLine += (totalLines - linesInChunk) + 1
|
||||||
finding.EndLine += (totalLines - linesInChunk) + 1
|
finding.EndLine += (totalLines - linesInChunk) + 1
|
||||||
d.AddFinding(finding)
|
d.AddFinding(finding)
|
||||||
}
|
}
|
||||||
|
if timer != nil {
|
||||||
|
timer.Stop()
|
||||||
|
timer = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gitleaks/go-gitdiff/gitdiff"
|
"github.com/gitleaks/go-gitdiff/gitdiff"
|
||||||
|
|
||||||
@@ -63,9 +64,19 @@ func (d *Detector) DetectGit(cmd *sources.GitCmd, remote *RemoteInfo) ([]report.
|
|||||||
FilePath: gitdiffFile.NewName,
|
FilePath: gitdiffFile.NewName,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
timer := time.AfterFunc(SlowWarningThreshold, func() {
|
||||||
|
logging.Debug().
|
||||||
|
Str("commit", commitSHA[:7]).
|
||||||
|
Str("path", fragment.FilePath).
|
||||||
|
Msgf("Taking longer than %s to inspect fragment", SlowWarningThreshold.String())
|
||||||
|
})
|
||||||
for _, finding := range d.Detect(fragment) {
|
for _, finding := range d.Detect(fragment) {
|
||||||
d.AddFinding(augmentGitFinding(remote, finding, textFragment, gitdiffFile))
|
d.AddFinding(augmentGitFinding(remote, finding, textFragment, gitdiffFile))
|
||||||
}
|
}
|
||||||
|
if timer != nil {
|
||||||
|
timer.Stop()
|
||||||
|
timer = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user