Skip to content

Commit

Permalink
No color (#1136)
Browse files Browse the repository at this point in the history
* safer out of bounds

* no color option

* readme
  • Loading branch information
zricethezav committed Mar 29, 2023
1 parent b5a726e commit 63c3076
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions cmd/detect.go
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions cmd/protect.go
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Expand Up @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions detect/detect.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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()
}
Expand Down
7 changes: 4 additions & 3 deletions detect/utils.go
Expand Up @@ -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
Expand All @@ -104,7 +104,7 @@ func printFinding(f report.Finding) {

skipColor := false

if matchInLineIDX == -1 {
if matchInLineIDX == -1 || noColor {
skipColor = true
matchInLineIDX = 0
}
Expand Down Expand Up @@ -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 == "" {
Expand Down

0 comments on commit 63c3076

Please sign in to comment.