Skip to content

Commit ff833e4

Browse files
committed
Improve naming and docs
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent efeea4f commit ff833e4

39 files changed

Lines changed: 198 additions & 216 deletions

vulnerabilities/data_source.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from vulnerabilities.severity_systems import ScoringSystem
4848
from vulnerabilities.helpers import is_cve
4949
from vulnerabilities.helpers import nearest_patched_package
50-
from vulnerabilities.helpers import AffectedPackageWithPatchedPackage
50+
from vulnerabilities.helpers import AffectedPackage
5151

5252
logger = logging.getLogger(__name__)
5353

@@ -88,9 +88,7 @@ class Advisory:
8888

8989
summary: str
9090
vulnerability_id: Optional[str] = None
91-
affected_packages_with_patched_package: List[
92-
AffectedPackageWithPatchedPackage
93-
] = dataclasses.field(default_factory=list)
91+
affected_packages: List[AffectedPackage] = dataclasses.field(default_factory=list)
9492
references: List[Reference] = dataclasses.field(default_factory=list)
9593

9694
def __post_init__(self):
@@ -107,9 +105,7 @@ def normalized(self):
107105
return Advisory(
108106
summary=self.summary,
109107
vulnerability_id=self.vulnerability_id,
110-
affected_packages_with_patched_package=sorted(
111-
self.affected_packages_with_patched_package
112-
),
108+
affected_packages=sorted(self.affected_packages),
113109
references=references,
114110
)
115111

@@ -533,7 +529,7 @@ def get_data_from_xml_doc(self, xml_doc: ET.ElementTree, pkg_metadata={}) -> Lis
533529
vuln_id = definition_data["vuln_id"]
534530
description = definition_data["description"]
535531
references = [Reference(url=url) for url in definition_data["reference_urls"]]
536-
affected_packages_with_patched_package = []
532+
affected_packages = []
537533
for test_data in definition_data["test_data"]:
538534
for package_name in test_data["package_list"]:
539535
if package_name and len(package_name) >= 50:
@@ -569,14 +565,14 @@ def get_data_from_xml_doc(self, xml_doc: ET.ElementTree, pkg_metadata={}) -> Lis
569565
else:
570566
safe_purls.append(purl)
571567

