Skip to content

Commit 56a3ad7

Browse files
committed
Update tests for new extract_advisories_from_page() #970
Reference: #970 Signed-off-by: John M. Horan <[email protected]>
1 parent 22389cb commit 56a3ad7

File tree

2 files changed

+74
-17
lines changed

2 files changed

+74
-17
lines changed

vulnerabilities/importers/apache_tomcat.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from vulnerabilities.importer import VulnerabilitySeverity
2626
from vulnerabilities.severity_systems import APACHE_TOMCAT
2727

28-
TRACE = True
28+
TRACE = False
2929

3030

3131
class ApacheTomcatImporter(Importer):
@@ -89,7 +89,6 @@ def debug_advisory_data(self, advisories):
8989
adv_dict = adv.to_dict()
9090
temp_advisory_to_dict_list.append(adv_dict)
9191

92-
# TODO: 2022-12-26 Monday 16:17:38. With my new ("better") approach, throws error: TypeError: Object of type Tag is not JSON serializable
9392
with open(
9493
"apache_tomcat_advisories_to_dict-02.json",
9594
"w",
@@ -133,6 +132,8 @@ def debug_advisory_data(self, advisories):
133132
for line in self.record_of_all_affected_version_strings:
134133
f.write(f"{line}\n")
135134

135+
# 2022-12-29 Thursday 13:11:16. We're in the process of refactoring this method.
136+
# See, e.g., function with the same name at the bottom of this file.
136137
def extract_advisories_from_page(self, apache_tomcat_advisory_html):
137138
"""
138139
Return a list of AdvisoryData extracted from the HTML text ``apache_tomcat_advisory_html``.
@@ -389,8 +390,6 @@ def to_version_ranges(self, versions_data, fixed_versions):
389390
)
390391

391392
elif "-" in version_item:
392-
# elif "-" in version_item and not any([i.isalpha() for i in version_item]):
393-
# version_item_split = version_item.split(" ")
394393
version_item_split = version_item.split("-")
395394

396395
constraints.append(
@@ -479,6 +478,7 @@ def extract_advisories_from_page(apache_tomcat_advisory_html):
479478
fixed_version = fixed_version_heading.text.split("Fixed in Apache Tomcat")[-1].strip()
480479
if TRACE:
481480
print("fixed_version = {}".format(fixed_version))
481+
print("===========================")
482482

483483
# We want to handle the occasional "and" in the fixed version headers, e.g.,
484484
# <h3 id="Fixed_in_Apache_Tomcat_8.5.5_and_8.0.37"><span class="pull-right">5 September 2016</span> Fixed in Apache Tomcat 8.5.5 and 8.0.37</h3>
@@ -489,6 +489,7 @@ def extract_advisories_from_page(apache_tomcat_advisory_html):
489489

490490
if TRACE:
491491
print("fixed_versions = {}".format(fixed_versions))
492+
print("===========================")
492493

493494
# Each group of fixed-version-related data is contained in a div that immediately follows the h3 element, e.g.,
494495
# <h3 id="Fixed_in_Apache_Tomcat_8.5.8"><span class="pull-right">8 November 2016</span> Fixed in Apache Tomcat 8.5.8</h3>
@@ -502,20 +503,23 @@ def extract_advisories_from_page(apache_tomcat_advisory_html):
502503
severities = ("Low:", "Moderate:", "Important:", "High:", "Critical:")
503504
# A list of groups of paragraphs, each for a single Tomcat Advisory.
504505
advisory_groups = []
505-
current_group = []
506+
506507
for para in fixed_version_paras.find_all("p"):
508+
current_group = []
507509
if para.text.startswith(severities):
508-
if current_group:
509-
current_group = []
510-
else:
511-
advisory_groups.append(current_group)
512-
513510
current_group.append(para)
514511

515-
else:
516-
if current_group:
517-
current_group.append(para)
518-
else:
519-
pass
512+
test_nextSiblings = para.find_next_siblings()
513+
for next_sibling in test_nextSiblings:
514+
if not next_sibling.text.startswith(severities):
515+
current_group.append(next_sibling)
516+
elif next_sibling.text.startswith(severities):
517+
break
518+
519+
advisory_groups.append(current_group)
520+
521+
if TRACE:
522+
print("\ncurrent_group = {}\n".format(current_group))
523+
print("\nadvisory_groups = {}\n".format(advisory_groups))
520524

521525
yield TomcatAdvisoryData(fixed_versions=fixed_versions, advisory_groups=advisory_groups)

vulnerabilities/tests/test_apache_tomcat.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def test_extract_advisories_from_page():
5959
'href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-30639" '
6060
'rel="nofollow">CVE-2021-30639</a></p>',
6161
"<p>An error introduced as part of a change to improve " "error handling.</p>",
62+
"<o>Applications that do not use non-blocking I/O are "
63+
"not exposed to this vulnerability.</o>",
6264
"<p>This was fixed with commit\n"
6365
" <a "
6466
'href="https://github.com/apache/tomcat/commit/b59099e4ca501a039510334ebe1024971cd6f959">b59099e4</a>.</p>',
@@ -200,7 +202,29 @@ def test_extract_advisories_from_page_with_multiple_groups():
200202
"The issue was made public\n"
201203
" on 1 March 2021.</p>",
202204
"<p>Affects: 10.0.0-M1 to 10.0.0</p>",
203-
]
205+
],
206+
[
207+
"<p><strong>Important: Request mix-up with "
208+
"h2c</strong>\n"
209+
"<a "
210+
'href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-25122" '
211+
'rel="nofollow">CVE-2021-25122</a></p>',
212+
"<p>When responding to new h2c connection requests, "
213+
"Apache Tomcat could\n"
214+
" duplicate request headers and a limited amount of "
215+
"request body from one\n"
216+
" request to another meaning user A and user B could "
217+
"both see the results of\n"
218+
" user A's request.</p>",
219+
"<p>This was fixed with commit\n"
220+
" <a "
221+
'href="https://github.com/apache/tomcat/commit/dd757c0a893e2e35f8bc1385d6967221ae8b9b9b">dd757c0a</a>.</p>',
222+
"<p>This issue was identified by the Apache Tomcat "
223+
"Security team on 11\n"
224+
" January 2021. The issue was made public on 1 "
225+
"March 2021.</p>",
226+
"<p>Affects: 10.0.0-M1 to 10.0.0</p>",
227+
],
204228
],
205229
"fixed_versions": ["10.0.2"],
206230
},
@@ -232,11 +256,40 @@ def test_extract_advisories_from_page_with_multiple_groups():
232256
" on 26 October 2020. The issue was made public "
233257
"on 14 January 2021.</p>",
234258
"<p>Affects: 10.0.0-M1 to 10.0.0-M9</p>",
235-
]
259+
],
260+
[
261+
"<p><strong>Moderate: HTTP/2 request header "
262+
"mix-up</strong>\n"
263+
"<a "
264+
'href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-17527" '
265+
'rel="nofollow">CVE-2020-17527</a></p>',
266+
"<p>While investigating issue <a "
267+
'href="https://bz.apache.org/bugzilla/show_bug.cgi?id=64830">64830</a> '
268+
"it was discovered that Apache\n"
269+
" Tomcat could re-use an HTTP request header "
270+
"value from the previous stream\n"
271+
" received on an HTTP/2 connection for the "
272+
"request associated with the\n"
273+
" subsequent stream. While this would most likely "
274+
"lead to an error and the\n"
275+
" closure of the HTTP/2 connection, it is "
276+
"possible that information could\n"
277+
" leak between requests.\n"
278+
" </p>",
279+
"<p>This was fixed with commit\n"
280+
" <a "
281+
'href="https://github.com/apache/tomcat/commit/8d2fe6894d6e258a6d615d7f786acca80e6020cb">8d2fe689</a>.</p>',
282+
"<p>This issue was identified by the Apache Tomcat "
283+
"Security team on 10\n"
284+
" November 2020. The issue was made public on 3 "
285+
"December 2020.</p>",
286+
"<p>Affects: 10.0.0-M1 to 10.0.0-M9</p>",
287+
],
236288
],
237289
"fixed_versions": ["10.0.0-M10"],
238290
},
239291
]
292+
240293
results = extract_advisories_from_page(page)
241294
results = [d.to_dict() for d in results]
242295
assert results == expected

0 commit comments

Comments
 (0)