From 63c3076ea2e6c922632f57bdcde4adbe506ac629 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Wed, 29 Mar 2023 15:56:01 -0600 Subject: [PATCH] No color (#1136) * safer out of bounds * no color option * readme --- README.md | 1 + cmd/detect.go | 4 ++++ cmd/protect.go | 4 ++++ cmd/root.go | 1 + detect/detect.go | 7 +++++-- detect/utils.go | 7 ++++--- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bf895b2a..d74caf6e 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ Flags: -h, --help help for gitleaks -l, --log-level string log level (trace, debug, info, warn, error, fatal) (default "info") --max-target-megabytes int files larger than this will be skipped + --no-color turn off color for verbose output --no-banner suppress banner --redact redact secrets from logs and stdout -f, --report-format string output format (json, csv, sarif) (default "json") diff --git a/cmd/detect.go b/cmd/detect.go index 92552fdb..7f264cf9 100644 --- a/cmd/detect.go +++ b/cmd/detect.go @@ -76,6 +76,10 @@ func runDetect(cmd *cobra.Command, args []string) { if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil { log.Fatal().Err(err).Msg("") } + // set color flag + if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil { + log.Fatal().Err(err).Msg("") + } if fileExists(filepath.Join(source, ".gitleaksignore")) { if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil { diff --git a/cmd/protect.go b/cmd/protect.go index da267096..99a0f352 100644 --- a/cmd/protect.go +++ b/cmd/protect.go @@ -69,6 +69,10 @@ func runProtect(cmd *cobra.Command, args []string) { if detector.MaxTargetMegaBytes, err = cmd.Flags().GetInt("max-target-megabytes"); err != nil { log.Fatal().Err(err).Msg("") } + // set color flag + if detector.NoColor, err = cmd.Flags().GetBool("no-color"); err != nil { + log.Fatal().Err(err).Msg("") + } if fileExists(filepath.Join(source, ".gitleaksignore")) { if err = detector.AddGitleaksIgnore(filepath.Join(source, ".gitleaksignore")); err != nil { diff --git a/cmd/root.go b/cmd/root.go index 8399a244..8c61fbdc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,6 +45,7 @@ func init() { rootCmd.PersistentFlags().StringP("baseline-path", "b", "", "path to baseline with issues that can be ignored") rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (trace, debug, info, warn, error, fatal)") rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan") + rootCmd.PersistentFlags().BoolP("no-color", "", false, "turn off color for verbose output") rootCmd.PersistentFlags().Int("max-target-megabytes", 0, "files larger than this will be skipped") rootCmd.PersistentFlags().Bool("redact", false, "redact secrets from logs and stdout") rootCmd.PersistentFlags().Bool("no-banner", false, "suppress banner") diff --git a/detect/detect.go b/detect/detect.go index d2773168..d37fc20f 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -57,6 +57,9 @@ type Detector struct { // followSymlinks is a flag to enable scanning symlink files FollowSymlinks bool + // NoColor is a flag to disable color output + NoColor bool + // commitMap is used to keep track of commits that have been scanned. // This is only used for logging purposes and git scans. commitMap map[string]bool @@ -536,7 +539,7 @@ func (d *Detector) DetectReader(r io.Reader, bufSize int) ([]report.Finding, err for _, finding := range d.Detect(fragment) { findings = append(findings, finding) if d.Verbose { - printFinding(finding) + printFinding(finding, d.NoColor) } } } @@ -610,7 +613,7 @@ func (d *Detector) addFinding(finding report.Finding) { d.findingMutex.Lock() d.findings = append(d.findings, finding) if d.Verbose { - printFinding(finding) + printFinding(finding, d.NoColor) } d.findingMutex.Unlock() } diff --git a/detect/utils.go b/detect/utils.go index 1a499513..be6e94fc 100644 --- a/detect/utils.go +++ b/detect/utils.go @@ -91,7 +91,7 @@ func filter(findings []report.Finding, redact bool) []report.Finding { return retFindings } -func printFinding(f report.Finding) { +func printFinding(f report.Finding, noColor bool) { // trim all whitespace and tabs from the line f.Line = strings.TrimSpace(f.Line) // trim all whitespace and tabs from the secret @@ -104,7 +104,7 @@ func printFinding(f report.Finding) { skipColor := false - if matchInLineIDX == -1 { + if matchInLineIDX == -1 || noColor { skipColor = true matchInLineIDX = 0 } @@ -144,11 +144,12 @@ func printFinding(f report.Finding) { if skipColor { fmt.Printf("%-12s %s\n", "Finding:", f.Match) + fmt.Printf("%-12s %s\n", "Secret:", f.Secret) } else { fmt.Printf("%-12s %s", "Finding:", finding) + fmt.Printf("%-12s %s\n", "Secret:", secret) } - fmt.Printf("%-12s %s\n", "Secret:", secret) fmt.Printf("%-12s %s\n", "RuleID:", f.RuleID) fmt.Printf("%-12s %f\n", "Entropy:", f.Entropy) if f.File == "" {