|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pulp_python.app.utils import write_simple_detail, write_simple_index |
| 4 | + |
| 5 | +XSS_VECTORS = [ |
| 6 | + ('<script>alert("xss")</script>', "<script>"), |
| 7 | + ("<img src=x onerror=alert(1)>", "<img"), |
| 8 | + ('" onclick="alert(1)"', """), |
| 9 | + ("a&b", "a&b"), |
| 10 | +] |
| 11 | + |
| 12 | + |
| 13 | +def test_index_renders_valid_names(): |
| 14 | + """Test that valid package names render correctly in the simple index page.""" |
| 15 | + page = write_simple_index(["aprojectname", "under_score", "da-sh", "ungefährlich"]) |
| 16 | + assert "aprojectname" in page |
| 17 | + assert "under-score" in page |
| 18 | + assert "da-sh" in page |
| 19 | + assert "ungefährlich" in page |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("name,escaped", XSS_VECTORS) |
| 23 | +def test_index_escapes_html(name, escaped): |
| 24 | + """Test that autoescape neutralizes HTML injection in the simple index page.""" |
| 25 | + page = write_simple_index([name]) |
| 26 | + assert name not in page |
| 27 | + assert escaped in page |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize("name,escaped", XSS_VECTORS) |
| 31 | +def test_detail_escapes_html(name, escaped): |
| 32 | + """Test that autoescape neutralizes HTML injection in the simple detail page.""" |
| 33 | + page = write_simple_detail(name, []) |
| 34 | + assert name not in page |
| 35 | + assert escaped in page |
| 36 | + |
| 37 | + |
| 38 | +def test_detail_escapes_package_fields(): |
| 39 | + """Test that autoescape neutralizes HTML injection in package metadata fields.""" |
| 40 | + packages = [ |
| 41 | + ( |
| 42 | + "<img src=x onerror=alert(1)>", |
| 43 | + 'http://evil.com/"><script>alert(1)</script>', |
| 44 | + "abc123", |
| 45 | + ) |
| 46 | + ] |
| 47 | + page = write_simple_detail("safe-name", packages) |
| 48 | + assert "<img src=x onerror=alert(1)>" not in page |
| 49 | + assert "<img src=x onerror=alert(1)>" in page |
| 50 | + assert 'http://evil.com/"><script>alert(1)</script>' not in page |
| 51 | + assert "http://evil.com/"><script>alert(1)</script>" in page |
0 commit comments