mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 04:34:46 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51ca0f89b9 | ||
|
|
81cf30855c | ||
|
|
bd8b145bea | ||
|
|
839f114f2b |
No files matched your search
@@ -182,7 +182,7 @@ You can scan files and directories by using the `--no-git` option.
|
|||||||
|
|
||||||
#### Protect
|
#### Protect
|
||||||
|
|
||||||
The `protect` command is used to uncommitted changes in a git repo. This command should be used on developer machines in accordance with
|
The `protect` command is used to scan uncommitted changes in a git repo. This command should be used on developer machines in accordance with
|
||||||
[shifting left on security](https://cloud.google.com/architecture/devops/devops-tech-shifting-left-on-security).
|
[shifting left on security](https://cloud.google.com/architecture/devops/devops-tech-shifting-left-on-security).
|
||||||
When running `protect` on a git repository, gitleaks will parse the output of a `git diff` command (you can see how this executed
|
When running `protect` on a git repository, gitleaks will parse the output of a `git diff` command (you can see how this executed
|
||||||
[here](https://github.com/zricethezav/gitleaks/blob/7240e16769b92d2a1b137c17d6bf9d55a8562899/git/git.go#L48-L49)). You can set the
|
[here](https://github.com/zricethezav/gitleaks/blob/7240e16769b92d2a1b137c17d6bf9d55a8562899/git/git.go#L48-L49)). You can set the
|
||||||
|
|||||||
+17
-12
@@ -154,7 +154,12 @@ func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
// https://github.com/securego/gosec/issues/512
|
||||||
|
defer func() {
|
||||||
|
if err := file.Close(); err != nil {
|
||||||
|
log.Warn().Msgf("Error closing .gitleaksignore file: %s\n", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
@@ -287,6 +292,17 @@ func (d *Detector) detectRule(fragment Fragment, rule config.Rule) []report.Find
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extract secret from secret group if set
|
||||||
|
if rule.SecretGroup != 0 {
|
||||||
|
groups := rule.Regex.FindStringSubmatch(secret)
|
||||||
|
if len(groups) <= rule.SecretGroup || len(groups) == 0 {
|
||||||
|
// Config validation should prevent this
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
secret = groups[rule.SecretGroup]
|
||||||
|
finding.Secret = secret
|
||||||
|
}
|
||||||
|
|
||||||
// check if the regexTarget is defined in the allowlist "regexes" entry
|
// check if the regexTarget is defined in the allowlist "regexes" entry
|
||||||
allowlistTarget := finding.Secret
|
allowlistTarget := finding.Secret
|
||||||
switch rule.Allowlist.RegexTarget {
|
switch rule.Allowlist.RegexTarget {
|
||||||
@@ -308,17 +324,6 @@ func (d *Detector) detectRule(fragment Fragment, rule config.Rule) []report.Find
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract secret from secret group if set
|
|
||||||
if rule.SecretGroup != 0 {
|
|
||||||
groups := rule.Regex.FindStringSubmatch(secret)
|
|
||||||
if len(groups) <= rule.SecretGroup || len(groups) == 0 {
|
|
||||||
// Config validation should prevent this
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
secret = groups[rule.SecretGroup]
|
|
||||||
finding.Secret = secret
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if the secret is in the list of stopwords
|
// check if the secret is in the list of stopwords
|
||||||
if rule.Allowlist.ContainsStopWord(finding.Secret) ||
|
if rule.Allowlist.ContainsStopWord(finding.Secret) ||
|
||||||
d.Config.Allowlist.ContainsStopWord(finding.Secret) {
|
d.Config.Allowlist.ContainsStopWord(finding.Secret) {
|
||||||
|
|||||||
+44
-38
@@ -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
|
||||||
|
|
||||||
|
// 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 {
|
if matchInLineIDX == -1 || noColor {
|
||||||
skipColor = true
|
skipColor = true
|
||||||
matchInLineIDX = 0
|
matchInLineIDX = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
start := f.Line[0:matchInLineIDX]
|
start := f.Line[0:matchInLineIDX]
|
||||||
startMatchIdx := 0
|
startMatchIdx := 0
|
||||||
if matchInLineIDX > 20 {
|
if matchInLineIDX > 20 {
|
||||||
startMatchIdx = matchInLineIDX - 20
|
startMatchIdx = matchInLineIDX - 20
|
||||||
start = "..." + f.Line[startMatchIdx:matchInLineIDX]
|
start = "..." + f.Line[startMatchIdx:matchInLineIDX]
|
||||||
}
|
}
|
||||||
|
|
||||||
matchBeginning := lipgloss.NewStyle().SetString(f.Match[0:secretInMatchIdx]).Foreground(lipgloss.Color("#f5d445"))
|
matchBeginning := lipgloss.NewStyle().SetString(f.Match[0:secretInMatchIdx]).Foreground(lipgloss.Color("#f5d445"))
|
||||||
secret := lipgloss.NewStyle().SetString(f.Secret).
|
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] + "...").
|
|
||||||
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