From bd8b145bea38150ace608da16c56cf36ffe7ed27 Mon Sep 17 00:00:00 2001 From: Richard Gomez <32133502+rgmz@users.noreply.github.com> Date: Wed, 19 Apr 2023 12:55:33 -0400 Subject: [PATCH] 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 --- detect/utils.go | 82 ++++++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/detect/utils.go b/detect/utils.go index be6e94fc..a7dc79ec 100644 --- a/detect/utils.go +++ b/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 + skipColor = false - if matchInLineIDX == -1 || noColor { - skipColor = true - matchInLineIDX = 0 - } + if matchInLineIDX == -1 || noColor { + skipColor = true + matchInLineIDX = 0 + } - start := f.Line[0:matchInLineIDX] - startMatchIdx := 0 - if matchInLineIDX > 20 { - startMatchIdx = matchInLineIDX - 20 - start = "..." + f.Line[startMatchIdx:matchInLineIDX] - } + start := f.Line[0:matchInLineIDX] + startMatchIdx := 0 + if matchInLineIDX > 20 { + startMatchIdx = matchInLineIDX - 20 + start = "..." + f.Line[startMatchIdx:matchInLineIDX] + } - 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")) - - 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] + "..."). + 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")) - } - if len(lineEnd) > 20 { - lineEnd = lineEnd[0:20] + "..." + matchEnd := lipgloss.NewStyle().SetString(f.Match[secretInMatchIdx+len(f.Secret):]).Foreground(lipgloss.Color("#f5d445")) + + lineEndIdx := matchInLineIDX + len(f.Match) + if len(f.Line)-1 <= lineEndIdx { + lineEndIdx = len(f.Line) - 1 + } + + lineEnd := f.Line[lineEndIdx:] + + 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] + "..." + } + + finding = fmt.Sprintf("%s%s%s%s%s\n", strings.TrimPrefix(strings.TrimLeft(start, " "), "\n"), matchBeginning, secret, matchEnd, lineEnd) } - 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 {