Respect contexts with timeouts (#1948)

rh-pre-commit.version: 2.3.2
rh-pre-commit.check-secrets: ENABLED
This commit is contained in:
bplaxco authored and GitHub committed 2025-10-24 12:30:13 -05:00
1 parent 3821f30b06
commit c5ccbb9468
13 files changed
+350 -245

No files matched your search

+14 -1
View File
@@ -2,6 +2,7 @@ package cmd
import (
"bytes"
"context"
"fmt"
"io"
"os"
@@ -44,6 +45,17 @@ var (
Use: "gitleaks",
Short: "Gitleaks scans code, past or present, for secrets",
Version: version.Version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Set the timeout for all the commands
if timeout, err := cmd.Flags().GetInt("timeout"); err != nil {
return err
} else if timeout > 0 {
ctx, cancel := context.WithTimeout(cmd.Context(), time.Duration(timeout)*time.Second)
cmd.SetContext(ctx)
cobra.OnFinalize(cancel)
}
return nil
},
}
// diagnostics manager is global to ensure it can be started before a scan begins
@@ -78,6 +90,7 @@ func init() {
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-archive-depth", 0, "allow scanning into nested archives up to this depth (default \"0\", no archive traversal is done)")
rootCmd.PersistentFlags().Int("timeout", 0, "set a timeout for gitleaks commands in seconds (default \"0\", no timeout is set)")
// Add diagnostics flags
rootCmd.PersistentFlags().String("diagnostics", "", "enable diagnostics (http OR comma-separated list: cpu,mem,trace). cpu=CPU prof, mem=memory prof, trace=exec tracing, http=serve via net/http/pprof")
@@ -237,7 +250,7 @@ func Detector(cmd *cobra.Command, cfg config.Config, source string) *detect.Dete
var err error
// Setup common detector
detector := detect.NewDetector(cfg)
detector := detect.NewDetectorContext(cmd.Context(), cfg)
if detector.MaxDecodeDepth, err = cmd.Flags().GetInt("max-decode-depth"); err != nil {
logging.Fatal().Err(err).Send()