-
-
Notifications
You must be signed in to change notification settings - Fork 328
Add rust security importer #148
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
3 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 |
|---|---|---|
|
|
@@ -31,3 +31,4 @@ tqdm==4.41.1 | |
| wcwidth==0.1.7 | ||
| whitenoise==5.0.1 | ||
| zipp==0.6.0 | ||
| pytoml==0.1.21 | ||
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,93 @@ | ||
| from io import BytesIO | ||
| import json | ||
|
|
||
| from dephell_specifier import RangeSpecifier | ||
| import pytoml as toml | ||
| from urllib.request import urlopen | ||
| import urllib.request | ||
| from urllib.error import HTTPError | ||
| from zipfile import ZipFile | ||
|
|
||
|
|
||
| RUSTSEC_DB_URL = 'https://github.com/RustSec/advisory-db/archive/master.zip' | ||
|
|
||
|
|
||
| def rust_crate_advisories(url, prefix='advisory-db-master/crates/'): | ||
| with urlopen(url) as response: | ||
| with ZipFile(BytesIO(response.read())) as zf: | ||
| for path in zf.namelist(): | ||
| if path.startswith(prefix) and path.endswith('.toml'): | ||
| yield toml.load(zf.open(path)) | ||
|
|
||
|
|
||
| def all_versions_of_crate(crate): | ||
| info_url = "https://crates.io/api/v1/crates/{}".format(crate) | ||
| with urlopen(info_url) as info: | ||
| info = json.load(info) | ||
| for version_info in info['versions']: | ||
| yield version_info['num'] | ||
|
|
||
|
|
||
| def import_vulnerabilities(): | ||
|
|
||
| vulnerability_package_dicts = [] | ||
| for advisory in rust_crate_advisories(RUSTSEC_DB_URL): | ||
|
|
||
| affected_version_range_lists = list(advisory.get( | ||
| 'affected', {}).get('functions', {}).values()) | ||
| unaffected_version_range_list = advisory['advisory'].get( | ||
| 'unaffected_versions', []) | ||
| patched_version_range_list = advisory['advisory']['patched_versions'] | ||
|
|
||
| # FIXME: Make distinction between unaffected and patched packages | ||
| # Check https://github.com/nexB/vulnerablecode/issues/144 | ||
| unaffected_version_range_list += patched_version_range_list | ||
|
|
||
| have_unaffected_version_range = len(unaffected_version_range_list) != 0 | ||
| have_affected_version_range = len(affected_version_range_lists) != 0 | ||
|
|
||
| if not (have_affected_version_range or have_unaffected_version_range): | ||
| continue | ||
|
|
||
| vuln_id = advisory['advisory']['id'] | ||
| crate_name = advisory['advisory']['package'] | ||
| description = advisory['advisory']['description'] | ||
| reference = advisory['advisory'].get('url', '') | ||
|
|
||
| all_versions = set(all_versions_of_crate(crate_name)) | ||
| affected_versions = set() | ||
| unaffected_versions = set() | ||
|
|
||
| for version in all_versions: | ||
| categorised = False | ||
| for affected_version_range_list in affected_version_range_lists: | ||
| for affected_version_range in affected_version_range_list: | ||
| affected_version_range = RangeSpecifier( | ||
| affected_version_range) | ||
| if version in affected_version_range: | ||
| affected_versions.add(version) | ||
| categorised = True | ||
| break | ||
|
|
||
| if have_unaffected_version_range and not categorised: | ||
| for unaffected_version_range in unaffected_version_range_list: | ||
| if version in RangeSpecifier(unaffected_version_range): | ||
| unaffected_versions.add(version) | ||
| break | ||
|
|
||
| if not have_unaffected_version_range: | ||
| unaffected_versions = all_versions - affected_versions | ||
|
|
||
| elif not have_affected_version_range: | ||
| affected_versions = all_versions - unaffected_versions | ||
|
|
||
| vulnerability_package_dicts.append({ | ||
| 'package_name': crate_name, | ||
| 'vuln_id': vuln_id, | ||
| 'fixed_versions': unaffected_versions, | ||
| 'affected_versions': affected_versions, | ||
| 'advisory': reference, | ||
| 'description': description | ||
| }) | ||
|
|
||
| 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.