Skip to content

Find, verify, and analyze leaked credentials

Fix GitHub App cross-org member enumeration using per-installation tokens (#4774)

Dustin Decker092db2aa9d83
diff --git a/pkg/sources/github/connector_app.go b/pkg/sources/github/connector_app.go
index cbe623e4a..23089b63d 100644
--- a/pkg/sources/github/connector_app.go
+++ b/pkg/sources/github/connector_app.go
@@ -20,6 +20,8 @@ type appConnector struct {
graphqlClient *githubv4.Client
installationClient *github.Client
installationID int64
+ appsTransport *ghinstallation.AppsTransport
+ apiEndpoint string
}
var _ Connector = (*appConnector)(nil)
@@ -54,33 +56,26 @@ func NewAppConnector(ctx context.Context, apiEndpoint string, app *credentialspb
return nil, fmt.Errorf("could not create installation client: %w", err)
}
- apiTransport, err := ghinstallation.New(
- httpClient.Transport,
- appID,
- installationID,
- []byte(app.PrivateKey))
- if err != nil {
- return nil, fmt.Errorf("could not create API client transport: %w", err)
+ connector := &appConnector{
+ installationClient: installationClient,
+ installationID: installationID,
+ appsTransport: installationTransport,
+ apiEndpoint: apiEndpoint,
}
- apiTransport.BaseURL = apiEndpoint
- httpClient.Transport = apiTransport
- apiClient, err := github.NewClient(httpClient).WithEnterpriseURLs(apiEndpoint, apiEndpoint)
+ apiClient, err := connector.APIClientForInstallation(installationID)
if err != nil {
- return nil, fmt.Errorf("could not create API client: %w", err)
+ return nil, fmt.Errorf("could not create API client for configured installation: %w", err)
}
+ connector.apiClient = apiClient
- graphqlClient, err := createGraphqlClient(ctx, httpClient, apiEndpoint)
+ graphqlClient, err := createGraphqlClient(ctx, apiClient.Client(), apiEndpoint)
if err != nil {
return nil, fmt.Errorf("error creating GraphQL client: %w", err)
}
+ connector.graphqlClient = graphqlClient
- return &appConnector{
- apiClient: apiClient,
- graphqlClient: graphqlClient,
- installationClient: installationClient,
- installationID: installationID,
- }, nil
+ return connector, nil
}
func (c *appConnector) APIClient() *github.Client {
@@ -107,3 +102,21 @@ func (c *appConnector) GraphQLClient() *githubv4.Client {
func (c *appConnector) InstallationClient() *github.Client {
return c.installationClient
}
+
+// APIClientForInstallation creates a GitHub API client scoped to a specific
+// installation. This is needed when a GitHub App is installed across multiple
+// orgs — each org's API calls must use that org's installation token to get
+// proper IP allowlist bypass and permission scoping.
+func (c *appConnector) APIClientForInstallation(installationID int64) (*github.Client, error) {
+ transport := ghinstallation.NewFromAppsTransport(c.appsTransport, installationID)
+ transport.BaseURL = c.apiEndpoint
+
+ httpClient := common.RetryableHTTPClientTimeout(60)
+ httpClient.Transport = transport
+
+ client, err := github.NewClient(httpClient).WithEnterpriseURLs(c.apiEndpoint, c.apiEndpoint)
+ if err != nil {
+ return nil, fmt.Errorf("could not create API client for installation %d: %w", installationID, err)
+ }
+ return client, nil
+}
diff --git a/pkg/sources/github/connector_app_test.go b/pkg/sources/github/connector_app_test.go
new file mode 100644
index 000000000..ca6280db0
--- /dev/null
+++ b/pkg/sources/github/connector_app_test.go
@@ -0,0 +1,193 @@
+package github
+
+import (
+ "crypto/rand"
+ "crypto/rsa"
+ "crypto/x509"
+ "encoding/json"
+ "encoding/pem"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync"
+ "testing"
+
+ "github.com/bradleyfalzon/ghinstallation/v2"
+ "github.com/google/go-github/v67/github"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ trContext "github.com/trufflesecurity/trufflehog/v3/pkg/context"
+ "github.com/trufflesecurity/trufflehog/v3/pkg/pb/credentialspb"
+)
+
+func generateTestPrivateKey(t *testing.T) []byte {
+ t.Helper()
+ key, err := rsa.GenerateKey(rand.Reader, 2048)
+ require.NoError(t, err)
+ return pem.EncodeToMemory(&pem.Block{
+ Type: "RSA PRIVATE KEY",
+ Bytes: x509.MarshalPKCS1PrivateKey(key),
+ })
+}
+
+func TestAPIClientForInstallation(t *testing.T) {
+ privKey := generateTestPrivateKey(t)
+
+ var mu sync.Mutex
+ var tokenRequestPaths []string
+
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+
+ if r.Method == "POST" && strings.Contains(r.URL.Path, "access_tokens") {
+ mu.Lock()
+ tokenRequestPaths = append(tokenRequestPaths, r.URL.Path)
+ mu.Unlock()
+ _ = json.NewEncoder(w).Encode(map[string]interface{}{
+ "token": "test-token",
+ "expires_at": "2099-01-01T00:00:00Z",
+ })
+ return
+ }
+
+ // Default: return empty JSON array for any list endpoint.
+ _ = json.NewEncoder(w).Encode([]interface{}{})
+ }))
+ defer server.Close()
+
+ appsTransport, err := ghinstallation.NewAppsTransport(
+ http.DefaultTransport,
+ 12345,
+ privKey,
+ )
+ require.NoError(t, err)
+ appsTransport.BaseURL = server.URL
+
+ connector := &appConnector{
+ appsTransport: appsTransport,
+ apiEndpoint: server.URL,
+ }
+
+ t.Run("creates distinct clients for different installations", func(t *testing.T) {
+ client1, err := connector.APIClientForInstallation(111)
+ require.NoError(t, err)
+ assert.NotNil(t, client1)
+
+ client2, err := connector.APIClientForInstallation(222)
+ require.NoError(t, err)
+ assert.NotNil(t, client2)
+
+ assert.NotSame(t, client1, client2, "should be different client instances")
+ })
+
+ t.Run("returned client uses the correct installation ID", func(t *testing.T) {
+ mu.Lock()
+ tokenRequestPaths = nil
+ mu.Unlock()
+
+ client, err := connector.APIClientForInstallation(42)
+ require.NoError(t, err)
+
+ ctx := trContext.Background()
+ _, _, _ = client.Organizations.ListMembers(ctx, "test-org", nil)
+
+ mu.Lock()
+ defer mu.Unlock()
+ require.Len(t, tokenRequestPaths, 1)
+ assert.Contains(t, tokenRequestPaths[0], "/app/installations/42/access_tokens")
+ })
+
+ t.Run("different installations use different tokens", func(t *testing.T) {
+ mu.Lock()
+ tokenRequestPaths = nil
+ mu.Unlock()
+
+ ctx := trContext.Background()
+
+ client1, err := connector.APIClientForInstallation(100)
+ require.NoError(t, err)
+ _, _, _ = client1.Organizations.ListMembers(ctx, "org-a", nil)
+
+ client2, err := connector.APIClientForInstallation(200)
+ require.NoError(t, err)
+ _, _, _ = client2.Organizations.ListMembers(ctx, "org-b", nil)
+
+ mu.Lock()
+ defer mu.Unlock()
+ require.Len(t, tokenRequestPaths, 2)
+ assert.Contains(t, tokenRequestPaths[0], "/installations/100/")
+ assert.Contains(t, tokenRequestPaths[1], "/installations/200/")
+ })
+}
+
+func TestNewAppConnectorDefaultAPIClientUsesConfiguredInstallation(t *testing.T) {
+ privKey := generateTestPrivateKey(t)
+
+ var mu sync.Mutex
+ var tokenRequestPaths []string
+
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+
+ if r.Method == "POST" && strings.Contains(r.URL.Path, "access_tokens") {
+ mu.Lock()
+ tokenRequestPaths = append(tokenRequestPaths, r.URL.Path)
+ mu.Unlock()
+ _ = json.NewEncoder(w).Encode(map[string]interface{}{
+ "token": "configured-installation-token",
+ "expires_at": "2099-01-01T00:00:00Z",
+ })
+ return
+ }
+
+ _ = json.NewEncoder(w).Encode([]map[string]string{{"login": "alice"}})
+ }))
+ defer server.Close()
+
+ connector, err := NewAppConnector(trContext.Background(), server.URL, &credentialspb.GitHubApp{
+ PrivateKey: string(privKey),
+ InstallationId: "4242",
+ AppId: "12345",
+ })
+ require.NoError(t, err)
+
+ _, _, err = connector.APIClient().Organizations.ListMembers(trContext.Background(), "test-org", nil)
+ require.NoError(t, err)
+
+ mu.Lock()
+ defer mu.Unlock()
+ require.Len(t, tokenRequestPaths, 1)
+ assert.Contains(t, tokenRequestPaths[0], "/app/installations/4242/access_tokens")
+}
+
+func TestAddMembersByOrgWithClient(t *testing.T) {
+ strPtr := func(s string) *string { return &s }
+ memberPage := []*github.User{
+ {Login: strPtr("alice")},
+ {Login: strPtr("bob")},
+ {Login: strPtr("charlie")},
+ }
+
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+ _ = json.NewEncoder(w).Encode(memberPage)
+ }))
+ defer server.Close()
+
+ client, err := github.NewClient(nil).WithEnterpriseURLs(server.URL, server.URL)
+ require.NoError(t, err)
+
+ s := &Source{
+ memberCache: make(map[string]struct{}),
+ }
+
+ ctx := trContext.Background()
+ err = s.addMembersByOrgWithClient(ctx, client, "test-org", nil)
+ require.NoError(t, err)
+
+ assert.Len(t, s.memberCache, 3)
+ assert.Contains(t, s.memberCache, "alice")
+ assert.Contains(t, s.memberCache, "bob")
+ assert.Contains(t, s.memberCache, "charlie")
+}
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.go
index 6e84f7abf..19086f8bd 100644
--- a/pkg/sources/github/github.go
+++ b/pkg/sources/github/github.go
@@ -421,7 +421,7 @@ func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) e
// this felt like a compromise that allowed me to isolate connection logic without rewriting the entire source.
switch c := s.connector.(type) {
case *appConnector:
- if err := s.enumerateWithApp(ctx, c.InstallationClient(), dedupeReporter); err != nil {
+ if err := s.enumerateWithApp(ctx, c, dedupeReporter); err != nil {
return err
}
case *basicAuthConnector:
@@ -631,7 +631,7 @@ func (s *Source) enumerateWithToken(ctx context.Context, isGithubEnterprise bool
return nil
}
-func (s *Source) enumerateWithApp(ctx context.Context, installationClient *github.Client, reporter sources.UnitReporter) error {
+func (s *Source) enumerateWithApp(ctx context.Context, connector *appConnector, reporter sources.UnitReporter) error {
// If no repos were provided, enumerate them.
if len(s.repos) == 0 {
if err := s.getReposByApp(ctx, reporter); err != nil {
@@ -640,7 +640,7 @@ func (s *Source) enumerateWithApp(ctx context.Context, installationClient *githu
// Check if we need to find user repos.
if s.conn.ScanUsers {
- err := s.addMembersByApp(ctx, installationClient, reporter)
+ err := s.addMembersByApp(ctx, connector, reporter)
if err != nil {
return err
}
@@ -938,7 +938,8 @@ func (s *Source) addUserGistsToCache(ctx context.Context, user string, reporter
return nil
}
-func (s *Source) addMembersByApp(ctx context.Context, installationClient *github.Client, reporter sources.UnitReporter) error {
+func (s *Source) addMembersByApp(ctx context.Context, connector *appConnector, reporter sources.UnitReporter) error {
+ installationClient := connector.InstallationClient()
opts := &github.ListOptions{
PerPage: membersAppPagination,
}
@@ -949,12 +950,25 @@ func (s *Source) addMembersByApp(ctx context.Context, installationClient *github
return fmt.Errorf("could not enumerate installed orgs: %w", err)
}
- for _, org := range installs {
- if org.Account.GetType() != "Organization" {
+ for _, install := range installs {
+ if install.Account.GetType() != "Organization" {
continue
}
- if err := s.addMembersByOrg(ctx, *org.Account.Login, reporter); err != nil {
- return err
+ org := install.Account.GetLogin()
+ logger := ctx.Logger().WithValues("org", org, "installation_id", install.GetID())
+
+ // Create a per-installation API client so that the token is scoped to
+ // this org. GitHub App installation tokens only bypass IP allowlists for
+ // their own org; using a cross-org token causes 403s.
+ client, err := connector.APIClientForInstallation(install.GetID())
+ if err != nil {
+ logger.Error(err, "could not create API client for installation")
+ continue
+ }
+
+ if err := s.addMembersByOrgWithClient(ctx, client, org, reporter); err != nil {
+ logger.Error(err, "Unable to add members for org")
+ continue
}
}
@@ -1036,6 +1050,10 @@ func (s *Source) addOrgsByUser(ctx context.Context, user string, reporter source
}
func (s *Source) addMembersByOrg(ctx context.Context, org string, reporter sources.UnitReporter) error {
+ return s.addMembersByOrgWithClient(ctx, s.connector.APIClient(), org, reporter)
+}
+
+func (s *Source) addMembersByOrgWithClient(ctx context.Context, client *github.Client, org string, reporter sources.UnitReporter) error {
opts := &github.ListMembersOptions{
PublicOnly: false,
ListOptions: github.ListOptions{
@@ -1045,7 +1063,7 @@ func (s *Source) addMembersByOrg(ctx context.Context, org string, reporter sourc
logger := ctx.Logger().WithValues("org", org)
for {
- members, res, err := s.connector.APIClient().Organizations.ListMembers(ctx, org, opts)
+ members, res, err := client.Organizations.ListMembers(ctx, org, opts)
if s.handleRateLimitWithUnitReporter(ctx, reporter, err) {
continue
}
diff --git a/pkg/sources/github/github_integration_test.go b/pkg/sources/github/github_integration_test.go
index 1bbed6305..527a92c5b 100644
--- a/pkg/sources/github/github_integration_test.go
+++ b/pkg/sources/github/github_integration_test.go
@@ -65,7 +65,7 @@ func TestSource_Token(t *testing.T) {
s.Init(ctx, "github integration test source", 0, 0, false, conn, 1)
s.filteredRepoCache = s.newFilteredRepoCache(ctx, simple.NewCache[string](), nil, nil)
- err = s.enumerateWithApp(ctx, s.connector.(*appConnector).InstallationClient(), noopReporter())
+ err = s.enumerateWithApp(ctx, s.connector.(*appConnector), noopReporter())
assert.NoError(t, err)
_, _, err = s.cloneRepo(ctx, "https://github.com/truffle-test-integration-org/another-test-repo.git")
diff --git a/pkg/sources/github/github_test.go b/pkg/sources/github/github_test.go
index b6361c5c5..58ce2dc42 100644
--- a/pkg/sources/github/github_test.go
+++ b/pkg/sources/github/github_test.go
@@ -302,7 +302,7 @@ func TestAddMembersByApp(t *testing.T) {
Get("/app/installations").
Reply(200).
JSON([]map[string]any{
- {"account": map[string]string{"login": "super-secret-org", "type": "Organization"}},
+ {"id": 1337, "account": map[string]string{"login": "super-secret-org", "type": "Organization"}},
})
gock.New("https://api.github.com").
Post("/app/installations/1337/access_tokens").
@@ -326,7 +326,7 @@ func TestAddMembersByApp(t *testing.T) {
AppId: "4141",
},
}})
- err := s.addMembersByApp(context.Background(), s.connector.(*appConnector).InstallationClient(), noopReporter())
+ err := s.addMembersByApp(context.Background(), s.connector.(*appConnector), noopReporter())
assert.Nil(t, err)
assert.Equal(t, 3, len(s.memberCache))
_, ok := s.memberCache["ssm1"]
@@ -884,7 +884,7 @@ func TestEnumerateWithApp(t *testing.T) {
},
},
})
- err := s.enumerateWithApp(context.Background(), s.connector.(*appConnector).InstallationClient(), noopReporter())
+ err := s.enumerateWithApp(context.Background(), s.connector.(*appConnector), noopReporter())
assert.Nil(t, err)
assert.Equal(t, 0, len(s.repos))
assert.False(t, gock.HasUnmatchedRequest())