Skip to content

Commit 6a9ac3b

Browse files
committed
Merge pull request #1418 from jobselko/autoescape_tests
Test autoescape in simple HTML templates (cherry picked from commit 178af6b)
1 parent 89c1aed commit 6a9ac3b

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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>', "&lt;script&gt;"),
7+
("<img src=x onerror=alert(1)>", "&lt;img"),
8+
('" onclick="alert(1)"', "&#34;"),
9+
("a&b", "a&amp;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 "&lt;img src=x onerror=alert(1)&gt;" in page
50+
assert 'http://evil.com/"><script>alert(1)</script>' not in page
51+
assert "http://evil.com/&#34;&gt;&lt;script&gt;alert(1)&lt;/script&gt;" in page

‎unittest_requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mock
22
pytest<8
3+
pytest-django

0 commit comments

Comments
 (0)