Skip to content

Find, verify, and analyze leaked credentials

explicit repositories now bypass wantRepo() filtering entirely. added ctx to newConnector (#4507)

Jordan Tunstill0f60f6ecfe34
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 38e8803d1..c6d954d1a 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -433,33 +433,42 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e
case *unauthenticatedConnector:
s.enumerateUnauthenticated(ctx, dedupeReporter)
}
- s.repos = make([]string, 0, s.filteredRepoCache.Count())
-
- // Double make sure that all enumerated repositories in the
- // filteredRepoCache have an entry in the repoInfoCache.
- for _, repo := range s.filteredRepoCache.Values() {
- // Extract the repository name from the URL for filtering
- repoName := repo
- if strings.Contains(repo, "/") {
- // Try to extract org/repo name from URL
- if strings.Contains(repo, "github.com") {
- parts := strings.Split(repo, "/")
- if len(parts) >= 2 {
- repoName = parts[len(parts)-2] + "/" + strings.TrimSuffix(parts[len(parts)-1], ".git")
+ // If explicit repositories were provided, use them directly without filtering
+ // Otherwise, rebuild s.repos from the filteredRepoCache
+ if len(s.conn.Repositories) > 0 {
+ // Explicit repositories bypass filtering - use them as-is
+ s.repos = s.conn.Repositories
+ ctx.Logger().V(1).Info("Using explicit repositories", "count", len(s.repos))
+ } else {
+ // No explicit repositories - rebuild from enumerated cache with filtering
+ s.repos = make([]string, 0, s.filteredRepoCache.Count())
+
+ // Double make sure that all enumerated repositories in the
+ // filteredRepoCache have an entry in the repoInfoCache.
+ for _, repo := range s.filteredRepoCache.Values() {
+ // Extract the repository name from the URL for filtering
+ repoName := repo
+ if strings.Contains(repo, "/") {
+ // Try to extract org/repo name from URL
+ if strings.Contains(repo, "github.com") {
+ parts := strings.Split(repo, "/")
+ if len(parts) >= 2 {
+ repoName = parts[len(parts)-2] + "/" + strings.TrimSuffix(parts[len(parts)-1], ".git")
+ }
}
}
- }
- // Final filter check - only include repositories that pass the filter
- if s.filteredRepoCache.wantRepo(repoName) {
- ctx = context.WithValue(ctx, "repo", repo)
+ // Final filter check - only include repositories that pass the filter
+ if s.filteredRepoCache.wantRepo(repoName) {
+ ctx = context.WithValue(ctx, "repo", repo)
- repo, err := s.ensureRepoInfoCache(ctx, repo, &unitErrorReporter{reporter})
- if err != nil {
- ctx.Logger().Error(err, "error caching repo info")
- _ = dedupeReporter.UnitErr(ctx, fmt.Errorf("error caching repo info: %w", err))
+ repo, err := s.ensureRepoInfoCache(ctx, repo, &unitErrorReporter{reporter})
+ if err != nil {
+ ctx.Logger().Error(err, "error caching repo info")
+ _ = dedupeReporter.UnitErr(ctx, fmt.Errorf("error caching repo info: %w", err))
+ }
+ s.repos = append(s.repos, repo)
}
- s.repos = append(s.repos, repo)
}
}
githubReposEnumerated.WithLabelValues(s.name).Set(float64(len(s.repos)))
diff --git a/pkg/sources/github/github_integration_test.go b/pkg/sources/github/github_integration_test.go
index f77524f72..42af8f4e2 100644
--- a/pkg/sources/github/github_integration_test.go
+++ b/pkg/sources/github/github_integration_test.go
@@ -957,7 +957,7 @@ func TestSource_Validate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- connector, err := newConnector(tt.sourceConfig)
+ connector, err := newConnector(ctx, tt.sourceConfig)
require.NoError(t, err)
tt.sourceConfig.connector = connector
diff --git a/pkg/sources/github/github_test.go b/pkg/sources/github/github_test.go
index c063d04d6..47cebaf9e 100644
--- a/pkg/sources/github/github_test.go
+++ b/pkg/sources/github/github_test.go
@@ -20,6 +20,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-github/v67/github"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/anypb"
"gopkg.in/h2non/gock.v1"
@@ -1046,6 +1047,55 @@ func TestRepositoryFiltering(t *testing.T) {
assert.False(t, cache5.wantRepo("other/repo1"))
}
+func TestExplicitRepositoryBypass(t *testing.T) {
+ // Test that explicit repositories are included in enumeration
+ ctx := context.Background()
+
+ // Set up mocks for the API calls
+ gock.New("https://api.github.com").
+ Get("/user").
+ Reply(200).
+ JSON(map[string]string{"login": "test-user"})
+
+ gock.New("https://api.github.com").
+ Get("/repos/org/explicit-repo").
+ Reply(200).
+ JSON(map[string]any{
+ "full_name": "org/explicit-repo",
+ "clone_url": "https://github.com/org/explicit-repo.git",
+ "size": 1,
+ })
+
+ gock.New("https://api.github.com").
+ Get("/repos/org/another-explicit").
+ Reply(200).
+ JSON(map[string]any{
+ "full_name": "org/another-explicit",
+ "clone_url": "https://github.com/org/another-explicit.git",
+ "size": 1,
+ })
+
+ // Create a source with explicit repositories
+ source := initTestSource(&sourcespb.GitHub{
+ Credential: &sourcespb.GitHub_Token{
+ Token: "super secret token",
+ },
+ Repositories: []string{
+ "https://github.com/org/explicit-repo.git",
+ "https://github.com/org/another-explicit.git",
+ },
+ })
+
+ // Test the Enumerate method
+ err := source.Enumerate(ctx, noopReporter())
+ require.NoError(t, err)
+
+ // Verify that explicit repositories are included in the enumeration
+ assert.Len(t, source.repos, 2, "Should have 2 explicit repositories")
+ assert.Contains(t, source.repos, "https://github.com/org/explicit-repo.git")
+ assert.Contains(t, source.repos, "https://github.com/org/another-explicit.git")
+}
+
func noopReporter() sources.UnitReporter {
return sources.VisitorReporter{
VisitUnit: func(context.Context, sources.SourceUnit) error {