mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
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
This commit is contained in:
1 parent
839f114f2b
commit
bd8b145bea
1 file changed
+43
-37
+43
-37
@@ -92,57 +92,63 @@ func filter(findings []report.Finding, redact bool) []report.Finding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func printFinding(f report.Finding, noColor bool) {
|
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)
|
f.Line = strings.TrimSpace(f.Line)
|
||||||
// trim all whitespace and tabs from the secret
|
|
||||||
f.Secret = strings.TrimSpace(f.Secret)
|
f.Secret = strings.TrimSpace(f.Secret)
|
||||||
// trim all whitespace and tabs from the match
|
|
||||||
f.Match = strings.TrimSpace(f.Match)
|
f.Match = strings.TrimSpace(f.Match)
|
||||||
|
|
||||||
matchInLineIDX := strings.Index(f.Line, f.Match)
|
isFileMatch := strings.HasPrefix(f.Match, "file detected:")
|
||||||
secretInMatchIdx := strings.Index(f.Match, f.Secret)
|
skipColor := noColor
|
||||||
|
finding := ""
|
||||||
|
var secret lipgloss.Style
|
||||||
|
|
||||||
skipColor := false
|
// 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)
|
||||||
|
|
||||||
if matchInLineIDX == -1 || noColor {
|
skipColor = false
|
||||||
skipColor = true
|
|
||||||
matchInLineIDX = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
start := f.Line[0:matchInLineIDX]
|
if matchInLineIDX == -1 || noColor {
|
||||||
startMatchIdx := 0
|
skipColor = true
|
||||||
if matchInLineIDX > 20 {
|
matchInLineIDX = 0
|
||||||
startMatchIdx = matchInLineIDX - 20
|
}
|
||||||
start = "..." + f.Line[startMatchIdx:matchInLineIDX]
|
|
||||||
}
|
|
||||||
|
|
||||||
matchBeginning := lipgloss.NewStyle().SetString(f.Match[0:secretInMatchIdx]).Foreground(lipgloss.Color("#f5d445"))
|
start := f.Line[0:matchInLineIDX]
|
||||||
secret := lipgloss.NewStyle().SetString(f.Secret).
|
startMatchIdx := 0
|
||||||
Bold(true).
|
if matchInLineIDX > 20 {
|
||||||
Italic(true).
|
startMatchIdx = matchInLineIDX - 20
|
||||||
Foreground(lipgloss.Color("#f05c07"))
|
start = "..." + f.Line[startMatchIdx:matchInLineIDX]
|
||||||
matchEnd := lipgloss.NewStyle().SetString(f.Match[secretInMatchIdx+len(f.Secret):]).Foreground(lipgloss.Color("#f5d445"))
|
}
|
||||||
|
|
||||||
lineEndIdx := matchInLineIDX + len(f.Match)
|
matchBeginning := lipgloss.NewStyle().SetString(f.Match[0:secretInMatchIdx]).Foreground(lipgloss.Color("#f5d445"))
|
||||||
if len(f.Line)-1 <= lineEndIdx {
|
secret = lipgloss.NewStyle().SetString(f.Secret).
|
||||||
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).
|
Bold(true).
|
||||||
Italic(true).
|
Italic(true).
|
||||||
Foreground(lipgloss.Color("#f05c07"))
|
Foreground(lipgloss.Color("#f05c07"))
|
||||||
}
|
matchEnd := lipgloss.NewStyle().SetString(f.Match[secretInMatchIdx+len(f.Secret):]).Foreground(lipgloss.Color("#f5d445"))
|
||||||
if len(lineEnd) > 20 {
|
|
||||||
lineEnd = lineEnd[0:20] + "..."
|
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 || isFileMatch {
|
||||||
|
|
||||||
if skipColor {
|
|
||||||
fmt.Printf("%-12s %s\n", "Finding:", f.Match)
|
fmt.Printf("%-12s %s\n", "Finding:", f.Match)
|
||||||
fmt.Printf("%-12s %s\n", "Secret:", f.Secret)
|
fmt.Printf("%-12s %s\n", "Secret:", f.Secret)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in new issue
Block a user