@@ -68,7 +68,10 @@ type scanService struct {
6868}
6969
7070var newGitHubClient = func () * github.Client {
71- return github .NewClient (nil )
71+ return github .NewClient (& http.Client {
72+ Transport : utils .EgressTransport ,
73+ Timeout : 10 * time .Minute ,
74+ })
7275}
7376
7477var downloadRawFileFn = DownloadRawFile
@@ -877,10 +880,6 @@ func (s *scanService) ScanSBOMWithoutSaving(ctx context.Context, bom *cyclonedx.
877880
878881func (s * scanService ) FetchOpenVexFromGitHub (ctx context.Context , targetURL string ) (vexReports []* normalize.VexReportOpenVEX , err error ) {
879882 client := newGitHubClient ()
880- githubDomain := "https://github.com"
881- if ! strings .HasPrefix (targetURL , githubDomain ) {
882- return nil , fmt .Errorf ("invalid github repository url" )
883- }
884883 owner , repo , err := ParseGitHubURL (targetURL )
885884 if err != nil {
886885 return nil , err
@@ -918,6 +917,7 @@ func (s *scanService) FetchOpenVexFromGitHub(ctx context.Context, targetURL stri
918917 }
919918
920919 content , err := downloadRawFileFn (
920+ ctx ,
921921 owner ,
922922 repo ,
923923 branch ,
@@ -947,11 +947,23 @@ func ParseGitHubURL(rawURL string) (owner string, repo string, err error) {
947947 if err != nil {
948948 return "" , "" , err
949949 }
950+ const githubDomain = "github.com"
951+ if u .Host != githubDomain {
952+ return "" , "" , fmt .Errorf ("invalid github repository url" )
953+ }
950954 parts := strings .Split (strings .Trim (u .Path , "/" ), "/" )
951- return parts [0 ], parts [1 ], nil
955+ if len (parts ) < 2 {
956+ return "" , "" , fmt .Errorf ("invalid github repository url path: expected /{owner}/{repo}, got %q" , u .Path )
957+ }
958+ owner = parts [0 ]
959+ repo = strings .TrimSuffix (parts [1 ], ".git" )
960+ if owner == "" || repo == "" {
961+ return "" , "" , fmt .Errorf ("invalid github repository url path: expected non-empty owner and repo, got %q" , u .Path )
962+ }
963+ return owner , repo , nil
952964}
953965
954- func DownloadRawFile (owner , repo , branch , filePath string ) ([]byte , error ) {
966+ func DownloadRawFile (ctx context. Context , owner , repo , branch , filePath string ) ([]byte , error ) {
955967
956968 rawURL := fmt .Sprintf (
957969 "https://raw.githubusercontent.com/%s/%s/%s/%s" ,
@@ -960,11 +972,25 @@ func DownloadRawFile(owner, repo, branch, filePath string) ([]byte, error) {
960972 branch ,
961973 filePath ,
962974 )
963- resp , err := http .Get ( rawURL )
975+ resp , err := http .NewRequestWithContext ( ctx , http . MethodGet , rawURL , nil )
964976 if err != nil {
965977 return nil , err
966978 }
967979 defer resp .Body .Close ()
968- return io .ReadAll (resp .Body )
969-
980+ switch resp .Response .StatusCode {
981+ case http .StatusOK :
982+ file , err := io .ReadAll (resp .Body )
983+ if err != nil {
984+ return nil , fmt .Errorf ("401 Unauthorized" )
985+ }
986+ return file , nil
987+ case http .StatusNotFound :
988+ return nil , fmt .Errorf ("404 Source not found" )
989+ case http .StatusUnauthorized :
990+ return nil , fmt .Errorf ("401 Unauthorized" )
991+ case http .StatusInternalServerError :
992+ return nil , fmt .Errorf ("500 Internal Server error" )
993+ default :
994+ return nil , fmt .Errorf ("Unexpected status: %d\n " , resp .Response .StatusCode )
995+ }
970996}
0 commit comments