-
-
Notifications
You must be signed in to change notification settings - Fork 328
Add ruby gems advisory importer #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,33 @@ | ||
| setuptools==36.5.0 | ||
|
|
||
| attrs==19.3.0 | ||
| beautifulsoup4==4.7.1 | ||
| lxml==4.3.3 | ||
| django==2.2.8 | ||
| djangorestframework==3.9.2 | ||
| dephell-specifier==0.2.1 | ||
| dj-database-url==0.4.2 | ||
| Django==2.2.8 | ||
| django-filter==2.2.0 | ||
| djangorestframework==3.9.2 | ||
| gunicorn==19.7.1 | ||
| importlib-metadata==1.3.0 | ||
| lxml==4.3.3 | ||
| more-itertools==8.0.2 | ||
| packageurl-python==0.8.7 | ||
| semantic-version==2.8.2 | ||
|
|
||
| # Tests | ||
| pytest==5.3.2 | ||
| pytest-django==3.7.0 | ||
| packaging==19.2 | ||
| pluggy==0.13.1 | ||
| psycopg2==2.8.4 | ||
| py==1.8.0 | ||
| pycodestyle==2.5.0 | ||
|
|
||
| # Deployment | ||
| gunicorn==19.7.1 | ||
| pyparsing==2.4.5 | ||
| pytest==5.3.2 | ||
| pytest-dependency==0.4.0 | ||
| pytest-django==3.7.0 | ||
| pytest-mock==1.13.0 | ||
| pytz==2019.3 | ||
| PyYAML==5.3 | ||
| saneyaml==0.4 | ||
| semantic-version==2.8.2 | ||
| six==1.13.0 | ||
| soupsieve==1.9.5 | ||
| sqlparse==0.3.0 | ||
| tqdm==4.41.1 | ||
| wcwidth==0.1.7 | ||
| whitenoise==5.0.1 | ||
| dj_database_url==0.4.2 | ||
| psycopg2==2.8.4 | ||
| zipp==0.6.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import os | ||
| import urllib.request | ||
| from urllib.error import HTTPError | ||
| from zipfile import ZipFile | ||
| from io import BytesIO | ||
| import saneyaml | ||
| from dephell_specifier import RangeSpecifier | ||
| from urllib.request import urlopen | ||
|
|
||
| RUBYSEC_DB_URL = 'https://github.com/rubysec/ruby-advisory-db/archive/master.zip' | ||
|
|
||
|
|
||
| def rubygem_advisories(url, prefix='ruby-advisory-db-master/gems/'): | ||
| with urlopen(url) as response: | ||
| with ZipFile(BytesIO(response.read())) as zf: | ||
| for path in zf.namelist(): | ||
| if path.startswith(prefix) and path.endswith('.yml'): | ||
| yield saneyaml.load(zf.open(path)) | ||
|
|
||
|
|
||
| def get_all_versions_of_package(package_name): | ||
| url_to_load = 'https://rubygems.org/api/v1/versions/' + package_name + '.yaml' | ||
| try: | ||
| page = urllib.request.urlopen(url_to_load) | ||
| package_history = saneyaml.load(page) | ||
| except HTTPError: | ||
| return [] | ||
| for version in package_history: | ||
| yield version['number'] | ||
|
|
||
|
|
||
| def get_patched_range(spec_list): | ||
| spec_list = [string.replace(' ', '') for string in spec_list] | ||
| for spec in spec_list: | ||
| if 'rc' in spec: | ||
| continue | ||
| yield RangeSpecifier(spec) | ||
|
|
||
|
|
||
| def import_vulnerabilities(): | ||
| vulnerability_package_dicts = [] | ||
| for vulnerability in rubygem_advisories(RUBYSEC_DB_URL): | ||
|
|
||
| package_name = vulnerability.get( | ||
| 'gem') | ||
|
|
||
| if not package_name: | ||
| continue | ||
|
|
||
| if 'cve' in vulnerability: | ||
| vulnerability_id = 'CVE-{}'.format(vulnerability['cve']) | ||
| else: | ||
| continue | ||
|
|
||
| advisory_url = vulnerability.get('url') | ||
| patched_version_ranges = list( | ||
| get_patched_range( | ||
| vulnerability.get('patched_versions', []))) | ||
| all_versions = set(get_all_versions_of_package(package_name)) | ||
| unaffected_versions = set() | ||
|
|
||
| if patched_version_ranges: | ||
| for version in all_versions: | ||
| for spec in patched_version_ranges: | ||
| if version in spec: | ||
| unaffected_versions.add(version) | ||
| break | ||
|
|
||
| affected_versions = all_versions - unaffected_versions | ||
| vulnerability_package_dicts.append({ | ||
| 'package_name': package_name, | ||
| 'cve_id': vulnerability_id, | ||
| 'fixed_versions': unaffected_versions, | ||
| 'affected_versions': affected_versions, | ||
| 'advisory': advisory_url | ||
| }) | ||
| return vulnerability_package_dicts |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.