Skip to content

Commit 178af6b

Browse files
authored
Merge pull request #1418 from jobselko/autoescape_tests
Test autoescape in simple HTML templates
2 parents a8d79d4 + f95be39 commit 178af6b

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
"filename": "<img src=x onerror=alert(1)>",
43+
"url": 'http://evil.com/"><script>alert(1)</script>',
44+
"sha256": "abc123",
45+
"requires_python": None,
46+
"yanked": False,
47+
"yanked_reason": "",
48+
"metadata_sha256": None,
49+
"provenance": None,
50+
}
51+
]
52+
page = write_simple_detail("safe-name", packages)
53+
assert "<img src=x onerror=alert(1)>" not in page
54+
assert "&lt;img src=x onerror=alert(1)&gt;" in page
55+
assert 'http://evil.com/"><script>alert(1)</script>' not in page
56+
assert "http://evil.com/&#34;&gt;&lt;script&gt;alert(1)&lt;/script&gt;" in page

0 commit comments

Comments
 (0)