Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFOR Commit Scanner #3145

Merged
merged 9 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ var (
githubScanPRComments = githubScan.Flag("pr-comments", "Include pull request descriptions and comments in scan.").Bool()
githubScanGistComments = githubScan.Flag("gist-comments", "Include gist comments in scan.").Bool()

// GitHub Cross Fork Object Reference Experimental Feature
githubExperimentalScan = cli.Command("github-experimental", "Run an experimental GitHub scan. Must specify at least one experimental sub-module to run: object-discovery.")
// GitHub Experimental SubModules
githubExperimentalObjectDiscovery = githubExperimentalScan.Flag("object-discovery", "Discover hidden data objects in GitHub repositories.").Bool()
// GitHub Experimental Options
githubExperimentalToken = githubExperimentalScan.Flag("token", "GitHub token. Can be provided with environment variable GITHUB_TOKEN.").Envar("GITHUB_TOKEN").String()
githubExperimentalRepo = githubExperimentalScan.Flag("repo", "GitHub repository to scan. Example: https://github.com/<user>/<repo>.git").Required().String()
githubExperimentalCollisionThreshold = githubExperimentalScan.Flag("collision-threshold", "Threshold for short-sha collisions in object-discovery submodule. Default is 1.").Default("1").Int()
githubExperimentalDeleteCache = githubExperimentalScan.Flag("delete-cached-data", "Delete cached data after object-discovery secret scanning.").Bool()

gitlabScan = cli.Command("gitlab", "Find credentials in GitLab repositories.")
// TODO: Add more GitLab options
gitlabScanEndpoint = gitlabScan.Flag("endpoint", "GitLab endpoint.").Default("https://gitlab.com").String()
Expand Down Expand Up @@ -667,6 +677,17 @@ func runSingleScan(ctx context.Context, cmd string, cfg engine.Config) (metrics,
if err := eng.ScanGitHub(ctx, cfg); err != nil {
return scanMetrics, fmt.Errorf("failed to scan Github: %v", err)
}
case githubExperimentalScan.FullCommand():
cfg := sources.GitHubExperimentalConfig{
Token: *githubExperimentalToken,
Repository: *githubExperimentalRepo,
ObjectDiscovery: *githubExperimentalObjectDiscovery,
CollisionThreshold: *githubExperimentalCollisionThreshold,
DeleteCachedData: *githubExperimentalDeleteCache,
}
if err := eng.ScanGitHubExperimental(ctx, cfg); err != nil {
return scanMetrics, fmt.Errorf("failed to scan using Github Experimental: %v", err)
}
case gitlabScan.FullCommand():
filter, err := common.FilterFromFiles(*gitlabScanIncludePaths, *gitlabScanExcludePaths)
if err != nil {
Expand Down
65 changes: 65 additions & 0 deletions pkg/engine/github_experimental.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package engine

import (
"fmt"
"runtime"

gogit "github.com/go-git/go-git/v5"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"

"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/github"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/github_experimental"
)

// ScanGitHubExperimental scans GitHub using an experimental feature. Consider all functionality to be in an alpha release here.
func (e *Engine) ScanGitHubExperimental(ctx context.Context, c sources.GitHubExperimentalConfig) error {
connection := sourcespb.GitHubExperimental{
Repository: c.Repository,
ObjectDiscovery: c.ObjectDiscovery,
CollisionThreshold: int64(c.CollisionThreshold),
DeleteCachedData: c.DeleteCachedData,
}

// Check at least one experimental sub-module is being used.
// Add to this list as more experimental sub-modules are added.
if !c.ObjectDiscovery {
return fmt.Errorf("at least one experimental submodule must be enabled")
}

if len(c.Token) > 0 {
connection.Credential = &sourcespb.GitHubExperimental_Token{
Token: c.Token,
}
} else {
return fmt.Errorf("token is required for github experimental")
}

var conn anypb.Any
err := anypb.MarshalFrom(&conn, &connection, proto.MarshalOptions{})
if err != nil {
ctx.Logger().Error(err, "failed to marshal github experimental connection")
return err
}

logOptions := &gogit.LogOptions{}
opts := []git.ScanOption{
git.ScanOptionLogOptions(logOptions),
}
scanOptions := git.NewScanOptions(opts...)

sourceName := "trufflehog - github experimental (alpha release)"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, github.SourceType)

githubExperimentalSource := &github_experimental.Source{}
if err := githubExperimentalSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
return err
}
githubExperimentalSource.WithScanOptions(scanOptions)
_, err = e.sourceManager.Run(ctx, sourceName, githubExperimentalSource)
return err
}
Loading
Loading