Przeglądaj źródła

switch out libs (#1259)

Zachary Rice 2 lat temu
rodzic
commit
a82ac29976
3 zmienionych plików z 9 dodań i 12 usunięć
  1. 6 12
      detect/detect.go
  2. 1 0
      go.mod
  3. 2 0
      go.sum

+ 6 - 12
detect/detect.go

@@ -17,9 +17,10 @@ import (
 	"github.com/zricethezav/gitleaks/v8/detect/git"
 	"github.com/zricethezav/gitleaks/v8/detect/git"
 	"github.com/zricethezav/gitleaks/v8/report"
 	"github.com/zricethezav/gitleaks/v8/report"
 
 
+	ahocorasick "github.com/BobuSumisu/aho-corasick"
 	"github.com/fatih/semgroup"
 	"github.com/fatih/semgroup"
 	"github.com/gitleaks/go-gitdiff/gitdiff"
 	"github.com/gitleaks/go-gitdiff/gitdiff"
-	ahocorasick "github.com/petar-dambovaliev/aho-corasick"
+
 	"github.com/rs/zerolog/log"
 	"github.com/rs/zerolog/log"
 	"github.com/spf13/viper"
 	"github.com/spf13/viper"
 )
 )
@@ -75,7 +76,7 @@ type Detector struct {
 
 
 	// prefilter is a ahocorasick struct used for doing efficient string
 	// prefilter is a ahocorasick struct used for doing efficient string
 	// matching given a set of words (keywords from the rules in the config)
 	// matching given a set of words (keywords from the rules in the config)
-	prefilter ahocorasick.AhoCorasick
+	prefilter ahocorasick.Trie
 
 
 	// a list of known findings that should be ignored
 	// a list of known findings that should be ignored
 	baseline []report.Finding
 	baseline []report.Finding
@@ -110,20 +111,13 @@ type Fragment struct {
 
 
 // NewDetector creates a new detector with the given config
 // NewDetector creates a new detector with the given config
 func NewDetector(cfg config.Config) *Detector {
 func NewDetector(cfg config.Config) *Detector {
-	builder := ahocorasick.NewAhoCorasickBuilder(ahocorasick.Opts{
-		AsciiCaseInsensitive: true,
-		MatchOnlyWholeWords:  false,
-		MatchKind:            ahocorasick.LeftMostLongestMatch,
-		DFA:                  true,
-	})
-
 	return &Detector{
 	return &Detector{
 		commitMap:      make(map[string]bool),
 		commitMap:      make(map[string]bool),
 		gitleaksIgnore: make(map[string]bool),
 		gitleaksIgnore: make(map[string]bool),
 		findingMutex:   &sync.Mutex{},
 		findingMutex:   &sync.Mutex{},
 		findings:       make([]report.Finding, 0),
 		findings:       make([]report.Finding, 0),
 		Config:         cfg,
 		Config:         cfg,
-		prefilter:      builder.Build(cfg.Keywords),
+		prefilter:      *ahocorasick.NewTrieBuilder().AddStrings(cfg.Keywords).Build(),
 	}
 	}
 }
 }
 
 
@@ -582,9 +576,9 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
 
 
 	// build keyword map for prefiltering rules
 	// build keyword map for prefiltering rules
 	normalizedRaw := strings.ToLower(fragment.Raw)
 	normalizedRaw := strings.ToLower(fragment.Raw)
-	matches := d.prefilter.FindAll(normalizedRaw)
+	matches := d.prefilter.MatchString(normalizedRaw)
 	for _, m := range matches {
 	for _, m := range matches {
-		fragment.keywords[normalizedRaw[m.Start():m.End()]] = true
+		fragment.keywords[normalizedRaw[m.Pos():int(m.Pos())+len(m.Match())]] = true
 	}
 	}
 
 
 	for _, rule := range d.Config.Rules {
 	for _, rule := range d.Config.Rules {

+ 1 - 0
go.mod

@@ -14,6 +14,7 @@ require (
 )
 )
 
 
 require (
 require (
+	github.com/BobuSumisu/aho-corasick v1.0.3 // indirect
 	github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
 	github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
 	github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
 	github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
 	github.com/mattn/go-isatty v0.0.17 // indirect
 	github.com/mattn/go-isatty v0.0.17 // indirect

+ 2 - 0
go.sum

@@ -37,6 +37,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
 cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
 cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
 cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
 cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
 dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
+github.com/BobuSumisu/aho-corasick v1.0.3 h1:uuf+JHwU9CHP2Vx+wAy6jcksJThhJS9ehR8a+4nPE9g=
+github.com/BobuSumisu/aho-corasick v1.0.3/go.mod h1:hm4jLcvZKI2vRF2WDU1N4p/jpWtpOzp3nLmi9AzX/XE=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
 github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
 github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=