Skip to content

Find, verify, and analyze leaked credentials

10.7 KB
Raw Blame
package github

import (
	"fmt"
	"strconv"
	"sync"

	gogit "github.com/go-git/go-git/v5"
	"github.com/google/go-github/v67/github"
	"github.com/shurcooL/githubv4"

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

type appConnector struct {
	installationClient *github.Client
	installationID     int64
	appID              int64
	apiEndpoint        string
	appPrivateKey      string

	mu sync.RWMutex

	// repoInstallationMap maps repo clone URLs to their owning installation
	// ID for repos discovered from non-default installations. Clone checks
	// this map to use the correct installation token.
	repoInstallationMap map[string]int64
}

var _ Connector = (*appConnector)(nil)

const githubHTTPTimeoutSeconds = 60

func NewAppConnector(ctx context.Context, apiEndpoint string, app *credentialspb.GitHubApp, scanAllInstallations bool) (Connector, error) {
	var installationID int64
	var err error

	if app.InstallationId != "" {
		installationID, err = strconv.ParseInt(app.InstallationId, 10, 64)
		if err != nil {
			return nil, fmt.Errorf("could not parse app installation ID %q: %w", app.InstallationId, err)
		}
	}

	if installationID == 0 && !scanAllInstallations {
		return nil, fmt.Errorf("githubApp.installationId is required unless scanAllInstallations is set")
	}

	appID, err := strconv.ParseInt(app.AppId, 10, 64)
	if err != nil {
		return nil, fmt.Errorf("could not parse app ID %q: %w", app.AppId, err)
	}

	installationClient, err := GetAppClient(ctx, apiEndpoint, appID, app.PrivateKey)
	if err != nil {
		return nil, fmt.Errorf("could not create installation client: %w", err)
	}

	connector := &appConnector{
		installationClient:  installationClient,
		installationID:      installationID,
		appID:               appID,
		apiEndpoint:         apiEndpoint,
		appPrivateKey:       app.PrivateKey,
		repoInstallationMap: make(map[string]int64),
	}

	// When no default installation is configured (installationID == 0, only
	// possible with scanAllInstallations), installations are discovered and
	// clients created lazily per-repo, so there is no default client to
	// validate up front.
	if connector.HasDefaultInstallation() {
		_, err := connector.clientsForInstallation(ctx, installationID)
		if err != nil {
			return nil, fmt.Errorf("could not create clients for configured installation: %w", err)
		}
	}

	return connector, nil
}

// HasDefaultInstallation reports whether a default installation is
// configured. False only when scanAllInstallations is set and
// githubApp.installationId was omitted, in which case there is no
// authoritative installation to fall back to for repos/gists discovered
// outside any installation's own listing.
func (c *appConnector) HasDefaultInstallation() bool {
	return c.installationID != 0
}

// APIClient returns the client for the connector's default installation.
// When scanAllInstallations is configured without a githubApp.installationId
// (installationID == 0), NewAppConnector does not pre-create this client, so
// it is created lazily here rather than returning nil: several callers
// (Validate, mapRemainingAccessibleRepos, member gist/repo lookups) use this
// as a fallback client for repos/resources outside any installation listing
// and call it unconditionally.
func (c *appConnector) APIClient() *github.Client {
	client, err := c.APIClientForInstallation(contextForClientLookup(), c.installationID)
	if err != nil {
		return nil
	}
	return client
}

func (c *appConnector) APIClientForRepo(repoURL string) (*github.Client, error) {
	installID, _ := c.installationIDForRepo(repoURL)
	return c.APIClientForInstallation(contextForClientLookup(), installID)
}

func (c *appConnector) GraphQLClientForRepo(ctx context.Context, repoURL string) (*githubv4.Client, error) {
	installID, _ := c.installationIDForRepo(repoURL)
	return c.graphqlClientForInstallation(ctx, installID)
}

func (c *appConnector) graphqlClientForInstallation(ctx context.Context, installID int64) (*githubv4.Client, error) {
	clients, err := c.clientsForInstallation(ctx, installID)
	if err != nil {
		return nil, err
	}

	return clients.GraphQLClient, nil
}

func (c *appConnector) Clone(ctx context.Context, repoURL string, args ...string) (string, *gogit.Repository, error) {
	installID, _ := c.installationIDForRepo(repoURL)
	if installID == 0 {
		// installID falls back to the connector's default installationID
		// (see installationIDForRepo/ensureRepoInstallation) for repos
		// discovered outside any installation's repo listing, e.g. org
		// members' personal repos with scanUsers. That default is unset (0)
		// when scanAllInstallations is true and githubApp.installationId
		// was omitted, since no single installation is authoritative.
		return "", nil, fmt.Errorf("no GitHub App installation resolved for repo %q; set githubApp.installationId to a fallback installation to scan repos outside installation listings (e.g. member repos with scanUsers) together with scanAllInstallations", repoURL)
	}

	cs, err := c.clientsForInstallation(ctx, installID)
	if err != nil {
		return "", nil, fmt.Errorf("cannot fetch installation clients for installation %d: %w", installID, err)
	}

	token, err := cs.AccessToken(ctx)
	if err != nil {
		return "", nil, fmt.Errorf("could not fetch installation access token for installation %d: %w", installID, err)
	}

	return git.CloneRepoUsingToken(ctx, token, repoURL, "", "x-access-token", true, args...)
}

// grantsForRepo returns what the installation that owns repoURL grants, for
// the permissions the comment scan needs. Installation permissions apply to
// every repo the installation can see, so repos on one installation share
// the answer, and the cached token makes it free after the repo's clone.
func (c *appConnector) grantsForRepo(ctx context.Context, repoURL string) (*installationGrants, error) {
	installID, _ := c.installationIDForRepo(repoURL)
	if installID == 0 {
		return nil, fmt.Errorf("no GitHub App installation resolved for repo %q", repoURL)
	}

	cs, err := c.clientsForInstallation(ctx, installID)
	if err != nil {
		return nil, fmt.Errorf("cannot fetch installation clients for installation %d: %w", installID, err)
	}

	perms, err := cs.GrantedPermissions(ctx)
	if err != nil {
		return nil, fmt.Errorf("could not read permissions for installation %d: %w", installID, err)
	}
	return newInstallationGrants(installID, perms), nil
}

// installationIDForRepo returns the mapped installation ID for repoURL. When no
// mapping exists, it falls back to the configured installation and returns false.
//
// Note: Because ensureRepoInstallation also stores values in this map with
// c.installationID, the second return value here is NOT indicative of using
// the fallback.
func (c *appConnector) installationIDForRepo(repoURL string) (int64, bool) {
	c.mu.RLock()
	defer c.mu.RUnlock()

	if id, ok := c.repoInstallationMap[repoURL]; ok {
		return id, true
	}
	return c.installationID, false
}

func (c *appConnector) hasRepoInstallation(repoURL string) bool {
	c.mu.RLock()
	defer c.mu.RUnlock()

	_, ok := c.repoInstallationMap[repoURL]
	return ok
}

// setRepoInstallation records which installation owns a repo and its derived
// wiki URL so Clone uses the correct installation token for cross-org repos.
func (c *appConnector) setRepoInstallation(repoURL string, installationID int64) {
	c.setRepoInstallationForWiki(repoURL, installationID, wikiCloneURLForRepo)
}

// ensureRepoInstallation maps a repo to the default installation unless an
// installation already claims it. Used for repos discovered outside
// installation listings (e.g. org members' personal repos), which no
// installation owns but which the default installation token can still
// access when they are public. The check and set happen under one lock so a
// concurrent mapping to a real installation is never overwritten.
func (c *appConnector) ensureRepoInstallation(repoURL, repoName string) {
	c.mu.Lock()
	defer c.mu.Unlock()

	if _, ok := c.repoInstallationMap[repoURL]; ok {
		return
	}
	c.repoInstallationMap[repoURL] = c.installationID
	if wikiURL, ok := wikiCloneURLForRepoName(repoURL, repoName); ok {
		if _, taken := c.repoInstallationMap[wikiURL]; !taken {
			c.repoInstallationMap[wikiURL] = c.installationID
		}
	}
}

func (c *appConnector) setRepoInstallationForRepoName(repoURL, repoName string, installationID int64) {
	c.setRepoInstallationForWiki(repoURL, installationID, func(repoURL string) (string, bool) {
		return wikiCloneURLForRepoName(repoURL, repoName)
	})
}

func (c *appConnector) setRepoInstallationForWiki(repoURL string, installationID int64, wikiURLForRepo func(string) (string, bool)) {
	c.mu.Lock()
	defer c.mu.Unlock()

	c.repoInstallationMap[repoURL] = installationID
	if wikiURL, ok := wikiURLForRepo(repoURL); ok {
		c.repoInstallationMap[wikiURL] = installationID
	}
}

// GraphQLClient returns the GraphQL client for the connector's default
// installation, lazily creating it if needed. See APIClient for why this
// cannot simply return nil when no default installation is configured.
func (c *appConnector) GraphQLClient() *githubv4.Client {
	client, err := c.graphqlClientForInstallation(contextForClientLookup(), c.installationID)
	if err != nil {
		return nil
	}
	return 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(ctx context.Context, installationID int64) (*github.Client, error) {
	clients, err := c.clientsForInstallation(ctx, installationID)
	if err != nil {
		return nil, err
	}

	return clients.APIClient, nil
}

func (c *appConnector) clientsForInstallation(ctx context.Context, installationID int64) (*AppInstallationClients, error) {
	return GetAppInstallationClients(ctx, c.apiEndpoint, c.appID, c.appPrivateKey, installationID)
}

// contextForClientLookup is only for the Connector methods that don't take a
// context: APIClient, APIClientForRepo and GraphQLClient. They only build a
// client or take it from the cache, which makes no network call. On a cache
// miss, though, the "Creating GraphQL client" line is logged without the
// source fields and ignores per-source debug. The fix is to pass a context
// through the Connector interface, which is left for a separate change since
// it touches every connector and its callers. Everything else passes its own
// context. That is why the semgrep rule against new contexts is turned off on
// this line.
func contextForClientLookup() context.Context {
	return context.Background() // nosemgrep
}