@@ -14,11 +14,9 @@ import (
1414 "github.com/google/go-github/v67/github"
1515 "github.com/k0kubun/go-ansi"
1616 "github.com/schollz/progressbar/v3"
17- "golang.org/x/oauth2"
1817
1918 "github.com/trufflesecurity/trufflehog/v3/pkg/context"
2019 "github.com/trufflesecurity/trufflehog/v3/pkg/sources"
21- "github.com/trufflesecurity/trufflehog/v3/pkg/sources/git"
2220)
2321
2422// Assumption: sleeping for 60 seconds is enough to reset the secondary rate limit
@@ -100,18 +98,7 @@ func (b *backoff) getValue() int {
10098 return int (b .value )
10199}
102100
103- // Github token
104- var ghToken = ""
105-
106- func getForksCount (owner , repoName string ) (int , error ) {
107- ctx := context .Background ()
108- ts := oauth2 .StaticTokenSource (
109- & oauth2.Token {AccessToken : ghToken },
110- )
111- tc := oauth2 .NewClient (ctx , ts )
112-
113- client := github .NewClient (tc )
114-
101+ func getForksCount (ctx context.Context , client * github.Client , owner , repoName string ) (int , error ) {
115102 repo , _ , err := client .Repositories .Get (ctx , owner , repoName )
116103 if err != nil {
117104 return 0 , err
@@ -120,22 +107,6 @@ func getForksCount(owner, repoName string) (int, error) {
120107 return repo .GetForksCount (), nil
121108}
122109
123- func getGitHubUser () (string , error ) {
124- ctx := context .Background ()
125- ts := oauth2 .StaticTokenSource (
126- & oauth2.Token {AccessToken : ghToken },
127- )
128- tc := oauth2 .NewClient (ctx , ts )
129-
130- client := github .NewClient (tc )
131-
132- ghUser , _ , err := client .Users .Get (ctx , "" )
133- if err != nil {
134- return "" , err
135- }
136- return ghUser .GetLogin (), nil
137- }
138-
139110// runGitCommand runs a git command
140111func runGitCommand (args []string ) ([]byte , error ) {
141112 cmd := exec .Command ("git" , args ... )
@@ -322,7 +293,7 @@ func removeBySHA(existingCommits, newCommits []string, charLen int) []string {
322293 return filteredCommits
323294}
324295
325- func processCommits (ctx context.Context , needsProcessing []string , owner , repo , path string ) {
296+ func processCommits (ctx context.Context , apiClient * github. Client , needsProcessing []string , owner , repo , path string ) {
326297 repoCtx := context .WithValue (ctx , "repo" , repo )
327298
328299 startingSize := float64 (len (needsProcessing ))
@@ -343,10 +314,12 @@ func processCommits(ctx context.Context, needsProcessing []string, owner, repo,
343314 chunk := needsProcessing [:chunkSize ]
344315 needsProcessing = needsProcessing [chunkSize :]
345316
346- commitData , err := checkHashes (owner , repo , chunk )
317+ commitData , err := checkHashes (repoCtx , apiClient , owner , repo , chunk )
347318 if err != nil {
348319 repoCtx .Logger ().V (2 ).Info ("Temporary error occurred in guessing commits" , "error" , err )
349- needsProcessing = append (needsProcessing , chunk ... )
320+ // Prepend the failed chunk to the FRONT of the queue for immediate retry
321+ // This ensures we retry the same hashes instead of moving to the next batch
322+ needsProcessing = append (chunk , needsProcessing ... )
350323 queryChunkSize .errorOccurred ()
351324 if strings .Contains (err .Error (), "You have exceeded a secondary rate limit" ) {
352325 repoCtx .Logger ().V (2 ).Info ("Reached secondary GitHub Rate Limit. Sleeping for 60 seconds." )
@@ -391,7 +364,7 @@ type responseData struct {
391364 Message string `json:"message"`
392365}
393366
394- func checkHashes (owner , repo string , hashes []string ) (map [string ][]string , error ) {
367+ func checkHashes (ctx context. Context , client * github. Client , owner , repo string , hashes []string ) (map [string ][]string , error ) {
395368 testCases := ""
396369 for _ , h := range hashes {
397370 testCase := fmt .Sprintf (`
@@ -413,7 +386,6 @@ func checkHashes(owner, repo string, hashes []string) (map[string][]string, erro
413386 ` , owner , repo , testCases )
414387
415388 headers := map [string ]string {
416- "Authorization" : "Bearer " + ghToken ,
417389 "Content-Type" : "application/json" ,
418390 "Github-Verified-Fetch" : "true" ,
419391 "X-Requested-With" : "XMLHttpRequest" ,
@@ -426,7 +398,7 @@ func checkHashes(owner, repo string, hashes []string) (map[string][]string, erro
426398 return nil , fmt .Errorf ("failed to marshal request body: %w" , err )
427399 }
428400
429- req , err := http .NewRequest ( "POST" , "https://api.github.com/graphql" , bytes .NewBuffer (requestBody ))
401+ req , err := http .NewRequestWithContext ( ctx , "POST" , "https://api.github.com/graphql" , bytes .NewBuffer (requestBody ))
430402 if err != nil {
431403 return nil , fmt .Errorf ("failed to create request: %w" , err )
432404 }
@@ -435,8 +407,9 @@ func checkHashes(owner, repo string, hashes []string) (map[string][]string, erro
435407 req .Header .Set (key , value )
436408 }
437409
438- client := & http.Client {}
439- resp , err := client .Do (req )
410+ // Use the authenticated HTTP client from the GitHub API client
411+ // This client already has the Bearer token configured via OAuth2 transport
412+ resp , err := client .Client ().Do (req )
440413 if err != nil {
441414 return nil , fmt .Errorf ("python request error: %w" , err )
442415 }
@@ -546,9 +519,6 @@ func downloadPatches(valid_cfor []string, path string) error {
546519
547520// scanHiddenData scans hidden data (and non-hidden data) for secrets in a GitHub repository
548521func (s * Source ) EnumerateAndScanAllObjects (ctx context.Context , chunksChan chan * sources.Chunk ) error {
549- // assign github token to global variable
550- ghToken = s .conn .GetToken ()
551-
552522 // set collision threshold to user input
553523 collisionThreshold = float64 (s .conn .CollisionThreshold )
554524
@@ -564,7 +534,7 @@ func (s *Source) EnumerateAndScanAllObjects(ctx context.Context, chunksChan chan
564534
565535 // get repo metadata and store in cacheRepoInfo
566536 repoCtx := context .WithValue (ctx , "repo" , owner + "/" + repoName )
567- ghRepo , _ , err := s .apiClient .Repositories .Get (repoCtx , owner , repoName )
537+ ghRepo , _ , err := s .connector . APIClient () .Repositories .Get (repoCtx , owner , repoName )
568538 if err != nil {
569539 return fmt .Errorf ("failed to fetch repository: %w" , err )
570540 }
@@ -582,20 +552,14 @@ func (s *Source) EnumerateAndScanAllObjects(ctx context.Context, chunksChan chan
582552 return fmt .Errorf ("failed to create .trufflehog folder in user's home directory: %w" , err )
583553 }
584554
585- // Get GitHub User tied to token
586- ghUser , err := getGitHubUser ()
587- if err != nil {
588- return fmt .Errorf ("failed to get GitHub user details: %w" , err )
589- }
590-
591555 // get the number of forks
592- forksCount , err := getForksCount (owner , repoName )
556+ forksCount , err := getForksCount (repoCtx , s . connector . APIClient (), owner , repoName )
593557 if err != nil {
594558 return fmt .Errorf ("failed to get forks count: %w" , err )
595559 }
596560
597- // download the repo
598- path , repo , err := git . CloneRepoUsingToken (ctx , ghToken , repoURL , "" , ghUser , true )
561+ // download the repo using the authenticated connector
562+ path , repo , err := s . connector . Clone (ctx , repoURL )
599563 if err != nil {
600564 return fmt .Errorf ("failed to clone the repository: %w" , err )
601565 }
@@ -639,7 +603,7 @@ func (s *Source) EnumerateAndScanAllObjects(ctx context.Context, chunksChan chan
639603 possibleCommits = removeByShortSHA (invalidCommits , possibleCommits )
640604
641605 // Guess all possible commit hashes
642- processCommits (ctx , possibleCommits , owner , repoName , folderPath )
606+ processCommits (ctx , s . connector . APIClient (), possibleCommits , owner , repoName , folderPath )
643607
644608 // Read in the new commits
645609 validHiddenCommits , err = readCommitsFromDisk (validHiddenCommit , folderPath )
0 commit comments