Skip to content

Commit 4c5830d

Browse files
committed
Avoid duplicate affected_packages
Signed-off-by: ziad hany <[email protected]>
1 parent 7a58010 commit 4c5830d

File tree

4 files changed

+22
-70
lines changed

4 files changed

+22
-70
lines changed

vulnerabilities/pipelines/v2_importers/gentoo_importer.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def process_file(self, file):
6464
xml_root = ET.parse(file).getroot()
6565
id = xml_root.attrib.get("id")
6666
glsa = "GLSA-" + id
67-
6867
vuln_references = [
6968
ReferenceV2(
7069
reference_id=glsa,
@@ -82,7 +81,21 @@ def process_file(self, file):
8281
summary = child.text
8382

8483
if child.tag == "affected":
85-
affected_packages = list(affected_and_safe_purls(child))
84+
affected_packages = []
85+
seen_packages = set()
86+
87+
for purl, constraint in get_affected_and_safe_purls(child):
88+
signature = (purl.to_string(), str(constraint))
89+
90+
if signature not in seen_packages:
91+
seen_packages.add(signature)
92+
93+
affected_package = AffectedPackageV2(
94+
package=purl,
95+
affected_version_range=EbuildVersionRange(constraints=[constraint]),
96+
fixed_version_range=None,
97+
)
98+
affected_packages.append(affected_package)
8699

87100
if child.tag == "impact":
88101
severity_value = child.attrib.get("type")
@@ -121,10 +134,7 @@ def cves_from_reference(reference):
121134
return cves
122135

123136

124-
def _yield_packages(pkg_name, pkg_ns, constraints, invert):
125-
"""
126-
Generate AffectedPackageV2 objects for a list of constraints.
127-
"""
137+
def extract_purls_and_constraints(pkg_name, pkg_ns, constraints, invert):
128138
for comparator, version, slot_value in constraints:
129139
qualifiers = {"slot": slot_value} if slot_value else {}
130140
purl = PackageURL(type="ebuild", name=pkg_name, namespace=pkg_ns, qualifiers=qualifiers)
@@ -135,16 +145,12 @@ def _yield_packages(pkg_name, pkg_ns, constraints, invert):
135145
if invert:
136146
constraint = constraint.invert()
137147

138-
yield AffectedPackageV2(
139-
package=purl,
140-
affected_version_range=EbuildVersionRange(constraints=[constraint]),
141-
fixed_version_range=None,
142-
)
148+
yield purl, constraint
143149
except InvalidVersion as e:
144150
logger.error(f"InvalidVersion constraints version: {version} error:{e}")
145151

146152

147-
def affected_and_safe_purls(affected_elem):
153+
def get_affected_and_safe_purls(affected_elem):
148154
for pkg in affected_elem:
149155
name = pkg.attrib.get("name")
150156
if not name:
@@ -153,8 +159,10 @@ def affected_and_safe_purls(affected_elem):
153159

154160
safe_constraints, affected_constraints = get_safe_and_affected_constraints(pkg)
155161

156-
yield from _yield_packages(pkg_name, pkg_ns, affected_constraints, invert=False)
157-
yield from _yield_packages(pkg_name, pkg_ns, safe_constraints, invert=True)
162+
yield from extract_purls_and_constraints(
163+
pkg_name, pkg_ns, affected_constraints, invert=False
164+
)
165+
yield from extract_purls_and_constraints(pkg_name, pkg_ns, safe_constraints, invert=True)
158166

159167

160168
def get_safe_and_affected_constraints(pkg):

vulnerabilities/tests/test_data/gentoo_v2/glsa-201709-09-expected.json

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@
2020
"introduced_by_commit_patches": [],
2121
"fixed_by_commit_patches": []
2222
},
23-
{
24-
"package": {
25-
"type": "ebuild",
26-
"namespace": "dev-vcs",
27-
"name": "subversion",
28-
"version": "",
29-
"qualifiers": "",
30-
"subpath": ""
31-
},
32-
"affected_version_range": "vers:ebuild/<1.9.7",
33-
"fixed_version_range": null,
34-
"introduced_by_commit_patches": [],
35-
"fixed_by_commit_patches": []
36-
},
3723
{
3824
"package": {
3925
"type": "ebuild",

vulnerabilities/tests/test_data/gentoo_v2/glsa-202511-02-expected.json

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,6 @@
4444
"introduced_by_commit_patches": [],
4545
"fixed_by_commit_patches": []
4646
},
47-
{
48-
"package": {
49-
"type": "ebuild",
50-
"namespace": "net-libs",
51-
"name": "webkit-gtk",
52-
"version": "",
53-
"qualifiers": "slot=4.1",
54-
"subpath": ""
55-
},
56-
"affected_version_range": "vers:ebuild/<2.48.5",
57-
"fixed_version_range": null,
58-
"introduced_by_commit_patches": [],
59-
"fixed_by_commit_patches": []
60-
},
61-
{
62-
"package": {
63-
"type": "ebuild",
64-
"namespace": "net-libs",
65-
"name": "webkit-gtk",
66-
"version": "",
67-
"qualifiers": "slot=6",
68-
"subpath": ""
69-
},
70-
"affected_version_range": "vers:ebuild/<2.48.5",
71-
"fixed_version_range": null,
72-
"introduced_by_commit_patches": [],
73-
"fixed_by_commit_patches": []
74-
},
7547
{
7648
"package": {
7749
"type": "ebuild",

vulnerabilities/tests/test_data/gentoo_v2/glsa-202512-01-expected.json

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@
44
"aliases": [],
55
"summary": "A vulnerability has been discovered in GnuPG, which can lead to arbitrary code execution.",
66
"affected_packages": [
7-
{
8-
"package": {
9-
"type": "ebuild",
10-
"namespace": "app-crypt",
11-
"name": "gnupg",
12-
"version": "",
13-
"qualifiers": "",
14-
"subpath": ""
15-
},
16-
"affected_version_range": "vers:ebuild/<2.5.14",
17-
"fixed_version_range": null,
18-
"introduced_by_commit_patches": [],
19-
"fixed_by_commit_patches": []
20-
},
217
{
228
"package": {
239
"type": "ebuild",

0 commit comments

Comments
 (0)