detect.go 4.0 KB

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