From 5063eabc7c9c25950b148b26b1dc5fe080018b22 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 22 Oct 2026 07:56:06 +0200 Subject: [PATCH 01/15] test(templates): cover markup the inspection already reads right Pin what the changes for #272 must keep: a script closed in its own template, a comment an include closes before a script, a variable in a comment that holds no template tag, comments that close early, and templates that end inside a comment or are extended. --- tests/test_template_open_constructs.py | 122 +++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/test_template_open_constructs.py diff --git a/tests/test_template_open_constructs.py b/tests/test_template_open_constructs.py new file mode 100644 index 0000000..cbd18a6 --- /dev/null +++ b/tests/test_template_open_constructs.py @@ -0,0 +1,122 @@ +"""Acceptance tests for issue #272: what a template leaves open, and a template tag inside a +comment, are read as the browser receives the rendered page. + +Every construct is read once, so the inspection is linear in the size of the template. +Each inspection and analysis runs in a subprocess with a timeout, so that one that does +not end fails instead of hanging the suite. +""" + +from __future__ import annotations + +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +TIMEOUT = 60 +# Far beyond what a quadratic inspection reads within the timeout; a linear one takes +# well under a second. +SIZE = 3_000_000 + +ESCAPED_TEMPLATES = ( + "import json, sys\n" + "from pathlib import Path\n" + "from coretrace_python.taint.templates import escaped_templates\n" + "print(json.dumps(sorted(escaped_templates(Path(sys.argv[1])))))\n" +) + +VIEW = ( + "from django.http import HttpRequest, HttpResponse\n" + "from django.template.loader import render_to_string\n\n" + "def page(request: HttpRequest):\n" + " return HttpResponse(render_to_string('page.html', {'name': request.GET['name']}))\n" +) + + +def run(command: list[str]) -> subprocess.CompletedProcess[str]: + try: + return subprocess.run(command, capture_output=True, text=True, check=False, timeout=TIMEOUT) + except subprocess.TimeoutExpired: + pytest.fail(f"{command[1:3]} did not end within {TIMEOUT} s") + + +def project(tmp_path: Path, templates: dict[str, str]) -> Path: + root = tmp_path / "project" + (root / "app" / "templates").mkdir(parents=True, exist_ok=True) + (root / "app" / "views.py").write_text(VIEW, encoding="utf-8") + for name, content in templates.items(): + (root / "app" / "templates" / name).write_text(content, encoding="utf-8") + return root + + +def escaped(tmp_path: Path, templates: dict[str, str]) -> list[str]: + completed = run([sys.executable, "-c", ESCAPED_TEMPLATES, str(project(tmp_path, templates))]) + assert completed.returncode == 0, completed.stderr + names: list[str] = json.loads(completed.stdout) + return names + + +def xss(tmp_path: Path, templates: dict[str, str]) -> list[tuple[str, int]]: + root = project(tmp_path, templates) + completed = run([sys.executable, "-m", "coretrace_python", "--check", str(root), "--format", "json"]) + findings = json.loads(completed.stdout)["findings"] + return [(f["location"]["path"], f["location"]["line"]) for f in findings if f["rule_id"] == "xss"] + + +# The reproductions of #272, rendered with Django and run in a browser: the variable runs +# as script in each (in C, the browser never runs a script left open at the end of the +# document, but a template cannot tell whether it is rendered alone). +@pytest.mark.parametrize( + "templates", + [ + {"page.html": "\n"}, + {"page.html": '"}, + ], + ids=[ + "B-closed-in-same-template", + "D-comment-closed-by-include", + ], +) +def test_a_variable_the_rendered_page_runs_as_script_is_cross_site_scripting( + tmp_path: Path, templates: dict[str, str] +) -> None: + assert xss(tmp_path, templates) == [("app/views.py", 5)] + + +def test_a_variable_in_a_comment_holding_no_template_tag_stays_escaped(tmp_path: Path) -> None: + templates = {"page.html": "

hello

\n"} + + assert escaped(tmp_path, templates) == ["page.html"] + assert xss(tmp_path, templates) == [] + + +@pytest.mark.parametrize( + "template", + [ + '{% if debug %}', + '', + '', + ], + ids=["condition-around-the-opener", "abrupt-empty", "abrupt-dash", "bang-close"], +) +def test_a_comment_ends_at_its_close_or_at_a_template_tag_inside_it(tmp_path: Path, template: str) -> None: + assert escaped(tmp_path, {"page.html": template, "x.html": ""}) == ["x.html"] + + +def test_including_a_template_that_ends_inside_a_comment_stays_established(tmp_path: Path) -> None: + templates = {"fragment.html": "\n

{{ name }}

\n"} + + assert escaped(tmp_path, templates) == ["fragment.html", "page.html"] + + +def test_extending_a_template_that_ends_inside_a_script_stays_established(tmp_path: Path) -> None: + # A child's blocks stand where the parent places them, before the parent's end. + templates = { + "base.html": "

{% block body %}{% endblock %}

\n"}, {"page.html": "\n"}, + {"page.html": "\n"}, {"page.html": '\n', "close.html": "-->"}, ], ids=[ @@ -92,6 +97,7 @@ def xss(tmp_path: Path, templates: dict[str, str]) -> list[tuple[str, int]]: "B-closed-in-same-template", "C-open-to-end-of-document", "D-comment-closed-by-include", + "E-includer-continues-open-script", "G-closed-comment-holding-include", ], ) @@ -148,6 +154,19 @@ def test_a_variable_in_a_construct_left_open_is_refused(tmp_path: Path, template assert escaped(tmp_path, {"page.html": template}) == [] +@pytest.mark.parametrize( + "fragment", + ["', + } + + assert escaped(tmp_path, templates) == sorted(set(templates) - {"page.html"}) + assert xss(tmp_path, templates) == [("app/views.py", 5)] + + def test_extending_a_template_that_ends_inside_a_script_stays_established(tmp_path: Path) -> None: # A child's blocks stand where the parent places them, before the parent's end. templates = { From 0ec8c4c3f8e410fbafb4937af4af033825bff3a9 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 22 Oct 2026 08:51:07 +0200 Subject: [PATCH 15/15] docs: state that a template extending another ends where that one ends --- docs/usage.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 581ea81..a22a5f6 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -204,10 +204,10 @@ checked, a quoted value stays quoted until its quote closes, and a variable in a `