Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add context to handler (#877)
Bill Richab71b93f7dc2
diff --git a/pkg/handlers/handlers.go b/pkg/handlers/handlers.goindex 2d120ebe2..5b78aec67 100644--- a/pkg/handlers/handlers.go+++ b/pkg/handlers/handlers.go@@ -1,6 +1,7 @@package handlersimport (+ "context""io""github.com/trufflesecurity/trufflehog/v3/pkg/sources"@@ -18,7 +19,7 @@ type Handler interface {New()}-func HandleFile(file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sources.Chunk)) bool {+func HandleFile(ctx context.Context, file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sources.Chunk)) bool {for _, handler := range DefaultHandlers() {handler.New()var isType bool@@ -27,10 +28,18 @@ func HandleFile(file io.Reader, chunkSkel *sources.Chunk, chunksChan chan (*sourcontinue}handlerChan := handler.FromFile(file)- for data := range handlerChan {- chunk := *chunkSkel- chunk.Data = data- chunksChan <- &chunk+ for {+ select {+ case data := <-handlerChan:+ chunk := *chunkSkel+ chunk.Data = data+ chunksChan <- &chunk+ case <-ctx.Done():+ return false+ }+ if handlerChan == nil {+ break+ }}return true}diff --git a/pkg/sources/filesystem/filesystem.go b/pkg/sources/filesystem/filesystem.goindex 2a9642fc9..5b71c31de 100644--- a/pkg/sources/filesystem/filesystem.go+++ b/pkg/sources/filesystem/filesystem.go@@ -131,7 +131,7 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) err},Verify: s.verify,}- if handlers.HandleFile(reReader, chunkSkel, chunksChan) {+ if handlers.HandleFile(ctx, reReader, chunkSkel, chunksChan) {return nil}diff --git a/pkg/sources/git/git.go b/pkg/sources/git/git.goindex fd2afbc98..6839425e5 100644--- a/pkg/sources/git/git.go+++ b/pkg/sources/git/git.go@@ -366,7 +366,7 @@ func (s *Git) ScanCommits(ctx context.Context, repo *git.Repository, path stringSourceMetadata: metadata,Verify: s.verify,}- if err := handleBinary(repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {+ if err := handleBinary(ctx, repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {log.WithError(err).WithField("file", fileName).Debug("Error handling binary file")}continue@@ -504,7 +504,7 @@ func (s *Git) ScanUnstaged(ctx context.Context, repo *git.Repository, path strinSourceMetadata: metadata,Verify: s.verify,}- if err := handleBinary(repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {+ if err := handleBinary(ctx, repo, chunksChan, chunkSkel, commitHash, fileName); err != nil {log.WithError(err).WithField("file", fileName).Debug("Error handling binary file")}continue@@ -782,7 +782,7 @@ func getSafeRemoteURL(repo *git.Repository, preferred string) string {return safeURL}-func handleBinary(repo *git.Repository, chunksChan chan *sources.Chunk, chunkSkel *sources.Chunk, commitHash plumbing.Hash, path string) error {+func handleBinary(ctx context.Context, repo *git.Repository, chunksChan chan *sources.Chunk, chunkSkel *sources.Chunk, commitHash plumbing.Hash, path string) error {log.WithField("path", path).Trace("Binary file found in repository.")commit, err := repo.CommitObject(commitHash)if err != nil {@@ -805,7 +805,7 @@ func handleBinary(repo *git.Repository, chunksChan chan *sources.Chunk, chunkSkereturn err}- if handlers.HandleFile(reader, chunkSkel, chunksChan) {+ if handlers.HandleFile(ctx, reader, chunkSkel, chunksChan) {return nil}diff --git a/pkg/sources/s3/s3.go b/pkg/sources/s3/s3.goindex 6c5dbb59c..a50cdc0b2 100644--- a/pkg/sources/s3/s3.go+++ b/pkg/sources/s3/s3.go@@ -287,7 +287,7 @@ func (s *Source) pageChunker(ctx context.Context, client *s3.S3, chunksChan chan},Verify: s.verify,}- if handlers.HandleFile(reader, chunkSkel, chunksChan) {+ if handlers.HandleFile(ctx, reader, chunkSkel, chunksChan) {return}