572-
affected_packages_with_patched_package.extend(
568+
affected_packages.extend(
573569
nearest_patched_package(affected_purls, safe_purls),
574570
)
575571

576572
all_adv.append(
577573
Advisory(
578574
summary=description,
579-
affected_packages_with_patched_package=affected_packages_with_patched_package,
575+
affected_packages=affected_packages,
580576
vulnerability_id=vuln_id,
581577
references=references,
582578
)

vulnerabilities/helpers.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import json
2626
import re
2727
from typing import Optional
28+
from typing import List
2829

2930
import requests
3031
import toml
@@ -39,7 +40,7 @@
3940

4041

4142
@dataclasses.dataclass(order=True, frozen=True)
42-
class AffectedPackageWithPatchedPackage:
43+
class AffectedPackage:
4344
vulnerable_package: PackageURL
4445
patched_package: Optional[PackageURL] = None
4546

@@ -118,11 +119,22 @@ def requests_with_5xx_retry(max_retries=5, backoff_factor=0.5):
118119
return session
119120

120121

121-
def nearest_patched_package(vulnerable_packages, resolved_packages):
122-
# This class is used to get around bisect module's lack of supplying custom
123-
# compartor. Get rid of this once we use python 3.10 which supports this.
124-
# See https://github.com/python/cpython/pull/20556
122+
def nearest_patched_package(
123+
vulnerable_packages: List[PackageURL], resolved_packages: List[PackageURL]
124+
) -> List[AffectedPackage]:
125+
"""
126+
Parameters:
127+
:vulnerable_packages(list)
128+
:resolved_packages(list)
129+
"""
130+
125131
class PackageURLWithVersionComparator:
132+
"""
133+
This class is used to get around bisect module's lack of supplying custom
134+
compartor. Get rid of this once we use python 3.10 which supports this.
135+
See https://github.com/python/cpython/pull/20556
136+
"""
137+
126138
def __init__(self, package):
127139
self.package = package
128140
self.version_object = version_class_by_package_type[package.type](package.version)
@@ -150,7 +162,7 @@ def __lt__(self, other):
150162
patched_package = resolved_packages[patched_package_index].package
151163

152164
affected_package_with_patched_package_objects.append(
153-
AffectedPackageWithPatchedPackage(
165+
AffectedPackage(
154166
vulnerable_package=vulnerable_package.package, patched_package=patched_package
155167
)
156168
)

vulnerabilities/import_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def process_advisories(data_source: DataSource) -> None:
133133
defaults={"value": str(score.value)},
134134
)
135135

136-
for aff_pkg_with_patched_pkg in advisory.affected_packages_with_patched_package:
136+
for aff_pkg_with_patched_pkg in advisory.affected_packages:
137137
vulnerable_package, _ = _get_or_create_package(
138138
aff_pkg_with_patched_pkg.vulnerable_package
139139
)

vulnerabilities/importers/apache_kafka.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ def to_advisory(self, advisory_page):
9696
Advisory(
9797
vulnerability_id=cve_id,
9898
summary=cve_description_paragraph.text,
99-
affected_packages_with_patched_package=nearest_patched_package(
100-
affected_packages, fixed_packages
101-
),
99+
affected_packages=nearest_patched_package(affected_packages, fixed_packages),
102100
references=[
103101
Reference(url=ASF_PAGE_URL),
104102
Reference(

vulnerabilities/importers/apache_tomcat.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ def to_advisories(self, apache_tomcat_advisory_html):
116116
advisories.append(
117117
Advisory(
118118
summary="",
119-
affected_packages_with_patched_package=nearest_patched_package(
120-
affected_packages, fixed_package
121-
),
119+
affected_packages=nearest_patched_package(affected_packages, fixed_package),
122120
vulnerability_id=cve_id,
123121
references=references,
124122
)

vulnerabilities/importers/archlinux.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def _parse(self, record) -> List[Advisory]:
8888
advisories = []
8989

9090
for cve_id in record["issues"]:
91-
affected_packages_with_patched_package = []
91+
affected_packages = []
9292
for name in record["packages"]:
9393
impacted_purls, resolved_purls = [], []
9494
impacted_purls.append(
@@ -109,9 +109,7 @@ def _parse(self, record) -> List[Advisory]:
109109
version=record["fixed"],
110110
)
111111
)
112-
affected_packages_with_patched_package.extend(
113-
nearest_patched_package(impacted_purls, resolved_purls)
114-
)
112+
affected_packages.extend(nearest_patched_package(impacted_purls, resolved_purls))
115113

116114
references = []
117115
references.append(
@@ -138,7 +136,7 @@ def _parse(self, record) -> List[Advisory]:
138136
Advisory(
139137
vulnerability_id=cve_id,
140138
summary="",
141-
affected_packages_with_patched_package=affected_packages_with_patched_package,
139+
affected_packages=affected_packages,
142140
references=references,
143141
)
144142
)

vulnerabilities/importers/debian.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ def _parse(self, pkg_name: str, records: Mapping[str, Any]) -> List[Advisory]:
154154
advisories.append(
155155
Advisory(
156156
vulnerability_id=cve_id,
157-
affected_packages_with_patched_package=nearest_patched_package(
158-
impacted_purls, resolved_purls
159-
),
157+
affected_packages=nearest_patched_package(impacted_purls, resolved_purls),
160158
summary=record.get("description", ""),
161159
references=references,
162160
)

vulnerabilities/importers/elixir_security.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def process_file(self, path):
143143

144144
return Advisory(
145145
summary=yaml_file["description"],
146-
affected_packages_with_patched_package=nearest_patched_package(vuln_purls, safe_purls),
146+
affected_packages=nearest_patched_package(vuln_purls, safe_purls),
147147
vulnerability_id=cve_id,
148148
references=references,
149149
)

vulnerabilities/importers/gentoo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def process_file(self, file):
8282
advisory = Advisory(
8383
vulnerability_id=cve,
8484
summary=xml_data["description"],
85-
affected_packages_with_patched_package=nearest_patched_package(
85+
affected_packages=nearest_patched_package(
8686
xml_data["affected_purls"], xml_data["unaffected_purls"]
8787
),
8888
references=vuln_reference,

vulnerabilities/importers/github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def process_response(self) -> List[Advisory]:
245245
Advisory(
246246
vulnerability_id=cve_id,
247247
summary=vuln_desc,
248-
affected_packages_with_patched_package=nearest_patched_package(
248+
affected_packages=nearest_patched_package(
249249
affected_purls, unaffected_purls
250250
),
251251
references=references,

0 commit comments

Comments
 (0)