Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
filesystem support for exclude and include filters (2nd attemp) (#1033)
Alexandr Marchenkob29b78c10daf
diff --git a/main.go b/main.goindex e8802abfd..3572dab01 100644--- a/main.go+++ b/main.go@@ -80,8 +80,8 @@ var (filesystemDirectories = filesystemScan.Flag("directory", "Path to directory to scan. You can repeat this flag.").Required().Strings()// TODO: Add more filesystem scan options. Currently only supports scanning a list of directories.// filesystemScanRecursive = filesystemScan.Flag("recursive", "Scan recursively.").Short('r').Bool()- // filesystemScanIncludePaths = filesystemScan.Flag("include-paths", "Path to file with newline separated regexes for files to include in scan.").Short('i').String()- // filesystemScanExcludePaths = filesystemScan.Flag("exclude-paths", "Path to file with newline separated regexes for files to exclude in scan.").Short('x').String()+ filesystemScanIncludePaths = filesystemScan.Flag("include-paths", "Path to file with newline separated regexes for files to include in scan.").Short('i').String()+ filesystemScanExcludePaths = filesystemScan.Flag("exclude-paths", "Path to file with newline separated regexes for files to exclude in scan.").Short('x').String()s3Scan = cli.Command("s3", "Find credentials in S3 buckets.")s3ScanKey = s3Scan.Flag("key", "S3 key used to authenticate. Can be provided with environment variable AWS_ACCESS_KEY_ID.").Envar("AWS_ACCESS_KEY_ID").String()@@ -201,15 +201,14 @@ func run(state overseer.State) {engine.WithFilterUnverified(*filterUnverified),)- filter, err := common.FilterFromFiles(*gitScanIncludePaths, *gitScanExcludePaths)- if err != nil {- logrus.WithError(err).Fatal("could not create filter")- }-var repoPath stringvar remote boolswitch cmd {case gitScan.FullCommand():+ filter, err := common.FilterFromFiles(*gitScanIncludePaths, *gitScanExcludePaths)+ if err != nil {+ logrus.WithError(err).Fatal("could not create filter")+ }repoPath, remote, err = git.PrepareRepoSinceCommit(ctx, *gitScanURI, *gitScanSinceCommit)if err != nil || repoPath == "" {logrus.WithError(err).Fatal("error preparing git repo for scanning")@@ -246,7 +245,7 @@ func run(state overseer.State) {c.IncludeRepos = *githubIncludeRepos}- if err = e.ScanGitHub(ctx, sources.NewConfig(github)); err != nil {+ if err := e.ScanGitHub(ctx, sources.NewConfig(github)); err != nil {logrus.WithError(err).Fatal("Failed to scan Github.")}case gitlabScan.FullCommand():@@ -262,12 +261,18 @@ func run(state overseer.State) {c.Filter = filter}- if err = e.ScanGitLab(ctx, sources.NewConfig(gitlab)); err != nil {+ if err := e.ScanGitLab(ctx, sources.NewConfig(gitlab)); err != nil {logrus.WithError(err).Fatal("Failed to scan GitLab.")}case filesystemScan.FullCommand():+ filter, err := common.FilterFromFiles(*filesystemScanIncludePaths, *filesystemScanExcludePaths)+ if err != nil {+ logrus.WithError(err).Fatal("could not create filter")+ }+fs := func(c *sources.Config) {c.Directories = *filesystemDirectories+ c.Filter = filter}if err = e.ScanFileSystem(ctx, sources.NewConfig(fs)); err != nil {@@ -280,7 +285,7 @@ func run(state overseer.State) {c.Buckets = *s3ScanBuckets}- if err = e.ScanS3(ctx, sources.NewConfig(s3)); err != nil {+ if err := e.ScanS3(ctx, sources.NewConfig(s3)); err != nil {logrus.WithError(err).Fatal("Failed to scan S3.")}case syslogScan.FullCommand():@@ -293,11 +298,11 @@ func run(state overseer.State) {c.Concurrency = *concurrency}- if err = e.ScanSyslog(ctx, sources.NewConfig(syslog)); err != nil {+ if err := e.ScanSyslog(ctx, sources.NewConfig(syslog)); err != nil {logrus.WithError(err).Fatal("Failed to scan syslog.")}case circleCiScan.FullCommand():- if err = e.ScanCircleCI(ctx, *circleCiScanToken); err != nil {+ if err := e.ScanCircleCI(ctx, *circleCiScanToken); err != nil {logrus.WithError(err).Fatal("Failed to scan CircleCI.")}}diff --git a/pkg/common/filter.go b/pkg/common/filter.goindex 7982d73c5..52814738a 100644--- a/pkg/common/filter.go+++ b/pkg/common/filter.go@@ -57,6 +57,7 @@ func FilterRulesFromFile(source string) (*FilterRuleSet, error) {}commentPattern := regexp.MustCompile(`^\s*#`)+ emptyLinePattern := regexp.MustCompile(`^\s*$`)file, err := os.Open(source)if err != nil {@@ -75,6 +76,9 @@ func FilterRulesFromFile(source string) (*FilterRuleSet, error) {if commentPattern.MatchString(line) {continue}+ if emptyLinePattern.MatchString(line) {+ continue+ }pattern, err := regexp.Compile(line)if err != nil {return nil, fmt.Errorf("can not compile regular expression: %s", line)diff --git a/pkg/common/filter_test.go b/pkg/common/filter_test.goindex c7a7e4066..a4df8db62 100644--- a/pkg/common/filter_test.go+++ b/pkg/common/filter_test.go@@ -129,6 +129,13 @@ func TestFilterFromFile(t *testing.T) {pattern: "test",pass: false,},+ "EmptyLinesAreIgnored": {+ includeFile: false,+ excludeFile: true,+ excludeFileContents: " \ntest.txt",+ pattern: "hello world.txt",+ pass: true,+ },}for name, test := range tests {var includeTestFile, excludeTestFile stringdiff --git a/pkg/engine/filesystem.go b/pkg/engine/filesystem.goindex ea1680525..b1110bdc3 100644--- a/pkg/engine/filesystem.go+++ b/pkg/engine/filesystem.go@@ -32,6 +32,7 @@ func (e *Engine) ScanFileSystem(ctx context.Context, c sources.Config) error {if err != nil {return errors.WrapPrefix(err, "could not init filesystem source", 0)}+ fileSystemSource.WithFilter(c.Filter)e.sourcesWg.Add(1)go func() {defer common.RecoverWithExit(ctx)diff --git a/pkg/sources/filesystem/filesystem.go b/pkg/sources/filesystem/filesystem.goindex 30fe20ee3..3dda10887 100644--- a/pkg/sources/filesystem/filesystem.go+++ b/pkg/sources/filesystem/filesystem.go@@ -13,6 +13,7 @@ import ("google.golang.org/protobuf/proto""google.golang.org/protobuf/types/known/anypb"+ "github.com/trufflesecurity/trufflehog/v3/pkg/common""github.com/trufflesecurity/trufflehog/v3/pkg/context""github.com/trufflesecurity/trufflehog/v3/pkg/handlers""github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"@@ -35,6 +36,7 @@ type Source struct {verify boolpaths []stringlog *log.Entry+ filter *common.Filtersources.Progress}@@ -74,6 +76,10 @@ func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64,return nil}+func (s *Source) WithFilter(filter *common.Filter) {+ s.filter = filter+}+// Chunks emits chunks of bytes over a channel.func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) error {for i, path := range s.paths {@@ -102,6 +108,10 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) errreturn nil}+ if s.filter != nil && !s.filter.Pass(path) {+ return nil+ }+inputFile, err := os.Open(path)if err != nil {log.Warn(err)