mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 12:44:22 +01:00
Make paths and fingerprints platform-agnostic (#1622)
* test: windows * fix: make paths platform-agnostic this change also fixes, or skips, windows failures
This commit is contained in:
1 parent
818e32f7a4
commit
f565e4e10c
13 files changed
+535
-91
No files matched your search
@@ -4,6 +4,7 @@ jobs:
|
|||||||
scan:
|
scan:
|
||||||
name: gitleaks
|
name: gitleaks
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
if: ${{ github.repository == 'gitleaks/gitleaks' }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
|
|||||||
@@ -10,20 +10,28 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
strategy:
|
||||||
|
matrix:
|
||||||
|
platform: [ ubuntu-latest, windows-latest ]
|
||||||
|
runs-on: ${{ matrix.platform }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.22
|
go-version: 1.23
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: go build -v ./...
|
run: go build -v ./...
|
||||||
|
|
||||||
|
- name: Set up gotestsum
|
||||||
|
run: |
|
||||||
|
go install gotest.tools/gotestsum@latest
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: make test
|
run: |
|
||||||
|
gotestsum --raw-command -- go test -json ./... --race
|
||||||
|
|
||||||
- name: Validate Config
|
- name: Validate Config
|
||||||
run: go generate ./... && git diff --exit-code
|
run: go generate ./... && git diff --exit-code
|
||||||
+70
-41
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -26,7 +27,10 @@ const (
|
|||||||
chunkSize = 100 * 1_000 // 100kb
|
chunkSize = 100 * 1_000 // 100kb
|
||||||
)
|
)
|
||||||
|
|
||||||
var newLineRegexp = regexp.MustCompile("\n")
|
var (
|
||||||
|
newLineRegexp = regexp.MustCompile("\n")
|
||||||
|
isWindows = runtime.GOOS == "windows"
|
||||||
|
)
|
||||||
|
|
||||||
// Detector is the main detector struct
|
// Detector is the main detector struct
|
||||||
type Detector struct {
|
type Detector struct {
|
||||||
@@ -80,7 +84,7 @@ type Detector struct {
|
|||||||
baselinePath string
|
baselinePath string
|
||||||
|
|
||||||
// gitleaksIgnore
|
// gitleaksIgnore
|
||||||
gitleaksIgnore map[string]bool
|
gitleaksIgnore map[string]struct{}
|
||||||
|
|
||||||
// Sema (https://github.com/fatih/semgroup) controls the concurrency
|
// Sema (https://github.com/fatih/semgroup) controls the concurrency
|
||||||
Sema *semgroup.Group
|
Sema *semgroup.Group
|
||||||
@@ -99,9 +103,13 @@ type Fragment struct {
|
|||||||
|
|
||||||
Bytes []byte
|
Bytes []byte
|
||||||
|
|
||||||
// FilePath is the path to the file if applicable
|
// FilePath is the path to the file, if applicable.
|
||||||
|
// The path separator MUST be normalized to `/`.
|
||||||
FilePath string
|
FilePath string
|
||||||
SymlinkFile string
|
SymlinkFile string
|
||||||
|
// WindowsFilePath is the path with the original separator.
|
||||||
|
// This provides a backwards-compatible solution to https://github.com/gitleaks/gitleaks/issues/1565.
|
||||||
|
WindowsFilePath string `json:"-"` // TODO: remove this in v9.
|
||||||
|
|
||||||
// CommitSHA is the SHA of the commit if applicable
|
// CommitSHA is the SHA of the commit if applicable
|
||||||
CommitSHA string
|
CommitSHA string
|
||||||
@@ -115,7 +123,7 @@ type Fragment struct {
|
|||||||
func NewDetector(cfg config.Config) *Detector {
|
func NewDetector(cfg config.Config) *Detector {
|
||||||
return &Detector{
|
return &Detector{
|
||||||
commitMap: make(map[string]bool),
|
commitMap: make(map[string]bool),
|
||||||
gitleaksIgnore: make(map[string]bool),
|
gitleaksIgnore: make(map[string]struct{}),
|
||||||
findingMutex: &sync.Mutex{},
|
findingMutex: &sync.Mutex{},
|
||||||
findings: make([]report.Finding, 0),
|
findings: make([]report.Finding, 0),
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
@@ -146,25 +154,41 @@ func NewDetectorDefaultConfig() (*Detector, error) {
|
|||||||
func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
|
func (d *Detector) AddGitleaksIgnore(gitleaksIgnorePath string) error {
|
||||||
logging.Debug().Msgf("found .gitleaksignore file: %s", gitleaksIgnorePath)
|
logging.Debug().Msgf("found .gitleaksignore file: %s", gitleaksIgnorePath)
|
||||||
file, err := os.Open(gitleaksIgnorePath)
|
file, err := os.Open(gitleaksIgnorePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://github.com/securego/gosec/issues/512
|
|
||||||
defer func() {
|
defer func() {
|
||||||
|
// https://github.com/securego/gosec/issues/512
|
||||||
if err := file.Close(); err != nil {
|
if err := file.Close(); err != nil {
|
||||||
logging.Warn().Msgf("Error closing .gitleaksignore file: %s\n", err)
|
logging.Warn().Msgf("Error closing .gitleaksignore file: %s\n", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
replacer := strings.NewReplacer("\\", "/")
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := strings.TrimSpace(scanner.Text())
|
line := strings.TrimSpace(scanner.Text())
|
||||||
// Skip lines that start with a comment
|
// Skip lines that start with a comment
|
||||||
if line != "" && !strings.HasPrefix(line, "#") {
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
d.gitleaksIgnore[line] = true
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normalize the path.
|
||||||
|
// TODO: Make this a breaking change in v9.
|
||||||
|
s := strings.Split(line, ":")
|
||||||
|
switch len(s) {
|
||||||
|
case 3:
|
||||||
|
// Global fingerprint.
|
||||||
|
// `file:rule-id:start-line`
|
||||||
|
s[0] = replacer.Replace(s[0])
|
||||||
|
case 4:
|
||||||
|
// Commit fingerprint.
|
||||||
|
// `commit:file:rule-id:start-line`
|
||||||
|
s[1] = replacer.Replace(s[1])
|
||||||
|
default:
|
||||||
|
logging.Warn().Str("fingerprint", line).Msg("Invalid .gitleaksignore entry")
|
||||||
|
}
|
||||||
|
d.gitleaksIgnore[strings.Join(s, ":")] = struct{}{}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -191,9 +215,13 @@ func (d *Detector) Detect(fragment Fragment) []report.Finding {
|
|||||||
var findings []report.Finding
|
var findings []report.Finding
|
||||||
|
|
||||||
// check if filepath is allowed
|
// check if filepath is allowed
|
||||||
if fragment.FilePath != "" && (d.Config.Allowlist.PathAllowed(fragment.FilePath) ||
|
if fragment.FilePath != "" {
|
||||||
fragment.FilePath == d.Config.Path || (d.baselinePath != "" && fragment.FilePath == d.baselinePath)) {
|
// is the path our config or baseline file?
|
||||||
return findings
|
if fragment.FilePath == d.Config.Path || (d.baselinePath != "" && fragment.FilePath == d.baselinePath) ||
|
||||||
|
// is the path excluded by the global allowlist?
|
||||||
|
(d.Config.Allowlist.PathAllowed(fragment.FilePath) || (fragment.WindowsFilePath != "" && d.Config.Allowlist.PathAllowed(fragment.WindowsFilePath))) {
|
||||||
|
return findings
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add newline indices for location calculation in detectRule
|
// add newline indices for location calculation in detectRule
|
||||||
@@ -269,7 +297,7 @@ func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rul
|
|||||||
var (
|
var (
|
||||||
isAllowed bool
|
isAllowed bool
|
||||||
commitAllowed, commit = a.CommitAllowed(fragment.CommitSHA)
|
commitAllowed, commit = a.CommitAllowed(fragment.CommitSHA)
|
||||||
pathAllowed = a.PathAllowed(fragment.FilePath)
|
pathAllowed = a.PathAllowed(fragment.FilePath) || (fragment.WindowsFilePath != "" && a.PathAllowed(fragment.WindowsFilePath))
|
||||||
)
|
)
|
||||||
if a.MatchCondition == config.AllowlistMatchAnd {
|
if a.MatchCondition == config.AllowlistMatchAnd {
|
||||||
// Determine applicable checks.
|
// Determine applicable checks.
|
||||||
@@ -306,25 +334,27 @@ func (d *Detector) detectRule(fragment Fragment, currentRaw string, r config.Rul
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Path != nil && r.Regex == nil && len(encodedSegments) == 0 {
|
if r.Path != nil {
|
||||||
// Path _only_ rule
|
if r.Regex == nil && len(encodedSegments) == 0 {
|
||||||
if r.Path.MatchString(fragment.FilePath) {
|
// Path _only_ rule
|
||||||
finding := report.Finding{
|
if r.Path.MatchString(fragment.FilePath) || (fragment.WindowsFilePath != "" && r.Path.MatchString(fragment.WindowsFilePath)) {
|
||||||
Description: r.Description,
|
finding := report.Finding{
|
||||||
File: fragment.FilePath,
|
RuleID: r.RuleID,
|
||||||
SymlinkFile: fragment.SymlinkFile,
|
Description: r.Description,
|
||||||
RuleID: r.RuleID,
|
File: fragment.FilePath,
|
||||||
Match: fmt.Sprintf("file detected: %s", fragment.FilePath),
|
SymlinkFile: fragment.SymlinkFile,
|
||||||
Tags: r.Tags,
|
Match: fmt.Sprintf("file detected: %s", fragment.FilePath),
|
||||||
|
Tags: r.Tags,
|
||||||
|
}
|
||||||
|
return append(findings, finding)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// if path is set _and_ a regex is set, then we need to check both
|
||||||
|
// so if the path does not match, then we should return early and not
|
||||||
|
// consider the regex
|
||||||
|
if !(r.Path.MatchString(fragment.FilePath) || (fragment.WindowsFilePath != "" && r.Path.MatchString(fragment.WindowsFilePath))) {
|
||||||
|
return findings
|
||||||
}
|
}
|
||||||
return append(findings, finding)
|
|
||||||
}
|
|
||||||
} else if r.Path != nil {
|
|
||||||
// if path is set _and_ a regex is set, then we need to check both
|
|
||||||
// so if the path does not match, then we should return early and not
|
|
||||||
// consider the regex
|
|
||||||
if !r.Path.MatchString(fragment.FilePath) {
|
|
||||||
return findings
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -382,22 +412,21 @@ MatchLoop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
finding := report.Finding{
|
finding := report.Finding{
|
||||||
Description: r.Description,
|
|
||||||
File: fragment.FilePath,
|
|
||||||
SymlinkFile: fragment.SymlinkFile,
|
|
||||||
RuleID: r.RuleID,
|
RuleID: r.RuleID,
|
||||||
|
Description: r.Description,
|
||||||
StartLine: loc.startLine,
|
StartLine: loc.startLine,
|
||||||
EndLine: loc.endLine,
|
EndLine: loc.endLine,
|
||||||
StartColumn: loc.startColumn,
|
StartColumn: loc.startColumn,
|
||||||
EndColumn: loc.endColumn,
|
EndColumn: loc.endColumn,
|
||||||
Secret: secret,
|
|
||||||
Match: secret,
|
|
||||||
Tags: append(r.Tags, metaTags...),
|
|
||||||
Line: fragment.Raw[loc.startLineIndex:loc.endLineIndex],
|
Line: fragment.Raw[loc.startLineIndex:loc.endLineIndex],
|
||||||
|
Match: secret,
|
||||||
|
Secret: secret,
|
||||||
|
File: fragment.FilePath,
|
||||||
|
SymlinkFile: fragment.SymlinkFile,
|
||||||
|
Tags: append(r.Tags, metaTags...),
|
||||||
}
|
}
|
||||||
|
|
||||||
if !d.IgnoreGitleaksAllow &&
|
if !d.IgnoreGitleaksAllow && strings.Contains(finding.Line, gitleaksAllowSignature) {
|
||||||
strings.Contains(fragment.Raw[loc.startLineIndex:loc.endLineIndex], gitleaksAllowSignature) {
|
|
||||||
logger.Trace().
|
logger.Trace().
|
||||||
Str("finding", finding.Secret).
|
Str("finding", finding.Secret).
|
||||||
Msg("skipping finding: 'gitleaks:allow' signature")
|
Msg("skipping finding: 'gitleaks:allow' signature")
|
||||||
@@ -487,7 +516,7 @@ MatchLoop:
|
|||||||
allowlistChecks = append(allowlistChecks, commitAllowed)
|
allowlistChecks = append(allowlistChecks, commitAllowed)
|
||||||
}
|
}
|
||||||
if len(a.Paths) > 0 {
|
if len(a.Paths) > 0 {
|
||||||
pathAllowed = a.PathAllowed(fragment.FilePath)
|
pathAllowed = a.PathAllowed(fragment.FilePath) || (fragment.WindowsFilePath != "" && a.PathAllowed(fragment.WindowsFilePath))
|
||||||
allowlistChecks = append(allowlistChecks, pathAllowed)
|
allowlistChecks = append(allowlistChecks, pathAllowed)
|
||||||
}
|
}
|
||||||
if len(a.Regexes) > 0 {
|
if len(a.Regexes) > 0 {
|
||||||
|
|||||||
+395
-37
@@ -4,17 +4,20 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/zricethezav/gitleaks/v8/cmd/scm"
|
"github.com/zricethezav/gitleaks/v8/cmd/scm"
|
||||||
"github.com/zricethezav/gitleaks/v8/config"
|
"github.com/zricethezav/gitleaks/v8/config"
|
||||||
|
"github.com/zricethezav/gitleaks/v8/logging"
|
||||||
"github.com/zricethezav/gitleaks/v8/regexp"
|
"github.com/zricethezav/gitleaks/v8/regexp"
|
||||||
"github.com/zricethezav/gitleaks/v8/report"
|
"github.com/zricethezav/gitleaks/v8/report"
|
||||||
"github.com/zricethezav/gitleaks/v8/sources"
|
"github.com/zricethezav/gitleaks/v8/sources"
|
||||||
@@ -497,6 +500,12 @@ func TestDetect(t *testing.T) {
|
|||||||
|
|
||||||
// TestFromGit tests the FromGit function
|
// TestFromGit tests the FromGit function
|
||||||
func TestFromGit(t *testing.T) {
|
func TestFromGit(t *testing.T) {
|
||||||
|
// TODO: Fix this test on windows.
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skipf("TODO: this fails on Windows: [git] fatal: bad object refs/remotes/origin/main?")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
cfgName string
|
cfgName string
|
||||||
source string
|
source string
|
||||||
@@ -517,6 +526,7 @@ func TestFromGit(t *testing.T) {
|
|||||||
Line: "\n awsToken := \"AKIALALEMEL33243OLIA\"",
|
Line: "\n awsToken := \"AKIALALEMEL33243OLIA\"",
|
||||||
Secret: "AKIALALEMEL33243OLIA",
|
Secret: "AKIALALEMEL33243OLIA",
|
||||||
Match: "AKIALALEMEL33243OLIA",
|
Match: "AKIALALEMEL33243OLIA",
|
||||||
|
Entropy: 3.0841837,
|
||||||
File: "main.go",
|
File: "main.go",
|
||||||
Date: "2021-11-02T23:37:53Z",
|
Date: "2021-11-02T23:37:53Z",
|
||||||
Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
|
Commit: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587",
|
||||||
@@ -524,7 +534,6 @@ func TestFromGit(t *testing.T) {
|
|||||||
Email: "zricer@protonmail.com",
|
Email: "zricer@protonmail.com",
|
||||||
Message: "Accidentally add a secret",
|
Message: "Accidentally add a secret",
|
||||||
Tags: []string{"key", "AWS"},
|
Tags: []string{"key", "AWS"},
|
||||||
Entropy: 3.0841837,
|
|
||||||
Fingerprint: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587:main.go:aws-access-key:20",
|
Fingerprint: "1b6da43b82b22e4eaa10bcf8ee591e91abbfc587:main.go:aws-access-key:20",
|
||||||
Link: "https://github.com/gitleaks/test/blob/1b6da43b82b22e4eaa10bcf8ee591e91abbfc587/main.go#L20",
|
Link: "https://github.com/gitleaks/test/blob/1b6da43b82b22e4eaa10bcf8ee591e91abbfc587/main.go#L20",
|
||||||
},
|
},
|
||||||
@@ -585,7 +594,7 @@ func TestFromGit(t *testing.T) {
|
|||||||
defer moveDotGit(t, ".git", "dotGit")
|
defer moveDotGit(t, ".git", "dotGit")
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(strings.Join([]string{tt.cfgName, tt.logOpts}, "/"), func(t *testing.T) {
|
t.Run(strings.Join([]string{tt.cfgName, tt.source, tt.logOpts}, "/"), func(t *testing.T) {
|
||||||
viper.AddConfigPath(configPath)
|
viper.AddConfigPath(configPath)
|
||||||
viper.SetConfigName("simple")
|
viper.SetConfigName("simple")
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigType("toml")
|
||||||
@@ -625,6 +634,7 @@ func TestFromGit(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFromGitStaged(t *testing.T) {
|
func TestFromGitStaged(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
cfgName string
|
cfgName string
|
||||||
@@ -667,7 +677,6 @@ func TestFromGitStaged(t *testing.T) {
|
|||||||
|
|
||||||
moveDotGit(t, "dotGit", ".git")
|
moveDotGit(t, "dotGit", ".git")
|
||||||
defer moveDotGit(t, ".git", "dotGit")
|
defer moveDotGit(t, ".git", "dotGit")
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
||||||
viper.AddConfigPath(configPath)
|
viper.AddConfigPath(configPath)
|
||||||
@@ -709,17 +718,17 @@ func TestFromFiles(t *testing.T) {
|
|||||||
cfgName: "simple",
|
cfgName: "simple",
|
||||||
expectedFindings: []report.Finding{
|
expectedFindings: []report.Finding{
|
||||||
{
|
{
|
||||||
|
RuleID: "aws-access-key",
|
||||||
Description: "AWS Access Key",
|
Description: "AWS Access Key",
|
||||||
StartLine: 20,
|
StartLine: 20,
|
||||||
EndLine: 20,
|
EndLine: 20,
|
||||||
StartColumn: 16,
|
StartColumn: 16,
|
||||||
EndColumn: 35,
|
EndColumn: 35,
|
||||||
|
Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
|
||||||
Match: "AKIALALEMEL33243OLIA",
|
Match: "AKIALALEMEL33243OLIA",
|
||||||
Secret: "AKIALALEMEL33243OLIA",
|
Secret: "AKIALALEMEL33243OLIA",
|
||||||
Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
|
|
||||||
File: "../testdata/repos/nogit/main.go",
|
File: "../testdata/repos/nogit/main.go",
|
||||||
SymlinkFile: "",
|
SymlinkFile: "",
|
||||||
RuleID: "aws-access-key",
|
|
||||||
Tags: []string{"key", "AWS"},
|
Tags: []string{"key", "AWS"},
|
||||||
Entropy: 3.0841837,
|
Entropy: 3.0841837,
|
||||||
Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
|
Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
|
||||||
@@ -731,16 +740,16 @@ func TestFromFiles(t *testing.T) {
|
|||||||
cfgName: "simple",
|
cfgName: "simple",
|
||||||
expectedFindings: []report.Finding{
|
expectedFindings: []report.Finding{
|
||||||
{
|
{
|
||||||
|
RuleID: "aws-access-key",
|
||||||
Description: "AWS Access Key",
|
Description: "AWS Access Key",
|
||||||
StartLine: 20,
|
StartLine: 20,
|
||||||
EndLine: 20,
|
EndLine: 20,
|
||||||
StartColumn: 16,
|
StartColumn: 16,
|
||||||
EndColumn: 35,
|
EndColumn: 35,
|
||||||
|
Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
|
||||||
Match: "AKIALALEMEL33243OLIA",
|
Match: "AKIALALEMEL33243OLIA",
|
||||||
Secret: "AKIALALEMEL33243OLIA",
|
Secret: "AKIALALEMEL33243OLIA",
|
||||||
Line: "\n\tawsToken := \"AKIALALEMEL33243OLIA\"",
|
|
||||||
File: "../testdata/repos/nogit/main.go",
|
File: "../testdata/repos/nogit/main.go",
|
||||||
RuleID: "aws-access-key",
|
|
||||||
Tags: []string{"key", "AWS"},
|
Tags: []string{"key", "AWS"},
|
||||||
Entropy: 3.0841837,
|
Entropy: 3.0841837,
|
||||||
Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
|
Fingerprint: "../testdata/repos/nogit/main.go:aws-access-key:20",
|
||||||
@@ -757,16 +766,16 @@ func TestFromFiles(t *testing.T) {
|
|||||||
cfgName: "generic",
|
cfgName: "generic",
|
||||||
expectedFindings: []report.Finding{
|
expectedFindings: []report.Finding{
|
||||||
{
|
{
|
||||||
|
RuleID: "generic-api-key",
|
||||||
Description: "Generic API Key",
|
Description: "Generic API Key",
|
||||||
StartLine: 4,
|
StartLine: 4,
|
||||||
EndLine: 4,
|
EndLine: 4,
|
||||||
StartColumn: 5,
|
StartColumn: 5,
|
||||||
EndColumn: 35,
|
EndColumn: 35,
|
||||||
|
Line: "\nDB_PASSWORD=8ae31cacf141669ddfb5da",
|
||||||
Match: "PASSWORD=8ae31cacf141669ddfb5da",
|
Match: "PASSWORD=8ae31cacf141669ddfb5da",
|
||||||
Secret: "8ae31cacf141669ddfb5da",
|
Secret: "8ae31cacf141669ddfb5da",
|
||||||
Line: "\nDB_PASSWORD=8ae31cacf141669ddfb5da",
|
|
||||||
File: "../testdata/repos/nogit/.env.prod",
|
File: "../testdata/repos/nogit/.env.prod",
|
||||||
RuleID: "generic-api-key",
|
|
||||||
Tags: []string{},
|
Tags: []string{},
|
||||||
Entropy: 3.5383105,
|
Entropy: 3.5383105,
|
||||||
Fingerprint: "../testdata/repos/nogit/.env.prod:generic-api-key:4",
|
Fingerprint: "../testdata/repos/nogit/.env.prod:generic-api-key:4",
|
||||||
@@ -776,39 +785,64 @@ func TestFromFiles(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
viper.AddConfigPath(configPath)
|
t.Run(tt.cfgName+" - "+tt.source, func(t *testing.T) {
|
||||||
viper.SetConfigName(tt.cfgName)
|
viper.AddConfigPath(configPath)
|
||||||
viper.SetConfigType("toml")
|
viper.SetConfigName(tt.cfgName)
|
||||||
err := viper.ReadInConfig()
|
viper.SetConfigType("toml")
|
||||||
require.NoError(t, err)
|
err := viper.ReadInConfig()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
cfg, _ := vc.Translate()
|
|
||||||
detector := NewDetector(cfg)
|
|
||||||
|
|
||||||
var ignorePath string
|
cfg, _ := vc.Translate()
|
||||||
info, err := os.Stat(tt.source)
|
detector := NewDetector(cfg)
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
if info.IsDir() {
|
info, err := os.Stat(tt.source)
|
||||||
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
|
require.NoError(t, err)
|
||||||
} else {
|
|
||||||
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
|
var ignorePath string
|
||||||
}
|
if info.IsDir() {
|
||||||
err = detector.AddGitleaksIgnore(ignorePath)
|
ignorePath = filepath.Join(tt.source, ".gitleaksignore")
|
||||||
require.NoError(t, err)
|
} else {
|
||||||
detector.FollowSymlinks = true
|
ignorePath = filepath.Join(filepath.Dir(tt.source), ".gitleaksignore")
|
||||||
paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlist.PathAllowed)
|
}
|
||||||
require.NoError(t, err)
|
err = detector.AddGitleaksIgnore(ignorePath)
|
||||||
findings, err := detector.DetectFiles(paths)
|
require.NoError(t, err)
|
||||||
require.NoError(t, err)
|
|
||||||
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
detector.FollowSymlinks = true
|
||||||
|
paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlist.PathAllowed)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
findings, err := detector.DetectFiles(paths)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// TODO: Temporary mitigation.
|
||||||
|
// https://github.com/gitleaks/gitleaks/issues/1641
|
||||||
|
normalizedFindings := make([]report.Finding, len(findings))
|
||||||
|
for i, f := range findings {
|
||||||
|
if strings.HasSuffix(f.Line, "\r") {
|
||||||
|
f.Line = strings.ReplaceAll(f.Line, "\r", "")
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(f.Match, "\r") {
|
||||||
|
f.EndColumn = f.EndColumn - 1
|
||||||
|
f.Match = strings.ReplaceAll(f.Match, "\r", "")
|
||||||
|
}
|
||||||
|
normalizedFindings[i] = f
|
||||||
|
}
|
||||||
|
assert.ElementsMatch(t, tt.expectedFindings, normalizedFindings)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDetectWithSymlinks(t *testing.T) {
|
func TestDetectWithSymlinks(t *testing.T) {
|
||||||
|
// TODO: Fix this test on windows.
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skipf("TODO: this returns no results on windows, I'm not sure why.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
cfgName string
|
cfgName string
|
||||||
source string
|
source string
|
||||||
@@ -819,6 +853,7 @@ func TestDetectWithSymlinks(t *testing.T) {
|
|||||||
cfgName: "simple",
|
cfgName: "simple",
|
||||||
expectedFindings: []report.Finding{
|
expectedFindings: []report.Finding{
|
||||||
{
|
{
|
||||||
|
RuleID: "apkey",
|
||||||
Description: "Asymmetric Private Key",
|
Description: "Asymmetric Private Key",
|
||||||
StartLine: 1,
|
StartLine: 1,
|
||||||
EndLine: 1,
|
EndLine: 1,
|
||||||
@@ -829,7 +864,6 @@ func TestDetectWithSymlinks(t *testing.T) {
|
|||||||
Line: "-----BEGIN OPENSSH PRIVATE KEY-----",
|
Line: "-----BEGIN OPENSSH PRIVATE KEY-----",
|
||||||
File: "../testdata/repos/symlinks/source_file/id_ed25519",
|
File: "../testdata/repos/symlinks/source_file/id_ed25519",
|
||||||
SymlinkFile: "../testdata/repos/symlinks/file_symlink/symlinked_id_ed25519",
|
SymlinkFile: "../testdata/repos/symlinks/file_symlink/symlinked_id_ed25519",
|
||||||
RuleID: "apkey",
|
|
||||||
Tags: []string{"key", "AsymmetricPrivateKey"},
|
Tags: []string{"key", "AsymmetricPrivateKey"},
|
||||||
Entropy: 3.587164,
|
Entropy: 3.587164,
|
||||||
Fingerprint: "../testdata/repos/symlinks/source_file/id_ed25519:apkey:1",
|
Fingerprint: "../testdata/repos/symlinks/source_file/id_ed25519:apkey:1",
|
||||||
@@ -848,11 +882,13 @@ func TestDetectWithSymlinks(t *testing.T) {
|
|||||||
var vc config.ViperConfig
|
var vc config.ViperConfig
|
||||||
err = viper.Unmarshal(&vc)
|
err = viper.Unmarshal(&vc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
cfg, _ := vc.Translate()
|
cfg, _ := vc.Translate()
|
||||||
detector := NewDetector(cfg)
|
detector := NewDetector(cfg)
|
||||||
detector.FollowSymlinks = true
|
detector.FollowSymlinks = true
|
||||||
paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlist.PathAllowed)
|
paths, err := sources.DirectoryTargets(tt.source, detector.Sema, true, cfg.Allowlist.PathAllowed)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
findings, err := detector.DetectFiles(paths)
|
findings, err := detector.DetectFiles(paths)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
assert.ElementsMatch(t, tt.expectedFindings, findings)
|
||||||
@@ -1117,3 +1153,325 @@ func moveDotGit(t *testing.T, from, to string) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// region Windows-specific tests[]
|
||||||
|
func TestNormalizeGitleaksIgnorePaths(t *testing.T) {
|
||||||
|
d, err := NewDetectorDefaultConfig()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = d.AddGitleaksIgnore("../testdata/gitleaksignore/.windowspaths")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Len(t, d.gitleaksIgnore, 3)
|
||||||
|
expected := map[string]struct{}{
|
||||||
|
"foo/bar/gitleaks-false-positive.yaml:aws-access-token:4": {},
|
||||||
|
"foo/bar/gitleaks-false-positive.yaml:aws-access-token:5": {},
|
||||||
|
"b55d88dc151f7022901cda41a03d43e0e508f2b7:test_data/test_local_repo_three_leaks.json:aws-access-token:73": {},
|
||||||
|
}
|
||||||
|
assert.ElementsMatch(t, maps.Keys(d.gitleaksIgnore), maps.Keys(expected))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWindowsFileSeparator_RulePath(t *testing.T) {
|
||||||
|
logging.Logger = logging.Logger.Level(zerolog.TraceLevel)
|
||||||
|
unixRule := config.Rule{
|
||||||
|
RuleID: "test-rule",
|
||||||
|
Path: regexp.MustCompile(`(^|/)\.m2/settings\.xml`),
|
||||||
|
}
|
||||||
|
windowsRule := config.Rule{
|
||||||
|
RuleID: "test-rule",
|
||||||
|
Path: regexp.MustCompile(`(^|\\)\.m2\\settings\.xml`),
|
||||||
|
}
|
||||||
|
expected := []report.Finding{
|
||||||
|
{
|
||||||
|
RuleID: "test-rule",
|
||||||
|
Match: "file detected: .m2/settings.xml",
|
||||||
|
File: ".m2/settings.xml",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tests := map[string]struct {
|
||||||
|
fragment Fragment
|
||||||
|
rule config.Rule
|
||||||
|
expected []report.Finding
|
||||||
|
}{
|
||||||
|
// unix rule
|
||||||
|
"unix rule - unix path separator": {
|
||||||
|
fragment: Fragment{
|
||||||
|
FilePath: `.m2/settings.xml`,
|
||||||
|
},
|
||||||
|
rule: unixRule,
|
||||||
|
expected: expected,
|
||||||
|
},
|
||||||
|
"unix rule - windows path separator": {
|
||||||
|
fragment: Fragment{
|
||||||
|
FilePath: `.m2/settings.xml`,
|
||||||
|
WindowsFilePath: `.m2\settings.xml`,
|
||||||
|
},
|
||||||
|
rule: unixRule,
|
||||||
|
expected: expected,
|
||||||
|
},
|
||||||
|
"unix regex+path rule - windows path separator": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `<password>s3cr3t</password>`,
|
||||||
|
FilePath: `.m2/settings.xml`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "test-rule",
|
||||||
|
Regex: regexp.MustCompile(`<password>(.+?)</password>`),
|
||||||
|
Path: regexp.MustCompile(`(^|/)\.m2/settings\.xml`),
|
||||||
|
},
|
||||||
|
expected: []report.Finding{
|
||||||
|
{
|
||||||
|
RuleID: "test-rule",
|
||||||
|
StartColumn: 1,
|
||||||
|
EndColumn: 27,
|
||||||
|
Line: "<password>s3cr3t</password>",
|
||||||
|
Match: "<password>s3cr3t</password>",
|
||||||
|
Secret: "s3cr3t",
|
||||||
|
Entropy: 2.251629114151001,
|
||||||
|
File: ".m2/settings.xml",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// windows rule
|
||||||
|
"windows rule - unix path separator": {
|
||||||
|
fragment: Fragment{
|
||||||
|
FilePath: `.m2/settings.xml`,
|
||||||
|
},
|
||||||
|
rule: windowsRule,
|
||||||
|
// This never worked, and continues not to work.
|
||||||
|
// Paths should be normalized to use Unix file separators.
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
"windows rule - windows path separator": {
|
||||||
|
fragment: Fragment{
|
||||||
|
FilePath: `.m2/settings.xml`,
|
||||||
|
WindowsFilePath: `.m2\settings.xml`,
|
||||||
|
},
|
||||||
|
rule: windowsRule,
|
||||||
|
expected: expected,
|
||||||
|
},
|
||||||
|
"windows regex+path rule - windows path separator": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `<password>s3cr3t</password>`,
|
||||||
|
FilePath: `.m2/settings.xml`,
|
||||||
|
WindowsFilePath: `.m2\settings.xml`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "test-rule",
|
||||||
|
Regex: regexp.MustCompile(`<password>(.+?)</password>`),
|
||||||
|
Path: regexp.MustCompile(`(^|\\)\.m2\\settings\.xml`),
|
||||||
|
},
|
||||||
|
expected: []report.Finding{
|
||||||
|
{
|
||||||
|
RuleID: "test-rule",
|
||||||
|
StartColumn: 1,
|
||||||
|
EndColumn: 27,
|
||||||
|
Line: "<password>s3cr3t</password>",
|
||||||
|
Match: "<password>s3cr3t</password>",
|
||||||
|
Secret: "s3cr3t",
|
||||||
|
Entropy: 2.251629114151001,
|
||||||
|
File: ".m2/settings.xml",
|
||||||
|
},
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
|
||||||
|
d, err := NewDetectorDefaultConfig()
|
||||||
|
require.NoError(t, err)
|
||||||
|
for name, test := range tests {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []EncodedSegment{})
|
||||||
|
if diff := cmp.Diff(test.expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-want +got)\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWindowsFileSeparator_RuleAllowlistPaths(t *testing.T) {
|
||||||
|
tests := map[string]struct {
|
||||||
|
fragment Fragment
|
||||||
|
rule config.Rule
|
||||||
|
expected []report.Finding
|
||||||
|
}{
|
||||||
|
// unix
|
||||||
|
"unix path separator - unix rule - OR allowlist path-only": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/unix.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "unix-rule",
|
||||||
|
Regex: regexp.MustCompile(`s3cr3t`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
"unix path separator - windows rule - OR allowlist path-only": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/unix.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "windows-rule",
|
||||||
|
Regex: regexp.MustCompile(`s3cr3t`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Windows separators in regex don't work for unix.
|
||||||
|
expected: []report.Finding{
|
||||||
|
{
|
||||||
|
RuleID: "windows-rule",
|
||||||
|
StartColumn: 9,
|
||||||
|
EndColumn: 14,
|
||||||
|
Line: `value: "s3cr3t"`,
|
||||||
|
Match: `s3cr3t`,
|
||||||
|
Secret: `s3cr3t`,
|
||||||
|
File: "ignoreme/unix.txt",
|
||||||
|
Entropy: 2.251629114151001,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"unix path separator - unix rule - AND allowlist path+stopwords": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "f4k3s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/unix.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "unix-rule",
|
||||||
|
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
MatchCondition: config.AllowlistMatchAnd,
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||||
|
StopWords: []string{"f4k3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
"unix path separator - windows rule - AND allowlist path+stopwords": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "f4k3s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/unix.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "windows-rule",
|
||||||
|
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
MatchCondition: config.AllowlistMatchAnd,
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||||
|
StopWords: []string{"f4k3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []report.Finding{
|
||||||
|
{
|
||||||
|
RuleID: "windows-rule",
|
||||||
|
StartColumn: 1,
|
||||||
|
EndColumn: 19,
|
||||||
|
Line: `value: "f4k3s3cr3t"`,
|
||||||
|
Match: `value: "f4k3s3cr3t"`,
|
||||||
|
Secret: `value: "f4k3s3cr3t"`,
|
||||||
|
File: "ignoreme/unix.txt",
|
||||||
|
Entropy: 3.892407178878784,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
// windows
|
||||||
|
"windows path separator - unix rule - OR allowlist path-only": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/windows.txt`,
|
||||||
|
WindowsFilePath: `ignoreme\windows.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "unix-rule",
|
||||||
|
Regex: regexp.MustCompile(`s3cr3t`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
"windows path separator - windows rule - OR allowlist path-only": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/windows.txt`,
|
||||||
|
WindowsFilePath: `ignoreme\windows.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "windows-rule",
|
||||||
|
Regex: regexp.MustCompile(`s3cr3t`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
"windows path separator - unix rule - AND allowlist path+stopwords": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "f4k3s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/unix.txt`,
|
||||||
|
WindowsFilePath: `ignoreme\windows.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "unix-rule",
|
||||||
|
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
MatchCondition: config.AllowlistMatchAnd,
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|/)ignoreme(/.*)?$`)},
|
||||||
|
StopWords: []string{"f4k3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
"windows path separator - windows rule - AND allowlist path+stopwords": {
|
||||||
|
fragment: Fragment{
|
||||||
|
Raw: `value: "f4k3s3cr3t"`,
|
||||||
|
FilePath: `ignoreme/unix.txt`,
|
||||||
|
WindowsFilePath: `ignoreme\windows.txt`,
|
||||||
|
},
|
||||||
|
rule: config.Rule{
|
||||||
|
RuleID: "windows-rule",
|
||||||
|
Regex: regexp.MustCompile(`value: "[^"]+"`),
|
||||||
|
Allowlists: []config.Allowlist{
|
||||||
|
{
|
||||||
|
MatchCondition: config.AllowlistMatchAnd,
|
||||||
|
Paths: []*regexp.Regexp{regexp.MustCompile(`(^|\\)ignoreme(\\.*)?$`)},
|
||||||
|
StopWords: []string{"f4k3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
d, err := NewDetectorDefaultConfig()
|
||||||
|
require.NoError(t, err)
|
||||||
|
for name, test := range tests {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
actual := d.detectRule(test.fragment, test.fragment.Raw, test.rule, []EncodedSegment{})
|
||||||
|
if diff := cmp.Diff(test.expected, actual); diff != "" {
|
||||||
|
t.Errorf("diff: (-want +got)\n%s", diff)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//endregion
|
||||||
+11
-3
@@ -5,6 +5,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/h2non/filetype"
|
"github.com/h2non/filetype"
|
||||||
@@ -83,13 +84,20 @@ func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Findin
|
|||||||
linesInChunk := strings.Count(chunk, "\n")
|
linesInChunk := strings.Count(chunk, "\n")
|
||||||
totalLines += linesInChunk
|
totalLines += linesInChunk
|
||||||
fragment := Fragment{
|
fragment := Fragment{
|
||||||
Raw: chunk,
|
Raw: chunk,
|
||||||
Bytes: peekBuf.Bytes(),
|
Bytes: peekBuf.Bytes(),
|
||||||
FilePath: pa.Path,
|
|
||||||
}
|
}
|
||||||
if pa.Symlink != "" {
|
if pa.Symlink != "" {
|
||||||
fragment.SymlinkFile = pa.Symlink
|
fragment.SymlinkFile = pa.Symlink
|
||||||
}
|
}
|
||||||
|
if isWindows {
|
||||||
|
fragment.FilePath = filepath.ToSlash(pa.Path)
|
||||||
|
fragment.SymlinkFile = filepath.ToSlash(fragment.SymlinkFile)
|
||||||
|
fragment.WindowsFilePath = pa.Path
|
||||||
|
} else {
|
||||||
|
fragment.FilePath = pa.Path
|
||||||
|
}
|
||||||
|
|
||||||
for _, finding := range d.Detect(fragment) {
|
for _, finding := range d.Detect(fragment) {
|
||||||
// need to add 1 since line counting starts at 1
|
// need to add 1 since line counting starts at 1
|
||||||
finding.StartLine += (totalLines - linesInChunk) + 1
|
finding.StartLine += (totalLines - linesInChunk) + 1
|
||||||
|
|||||||
+5
-1
@@ -53,6 +53,7 @@ func TestWriteCSV(t *testing.T) {
|
|||||||
t.Run(test.testReportName, func(t *testing.T) {
|
t.Run(test.testReportName, func(t *testing.T) {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".csv"))
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".csv"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
err = reporter.Write(tmpfile, test.findings)
|
err = reporter.Write(tmpfile, test.findings)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -67,7 +68,10 @@ func TestWriteCSV(t *testing.T) {
|
|||||||
|
|
||||||
want, err := os.ReadFile(test.expected)
|
want, err := os.ReadFile(test.expected)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, string(want), string(got))
|
|
||||||
|
wantStr := lineEndingReplacer.Replace(string(want))
|
||||||
|
gotStr := lineEndingReplacer.Replace(string(got))
|
||||||
|
assert.Equal(t, wantStr, gotStr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+8
-1
@@ -53,18 +53,25 @@ func TestWriteJSON(t *testing.T) {
|
|||||||
t.Run(test.testReportName, func(t *testing.T) {
|
t.Run(test.testReportName, func(t *testing.T) {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
err = reporter.Write(tmpfile, test.findings)
|
err = reporter.Write(tmpfile, test.findings)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.FileExists(t, tmpfile.Name())
|
assert.FileExists(t, tmpfile.Name())
|
||||||
|
|
||||||
got, err := os.ReadFile(tmpfile.Name())
|
got, err := os.ReadFile(tmpfile.Name())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
if test.wantEmpty {
|
if test.wantEmpty {
|
||||||
assert.Empty(t, got)
|
assert.Empty(t, got)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
want, err := os.ReadFile(test.expected)
|
want, err := os.ReadFile(test.expected)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, string(want), string(got))
|
|
||||||
|
wantStr := lineEndingReplacer.Replace(string(want))
|
||||||
|
gotStr := lineEndingReplacer.Replace(string(got))
|
||||||
|
assert.Equal(t, wantStr, gotStr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,6 +69,7 @@ func TestWriteJunit(t *testing.T) {
|
|||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".xml"))
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".xml"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
err = reporter.Write(tmpfile, test.findings)
|
err = reporter.Write(tmpfile, test.findings)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -83,6 +84,9 @@ func TestWriteJunit(t *testing.T) {
|
|||||||
|
|
||||||
want, err := os.ReadFile(test.expected)
|
want, err := os.ReadFile(test.expected)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, string(want), string(got))
|
|
||||||
|
wantStr := lineEndingReplacer.Replace(string(want))
|
||||||
|
gotStr := lineEndingReplacer.Replace(string(got))
|
||||||
|
assert.Equal(t, wantStr, gotStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package report
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -9,7 +10,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const expectPath = "../testdata/expected/"
|
const expectPath = "../testdata/expected/"
|
||||||
const configPath = "../testdata/config/"
|
|
||||||
const templatePath = "../testdata/report/"
|
const templatePath = "../testdata/report/"
|
||||||
|
|
||||||
func TestWriteStdout(t *testing.T) {
|
func TestWriteStdout(t *testing.T) {
|
||||||
@@ -40,3 +40,10 @@ type testWriter struct {
|
|||||||
func (t testWriter) Close() error {
|
func (t testWriter) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lineEndingReplacer normalizes CRLF to LF so tests pass on Windows.
|
||||||
|
var lineEndingReplacer = strings.NewReplacer(
|
||||||
|
"\\r\\n", "\\n",
|
||||||
|
"\r", "",
|
||||||
|
"\\r", "",
|
||||||
|
)
|
||||||
@@ -49,6 +49,7 @@ func TestWriteSarif(t *testing.T) {
|
|||||||
t.Run(test.cfgName, func(t *testing.T) {
|
t.Run(test.cfgName, func(t *testing.T) {
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+".json"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
reporter := SarifReporter{
|
reporter := SarifReporter{
|
||||||
OrderedRules: []config.Rule{
|
OrderedRules: []config.Rule{
|
||||||
@@ -76,7 +77,10 @@ func TestWriteSarif(t *testing.T) {
|
|||||||
|
|
||||||
want, err := os.ReadFile(test.expected)
|
want, err := os.ReadFile(test.expected)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, string(want), string(got))
|
|
||||||
|
wantStr := lineEndingReplacer.Replace(string(want))
|
||||||
|
gotStr := lineEndingReplacer.Replace(string(got))
|
||||||
|
assert.Equal(t, wantStr, gotStr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,6 +74,7 @@ func TestWriteTemplate(t *testing.T) {
|
|||||||
|
|
||||||
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+filepath.Ext(test.expected)))
|
tmpfile, err := os.Create(filepath.Join(t.TempDir(), test.testReportName+filepath.Ext(test.expected)))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
err = reporter.Write(tmpfile, test.findings)
|
err = reporter.Write(tmpfile, test.findings)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@@ -88,7 +89,10 @@ func TestWriteTemplate(t *testing.T) {
|
|||||||
|
|
||||||
want, err := os.ReadFile(test.expected)
|
want, err := os.ReadFile(test.expected)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, string(want), string(got))
|
|
||||||
|
wantStr := lineEndingReplacer.Replace(string(want))
|
||||||
|
gotStr := lineEndingReplacer.Replace(string(got))
|
||||||
|
assert.Equal(t, wantStr, gotStr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/fatih/semgroup"
|
"github.com/fatih/semgroup"
|
||||||
|
|
||||||
@@ -15,6 +16,8 @@ type ScanTarget struct {
|
|||||||
Symlink string
|
Symlink string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var isWindows = runtime.GOOS == "windows"
|
||||||
|
|
||||||
func DirectoryTargets(source string, s *semgroup.Group, followSymlinks bool, shouldSkip func(string) bool) (<-chan ScanTarget, error) {
|
func DirectoryTargets(source string, s *semgroup.Group, followSymlinks bool, shouldSkip func(string) bool) (<-chan ScanTarget, error) {
|
||||||
paths := make(chan ScanTarget)
|
paths := make(chan ScanTarget)
|
||||||
s.Go(func() error {
|
s.Go(func() error {
|
||||||
@@ -63,7 +66,10 @@ func DirectoryTargets(source string, s *semgroup.Group, followSymlinks bool, sho
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Also run this check against the resolved symlink?
|
// TODO: Also run this check against the resolved symlink?
|
||||||
skip := shouldSkip(path)
|
skip := shouldSkip(path) ||
|
||||||
|
// TODO: Remove this in v9.
|
||||||
|
// This is an awkward hack to mitigate https://github.com/gitleaks/gitleaks/issues/1641.
|
||||||
|
(isWindows && shouldSkip(filepath.ToSlash(path)))
|
||||||
if fInfo.IsDir() {
|
if fInfo.IsDir() {
|
||||||
// Directory
|
// Directory
|
||||||
if skip {
|
if skip {
|
||||||
|
|||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
foo\bar\gitleaks-false-positive.yaml:aws-access-token:4
|
||||||
|
b55d88dc151f7022901cda41a03d43e0e508f2b7:test_data\test_local_repo_three_leaks.json:aws-access-token:73
|
||||||
|
foo/bar/gitleaks-false-positive.yaml:aws-access-token:5
|
||||||
|
# comment
|
||||||
Reference in new issue
Block a user