Add scanning from a pipe with --pipe (#1012)

This commit is contained in:
Zachary Rice authored and GitHub committed 2022-10-13 09:08:43 -05:00
1 parent 7cfebca510
commit 6ef704f28f
6 files changed
+75 -24

No files matched your search

+5
View File
@@ -745,3 +745,8 @@ testdata/repos/nogit/main.go:aws-access-token:20
bfbc39eb185a217ead81a7174026f1c55f963660:cmd/generate/config/rules/jwt.go:jwt:17 bfbc39eb185a217ead81a7174026f1c55f963660:cmd/generate/config/rules/jwt.go:jwt:17
bfbc39eb185a217ead81a7174026f1c55f963660:cmd/generate/config/rules/jwt.go:jwt:19 bfbc39eb185a217ead81a7174026f1c55f963660:cmd/generate/config/rules/jwt.go:jwt:19
acce01f2338434a78f6a4a06a097b0fd23280484:README.md:aws-access-token:220 acce01f2338434a78f6a4a06a097b0fd23280484:README.md:aws-access-token:220
99d620c719020fa24baa831299f0c964da79875e:detect/detect_test.go:aws-access-token:514
111c8b47b65627db117bd20be85ff0aee8961eb9:detect/detect_test.go:private-key:567
99d620c719020fa24baa831299f0c964da79875e:detect/detect_test.go:aws-access-token:512
99d620c719020fa24baa831299f0c964da79875e:detect/detect_test.go:aws-access-token:513
+20 -20
View File
@@ -138,29 +138,29 @@ Usage:
Available Commands: Available Commands:
completion generate the autocompletion script for the specified shell completion generate the autocompletion script for the specified shell
detect Detect secrets in code detect detect secrets in code
help Help about any command help Help about any command
protect Protect secrets in code protect protect secrets in code
version Display gitleaks version version display gitleaks version
Flags: Flags:
-c, --config string config file path -b, --baseline-path string path to baseline with issues that can be ignored
order of precedence: -c, --config string config file path
1. --config/-c order of precedence:
2. env var GITLEAKS_CONFIG 1. --config/-c
3. (--source/-s)/.gitleaks.toml 2. env var GITLEAKS_CONFIG
If none of the three options are used, then gitleaks will use the default config 3. (--source/-s)/.gitleaks.toml
--exit-code string exit code when leaks have been encountered (default: 1) If none of the three options are used, then gitleaks will use the default config
-h, --help help for gitleaks --exit-code int exit code when leaks have been encountered (default 1)
-l, --log-level string log level (debug, info, warn, error, fatal) (default "info") -h, --help help for gitleaks
--no-banner suppress banner -l, --log-level string log level (trace, debug, info, warn, error, fatal) (default "info")
--redact redact secrets from logs and stdout --max-target-megabytes int files larger than this will be skipped
-f, --report-format string output format (json, csv, sarif) --no-banner suppress banner
-r, --report-path string report file --redact redact secrets from logs and stdout
-b, --baseline-path path to a previously generated report with known issues that gitleaks should ignore -f, --report-format string output format (json, csv, sarif) (default "json")
-s, --source string path to source (git repo, directory, file) -r, --report-path string report file
-v, --verbose show verbose output from scan -s, --source string path to source (default: $PWD) (default ".")
--max-target-megabytes int files larger than this will be skipped -v, --verbose show verbose output from scan
Use "gitleaks [command] --help" for more information about a command. Use "gitleaks [command] --help" for more information about a command.
``` ```
+13 -2
View File
@@ -18,7 +18,8 @@ func init() {
rootCmd.AddCommand(detectCmd) rootCmd.AddCommand(detectCmd)
detectCmd.Flags().String("log-opts", "", "git log options") detectCmd.Flags().String("log-opts", "", "git log options")
detectCmd.Flags().Bool("no-git", false, "treat git repo as a regular directory and scan those files, --log-opts has no effect on the scan when --no-git is set") detectCmd.Flags().Bool("no-git", false, "treat git repo as a regular directory and scan those files, --log-opts has no effect on the scan when --no-git is set")
detectCmd.Flags().Bool("follow-symlinks", false, "Scan files that are symlinks to other files") detectCmd.Flags().Bool("pipe", false, "scan input from stdin, ex: `cat some_file | gitleaks detect --pipe`")
detectCmd.Flags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
} }
@@ -109,6 +110,10 @@ func runDetect(cmd *cobra.Command, args []string) {
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("could not call GetBool() for no-git") log.Fatal().Err(err).Msg("could not call GetBool() for no-git")
} }
fromPipe, err := cmd.Flags().GetBool("pipe")
if err != nil {
log.Fatal().Err(err)
}
// start the detector scan // start the detector scan
if noGit { if noGit {
@@ -117,7 +122,13 @@ func runDetect(cmd *cobra.Command, args []string) {
// don't exit on error, just log it // don't exit on error, just log it
log.Error().Err(err).Msg("") log.Error().Err(err).Msg("")
} }
} else if fromPipe {
findings, err = detector.DetectReader(os.Stdin, 10)
if err != nil {
// log fatal to exit, no need to continue since a report
// will not be generated when scanning from a pipe...for now
log.Fatal().Err(err).Msg("")
}
} else { } else {
var logOpts string var logOpts string
logOpts, err = cmd.Flags().GetString("log-opts") logOpts, err = cmd.Flags().GetString("log-opts")
+32 -1
View File
@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"context" "context"
"fmt" "fmt"
"io"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
@@ -11,13 +12,13 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/h2non/filetype"
"github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/config"
"github.com/zricethezav/gitleaks/v8/detect/git" "github.com/zricethezav/gitleaks/v8/detect/git"
"github.com/zricethezav/gitleaks/v8/report" "github.com/zricethezav/gitleaks/v8/report"
"github.com/fatih/semgroup" "github.com/fatih/semgroup"
"github.com/gitleaks/go-gitdiff/gitdiff" "github.com/gitleaks/go-gitdiff/gitdiff"
"github.com/h2non/filetype"
ahocorasick "github.com/petar-dambovaliev/aho-corasick" ahocorasick "github.com/petar-dambovaliev/aho-corasick"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
@@ -479,6 +480,36 @@ func (d *Detector) DetectFiles(source string) ([]report.Finding, error) {
return d.findings, nil return d.findings, nil
} }
// DetectReader accepts an io.Reader and a buffer size for the reader in KB
func (d *Detector) DetectReader(r io.Reader, bufSize int) ([]report.Finding, error) {
reader := bufio.NewReader(r)
buf := make([]byte, 0, 1000*bufSize)
findings := []report.Finding{}
for {
n, err := reader.Read(buf[:cap(buf)])
buf = buf[:n]
if err != nil {
if err != io.EOF {
return findings, err
}
break
}
fragment := Fragment{
Raw: string(buf),
}
for _, finding := range d.Detect(fragment) {
findings = append(findings, finding)
if d.Verbose {
printFinding(finding)
}
}
}
return findings, nil
}
// Detect scans the given fragment and returns a list of findings // Detect scans the given fragment and returns a list of findings
func (d *Detector) Detect(fragment Fragment) []report.Finding { func (d *Detector) Detect(fragment Fragment) []report.Finding {
var findings []report.Finding var findings []report.Finding
+1 -1
View File
@@ -470,7 +470,7 @@ func TestFromGit(t *testing.T) {
} }
} }
// TestFromGit tests the FromGit function // TestFromFiles tests the FromFiles function
func TestFromFiles(t *testing.T) { func TestFromFiles(t *testing.T) {
tests := []struct { tests := []struct {
cfgName string cfgName string
+4
View File
@@ -131,6 +131,10 @@ func printFinding(f report.Finding) {
fmt.Printf("%-12s %s\n", "Secret:", secret) fmt.Printf("%-12s %s\n", "Secret:", secret)
fmt.Printf("%-12s %s\n", "RuleID:", f.RuleID) fmt.Printf("%-12s %s\n", "RuleID:", f.RuleID)
fmt.Printf("%-12s %f\n", "Entropy:", f.Entropy) fmt.Printf("%-12s %f\n", "Entropy:", f.Entropy)
if f.File == "" {
fmt.Println("")
return
}
fmt.Printf("%-12s %s\n", "File:", f.File) fmt.Printf("%-12s %s\n", "File:", f.File)
fmt.Printf("%-12s %d\n", "Line:", f.StartLine) fmt.Printf("%-12s %d\n", "Line:", f.StartLine)
if f.Commit == "" { if f.Commit == "" {