-
-
Notifications
You must be signed in to change notification settings - Fork 328
Elixir Security Importer #294
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
Changes from 2 commits
38ec05f
70ac4cd
f809a89
922e34f
ee6dd62
58a253f
6e62cdf
fd25df3
0fa1bf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
| # The VulnerableCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with VulnerableCode require an acknowledgment. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| # | ||
| # When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # VulnerableCode is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
|
|
||
| import yaml | ||
| import re | ||
| import json | ||
| import requests | ||
|
tushar912 marked this conversation as resolved.
Outdated
|
||
| from typing import Set | ||
|
tushar912 marked this conversation as resolved.
Outdated
|
||
| from typing import List | ||
| from dephell_specifier import RangeSpecifier | ||
| from packageurl import PackageURL | ||
|
|
||
| from vulnerabilities.data_source import GitDataSource | ||
| from vulnerabilities.data_source import GitDataSourceConfiguration | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import is not required |
||
| from vulnerabilities.data_source import Advisory | ||
| from vulnerabilities.data_source import Reference | ||
|
|
||
|
|
||
| class ElixirSecurityDataSource(GitDataSource): | ||
| def __enter__(self): | ||
| super(ElixirSecurityDataSource, self).__enter__() | ||
|
|
||
| if not getattr(self, "_added_files", None): | ||
| self._added_files, self._updated_files = self.file_changes( | ||
| recursive=True, file_ext="yml", subdir="./packages" | ||
| ) | ||
|
|
||
| def updated_advisories(self) -> Set[Advisory]: | ||
| files = self._updated_files | ||
| advisories = [] | ||
| for f in files: | ||
| processed_data = self.process_file(f) | ||
| if processed_data: | ||
| advisories.append(processed_data) | ||
| return self.batch_advisories(advisories) | ||
|
|
||
| def added_advisories(self) -> Set[Advisory]: | ||
| files = self._added_files | ||
| advisories = [] | ||
| for f in files: | ||
| processed_data = self.process_file(f) | ||
| if processed_data: | ||
| advisories.append(processed_data) | ||
| return self.batch_advisories(advisories) | ||
|
|
||
| @staticmethod | ||
| def generate_all_version_list(pkg_name): | ||
|
tushar912 marked this conversation as resolved.
Outdated
|
||
| resp = requests.get(f"https://hex.pm/api/packages/{pkg_name}") | ||
| resp = resp.content | ||
| json_resp = json.loads(resp) | ||
|
tushar912 marked this conversation as resolved.
Outdated
|
||
| version_list = [] | ||
| for release in json_resp["releases"]: | ||
| version_list.append(release["version"]) | ||
| return version_list | ||
|
|
||
| def get_pkg_from_range(self, version_list, pkg_name): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function says
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
| pkg_versions = [] | ||
| all_version_list = self.generate_all_version_list(pkg_name) | ||
| if version_list is None: | ||
|
sbs2001 marked this conversation as resolved.
Outdated
sbs2001 marked this conversation as resolved.
Outdated
|
||
| return | ||
|
sbs2001 marked this conversation as resolved.
Outdated
|
||
| version_ranges = {RangeSpecifier(r) for r in version_list} | ||
| for version in all_version_list: | ||
| if any([version in v for v in version_ranges]): | ||
| pkg_versions.append(version) | ||
| return pkg_versions | ||
|
|
||
| def process_file(self, path): | ||
| with open(path) as f: | ||
|
tushar912 marked this conversation as resolved.
Outdated
|
||
| yaml_file = yaml.safe_load(f) | ||
| pkg_name = yaml_file["package"] | ||
| safe_pkg_versions = [] | ||
| if yaml_file.get("unaffected_versions"): | ||
| safe_pkg_versions = self.get_pkg_from_range( | ||
| yaml_file["patched_versions"] + yaml_file["unaffected_versions"], | ||
| pkg_name, | ||
| ) | ||
| else: | ||
| safe_pkg_versions = self.get_pkg_from_range( | ||
| yaml_file["patched_versions"], pkg_name | ||
| ) | ||
| cve_id = yaml_file["cve"] | ||
|
tushar912 marked this conversation as resolved.
Outdated
|
||
| safe_purls = [] | ||
| if safe_pkg_versions is not None: | ||
| safe_purls = { | ||
| PackageURL(name=pkg_name, type="hex", version=version) | ||
| for version in safe_pkg_versions | ||
| } | ||
|
|
||
| vuln_reference = [ | ||
|
sbs2001 marked this conversation as resolved.
Outdated
|
||
| Reference( | ||
| url=yaml_file["link"], | ||
| ) | ||
| ] | ||
|
|
||
| return Advisory( | ||
| summary=yaml_file["description"], | ||
| impacted_package_urls=[], | ||
| resolved_package_urls=safe_purls, | ||
| cve_id=cve_id, | ||
| vuln_references=vuln_reference, | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| id: 2aae6e3a-24a3-4d5f-86ff-b964eaf7c6d1 | ||
| package: coherence | ||
| disclosure_date: 2017-08-02 | ||
| cve: 2018-20301 | ||
| link: https://github.com/smpallen99/coherence/issues/270 | ||
| title: | | ||
| Permissive parameters and privilege escalation | ||
| description: | | ||
| The Coherence library has "Mass Assignment"-like vulnerabilities. | ||
| patched_versions: | ||
| - ">= 0.5.2" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
| # The VulnerableCode software is licensed under the Apache License version 2.0. | ||
| # Data generated with VulnerableCode require an acknowledgment. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| # | ||
| # When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
| # derivative work, you must accompany this data with the following acknowledgment: | ||
| # | ||
| # Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # VulnerableCode is a free software code scanning tool from nexB Inc. and others. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are trying correct the old headers by replacing |
||
| # Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
|
|
||
| import os | ||
| from unittest import TestCase | ||
| from collections import OrderedDict | ||
|
|
||
| from vulnerabilities.data_source import Reference | ||
|
tushar912 marked this conversation as resolved.
Outdated
|
||
| from packageurl import PackageURL | ||
|
|
||
| from vulnerabilities.importers.elixir_security import ElixirSecurityDataSource | ||
| from vulnerabilities.data_source import Advisory | ||
|
sbs2001 marked this conversation as resolved.
Outdated
|
||
|
|
||
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
|
|
||
| class TestElixirSecurityDataSource(TestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| data_source_cfg = { | ||
| "repository_url": "https://test.net", | ||
| } | ||
| cls.data_src = ElixirSecurityDataSource(1, config=data_source_cfg) | ||
|
|
||
| def test_generate_all_version_list(self): | ||
|
sbs2001 marked this conversation as resolved.
Outdated
|
||
| package = "coherence" | ||
| actual_list = self.data_src.generate_all_version_list(package) | ||
| expected_list = [ | ||
| "0.5.2", | ||
| "0.5.1", | ||
| "0.5.0", | ||
| "0.4.0", | ||
| "0.3.1", | ||
| "0.3.0", | ||
| "0.2.0", | ||
| "0.1.3", | ||
| "0.1.2", | ||
| "0.1.1", | ||
| "0.1.0", | ||
| ] | ||
| assert actual_list == expected_list | ||
|
|
||
| def test_process_file(self): | ||
|
|
||
| path = os.path.join(BASE_DIR, "test_data/elixir_security/test_file.yml") | ||
| expected_data = Advisory( | ||
| summary=( | ||
| 'The Coherence library has "Mass Assignment"-like vulnerabilities.\n' | ||
| ), | ||
| impacted_package_urls=[], | ||
| resolved_package_urls={ | ||
| PackageURL( | ||
| type="hex", | ||
| name="coherence", | ||
| version="0.5.2", | ||
| ), | ||
| }, | ||
| vuln_references=[ | ||
| Reference(url="https://github.com/smpallen99/coherence/issues/270") | ||
| ], | ||
| cve_id="2018-20301", | ||
| ) | ||
|
|
||
| found_data = self.data_src.process_file(path) | ||
|
|
||
| assert expected_data == found_data | ||
Uh oh!
There was an error while loading. Please reload this page.