Skip to content

Commit 63a1789

Browse files
committed
Added more specific exceptions
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 42c789b commit 63a1789

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

vulnerabilities/scraper/npm.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
from dephell_specifier import RangeSpecifier
2626
from urllib.request import urlopen
2727
from urllib.error import HTTPError
28-
from itertools import count
2928

3029

3130
NPM_URL = 'https://registry.npmjs.org{}'
32-
PAGE = '/-/npm/v1/security/advisories?'
33-
31+
PAGE = '/-/npm/v1/security/advisories?page=0'
3432

3533
def get_all_versions(package_name):
3634
"""
@@ -41,8 +39,11 @@ def get_all_versions(package_name):
4139
try:
4240
with urlopen(package_url) as response:
4341
data = json.load(response)
44-
except HTTPError:
45-
return []
42+
except HTTPError as e:
43+
if e.code == 404 :
44+
return []
45+
else:
46+
raise
4647
# NPM registry has no data regarding this package, we skip these
4748
return [v for v in data.get('versions', {})]
4849

@@ -87,7 +88,7 @@ def extract_data(JSON):
8788
obj.get('vulnerable_versions', ''),
8889
obj.get('patched_versions', '')
8990
)
90-
if affected_versions == [] and fixed_versions == []:
91+
if affected_versions == fixed_versions == [] :
9192
continue
9293
# NPM registry has no data regarding this package finally we skip these
9394

@@ -108,14 +109,18 @@ def scrape_vulnerabilities():
108109
Extract JSON From NPM registry
109110
"""
110111
package_vulnerabilities = []
111-
for page_num in count():
112-
112+
nextpage = PAGE
113+
while nextpage:
113114
try:
114-
cururl = NPM_URL.format(PAGE) + 'page=' + str(page_num)
115+
cururl = NPM_URL.format(nextpage)
115116
response = json.load(urlopen(cururl))
116117
package_vulnerabilities.extend(extract_data(response))
118+
nextpage = response.get('urls', {}).get('next')
117119

118-
except HTTPError:
119-
break
120+
except HTTPError as error:
121+
if error.code == 404 :
122+
break
123+
else :
124+
raise
120125

121126
return package_vulnerabilities

0 commit comments

Comments
 (0)