|
25 | 25 | from coverage import env, Coverage |
26 | 26 | from coverage.exceptions import NoDataError, NotPython, NoSource |
27 | 27 | from coverage.files import abs_file, flat_rootname |
| 28 | +from coverage.misc import import_local_file |
28 | 29 | from coverage.report_core import get_analysis_to_report |
29 | 30 | from coverage.types import TLineNo, TMorf |
30 | 31 |
|
@@ -1545,6 +1546,27 @@ def test_dynamic_contexts_relative_files(self) -> None: |
1545 | 1546 | ] |
1546 | 1547 | assert sorted(expected) == sorted(actual) |
1547 | 1548 |
|
| 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 | + |
1548 | 1570 |
|
1549 | 1571 | class HtmlHelpersTest(HtmlTestHelpers, CoverageTest): |
1550 | 1572 | """Tests of the helpers in HtmlTestHelpers.""" |
|
0 commit comments