Skip to content

Find, verify, and analyze leaked credentials

fix(github): fallback resource Name to avoid empty-string proto validation failure (#5182)

Kashif Khana7082b69f5bc
diff --git a/pkg/analyzer/analyzers/github/github.go b/pkg/analyzer/analyzers/github/github.go
index 360123871..315384c05 100644
--- a/pkg/analyzer/analyzers/github/github.go
+++ b/pkg/analyzer/analyzers/github/github.go
@@ -120,8 +120,17 @@ func secretInfoToRepoBindings(info *common.SecretInfo) []analyzers.Binding {
if owner := repo.GetOwner(); owner != nil {
parent = userToResource(owner)
}
+ // repo.GetName() could theoretically be empty for a deleted/ghost repo
+ // object; fall back to FullName so Name is never empty.
+ name := repo.GetName()
+ if name == "" {
+ name = repo.GetFullName()
+ }
+ if name == "" {
+ continue
+ }
resource := analyzers.Resource{
- Name: repo.GetName(),
+ Name: name,
FullyQualifiedName: fmt.Sprintf("github.com/%s", repo.GetFullName()),
Type: "repository",
Parent: parent,
@@ -140,8 +149,14 @@ func secretInfoToGistBindings(info *common.SecretInfo) []analyzers.Binding {
if gist.GetOwner() == nil {
continue
}
+ // Gist descriptions are optional; fall back to the gist ID so Name is
+ // never empty (proto validation requires len >= 1).
+ name := gist.GetDescription()
+ if name == "" {
+ name = gist.GetID()
+ }
resource := analyzers.Resource{
- Name: gist.GetDescription(),
+ Name: name,
FullyQualifiedName: fmt.Sprintf("gist.github.com/%s/%s", gist.GetOwner().GetLogin(), gist.GetID()),
Type: "gist",
Parent: userToResource(gist.GetOwner()),
diff --git a/pkg/analyzer/analyzers/github/github_test.go b/pkg/analyzer/analyzers/github/github_test.go
index 6ac3d3b65..f086b4717 100644
--- a/pkg/analyzer/analyzers/github/github_test.go
+++ b/pkg/analyzer/analyzers/github/github_test.go
@@ -20,7 +20,9 @@ func makeOwner(login, userType string) *gh.User {
}
// TestSecretInfoToGistBindings exercises nil-field edge cases that caused a
-// production panic (nil Description and nil Owner from the GitHub API).
+// production panic (nil Description and nil Owner from the GitHub API), and
+// the empty-description proto-validation failure (CSM-1554) where Name must
+// be non-empty.
func TestSecretInfoToGistBindings(t *testing.T) {
owner := makeOwner("truffle-sandbox", "User")
scope := []analyzers.Permission{{Value: "gist"}}
@@ -33,13 +35,22 @@ func TestSecretInfoToGistBindings(t *testing.T) {
wantNames []string // parallel to wantFQNs; checked unconditionally (including empty string)
}{
{
- name: "nil description produces empty name, gist is still included",
+ name: "nil description falls back to gist ID as name",
gists: []*gh.Gist{
{ID: strPtr("abc123"), Description: nil, Owner: owner},
},
wantLen: 1,
wantFQNs: []string{"gist.github.com/truffle-sandbox/abc123"},
- wantNames: []string{""},
+ wantNames: []string{"abc123"},
+ },
+ {
+ name: "empty description falls back to gist ID as name",
+ gists: []*gh.Gist{
+ {ID: strPtr("abc123"), Description: strPtr(""), Owner: owner},
+ },
+ wantLen: 1,
+ wantFQNs: []string{"gist.github.com/truffle-sandbox/abc123"},
+ wantNames: []string{"abc123"},
},
{
name: "nil owner: gist is skipped entirely",
@@ -107,7 +118,8 @@ func TestSecretInfoToRepoBindings(t *testing.T) {
repos []*gh.Repository
wantLen int
wantFQNs []string
- wantParentNil []bool // parallel to wantFQNs
+ wantNames []string // parallel to wantFQNs; nil slice skips the check
+ wantParentNil []bool // parallel to wantFQNs
}{
{
name: "valid repo produces binding with populated parent",
@@ -116,8 +128,26 @@ func TestSecretInfoToRepoBindings(t *testing.T) {
},
wantLen: 1,
wantFQNs: []string{"github.com/truffle-sandbox/my-repo"},
+ wantNames: []string{"my-repo"},
wantParentNil: []bool{false},
},
+ {
+ name: "empty repo name falls back to full name (CSM-1554: Name must be non-empty)",
+ repos: []*gh.Repository{
+ {Name: strPtr(""), FullName: strPtr("truffle-sandbox/ghost-repo"), Owner: owner},
+ },
+ wantLen: 1,
+ wantFQNs: []string{"github.com/truffle-sandbox/ghost-repo"},
+ wantNames: []string{"truffle-sandbox/ghost-repo"},
+ wantParentNil: []bool{false},
+ },
+ {
+ name: "both name and full name empty: repo is skipped",
+ repos: []*gh.Repository{
+ {Name: strPtr(""), FullName: strPtr(""), Owner: owner},
+ },
+ wantLen: 0,
+ },
{
name: "nil owner: repo is still included with nil parent",
repos: []*gh.Repository{
@@ -169,6 +199,14 @@ func TestSecretInfoToRepoBindings(t *testing.T) {
t.Errorf("binding[%d].Parent nil = %v, want %v", i, gotNil, wantNil)
}
}
+ for i, name := range tt.wantNames {
+ if i >= len(got) {
+ break
+ }
+ if got[i].Resource.Name != name {
+ t.Errorf("binding[%d].Name = %q, want %q", i, got[i].Resource.Name, name)
+ }
+ }
})
}
}