Skip to content

Commit 906420f

Browse files
committed
Validate and autoescape data when rendering html
CVE-2026-103871
1 parent 8e94086 commit 906420f

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎CHANGES/+simple_html_xss.bug‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed quoting mistake in simple html index pages that can lead to a possible XSS attack given a carefully crafted package name. CVE-2026-103871

‎pulp_python/app/utils.py‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tempfile
88
import zipfile
99
from collections import defaultdict
10+
from contextlib import suppress
1011
from datetime import timezone
1112

1213
import pkginfo
@@ -16,7 +17,7 @@
1617
from django.db.utils import IntegrityError
1718
from jinja2 import Template
1819
from packaging.requirements import Requirement
19-
from packaging.utils import canonicalize_name
20+
from packaging.utils import InvalidName, canonicalize_name
2021
from packaging.version import InvalidVersion, parse
2122
from pypi_simple import ACCEPT_JSON_PREFERRED, ProjectPage
2223

@@ -575,18 +576,27 @@ def find_artifact():
575576
}
576577

577578

579+
def _canonicalize_name_iterator(names):
580+
for name in names:
581+
with suppress(InvalidName):
582+
yield (name, canonicalize_name(name, validate=True))
583+
584+
578585
def write_simple_index(project_names):
579586
"""Writes the simple index."""
580-
simple = Template(simple_index_template)
587+
simple = Template(simple_index_template, autoescape=True)
581588
context = {
582589
"SIMPLE_API_VERSION": SIMPLE_API_VERSION,
583-
"projects": ((x, canonicalize_name(x)) for x in project_names),
590+
"projects": _canonicalize_name_iterator(project_names),
584591
}
585592
return simple.render(**context)
586593

587594

588595
def write_simple_detail(project_name, project_packages):
589596
"""Writes the simple detail page of a package."""
597+
# TODO: Think about adding this regex to the database:
598+
# https://packaging.python.org/en/latest/specifications/name-normalization/#name-format
599+
canonicalize_name(project_name, validate=True)
590600
detail = Template(simple_detail_template, autoescape=True)
591601
context = {
592602
"SIMPLE_API_VERSION": SIMPLE_API_VERSION,
@@ -610,7 +620,7 @@ def write_simple_detail_json(project_name, project_packages):
610620
"""Writes the simple detail page in JSON format."""
611621
return {
612622
"meta": {"api-version": SIMPLE_API_VERSION, "_last-serial": PYPI_SERIAL_CONSTANT},
613-
"name": canonicalize_name(project_name),
623+
"name": canonicalize_name(project_name, validate=True),
614624
"files": [
615625
{
616626
# v1.0, PEP 691
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from pulp_python.app.utils import write_simple_index
4+
5+
6+
def test_write_simple_index():
7+
project_names = ["aprojectname", "under_score", "da-sh"]
8+
page = write_simple_index(project_names)
9+
assert "aprojectname" in page
10+
assert "under-score" in page
11+
assert "da-sh" in page
12+
13+
14+
@pytest.mark.parametrize(
15+
"name", ['<script lang="javascript">alert("Evil!")</script>', "gefährlich"]
16+
)
17+
def test_write_simple_index_rejects_invalid_package_name(name):
18+
project_names = [name]
19+
page = write_simple_index(project_names)
20+
assert name not in page

0 commit comments

Comments
 (0)