Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions django/utils/feedgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from io import StringIO
from urllib.parse import urlparse

from django.forms.utils import flatatt
from django.utils.encoding import iri_to_uri
from django.utils.xmlutils import SimplerXMLGenerator

Expand Down Expand Up @@ -95,12 +96,12 @@ def mimetype(self):
return self._mimetype

def __str__(self):
data = [f'href="{self.url}"']
if self.mimetype is not None:
data.append(f'type="{self.mimetype}"')
if self.media is not None:
data.append(f'media="{self.media}"')
return " ".join(data)
attrs = {
"href": iri_to_uri(self._url),
"type": self.mimetype,
"media": self.media,
}
return flatatt(attrs).strip()

def __repr__(self):
return repr((self.url, self.mimetype, self.media))
Expand Down
5 changes: 4 additions & 1 deletion docs/releases/5.2.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ Django 5.2.9 fixes several bugs in 5.2.8.
Bugfixes
========

* ...
* Fixed a bug in Django 5.2 where
``django.utils.feedgenerator.Stylesheet.__str__()`` did not escape
the ``url``, ``mimetype``, and ``media`` attributes, potentially leading
to invalid XML markup (:ticket:`36733`).
28 changes: 14 additions & 14 deletions tests/syndication_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,51 +578,51 @@ def test_stylesheets_none(self):
def test_stylesheets(self):
testdata = [
# Plain strings.
("/test.xsl", 'href="/test.xsl" type="text/xsl" media="screen"'),
("/test.xslt", 'href="/test.xslt" type="text/xsl" media="screen"'),
("/test.css", 'href="/test.css" type="text/css" media="screen"'),
("/test.xsl", 'href="/test.xsl" media="screen" type="text/xsl"'),
("/test.xslt", 'href="/test.xslt" media="screen" type="text/xsl"'),
("/test.css", 'href="/test.css" media="screen" type="text/css"'),
("/test", 'href="/test" media="screen"'),
(
"https://example.com/test.xsl",
'href="https://example.com/test.xsl" type="text/xsl" media="screen"',
'href="https://example.com/test.xsl" media="screen" type="text/xsl"',
),
(
"https://example.com/test.css",
'href="https://example.com/test.css" type="text/css" media="screen"',
'href="https://example.com/test.css" media="screen" type="text/css"',
),
(
"https://example.com/test",
'href="https://example.com/test" media="screen"',
),
("/♥.xsl", 'href="/%E2%99%A5.xsl" type="text/xsl" media="screen"'),
("/♥.xsl", 'href="/%E2%99%A5.xsl" media="screen" type="text/xsl"'),
(
static("stylesheet.xsl"),
'href="/static/stylesheet.xsl" type="text/xsl" media="screen"',
'href="/static/stylesheet.xsl" media="screen" type="text/xsl"',
),
(
static("stylesheet.css"),
'href="/static/stylesheet.css" type="text/css" media="screen"',
'href="/static/stylesheet.css" media="screen" type="text/css"',
),
(static("stylesheet"), 'href="/static/stylesheet" media="screen"'),
(
reverse("syndication-xsl-stylesheet"),
'href="/syndication/stylesheet.xsl" type="text/xsl" media="screen"',
'href="/syndication/stylesheet.xsl" media="screen" type="text/xsl"',
),
(
reverse_lazy("syndication-xsl-stylesheet"),
'href="/syndication/stylesheet.xsl" type="text/xsl" media="screen"',
'href="/syndication/stylesheet.xsl" media="screen" type="text/xsl"',
),
# Stylesheet objects.
(
Stylesheet("/test.xsl"),
'href="/test.xsl" type="text/xsl" media="screen"',
'href="/test.xsl" media="screen" type="text/xsl"',
),
(Stylesheet("/test.xsl", mimetype=None), 'href="/test.xsl" media="screen"'),
(Stylesheet("/test.xsl", media=None), 'href="/test.xsl" type="text/xsl"'),
(Stylesheet("/test.xsl", mimetype=None, media=None), 'href="/test.xsl"'),
(
Stylesheet("/test.xsl", mimetype="text/xml"),
'href="/test.xsl" type="text/xml" media="screen"',
'href="/test.xsl" media="screen" type="text/xml"',
),
]
for stylesheet, expected in testdata:
Expand All @@ -642,12 +642,12 @@ def test_stylesheets_instructions_are_at_the_top(self):
self.assertEqual(doc.childNodes[0].nodeName, "xml-stylesheet")
self.assertEqual(
doc.childNodes[0].data,
'href="/stylesheet1.xsl" type="text/xsl" media="screen"',
'href="/stylesheet1.xsl" media="screen" type="text/xsl"',
)
self.assertEqual(doc.childNodes[1].nodeName, "xml-stylesheet")
self.assertEqual(
doc.childNodes[1].data,
'href="/stylesheet2.xsl" type="text/xsl" media="screen"',
'href="/stylesheet2.xsl" media="screen" type="text/xsl"',
)

def test_stylesheets_typeerror_if_str_or_stylesheet(self):
Expand Down
16 changes: 15 additions & 1 deletion tests/utils_tests/test_feedgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ def test_stylesheet_keeps_lazy_urls(self):
stylesheet = feedgenerator.Stylesheet(SimpleLazyObject(m))
m.assert_not_called()
self.assertEqual(
str(stylesheet), 'href="test.css" type="text/css" media="screen"'
str(stylesheet), 'href="test.css" media="screen" type="text/css"'
)
m.assert_called_once()

def test_stylesheet_attribute_escaping(self):
style = feedgenerator.Stylesheet(
url='http://example.com/style.css?foo="bar"&baz=<>',
mimetype='text/css; charset="utf-8"',
media='screen and (max-width: "600px")',
)

self.assertEqual(
str(style),
'href="http://example.com/style.css?foo=%22bar%22&amp;baz=%3C%3E" '
'media="screen and (max-width: &quot;600px&quot;)" '
'type="text/css; charset=&quot;utf-8&quot;"',
)