Skip to content

Find, verify, and analyze leaked credentials

Added support to scan github commit metadata for targeted scans (#4189)

Kashif Khan957ece98f3a8
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 793ef5fba..6a2ae00c2 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -3,6 +3,7 @@ package github
import (
"errors"
"fmt"
+ "io"
"math/rand/v2"
"net/http"
"net/url"
@@ -1531,6 +1532,18 @@ func (s *Source) scanTarget(ctx context.Context, target sources.ChunkingTarget,
}
meta := metaType.Github
+ chunkSkel := sources.Chunk{
+ SourceType: s.Type(),
+ SourceName: s.name,
+ SourceID: s.SourceID(),
+ JobID: s.JobID(),
+ SecretID: target.SecretID,
+ SourceMetadata: &source_metadatapb.MetaData{
+ Data: &source_metadatapb.MetaData_Github{Github: meta},
+ },
+ Verify: s.verify,
+ }
+
u, err := url.Parse(meta.GetLink())
if err != nil {
return fmt.Errorf("unable to parse GitHub URL: %w", err)
@@ -1543,6 +1556,14 @@ func (s *Source) scanTarget(ctx context.Context, target sources.ChunkingTarget,
return fmt.Errorf("invalid GitHub URL")
}
+ if meta.GetFile() == "" && meta.GetCommit() != "" {
+ ctx := context.WithValues(ctx, "commit_hash", meta.GetCommit())
+ ctx.Logger().V(2).Info("secret metadata has no file; scanning commit metadata instead")
+
+ return s.scanCommitMetadata(ctx, segments[1], segments[2], meta, &chunkSkel, reporter)
+ }
+
+ // else try downloading the file content to scan
readCloser, resp, err := s.connector.APIClient().Repositories.DownloadContents(
ctx,
segments[1],
@@ -1561,21 +1582,38 @@ func (s *Source) scanTarget(ctx context.Context, target sources.ChunkingTarget,
return fmt.Errorf("unexpected HTTP response status when trying to download file for scan: %v", resp.Status)
}
- chunkSkel := sources.Chunk{
- SourceType: s.Type(),
- SourceName: s.name,
- SourceID: s.SourceID(),
- JobID: s.JobID(),
- SecretID: target.SecretID,
- SourceMetadata: &source_metadatapb.MetaData{
- Data: &source_metadatapb.MetaData_Github{Github: meta},
- },
- Verify: s.verify,
- }
fileCtx := context.WithValues(ctx, "path", meta.GetFile())
return handlers.HandleFile(fileCtx, readCloser, &chunkSkel, reporter)
}
+func (s *Source) scanCommitMetadata(ctx context.Context, owner, repo string, meta *source_metadatapb.Github, chunkSkel *sources.Chunk, reporter sources.ChunkReporter) error {
+ // fetch the commit
+ commit, resp, err := s.connector.APIClient().Repositories.GetCommit(ctx, owner, repo, meta.GetCommit(), nil)
+ if resp != nil && resp.Body != nil {
+ defer resp.Body.Close()
+ }
+ if err != nil {
+ return fmt.Errorf("could not fetch commit for metadata scan: %w", err)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ return fmt.Errorf("unexpected HTTP response status when fetching commit: %v", resp.Status)
+ }
+
+ // create the string with the exact format we use in Git.ScanCommits()
+ // author email + "\n" + committer + "\n" + commit message
+ var sb strings.Builder
+
+ sb.WriteString(commit.GetCommit().Author.GetEmail())
+ sb.WriteString("\n")
+ sb.WriteString(commit.GetCommitter().GetEmail())
+ sb.WriteString("\n")
+ sb.WriteString(commit.GetCommit().GetMessage())
+
+ content := strings.NewReader(sb.String())
+ return handlers.HandleFile(ctx, io.NopCloser(content), chunkSkel, reporter)
+}
+
func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporter sources.ChunkReporter) error {
repoURL, _ := unit.SourceUnitID()
ctx = context.WithValue(ctx, "repo", repoURL)
diff --git a/pkg/sources/github/github_integration_test.go b/pkg/sources/github/github_integration_test.go
index c58150d05..f77524f72 100644
--- a/pkg/sources/github/github_integration_test.go
+++ b/pkg/sources/github/github_integration_test.go
@@ -775,6 +775,24 @@ func TestSource_Chunks_TargetedScan(t *testing.T) {
},
wantChunks: 607,
},
+ {
+ name: "targeted scan, commit metadata",
+ init: init{
+ name: "test source",
+ connection: &sourcespb.GitHub{Credential: &sourcespb.GitHub_Token{Token: githubToken}},
+ queryCriteria: &source_metadatapb.MetaData{
+ Data: &source_metadatapb.MetaData_Github{
+ Github: &source_metadatapb.Github{
+ Repository: "https://github.com/trufflesecurity/trufflehog.git",
+ Link: "https://github.com/trufflesecurity/trufflehog/commit/1c51106e35c3b3c327fe12e358177c03079bb771",
+ Commit: "1c51106e35c3b3c327fe12e358177c03079bb771",
+ File: "", // no file
+ },
+ },
+ },
+ },
+ wantChunks: 1,
+ },
{
name: "no file in commit",
init: init{