Skip to content

Commit dd80635

Browse files
rajath201nedbat
andauthored
fix: escape context labels in html report inline script block (#2224)
* fix: escape context labels in html report inline script block * tweak the test for full coverage, and give credit (nedbat) * r-strings are better (nedbat) --------- Co-authored-by: Ned Batchelder <[email protected]>
1 parent 7c3ae71 commit dd80635

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎CHANGES.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26+
- Fix: in the HTML report with ``show_contexts`` enabled, a context label
27+
containing ``</script>`` (for example a parametrized pytest node id) could
28+
close the inline ``<script>`` element in a file page early, injecting markup.
29+
Context labels are now fully escaped. Thanks, `Rajath Mohare <pull 2224_>`_.
30+
2631
- A number of performance improvements thanks to Paul Kehrer, in pull requests
2732
`2213 <pull 2213_>`_, `2214 <pull 2214_>`_, `2215 <pull 2215_>`_, `2216
2833
<pull 2216_>`_, and `2218 <pull 2218_>`_.
@@ -32,6 +37,7 @@ Unreleased
3237
.. _pull 2215: https://github.com/coveragepy/coveragepy/pull/2215
3338
.. _pull 2216: https://github.com/coveragepy/coveragepy/pull/2216
3439
.. _pull 2218: https://github.com/coveragepy/coveragepy/pull/2218
40+
.. _pull 2224: https://github.com/coveragepy/coveragepy/pull/2224
3541

3642
.. start-releases
3743

‎CONTRIBUTORS.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ Peter Baughman
213213
Peter Ebden
214214
Peter Portante
215215
Phebe Polk
216+
Rajath Mohare
216217
Reya B
217218
Ricardo Newbery
218219
Robert Harris

‎coverage/html.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,18 @@ def write_html_page(self, ftr: FileToReport) -> None:
483483
contexts = collections.Counter(c for cline in file_data.lines for c in cline.contexts)
484484
context_codes = {y: i for (i, y) in enumerate(x[0] for x in contexts.most_common())}
485485
if context_codes:
486-
contexts_json = json.dumps(
487-
{encode_int(v): k for (k, v) in context_codes.items()},
488-
indent=2,
486+
# This JSON is dropped straight into an inline <script> element, so
487+
# the HTML-significant characters have to be escaped as \uXXXX to keep
488+
# a context label like "</script>" from closing the element early.
489+
# The escapes are still valid JSON, so the parsed values are unchanged.
490+
contexts_json = (
491+
json.dumps(
492+
{encode_int(v): k for (k, v) in context_codes.items()},
493+
indent=2,
494+
)
495+
.replace("<", r"\u003c")
496+
.replace(">", r"\u003e")
497+
.replace("&", r"\u0026")
489498
)
490499
else:
491500
contexts_json = None

‎tests/test_html.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from coverage import env, Coverage
2626
from coverage.exceptions import NoDataError, NotPython, NoSource
2727
from coverage.files import abs_file, flat_rootname
28+
from coverage.misc import import_local_file
2829
from coverage.report_core import get_analysis_to_report
2930
from coverage.types import TLineNo, TMorf
3031

@@ -1545,6 +1546,27 @@ def test_dynamic_contexts_relative_files(self) -> None:
15451546
]
15461547
assert sorted(expected) == sorted(actual)
15471548

1549+
def test_context_labels_cant_break_out_of_script(self) -> None:
1550+
# switch_context takes an arbitrary string (pytest-cov passes test node
1551+
# ids), so a label can contain "</script>". It's dropped into an inline
1552+
# <script> block in the file page, and must not close the element early.
1553+
label = "</script><img src=x onerror=alert(1)>"
1554+
self.make_file("two_tests.py", self.SOURCE)
1555+
cov = coverage.Coverage(source=["."])
1556+
cov.set_option("html:show_contexts", True)
1557+
with cov.collect():
1558+
cov.switch_context(label)
1559+
mod = import_local_file("two_tests")
1560+
cov.html_report(mod, directory="htmlcov")
1561+
1562+
html = self.get_html_report_content("two_tests.py")
1563+
assert label not in html
1564+
assert "\\u003c/script\\u003e" in html
1565+
# The escaped JSON still decodes to the original label.
1566+
blob = re.search(r"contexts = (\{.*?\n\s*\})", html, re.DOTALL)
1567+
assert blob is not None
1568+
assert label in json.loads(blob.group(1)).values()
1569+
15481570

15491571
class HtmlHelpersTest(HtmlTestHelpers, CoverageTest):
15501572
"""Tests of the helpers in HtmlTestHelpers."""

0 commit comments

Comments
 (0)