Skip to content

Commit 7504a5e

Browse files
feat: Add git-commit-history flag for Secret Detection scans
1 parent 218a852 commit 7504a5e

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

internal/commands/scan.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ func scanCreateSubCommand(
864864
createScanCmd.PersistentFlags().String(commonParams.SCSRepoTokenFlag, "", "Provide a token with read permission for the repo that you are scanning (for scorecard scans)")
865865
createScanCmd.PersistentFlags().String(commonParams.SCSRepoURLFlag, "", "The URL of the repo that you are scanning with scs (for scorecard scans)")
866866
createScanCmd.PersistentFlags().String(commonParams.SCSEnginesFlag, "", "Specify which scs engines will run (default: all licensed engines)")
867+
createScanCmd.PersistentFlags().String(commonParams.GitCommitHistoryFlag, "false", commonParams.GitCommitHistoryFlagUsage)
867868
createScanCmd.PersistentFlags().Bool(commonParams.ScaHideDevAndTestDepFlag, false, scaHideDevAndTestDepFlagDescription)
868869

869870
// Container config flags
@@ -1403,6 +1404,11 @@ func addSCSScan(cmd *cobra.Command, resubmitConfig []wrappers.Config, scsLicensi
14031404

14041405
if scsSecretDetectionSelected && scsSecretDetectionAllowed {
14051406
scsConfig.Twoms = trueString
1407+
1408+
// Set git commit history if enabled and all validations pass
1409+
if shouldEnableGitCommitHistory(cmd) {
1410+
scsConfig.GitCommitHistory = trueString
1411+
}
14061412
}
14071413

14081414
isScsEnginesFlagSet := scsEngines != ""
@@ -3466,6 +3472,13 @@ func validateCreateScanFlags(cmd *cobra.Command) error {
34663472
}
34673473
}
34683474
}
3475+
3476+
// Validate git-commit-history flag
3477+
err = validateGitCommitHistoryFlag(cmd)
3478+
if err != nil {
3479+
return err
3480+
}
3481+
34693482
return nil
34703483
}
34713484

@@ -3729,6 +3742,78 @@ func validateBooleanString(value string) error {
37293742
return nil
37303743
}
37313744

3745+
// validateGitCommitHistoryFlag validates the git-commit-history flag (needed for Secret Detection)
3746+
func validateGitCommitHistoryFlag(cmd *cobra.Command) error {
3747+
gitCommitHistory, _ := cmd.Flags().GetString(commonParams.GitCommitHistoryFlag)
3748+
3749+
err := validateBooleanString(gitCommitHistory)
3750+
if err != nil {
3751+
return errors.Errorf("Invalid value for --git-commit-history. Use 'true' or 'false'.")
3752+
}
3753+
3754+
return nil
3755+
}
3756+
3757+
// shouldEnableGitCommitHistory checks if the git-commit-history flag should be enabled
3758+
func shouldEnableGitCommitHistory(cmd *cobra.Command) bool {
3759+
gitCommitHistory, _ := cmd.Flags().GetString(commonParams.GitCommitHistoryFlag)
3760+
3761+
// If flag is not set to true, return false
3762+
if strings.ToLower(strings.TrimSpace(gitCommitHistory)) != trueString {
3763+
return false
3764+
}
3765+
3766+
userScanTypes, _ := cmd.Flags().GetString(commonParams.ScanTypes)
3767+
if !strings.Contains(strings.ToLower(userScanTypes), commonParams.ScsType) {
3768+
fmt.Println("Warning: '--git-commit-history' was provided, but SCS is not selected. Ignoring this flag.")
3769+
return false
3770+
}
3771+
3772+
// Check if only scorecard is enabled (no secret detection)
3773+
scsEngines, _ := cmd.Flags().GetString(commonParams.SCSEnginesFlag)
3774+
scsScoreCardSelected, scsSecretDetectionSelected := getSCSEnginesSelected(scsEngines)
3775+
if scsScoreCardSelected && !scsSecretDetectionSelected {
3776+
fmt.Println("Warning: Commit History applies only to Secret Detection. The flag will be ignored.")
3777+
return false
3778+
}
3779+
3780+
// Check if there's a git repository context
3781+
source, _ := cmd.Flags().GetString(commonParams.SourcesFlag)
3782+
3783+
hasGitContext := false
3784+
3785+
// Check if source directory has .git folder (in root or subdirectories)
3786+
if source != "" && !hasGitContext {
3787+
sourceTrimmed := strings.TrimSpace(source)
3788+
info, statErr := os.Stat(sourceTrimmed)
3789+
if statErr == nil && info != nil && info.IsDir() {
3790+
gitPath := filepath.Join(sourceTrimmed, ".git")
3791+
if _, err := os.Stat(gitPath); err == nil {
3792+
hasGitContext = true
3793+
} else {
3794+
// If not found in root, search subdirectories
3795+
_ = filepath.Walk(sourceTrimmed, func(path string, info os.FileInfo, err error) error {
3796+
if err != nil || hasGitContext {
3797+
return nil
3798+
}
3799+
if info.IsDir() && info.Name() == ".git" {
3800+
hasGitContext = true
3801+
return filepath.SkipAll
3802+
}
3803+
return nil
3804+
})
3805+
}
3806+
}
3807+
}
3808+
3809+
if !hasGitContext {
3810+
fmt.Println("Warning: No Git history found. Secret Detection will scan the working tree only.")
3811+
return false
3812+
}
3813+
3814+
return true
3815+
}
3816+
37323817
func parseArgs(input string) []string {
37333818
var args []string
37343819
var current strings.Builder

internal/params/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ const (
237237
SCSRepoTokenFlag = "scs-repo-token"
238238
SCSRepoURLFlag = "scs-repo-url"
239239

240+
// Secret Detection Git Commit History
241+
GitCommitHistoryFlag = "git-commit-history"
242+
GitCommitHistoryFlagUsage = "Enable or disable commit history scan for Secret Detection (default: false)"
243+
240244
// Containers Config Flags
241245
ContainersFileFolderFilterFlag = "containers-file-folder-filter"
242246
ContainersImageTagFilterFlag = "containers-image-tag-filter"

internal/wrappers/scans.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ type APISecConfig struct {
157157
}
158158

159159
type SCSConfig struct {
160-
Twoms string `json:"2ms,omitempty"`
161-
Scorecard string `json:"scorecard,omitempty"`
162-
RepoURL string `json:"repoUrl,omitempty"`
163-
RepoToken string `json:"repoToken,omitempty"`
160+
Twoms string `json:"2ms,omitempty"`
161+
Scorecard string `json:"scorecard,omitempty"`
162+
RepoURL string `json:"repoUrl,omitempty"`
163+
RepoToken string `json:"repoToken,omitempty"`
164+
GitCommitHistory string `json:"gitCommitHistory,omitempty"`
164165
}

0 commit comments

Comments
 (0)