Skip to content

Commit e8f0a57

Browse files
committed
Fix tests
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 2bb88fc commit e8f0a57

26 files changed

Lines changed: 615 additions & 273 deletions

vulnerabilities/importers/apache_httpd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def to_advisory(self, data):
106106
fixed_packages.extend(
107107
[
108108
PackageURL(type="apache", name="httpd", version=version)
109-
for version in self.version_api.get("apache/httpd")
109+
for version in self.version_api.get("apache/httpd")["valid"]
110110
if MavenVersion(version) in version_range
111111
]
112112
)
@@ -115,7 +115,7 @@ def to_advisory(self, data):
115115
affected_packages.extend(
116116
[
117117
PackageURL(type="apache", name="httpd", version=version)
118-
for version in self.version_api.get("apache/httpd")
118+
for version in self.version_api.get("apache/httpd")["valid"]
119119
if MavenVersion(version) in version_range
120120
]
121121
)

vulnerabilities/importers/apache_kafka.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def to_advisory(self, advisory_page):
7272

7373
fixed_packages = [
7474
PackageURL(type="apache", name="kafka", version=version)
75-
for version in self.version_api.get("apache/kafka")
75+
for version in self.version_api.get("apache/kafka")["valid"]
7676
if any(
7777
[
7878
MavenVersion(version) in version_range
@@ -83,7 +83,7 @@ def to_advisory(self, advisory_page):
8383

8484
affected_packages = [
8585
PackageURL(type="apache", name="kafka", version=version)
86-
for version in self.version_api.get("apache/kafka")
86+
for version in self.version_api.get("apache/kafka")["valid"]
8787
if any(
8888
[
8989
MavenVersion(version) in version_range

vulnerabilities/importers/apache_tomcat.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def updated_advisories(self):
6262
return self.batch_advisories(advisories)
6363

6464
def fetch_pages(self):
65-
tomcat_major_versions = {i[0] for i in self.version_api.get("org.apache.tomcat:tomcat")}
65+
tomcat_major_versions = {
66+
i[0] for i in self.version_api.get("org.apache.tomcat:tomcat")["valid"]
67+
}
6668
for version in tomcat_major_versions:
6769
page_url = self.base_url.format(version)
6870
if create_etag(self, page_url, "ETag"):
@@ -102,7 +104,7 @@ def to_advisories(self, apache_tomcat_advisory_html):
102104
PackageURL(
103105
type="maven", namespace="apache", name="tomcat", version=version
104106
)
105-
for version in self.version_api.get("org.apache.tomcat:tomcat")
107+
for version in self.version_api.get("org.apache.tomcat:tomcat")["valid"]
106108
if MavenVersion(version) in version_range
107109
]
108110
)

vulnerabilities/importers/nginx.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ def extract_vuln_pkgs(self, vuln_info):
171171
)
172172
)
173173

174-
valid_versions = find_valid_versions(self.version_api.get("nginx/nginx"), version_ranges)
174+
valid_versions = find_valid_versions(
175+
self.version_api.get("nginx/nginx")["valid"], version_ranges
176+
)
175177
qualifiers = {}
176178
if windows_only:
177179
qualifiers["os"] = "windows"

vulnerabilities/importers/npm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def process_file(self, file) -> List[Advisory]:
8888
publish_date = parse(record["updated_at"])
8989
publish_date.replace(tzinfo=pytz.UTC)
9090

91-
all_versions = self.versions.get(package_name, until=publish_date)
91+
all_versions = self.versions.get(package_name, until=publish_date)["valid"]
9292
aff_range = record.get("vulnerable_versions")
9393
if not aff_range:
9494
aff_range = ""

vulnerabilities/importers/project_kb_msr2019.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222

2323
import csv
2424
import dataclasses
25-
import re
2625
import urllib.request
2726

2827
# Reading CSV file from a url using `requests` is bit too complicated.
2928
# Use `urllib.request` for that purpose.
30-
from packageurl import PackageURL
3129

3230

3331
from vulnerabilities.data_source import Advisory

vulnerabilities/importers/ruby.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import asyncio
2424
from typing import Set
2525
from typing import List
26+
from dateutil.parser import parse
27+
from pytz import UTC
2628

2729
from packageurl import PackageURL
2830
from univers.version_specifier import VersionSpecifier
@@ -90,6 +92,7 @@ def process_file(self, path) -> List[Advisory]:
9092
else:
9193
return
9294

95+
publish_time = parse(record["date"]).replace(tzinfo=UTC)
9396
safe_version_ranges = record.get("patched_versions", [])
9497
# this case happens when the advisory contain only 'patched_versions' field
9598
# and it has value None(i.e it is empty :( ).
@@ -100,7 +103,10 @@ def process_file(self, path) -> List[Advisory]:
100103

101104
if not getattr(self, "pkg_manager_api", None):
102105
self.pkg_manager_api = RubyVersionAPI()
103-
all_vers = self.pkg_manager_api.get(package_name)
106+
all_vers = self.pkg_manager_api.get(package_name, until=publish_time)["valid"]
107+
print(
108+
f"Ignored {len(self.pkg_manager_api.get(package_name,until=publish_time)['new'])} versions"
109+
)
104110
safe_versions, affected_versions = self.categorize_versions(all_vers, safe_version_ranges)
105111

106112
impacted_purls = [

vulnerabilities/importers/safety_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def updated_advisories(self) -> Set[Advisory]:
111111
logger.error(e)
112112
continue
113113

114-
all_package_versions = self.versions.get(package_name)
114+
all_package_versions = self.versions.get(package_name)["valid"]
115115
if not len(all_package_versions):
116116
# PyPi does not have data about this package, we skip these
117117
continue
Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
1-
# # Copyright (c) 2017 nexB Inc. and others. All rights reserved.
2-
# # http://nexb.com and https://github.com/nexB/vulnerablecode/
3-
# # The VulnerableCode software is licensed under the Apache License version 2.0.
4-
# # Data generated with VulnerableCode require an acknowledgment.
5-
# #
6-
# # You may not use this software except in compliance with the License.
7-
# # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
8-
# # 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
10-
# # CONDITIONS OF ANY KIND, either express or implied. See the License for the
11-
# # specific language governing permissions and limitations under the License.
12-
# #
13-
# # When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
14-
# # derivative work, you must accompany this data with the following acknowledgment:
15-
# #
16-
# # Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
17-
# # OR CONDITIONS OF ANY KIND, either express or implied. No content created from
18-
# # VulnerableCode should be considered or used as legal advice. Consult an Attorney
19-
# # for any legal advice.
20-
# # VulnerableCode is a free software code scanning tool from nexB Inc. and others.
21-
# # Visit https://github.com/nexB/vulnerablecode/ for support and download.
22-
# import dataclasses
1+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
2+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
3+
# The VulnerableCode software is licensed under the Apache License version 2.0.
4+
# Data generated with VulnerableCode require an acknowledgment.
5+
#
6+
# You may not use this software except in compliance with the License.
7+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
8+
# 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
10+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
# specific language governing permissions and limitations under the License.
12+
#
13+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
14+
# derivative work, you must accompany this data with the following acknowledgment:
15+
#
16+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
17+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
18+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
19+
# for any legal advice.
20+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
22+
import dataclasses
2323

24-
# import requests
25-
# import saneyaml
26-
# from bs4 import BeautifulSoup
27-
# from packageurl import PackageURL
24+
import requests
25+
import saneyaml
26+
from bs4 import BeautifulSoup
27+
from packageurl import PackageURL
2828

29-
# from vulnerabilities.data_source import Advisory
30-
# from vulnerabilities.data_source import DataSource
31-
# from vulnerabilities.data_source import DataSourceConfiguration
32-
# from vulnerabilities.helpers import create_etag
29+
from vulnerabilities.data_source import Advisory
30+
from vulnerabilities.data_source import DataSource
31+
from vulnerabilities.data_source import DataSourceConfiguration
32+
from vulnerabilities.helpers import create_etag
3333

3434

35-
# @dataclasses.dataclass
36-
# class SUSEBackportsConfiguration(DataSourceConfiguration):
37-
# url: str
38-
# etags: dict
35+
@dataclasses.dataclass
36+
class SUSEBackportsConfiguration(DataSourceConfiguration):
37+
url: str
38+
etags: dict
3939

4040

41-
# class SUSEBackportsDataSource(DataSource):
41+
class SUSEBackportsDataSource(DataSource):
4242

43-
# CONFIG_CLASS = SUSEBackportsConfiguration
43+
CONFIG_CLASS = SUSEBackportsConfiguration
4444

45-
# @staticmethod
46-
# def get_all_urls_of_backports(url):
47-
# r = requests.get(url)
48-
# soup = BeautifulSoup(r.content, "lxml")
49-
# for a_tag in soup.find_all("a", href=True):
50-
# if a_tag["href"].endswith(".yaml") and a_tag["href"].startswith("backports"):
51-
# yield url + a_tag["href"]
45+
@staticmethod
46+
def get_all_urls_of_backports(url):
47+
r = requests.get(url)
48+
soup = BeautifulSoup(r.content, "lxml")
49+
for a_tag in soup.find_all("a", href=True):
50+
if a_tag["href"].endswith(".yaml") and a_tag["href"].startswith("backports"):
51+
yield url + a_tag["href"]
5252

53-
# def updated_advisories(self):
54-
# advisories = []
55-
# all_urls = self.get_all_urls_of_backports(self.config.url)
56-
# for url in all_urls:
57-
# if not create_etag(data_src=self, url=url, etag_key="ETag"):
58-
# continue
59-
# advisories.extend(self.process_file(self._fetch_yaml(url)))
60-
# return self.batch_advisories(advisories)
53+
def updated_advisories(self):
54+
advisories = []
55+
all_urls = self.get_all_urls_of_backports(self.config.url)
56+
for url in all_urls:
57+
if not create_etag(data_src=self, url=url, etag_key="ETag"):
58+
continue
59+
advisories.extend(self.process_file(self._fetch_yaml(url)))
60+
return self.batch_advisories(advisories)
6161

62-
# def _fetch_yaml(self, url):
62+
def _fetch_yaml(self, url):
6363

64-
# try:
65-
# resp = requests.get(url)
66-
# resp.raise_for_status()
67-
# return saneyaml.load(resp.content)
64+
try:
65+
resp = requests.get(url)
66+
resp.raise_for_status()
67+
return saneyaml.load(resp.content)
6868

69-
# except requests.HTTPError:
70-
# return {}
69+
except requests.HTTPError:
70+
return {}
7171

72-
# @staticmethod
73-
# def process_file(yaml_file):
74-
# advisories = []
75-
# try:
76-
# for pkg in yaml_file[0]["packages"]:
77-
# for version in yaml_file[0]["packages"][pkg]["fixed"]:
78-
# for vuln in yaml_file[0]["packages"][pkg]["fixed"][version]:
79-
# # yaml_file specific data can be added
80-
# purl = [
81-
# PackageURL(name=pkg, type="rpm", version=version, namespace="opensuse")
82-
# ]
83-
# advisories.append(
84-
# Advisory(
85-
# vulnerability_id=vuln,
86-
# resolved_package_urls=purl,
87-
# summary="",
88-
# impacted_package_urls=[],
89-
# )
90-
# )
91-
# except TypeError:
92-
# # could've used pass
93-
# return advisories
72+
@staticmethod
73+
def process_file(yaml_file):
74+
advisories = []
75+
try:
76+
for pkg in yaml_file[0]["packages"]:
77+
for version in yaml_file[0]["packages"][pkg]["fixed"]:
78+
for vuln in yaml_file[0]["packages"][pkg]["fixed"][version]:
79+
# yaml_file specific data can be added
80+
purl = [
81+
PackageURL(name=pkg, type="rpm", version=version, namespace="opensuse")
82+
]
83+
advisories.append(
84+
Advisory(
85+
vulnerability_id=vuln,
86+
resolved_package_urls=purl,
87+
summary="",
88+
impacted_package_urls=[],
89+
)
90+
)
91+
except TypeError:
92+
# could've used pass
93+
return advisories
9494

95-
# return advisories
95+
return advisories

vulnerabilities/importers/ubuntu.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,11 @@
2525
import bz2
2626
import dataclasses
2727
import logging
28-
from typing import Iterable
29-
from typing import List
30-
from typing import Mapping
31-
from typing import Set
3228
import xml.etree.ElementTree as ET
33-
34-
from aiohttp import ClientSession
35-
from aiohttp.client_exceptions import ClientResponseError
3629
import requests
3730

3831
from vulnerabilities.data_source import OvalDataSource, DataSourceConfiguration
3932
from vulnerabilities.package_managers import LaunchpadVersionAPI
40-
from vulnerabilities.helpers import create_etag
4133

4234
logger = logging.getLogger(__name__)
4335

0 commit comments

Comments
 (0)