|
| 1 | +// Copyright 2020 Security Scorecard Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package cmd implements Scorecard commandline. |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "time" |
| 23 | + |
| 24 | + sce "github.com/ossf/scorecard/v4/errors" |
| 25 | +) |
| 26 | + |
| 27 | +type packageMangerResponse struct { |
| 28 | + associatedRepo string |
| 29 | + exists bool |
| 30 | +} |
| 31 | + |
| 32 | +// TODO(#1568): Add unit tests for this. |
| 33 | +func fetchGitRepositoryFromPackageManagers(npm, pypi, rubygems string) (packageMangerResponse, error) { |
| 34 | + if npm != "" { |
| 35 | + gitRepo, err := fetchGitRepositoryFromNPM(npm) |
| 36 | + return packageMangerResponse{ |
| 37 | + exists: true, |
| 38 | + associatedRepo: gitRepo, |
| 39 | + }, err |
| 40 | + } |
| 41 | + if pypi != "" { |
| 42 | + gitRepo, err := fetchGitRepositoryFromPYPI(pypi) |
| 43 | + return packageMangerResponse{ |
| 44 | + exists: true, |
| 45 | + associatedRepo: gitRepo, |
| 46 | + }, err |
| 47 | + } |
| 48 | + if rubygems != "" { |
| 49 | + gitRepo, err := fetchGitRepositoryFromRubyGems(rubygems) |
| 50 | + return packageMangerResponse{ |
| 51 | + exists: true, |
| 52 | + associatedRepo: gitRepo, |
| 53 | + }, err |
| 54 | + } |
| 55 | + |
| 56 | + return packageMangerResponse{}, nil |
| 57 | +} |
| 58 | + |
| 59 | +type npmSearchResults struct { |
| 60 | + Objects []struct { |
| 61 | + Package struct { |
| 62 | + Links struct { |
| 63 | + Repository string `json:"repository"` |
| 64 | + } `json:"links"` |
| 65 | + } `json:"package"` |
| 66 | + } `json:"objects"` |
| 67 | +} |
| 68 | + |
| 69 | +type pypiSearchResults struct { |
| 70 | + Info struct { |
| 71 | + ProjectUrls struct { |
| 72 | + Source string `json:"Source"` |
| 73 | + } `json:"project_urls"` |
| 74 | + } `json:"info"` |
| 75 | +} |
| 76 | + |
| 77 | +type rubyGemsSearchResults struct { |
| 78 | + SourceCodeURI string `json:"source_code_uri"` |
| 79 | +} |
| 80 | + |
| 81 | +// Gets the GitHub repository URL for the npm package. |
| 82 | +// nolint: noctx |
| 83 | +func fetchGitRepositoryFromNPM(packageName string) (string, error) { |
| 84 | + npmSearchURL := "https://registry.npmjs.org/-/v1/search?text=%s&size=1" |
| 85 | + const timeout = 10 |
| 86 | + client := &http.Client{ |
| 87 | + Timeout: timeout * time.Second, |
| 88 | + } |
| 89 | + resp, err := client.Get(fmt.Sprintf(npmSearchURL, packageName)) |
| 90 | + if err != nil { |
| 91 | + return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get npm package json: %v", err)) |
| 92 | + } |
| 93 | + |
| 94 | + defer resp.Body.Close() |
| 95 | + v := &npmSearchResults{} |
| 96 | + err = json.NewDecoder(resp.Body).Decode(v) |
| 97 | + if err != nil { |
| 98 | + return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse npm package json: %v", err)) |
| 99 | + } |
| 100 | + if len(v.Objects) == 0 { |
| 101 | + return "", sce.WithMessage(sce.ErrScorecardInternal, |
| 102 | + fmt.Sprintf("could not find source repo for npm package: %s", packageName)) |
| 103 | + } |
| 104 | + return v.Objects[0].Package.Links.Repository, nil |
| 105 | +} |
| 106 | + |
| 107 | +// Gets the GitHub repository URL for the pypi package. |
| 108 | +// nolint: noctx |
| 109 | +func fetchGitRepositoryFromPYPI(packageName string) (string, error) { |
| 110 | + pypiSearchURL := "https://pypi.org/pypi/%s/json" |
| 111 | + const timeout = 10 |
| 112 | + client := &http.Client{ |
| 113 | + Timeout: timeout * time.Second, |
| 114 | + } |
| 115 | + resp, err := client.Get(fmt.Sprintf(pypiSearchURL, packageName)) |
| 116 | + if err != nil { |
| 117 | + return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get pypi package json: %v", err)) |
| 118 | + } |
| 119 | + |
| 120 | + defer resp.Body.Close() |
| 121 | + v := &pypiSearchResults{} |
| 122 | + err = json.NewDecoder(resp.Body).Decode(v) |
| 123 | + if err != nil { |
| 124 | + return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse pypi package json: %v", err)) |
| 125 | + } |
| 126 | + if v.Info.ProjectUrls.Source == "" { |
| 127 | + return "", sce.WithMessage(sce.ErrScorecardInternal, |
| 128 | + fmt.Sprintf("could not find source repo for pypi package: %s", packageName)) |
| 129 | + } |
| 130 | + return v.Info.ProjectUrls.Source, nil |
| 131 | +} |
| 132 | + |
| 133 | +// Gets the GitHub repository URL for the rubygems package. |
| 134 | +// nolint: noctx |
| 135 | +func fetchGitRepositoryFromRubyGems(packageName string) (string, error) { |
| 136 | + rubyGemsSearchURL := "https://rubygems.org/api/v1/gems/%s.json" |
| 137 | + const timeout = 10 |
| 138 | + client := &http.Client{ |
| 139 | + Timeout: timeout * time.Second, |
| 140 | + } |
| 141 | + resp, err := client.Get(fmt.Sprintf(rubyGemsSearchURL, packageName)) |
| 142 | + if err != nil { |
| 143 | + return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to get ruby gem json: %v", err)) |
| 144 | + } |
| 145 | + |
| 146 | + defer resp.Body.Close() |
| 147 | + v := &rubyGemsSearchResults{} |
| 148 | + err = json.NewDecoder(resp.Body).Decode(v) |
| 149 | + if err != nil { |
| 150 | + return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("failed to parse ruby gem json: %v", err)) |
| 151 | + } |
| 152 | + if v.SourceCodeURI == "" { |
| 153 | + return "", sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("could not find source repo for ruby gem: %v", err)) |
| 154 | + } |
| 155 | + return v.SourceCodeURI, nil |
| 156 | +} |
0 commit comments