@@ -56,8 +56,8 @@ def __init__(self, *args, **kwargs):
5656 super ().__init__ (* args , ** kwargs )
5757 try :
5858 self .gh_token = os .environ ["GH_TOKEN" ]
59- except KeyError as e :
60- raise GitHubTokenMissingError ("Envirnomental variable GH_TOKEN is missing" )
59+ except KeyError :
60+ raise GitHubTokenError ("Envirnomental variable GH_TOKEN is missing" )
6161
6262 def __enter__ (self ):
6363 self .advisories = self .fetch ()
@@ -136,25 +136,40 @@ def set_version_api(self, ecosystem):
136136 elif ecosystem == "COMPOSER" :
137137 self .version_api = ComposerVersionAPI ()
138138
139+ @staticmethod
140+ def process_name (ecosystem , pkg_name ):
141+
142+ if ecosystem == "MAVEN" :
143+
144+ artifact_comps = pkg_name .split (":" )
145+ if len (artifact_comps ) != 2 :
146+ return
147+ ns , name = artifact_comps
148+ return ns , name
149+
150+ if ecosystem == "NUGET" :
151+ return None , pkg_name
152+
153+ if ecosystem == "COMPOSER" :
154+ raise NotImplementedError
155+
139156 def process_response (self ) -> List [Advisory ]:
140157 adv_list = []
141158 for ecosystem in self .advisories :
142159 self .set_version_api (ecosystem )
143160 pkg_type = ecosystem .lower ()
144161 for resp_page in self .advisories [ecosystem ]:
145162 for adv in resp_page ["data" ]["securityVulnerabilities" ]["edges" ]:
146- artifact = adv ["node" ]["package" ]["name" ]
147- artifact_comps = artifact .split (":" )
163+ name = adv ["node" ]["package" ]["name" ]
148164
149- if len (artifact_comps ) != 2 :
165+ if self .process_name (ecosystem , name ):
166+ ns , pkg_name = self .process_name (ecosystem , name )
167+ else :
150168 continue
151-
152- ns , pkg_name = artifact_comps
153169 aff_range = adv ["node" ]["vulnerableVersionRange" ]
154- # print(pkg_name,aff_range)
155- self .version_api .load_to_api (artifact )
170+ self .version_api .load_to_api (name )
156171 aff_vers , unaff_vers = self .categorize_versions (
157- aff_range , self .version_api .get (artifact )
172+ aff_range , self .version_api .get (name )
158173 )
159174
160175 affected_purls = {
@@ -190,7 +205,7 @@ def process_response(self) -> List[Advisory]:
190205 reference_ids = ref_ids ,
191206 )
192207 )
193- print (adv_list [- 1 ])
208+ # print(adv_list[-1])
194209 return adv_list
195210
196211 @staticmethod
@@ -252,7 +267,41 @@ def extract_versions(xml_response: ET.ElementTree) -> Set[str]:
252267
253268class NugetVersionAPI :
254269 def __init__ (self ):
255- raise NotImplementedError
270+ self .cache = {}
271+
272+ def get (self , pkg_name ):
273+ return self .cache .get (pkg_name .lower (), set ())
274+
275+ def load_to_api (self , pkg_name : str ):
276+ if pkg_name in self .cache :
277+ return
278+ endpoint = self .nuget_url (pkg_name )
279+ try :
280+ resp = requests .get (endpoint ).json ()
281+ # pkg_name=Microsoft.NETCore.UniversalWindowsPlatform triggers
282+ # JSONDecodeError.
283+ except (json .decoder .JSONDecodeError , KeyError ):
284+ self .cache [pkg_name .lower ()] = set ()
285+ return
286+
287+ self .cache [pkg_name .lower ()] = self .extract_versions (resp )
288+
289+ @staticmethod
290+ def nuget_url (pkg_name ):
291+ base_url = "https://api.nuget.org/v3/registration5-semver1/{}/index.json"
292+ return base_url .format (pkg_name .lower ())
293+
294+ @staticmethod
295+ def extract_versions (json_resp ):
296+ all_versions = set ()
297+ try :
298+ for entry in json_resp ["items" ][0 ]["items" ]:
299+ all_versions .add (entry ["catalogEntry" ]["version" ])
300+ # json response for YamlDotNet.Signed triggers this exception
301+ except KeyError :
302+ return all_versions
303+
304+ return all_versions
256305
257306
258307class ComposerVersionAPI :
0 commit comments