Pārlūkot izejas kodu

fix(detect): avoid panic with verbose flag (#1143)

* fix(detect): avoid panic with verbose flag

* chore: fix minor syntax error

* fix: properly print secret with colour

* fix(detect): use lineEndIdx
Richard Gomez 2 gadi atpakaļ
vecāks
revīzija
bd8b145bea
1 mainītis faili ar 45 papildinājumiem un 39 dzēšanām
  1. 45 39
      detect/utils.go

+ 45 - 39
detect/utils.go

@@ -92,57 +92,63 @@ func filter(findings []report.Finding, redact bool) []report.Finding {
 }
 
 func printFinding(f report.Finding, noColor bool) {
-	// trim all whitespace and tabs from the line
+	// trim all whitespace and tabs
 	f.Line = strings.TrimSpace(f.Line)
-	// trim all whitespace and tabs from the secret
 	f.Secret = strings.TrimSpace(f.Secret)
-	// trim all whitespace and tabs from the match
 	f.Match = strings.TrimSpace(f.Match)
 
-	matchInLineIDX := strings.Index(f.Line, f.Match)
-	secretInMatchIdx := strings.Index(f.Match, f.Secret)
+	isFileMatch := strings.HasPrefix(f.Match, "file detected:")
+	skipColor := noColor
+	finding := ""
+	var secret lipgloss.Style
+	
+	// Matches from filenames do not have a |line| or |secret|
+	if !isFileMatch {
+		matchInLineIDX := strings.Index(f.Line, f.Match)
+		secretInMatchIdx := strings.Index(f.Match, f.Secret)
+
+		skipColor = false
+
+		if matchInLineIDX == -1 || noColor {
+			skipColor = true
+			matchInLineIDX = 0
+		}
 
-	skipColor := false
+		start := f.Line[0:matchInLineIDX]
+		startMatchIdx := 0
+		if matchInLineIDX > 20 {
+			startMatchIdx = matchInLineIDX - 20
+			start = "..." + f.Line[startMatchIdx:matchInLineIDX]
+		}
 
-	if matchInLineIDX == -1 || noColor {
-		skipColor = true
-		matchInLineIDX = 0
-	}
+		matchBeginning := lipgloss.NewStyle().SetString(f.Match[0:secretInMatchIdx]).Foreground(lipgloss.Color("#f5d445"))
+		secret = lipgloss.NewStyle().SetString(f.Secret).
+			Bold(true).
+			Italic(true).
+			Foreground(lipgloss.Color("#f05c07"))
+		matchEnd := lipgloss.NewStyle().SetString(f.Match[secretInMatchIdx+len(f.Secret):]).Foreground(lipgloss.Color("#f5d445"))
 
-	start := f.Line[0:matchInLineIDX]
-	startMatchIdx := 0
-	if matchInLineIDX > 20 {
-		startMatchIdx = matchInLineIDX - 20
-		start = "..." + f.Line[startMatchIdx:matchInLineIDX]
-	}
+		lineEndIdx := matchInLineIDX + len(f.Match)
+		if len(f.Line)-1 <= lineEndIdx {
+			lineEndIdx = len(f.Line) - 1
+		}
 
-	matchBeginning := lipgloss.NewStyle().SetString(f.Match[0:secretInMatchIdx]).Foreground(lipgloss.Color("#f5d445"))
-	secret := lipgloss.NewStyle().SetString(f.Secret).
-		Bold(true).
-		Italic(true).
-		Foreground(lipgloss.Color("#f05c07"))
-	matchEnd := lipgloss.NewStyle().SetString(f.Match[secretInMatchIdx+len(f.Secret):]).Foreground(lipgloss.Color("#f5d445"))
+		lineEnd := f.Line[lineEndIdx:]
 
-	lineEndIdx := matchInLineIDX + len(f.Match)
-	if len(f.Line)-1 <= lineEndIdx {
-		lineEndIdx = len(f.Line) - 1
-	}
-
-	lineEnd := f.Line[matchInLineIDX+len(f.Match):]
+		if len(f.Secret) > 100 {
+			secret = lipgloss.NewStyle().SetString(f.Secret[0:100] + "...").
+				Bold(true).
+				Italic(true).
+				Foreground(lipgloss.Color("#f05c07"))
+		}
+		if len(lineEnd) > 20 {
+			lineEnd = lineEnd[0:20] + "..."
+		}
 
-	if len(f.Secret) > 100 {
-		secret = lipgloss.NewStyle().SetString(f.Secret[0:100] + "...").
-			Bold(true).
-			Italic(true).
-			Foreground(lipgloss.Color("#f05c07"))
+		finding = fmt.Sprintf("%s%s%s%s%s\n", strings.TrimPrefix(strings.TrimLeft(start, " "), "\n"), matchBeginning, secret, matchEnd, lineEnd)
 	}
-	if len(lineEnd) > 20 {
-		lineEnd = lineEnd[0:20] + "..."
-	}
-
-	finding := fmt.Sprintf("%s%s%s%s%s\n", strings.TrimPrefix(strings.TrimLeft(start, " "), "\n"), matchBeginning, secret, matchEnd, lineEnd)
 
-	if skipColor {
+	if skipColor || isFileMatch {
 		fmt.Printf("%-12s %s\n", "Finding:", f.Match)
 		fmt.Printf("%-12s %s\n", "Secret:", f.Secret)
 	} else {