From 54697afe09707907a94894974553b38954f48047 Mon Sep 17 00:00:00 2001 From: Jitka Halova <41325380+jobselko@users.noreply.github.com> Date: Mon, 12 Oct 2026 12:00:53 +0200 Subject: [PATCH] Merge pull request #1418 from jobselko/autoescape_tests Test autoescape in simple HTML templates (cherry picked from commit 178af6be05092216f02bc4c9a548593f6444d4c8) --- .../tests/unit/test_simple_templates.py | 54 +++++++++++++++++++ unittest_requirements.txt | 1 + 2 files changed, 55 insertions(+) create mode 100644 pulp_python/tests/unit/test_simple_templates.py diff --git a/pulp_python/tests/unit/test_simple_templates.py b/pulp_python/tests/unit/test_simple_templates.py new file mode 100644 index 000000000..7fafb53f3 --- /dev/null +++ b/pulp_python/tests/unit/test_simple_templates.py @@ -0,0 +1,54 @@ +import pytest + +from pulp_python.app.utils import write_simple_detail, write_simple_index + +XSS_VECTORS = [ + ('', "<script>"), + ("", "<img"), + ('" onclick="alert(1)"', """), + ("a&b", "a&b"), +] + + +def test_index_renders_valid_names(): + """Test that valid package names render correctly in the simple index page.""" + page = write_simple_index(["aprojectname", "under_score", "da-sh", "ungefährlich"]) + assert "aprojectname" in page + assert "under-score" in page + assert "da-sh" in page + assert "ungefährlich" in page + + +@pytest.mark.parametrize("name,escaped", XSS_VECTORS) +def test_index_escapes_html(name, escaped): + """Test that autoescape neutralizes HTML injection in the simple index page.""" + page = write_simple_index([name]) + assert name not in page + assert escaped in page + + +@pytest.mark.parametrize("name,escaped", XSS_VECTORS) +def test_detail_escapes_html(name, escaped): + """Test that autoescape neutralizes HTML injection in the simple detail page.""" + page = write_simple_detail(name, []) + assert name not in page + assert escaped in page + + +def test_detail_escapes_package_fields(): + """Test that autoescape neutralizes HTML injection in package metadata fields.""" + packages = [ + { + "filename": "", + "url": 'http://evil.com/">', + "sha256": "abc123", + "requires_python": None, + "metadata_sha256": None, + "provenance": None, + } + ] + page = write_simple_detail("safe-name", packages) + assert "" not in page + assert "<img src=x onerror=alert(1)>" in page + assert 'http://evil.com/">' not in page + assert "http://evil.com/"><script>alert(1)</script>" in page diff --git a/unittest_requirements.txt b/unittest_requirements.txt index f5b662af5..030f79517 100644 --- a/unittest_requirements.txt +++ b/unittest_requirements.txt @@ -1,2 +1,3 @@ mock pytest<8 +pytest-django