Compare commits

...
3 Commits
Author SHA1 Message Date
David Mateo 906085fba6 Update golang version to 1.23 (#1672)
Because go mod 1.23 has been updated, the golang image needs it too
2024-12-20 08:38:11 -06:00
Zach d0ba52e2be bump go in dockerfile 2024-12-20 05:54:52 -06:00
Zachary Rice 8a8306288e log bytes (#1670) 2024-12-20 05:46:25 -06:00
4 changed files with 53 additions and 3 deletions

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
FROM golang:1.22 AS build FROM golang:1.23 AS build
WORKDIR /go/src/github.com/zricethezav/gitleaks WORKDIR /go/src/github.com/zricethezav/gitleaks
COPY . . COPY . .
RUN VERSION=$(git describe --tags --abbrev=0) && \ RUN VERSION=$(git describe --tags --abbrev=0) && \
+40 -1
View File
@@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync/atomic"
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@@ -40,6 +41,13 @@ var rootCmd = &cobra.Command{
Version: Version, Version: Version,
} }
const (
BYTE = 1.0
KILOBYTE = BYTE * 1000
MEGABYTE = KILOBYTE * 1000
GIGABYTE = MEGABYTE * 1000
)
func init() { func init() {
cobra.OnInitialize(initLog) cobra.OnInitialize(initLog)
rootCmd.PersistentFlags().StringP("config", "c", "", configDescription) rootCmd.PersistentFlags().StringP("config", "c", "", configDescription)
@@ -59,6 +67,7 @@ func init() {
rootCmd.PersistentFlags().StringSlice("enable-rule", []string{}, "only enable specific rules by id") rootCmd.PersistentFlags().StringSlice("enable-rule", []string{}, "only enable specific rules by id")
rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one") rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
rootCmd.PersistentFlags().Int("max-decode-depth", 0, "allow recursive decoding up to this depth (default \"0\", no decoding is done)") rootCmd.PersistentFlags().Int("max-decode-depth", 0, "allow recursive decoding up to this depth (default \"0\", no decoding is done)")
err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
if err != nil { if err != nil {
log.Fatal().Msgf("err binding config %s", err.Error()) log.Fatal().Msgf("err binding config %s", err.Error())
@@ -321,15 +330,45 @@ func mustGetStringFlag(name string) string {
return reportPath return reportPath
} }
func bytesConvert(bytes uint64) string {
unit := ""
value := float32(bytes)
switch {
case bytes >= GIGABYTE:
unit = "GB"
value = value / GIGABYTE
case bytes >= MEGABYTE:
unit = "MB"
value = value / MEGABYTE
case bytes >= KILOBYTE:
unit = "KB"
value = value / KILOBYTE
case bytes >= BYTE:
unit = "bytes"
case bytes == 0:
return "0"
}
stringValue := strings.TrimSuffix(
fmt.Sprintf("%.2f", value), ".00",
)
return fmt.Sprintf("%s %s", stringValue, unit)
}
func findingSummaryAndExit(detector *detect.Detector, findings []report.Finding, exitCode int, start time.Time, err error) { func findingSummaryAndExit(detector *detect.Detector, findings []report.Finding, exitCode int, start time.Time, err error) {
totalBytes := atomic.LoadUint64(&detector.TotalBytes)
bytesMsg := fmt.Sprintf("scanned ~%d bytes (%s)", totalBytes, bytesConvert(totalBytes))
if err == nil { if err == nil {
log.Info().Msgf("scan completed in %s", FormatDuration(time.Since(start))) log.Info().Msgf("%s in %s", bytesMsg, FormatDuration(time.Since(start)))
if len(findings) != 0 { if len(findings) != 0 {
log.Warn().Msgf("leaks found: %d", len(findings)) log.Warn().Msgf("leaks found: %d", len(findings))
} else { } else {
log.Info().Msg("no leaks found") log.Info().Msg("no leaks found")
} }
} else { } else {
log.Warn().Msg(bytesMsg)
log.Warn().Msgf("partial scan completed in %s", FormatDuration(time.Since(start))) log.Warn().Msgf("partial scan completed in %s", FormatDuration(time.Since(start)))
if len(findings) != 0 { if len(findings) != 0 {
log.Warn().Msgf("%d leaks found in partial scan", len(findings)) log.Warn().Msgf("%d leaks found in partial scan", len(findings))
+10
View File
@@ -8,6 +8,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"github.com/zricethezav/gitleaks/v8/config" "github.com/zricethezav/gitleaks/v8/config"
"github.com/zricethezav/gitleaks/v8/report" "github.com/zricethezav/gitleaks/v8/report"
@@ -87,6 +88,8 @@ type Detector struct {
// report-related settings. // report-related settings.
ReportPath string ReportPath string
Reporter report.Reporter Reporter report.Reporter
TotalBytes uint64
} }
// Fragment contains the data to be scanned // Fragment contains the data to be scanned
@@ -94,6 +97,8 @@ type Fragment struct {
// Raw is the raw content of the fragment // Raw is the raw content of the fragment
Raw string Raw string
Bytes []byte
// FilePath is the path to the file if applicable // FilePath is the path to the file if applicable
FilePath string FilePath string
SymlinkFile string SymlinkFile string
@@ -178,6 +183,11 @@ func (d *Detector) DetectString(content string) []report.Finding {
// 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 {
if fragment.Bytes == nil {
atomic.AddUint64(&d.TotalBytes, uint64(len(fragment.Raw)))
}
atomic.AddUint64(&d.TotalBytes, uint64(len(fragment.Bytes)))
var findings []report.Finding var findings []report.Finding
// check if filepath is allowed // check if filepath is allowed
+2 -1
View File
@@ -110,11 +110,12 @@ func (d *Detector) DetectFiles(paths <-chan sources.ScanTarget) ([]report.Findin
} }
// Count the number of newlines in this chunk // Count the number of newlines in this chunk
chunk := string(peekBuf.Bytes()) chunk := peekBuf.String()
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(),
FilePath: pa.Path, FilePath: pa.Path,
} }
if pa.Symlink != "" { if pa.Symlink != "" {