66# You may not use this software except in compliance with the License.
77# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
88# Unless required by applicable law or agreed to in writing, software distributed
9- # under the License is distributed on an " AS IS" BASIS, WITHOUT WARRANTIES OR
9+ # under the License is distributed on an ' AS IS' BASIS, WITHOUT WARRANTIES OR
1010# CONDITIONS OF ANY KIND, either express or implied. See the License for the
1111# specific language governing permissions and limitations under the License.
1212#
1313# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
1414# derivative work, you must accompany this data with the following acknowledgment:
1515#
16- # Generated with VulnerableCode and provided on an " AS IS" BASIS, WITHOUT WARRANTIES
16+ # Generated with VulnerableCode and provided on an ' AS IS' BASIS, WITHOUT WARRANTIES
1717# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
1818# VulnerableCode should be considered or used as legal advice. Consult an Attorney
1919# for any legal advice.
4343# set of all possible values of first '%s' = {'MAVEN','COMPOSER', 'NUGET'}
4444# second '%s' is interesting, it will have the value '' for the first request,
4545# since we don't have any value for endCursor at the beginning
46- # for all the subsequent requests it will have value 'after: "{endCursor}"'
47- query = """
46+ # for all the subsequent requests it will have value 'after: "{endCursor}""
47+ query = '''
4848 query MyQuery {
4949 securityVulnerabilities(first: 100, ecosystem: %s, %s) {
5050 edges {
6868 }
6969 }
7070 }
71- """
71+ '''
7272
7373
7474class GitHubTokenError (Exception ):
@@ -88,9 +88,9 @@ class GitHubAPIDataSource(DataSource):
8888 def __init__ (self , * args , ** kwargs ):
8989 super ().__init__ (* args , ** kwargs )
9090 try :
91- self .gh_token = os .environ [" GH_TOKEN" ]
91+ self .gh_token = os .environ [' GH_TOKEN' ]
9292 except KeyError :
93- raise GitHubTokenError (" Envirnomental variable GH_TOKEN is missing" )
93+ raise GitHubTokenError (' Envirnomental variable GH_TOKEN is missing' )
9494
9595 def __enter__ (self ):
9696 self .advisories = self .fetch ()
@@ -100,64 +100,65 @@ def updated_advisories(self) -> Set[Advisory]:
100100
101101 def fetch (self ) -> Mapping [str , List [Mapping ]]:
102102
103- headers = {" Authorization" : " token " + self .gh_token }
103+ headers = {' Authorization' : ' token ' + self .gh_token }
104104 api_data = {}
105105 for ecosystem in self .config .ecosystems :
106106
107107 api_data [ecosystem ] = []
108- end_cursor_exp = ""
108+ end_cursor_exp = ''
109109
110110 while True :
111111
112- query_json = {" query" : query % (ecosystem , end_cursor_exp )}
112+ query_json = {' query' : query % (ecosystem , end_cursor_exp )}
113113 resp = requests .post (
114114 self .config .endpoint , headers = headers , json = query_json
115115 ).json ()
116116
117- if resp .get (" message" ) == " Bad credentials" :
118- raise GitHubTokenError (" Invalid GitHub token" )
117+ if resp .get (' message' ) == ' Bad credentials' :
118+ raise GitHubTokenError (' Invalid GitHub token' )
119119
120- end_cursor = resp [" data" ][ " securityVulnerabilities" ][ " pageInfo" ][
121- " endCursor"
120+ end_cursor = resp [' data' ][ ' securityVulnerabilities' ][ ' pageInfo' ][
121+ ' endCursor'
122122 ]
123- end_cursor_exp = " after: {}" .format ('"{}"' .format (end_cursor ))
123+ end_cursor_exp = ' after: {}' .format ('"{}"' .format (end_cursor ))
124124 api_data [ecosystem ].append (resp )
125+ print (resp )
125126
126- if not resp [" data" ][ " securityVulnerabilities" ][ " pageInfo" ][
127- " hasNextPage"
127+ if not resp [' data' ][ ' securityVulnerabilities' ][ ' pageInfo' ][
128+ ' hasNextPage'
128129 ]:
129130 break
130131 return api_data
131132
132133 def set_version_api (self , ecosystem : str ) -> None :
133134
134- if ecosystem == " MAVEN" :
135+ if ecosystem == ' MAVEN' :
135136 self .version_api = MavenVersionAPI ()
136137
137- elif ecosystem == " NUGET" :
138+ elif ecosystem == ' NUGET' :
138139 self .version_api = NugetVersionAPI ()
139140
140- elif ecosystem == " COMPOSER" :
141+ elif ecosystem == ' COMPOSER' :
141142 self .version_api = ComposerVersionAPI ()
142143
143144 @staticmethod
144145 def process_name (
145146 ecosystem : str , pkg_name : str
146147 ) -> Optional [Tuple [Optional [str ], str ]]:
147148
148- if ecosystem == " MAVEN" :
149+ if ecosystem == ' MAVEN' :
149150
150- artifact_comps = pkg_name .split (":" )
151+ artifact_comps = pkg_name .split (':' )
151152 if len (artifact_comps ) != 2 :
152153 return
153154 ns , name = artifact_comps
154155 return ns , name
155156
156- if ecosystem == " NUGET" :
157+ if ecosystem == ' NUGET' :
157158 return None , pkg_name
158159
159- if ecosystem == " COMPOSER" :
160- vendor , name = pkg_name .split ("/" )
160+ if ecosystem == ' COMPOSER' :
161+ vendor , name = pkg_name .split ('/' )
161162 return vendor , name
162163
163164 def process_response (self ) -> List [Advisory ]:
@@ -166,14 +167,14 @@ def process_response(self) -> List[Advisory]:
166167 self .set_version_api (ecosystem )
167168 pkg_type = ecosystem .lower ()
168169 for resp_page in self .advisories [ecosystem ]:
169- for adv in resp_page [" data" ][ " securityVulnerabilities" ][ " edges" ]:
170- name = adv [" node" ][ " package" ][ " name" ]
170+ for adv in resp_page [' data' ][ ' securityVulnerabilities' ][ ' edges' ]:
171+ name = adv [' node' ][ ' package' ][ ' name' ]
171172
172173 if self .process_name (ecosystem , name ):
173174 ns , pkg_name = self .process_name (ecosystem , name )
174175 else :
175176 continue
176- aff_range = adv [" node" ][ " vulnerableVersionRange" ]
177+ aff_range = adv [' node' ][ ' vulnerableVersionRange' ]
177178 self .version_api .load_to_api (name )
178179 aff_vers , unaff_vers = self .categorize_versions (
179180 aff_range , self .version_api .get (name )
@@ -195,13 +196,13 @@ def process_response(self) -> List[Advisory]:
195196
196197 cve_ids = set ()
197198 ref_ids = set ()
198- vuln_desc = adv [" node" ][ " advisory" ][ " summary" ]
199+ vuln_desc = adv [' node' ][ ' advisory' ][ ' summary' ]
199200
200- for vuln in adv [" node" ][ " advisory" ][ " identifiers" ]:
201- if vuln [" type" ] == " CVE" :
202- cve_ids .add (vuln [" value" ])
201+ for vuln in adv [' node' ][ ' advisory' ][ ' identifiers' ]:
202+ if vuln [' type' ] == ' CVE' :
203+ cve_ids .add (vuln [' value' ])
203204 else :
204- ref_ids .add (vuln [" value" ])
205+ ref_ids .add (vuln [' value' ])
205206 for cve_id in cve_ids :
206207 adv_list .append (
207208 Advisory (
@@ -237,13 +238,13 @@ def load_to_api(self, pkg_name: str) -> None:
237238 if pkg_name in self .cache :
238239 return
239240
240- artifact_comps = pkg_name .split (":" )
241+ artifact_comps = pkg_name .split (':' )
241242 endpoint = self .artifact_url (artifact_comps )
242243 resp = requests .get (endpoint ).content
243244
244245 try :
245246
246- xml_resp = ET .ElementTree (ET .fromstring (resp .decode (" utf-8" )))
247+ xml_resp = ET .ElementTree (ET .fromstring (resp .decode (' utf-8' )))
247248 self .cache [pkg_name ] = self .extract_versions (xml_resp )
248249
249250 except ET .ParseError :
@@ -252,10 +253,10 @@ def load_to_api(self, pkg_name: str) -> None:
252253 @staticmethod
253254 def artifact_url (artifact_comps : List [str ]) -> str :
254255
255- base_url = " https://repo.maven.apache.org/maven2/{}"
256+ base_url = ' https://repo.maven.apache.org/maven2/{}'
256257 group_id , artifact_id = artifact_comps
257- group_url = group_id .replace ("." , "/" )
258- suffix = group_url + "/" + artifact_id + "/" + " maven-metadata.xml"
258+ group_url = group_id .replace ('.' , '/' )
259+ suffix = group_url + '/' + artifact_id + '/' + ' maven-metadata.xml'
259260 endpoint = base_url .format (suffix )
260261
261262 return endpoint
@@ -265,7 +266,7 @@ def extract_versions(xml_response: ET.ElementTree) -> Set[str]:
265266
266267 all_versions = set ()
267268 for child in xml_response .getroot ().iter ():
268- if child .tag == " version" :
269+ if child .tag == ' version' :
269270 all_versions .add (child .text )
270271
271272 return all_versions
@@ -294,15 +295,15 @@ def load_to_api(self, pkg_name: str) -> None:
294295
295296 @staticmethod
296297 def nuget_url (pkg_name : str ) -> str :
297- base_url = " https://api.nuget.org/v3/registration5-semver1/{}/index.json"
298+ base_url = ' https://api.nuget.org/v3/registration5-semver1/{}/index.json'
298299 return base_url .format (pkg_name .lower ())
299300
300301 @staticmethod
301302 def extract_versions (json_resp : dict ) -> Set [str ]:
302303 all_versions = set ()
303304 try :
304- for entry in json_resp [" items" ][0 ][" items" ]:
305- all_versions .add (entry [" catalogEntry" ][ " version" ])
305+ for entry in json_resp [' items' ][0 ][' items' ]:
306+ all_versions .add (entry [' catalogEntry' ][ ' version' ])
306307 # json response for YamlDotNet.Signed triggers this exception
307308 except KeyError :
308309 return all_versions
@@ -326,16 +327,16 @@ def load_to_api(self, pkg_name: str) -> None:
326327
327328 @staticmethod
328329 def composer_url (pkg_name : str ) -> str :
329- vendor , name = pkg_name .split ("/" )
330- return f" https://repo.packagist.org/p/{ vendor } /{ name } .json"
330+ vendor , name = pkg_name .split ('/' )
331+ return f' https://repo.packagist.org/p/{ vendor } /{ name } .json'
331332
332333 @staticmethod
333334 def extract_versions (json_resp : dict , pkg_name : str ) -> Set [str ]:
334- all_versions = json_resp [" packages" ][pkg_name ].keys ()
335+ all_versions = json_resp [' packages' ][pkg_name ].keys ()
335336 # This filter ensures, that all_versions contains only released versions
336- all_versions = set (filter (lambda x : " dev" not in x , all_versions ))
337+ all_versions = set (filter (lambda x : ' dev' not in x , all_versions ))
337338 # See https://github.com/composer/composer/blob/44a4429978d1b3c6223277b875762b2930e83e8c/doc/articles/versions.md#tags # nopep8
338339 # for explanation of removing 'v'
339- all_versions = set (map (lambda x : x .replace ("v" , "" ), all_versions ))
340+ all_versions = set (map (lambda x : x .replace ('v' , '' ), all_versions ))
340341
341342 return all_versions
0 commit comments