detect.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // The `detect` and `protect` command is now deprecated. Here are some equivalent commands
  2. // to help guide you.
  3. // OLD CMD: gitleaks detect --source={repo}
  4. // NEW CMD: gitleaks git {repo}
  5. // OLD CMD: gitleaks protect --source={repo}
  6. // NEW CMD: gitleaks git --pre-commit {repo}
  7. // OLD CMD: gitleaks protect --staged --source={repo}
  8. // NEW CMD: gitleaks git --pre-commit --staged {repo}
  9. // OLD CMD: gitleaks detect --no-git --source={repo}
  10. // NEW CMD: gitleaks directory {directory/file}
  11. // OLD CMD: gitleaks detect --no-git --pipe
  12. // NEW CMD: gitleaks stdin
  13. package cmd
  14. import (
  15. "os"
  16. "time"
  17. "github.com/spf13/cobra"
  18. "github.com/zricethezav/gitleaks/v8/cmd/scm"
  19. "github.com/zricethezav/gitleaks/v8/logging"
  20. "github.com/zricethezav/gitleaks/v8/report"
  21. "github.com/zricethezav/gitleaks/v8/sources"
  22. )
  23. func init() {
  24. rootCmd.AddCommand(detectCmd)
  25. 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")
  26. detectCmd.Flags().Bool("pipe", false, "scan input from stdin, ex: `cat some_file | gitleaks detect --pipe`")
  27. detectCmd.Flags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
  28. detectCmd.Flags().StringP("source", "s", ".", "path to source")
  29. detectCmd.Flags().String("log-opts", "", "git log options")
  30. detectCmd.Flags().String("platform", "", "the target platform used to generate links (github, gitlab)")
  31. }
  32. var detectCmd = &cobra.Command{
  33. Use: "detect",
  34. Short: "detect secrets in code",
  35. Run: runDetect,
  36. Hidden: true,
  37. }
  38. func runDetect(cmd *cobra.Command, args []string) {
  39. // start timer
  40. start := time.Now()
  41. sourcePath := mustGetStringFlag(cmd, "source")
  42. // setup config (aka, the thing that defines rules)
  43. initConfig(sourcePath)
  44. initDiagnostics()
  45. cfg := Config(cmd)
  46. // create detector
  47. detector := Detector(cmd, cfg, sourcePath)
  48. // parse flags
  49. detector.FollowSymlinks = mustGetBoolFlag(cmd, "follow-symlinks")
  50. exitCode := mustGetIntFlag(cmd, "exit-code")
  51. noGit := mustGetBoolFlag(cmd, "no-git")
  52. fromPipe := mustGetBoolFlag(cmd, "pipe")
  53. // determine what type of scan:
  54. // - git: scan the history of the repo
  55. // - no-git: scan files by treating the repo as a plain directory
  56. var (
  57. err error
  58. findings []report.Finding
  59. )
  60. if noGit {
  61. findings, err = detector.DetectSource(
  62. cmd.Context(), &sources.Files{
  63. Config: &cfg,
  64. FollowSymlinks: detector.FollowSymlinks,
  65. MaxFileSize: detector.MaxTargetMegaBytes * 1_000_000,
  66. Path: sourcePath,
  67. Sema: detector.Sema,
  68. MaxArchiveDepth: detector.MaxArchiveDepth,
  69. },
  70. )
  71. if err != nil {
  72. // don't exit on error, just log it
  73. logging.Error().Err(err).Msg("failed to scan directory")
  74. }
  75. } else if fromPipe {
  76. findings, err = detector.DetectSource(
  77. cmd.Context(), &sources.File{
  78. Content: os.Stdin,
  79. MaxArchiveDepth: detector.MaxArchiveDepth,
  80. },
  81. )
  82. if err != nil {
  83. // log fatal to exit, no need to continue since a report
  84. // will not be generated when scanning from a pipe...for now
  85. logging.Fatal().Err(err).Msg("failed scan input from stdin")
  86. }
  87. } else {
  88. var (
  89. gitCmd *sources.GitCmd
  90. scmPlatform scm.Platform
  91. )
  92. logOpts := mustGetStringFlag(cmd, "log-opts")
  93. if gitCmd, err = sources.NewGitLogCmdContext(cmd.Context(), sourcePath, logOpts); err != nil {
  94. logging.Fatal().Err(err).Msg("could not create Git cmd")
  95. }
  96. if scmPlatform, err = scm.PlatformFromString(mustGetStringFlag(cmd, "platform")); err != nil {
  97. logging.Fatal().Err(err).Send()
  98. }
  99. findings, err = detector.DetectSource(
  100. cmd.Context(), &sources.Git{
  101. Cmd: gitCmd,
  102. Config: &detector.Config,
  103. Remote: sources.NewRemoteInfoContext(cmd.Context(), scmPlatform, sourcePath),
  104. Sema: detector.Sema,
  105. MaxArchiveDepth: detector.MaxArchiveDepth,
  106. },
  107. )
  108. if err != nil {
  109. // don't exit on error, just log it
  110. logging.Error().Err(err).Msg("failed to scan Git repository")
  111. }
  112. }
  113. findingSummaryAndExit(detector, findings, exitCode, start, err)
  114. }