Skip to content

Commit 335f4ac

Browse files
committed
[1.0.X] Fixed #6160, #9111 -- Consistently apply conditional_escape to form errors and labels when outputing them as HTML.
[9365] from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9366 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 675ee5c commit 335f4ac

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

django/contrib/auth/tests/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_email_not_found(self):
1616
response = self.client.get('/password_reset/')
1717
self.assertEquals(response.status_code, 200)
1818
response = self.client.post('/password_reset/', {'email': '[email protected]'})
19-
self.assertContains(response, "That e-mail address doesn't have an associated user account")
19+
self.assertContains(response, "That e-mail address doesn't have an associated user account")
2020
self.assertEquals(len(mail.outbox), 0)
2121

2222
def test_email_found(self):
@@ -87,7 +87,7 @@ def test_confirm_different_passwords(self):
8787
response = self.client.post(path, {'new_password1': 'anewpassword',
8888
'new_password2':' x'})
8989
self.assertEquals(response.status_code, 200)
90-
self.assert_("The two password fields didn't match" in response.content)
90+
self.assert_("The two password fields didn't match" in response.content)
9191

9292

9393
class ChangePasswordTest(TestCase):
@@ -147,7 +147,7 @@ def test_password_change_fails_with_mismatched_passwords(self):
147147
}
148148
)
149149
self.assertEquals(response.status_code, 200)
150-
self.assert_("The two password fields didn't match." in response.content)
150+
self.assert_("The two password fields didn't match." in response.content)
151151

152152
def test_password_change_succeeds(self):
153153
self.login()

django/forms/forms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from copy import deepcopy
66

77
from django.utils.datastructures import SortedDict
8-
from django.utils.html import escape
8+
from django.utils.html import conditional_escape
99
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
1010
from django.utils.safestring import mark_safe
1111

@@ -140,7 +140,7 @@ def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_
140140
output, hidden_fields = [], []
141141
for name, field in self.fields.items():
142142
bf = BoundField(self, field, name)
143-
bf_errors = self.error_class([escape(error) for error in bf.errors]) # Escape and cache in local variable.
143+
bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
144144
if bf.is_hidden:
145145
if bf_errors:
146146
top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
@@ -149,7 +149,7 @@ def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_
149149
if errors_on_separate_row and bf_errors:
150150
output.append(error_row % force_unicode(bf_errors))
151151
if bf.label:
152-
label = escape(force_unicode(bf.label))
152+
label = conditional_escape(force_unicode(bf.label))
153153
# Only add the suffix if the label does not end in
154154
# punctuation.
155155
if self.label_suffix:
@@ -395,7 +395,7 @@ def label_tag(self, contents=None, attrs=None):
395395
396396
If attrs are given, they're used as HTML attributes on the <label> tag.
397397
"""
398-
contents = contents or escape(self.label)
398+
contents = contents or conditional_escape(self.label)
399399
widget = self.field.widget
400400
id_ = widget.attrs.get('id') or self.auto_id
401401
if id_:

django/forms/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __unicode__(self):
3939
def as_ul(self):
4040
if not self: return u''
4141
return mark_safe(u'<ul class="errorlist">%s</ul>'
42-
% ''.join([u'<li>%s</li>' % force_unicode(e) for e in self]))
42+
% ''.join([u'<li>%s</li>' % conditional_escape(force_unicode(e)) for e in self]))
4343

4444
def as_text(self):
4545
if not self: return u''

tests/regressiontests/forms/forms.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,17 +593,25 @@
593593
u'Yesterday'
594594
595595
Validation errors are HTML-escaped when output as HTML.
596+
>>> from django.utils.safestring import mark_safe
596597
>>> class EscapingForm(Form):
597-
... special_name = CharField()
598+
... special_name = CharField(label="<em>Special</em> Field")
599+
... special_safe_name = CharField(label=mark_safe("<em>Special</em> Field"))
598600
... def clean_special_name(self):
599601
... raise ValidationError("Something's wrong with '%s'" % self.cleaned_data['special_name'])
602+
... def clean_special_safe_name(self):
603+
... raise ValidationError(mark_safe("'<b>%s</b>' is a safe string" % self.cleaned_data['special_safe_name']))
600604
601-
>>> f = EscapingForm({'special_name': "Nothing to escape"}, auto_id=False)
605+
>>> f = EscapingForm({'special_name': "Nothing to escape", 'special_safe_name': "Nothing to escape"}, auto_id=False)
602606
>>> print f
603-
<tr><th>Special name:</th><td><ul class="errorlist"><li>Something&#39;s wrong with &#39;Nothing to escape&#39;</li></ul><input type="text" name="special_name" value="Nothing to escape" /></td></tr>
604-
>>> f = EscapingForm({'special_name': "Should escape < & > and <script>alert('xss')</script>"}, auto_id=False)
607+
<tr><th>&lt;em&gt;Special&lt;/em&gt; Field:</th><td><ul class="errorlist"><li>Something&#39;s wrong with &#39;Nothing to escape&#39;</li></ul><input type="text" name="special_name" value="Nothing to escape" /></td></tr>
608+
<tr><th><em>Special</em> Field:</th><td><ul class="errorlist"><li>'<b>Nothing to escape</b>' is a safe string</li></ul><input type="text" name="special_safe_name" value="Nothing to escape" /></td></tr>
609+
>>> f = EscapingForm(
610+
... {'special_name': "Should escape < & > and <script>alert('xss')</script>",
611+
... 'special_safe_name': "<i>Do not escape</i>"}, auto_id=False)
605612
>>> print f
606-
<tr><th>Special name:</th><td><ul class="errorlist"><li>Something&#39;s wrong with &#39;Should escape &lt; &amp; &gt; and &lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;&#39;</li></ul><input type="text" name="special_name" value="Should escape &lt; &amp; &gt; and &lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;" /></td></tr>
613+
<tr><th>&lt;em&gt;Special&lt;/em&gt; Field:</th><td><ul class="errorlist"><li>Something&#39;s wrong with &#39;Should escape &lt; &amp; &gt; and &lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;&#39;</li></ul><input type="text" name="special_name" value="Should escape &lt; &amp; &gt; and &lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;" /></td></tr>
614+
<tr><th><em>Special</em> Field:</th><td><ul class="errorlist"><li>'<b><i>Do not escape</i></b>' is a safe string</li></ul><input type="text" name="special_safe_name" value="&lt;i&gt;Do not escape&lt;/i&gt;" /></td></tr>
607615
608616
""" + \
609617
r""" # [This concatenation is to keep the string below the jython's 32K limit].

tests/regressiontests/forms/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@
4949
# Can take a non-string.
5050
>>> print ValidationError(VeryBadError()).messages
5151
<ul class="errorlist"><li>A very bad error.</li></ul>
52+
53+
# Escapes non-safe input but not input marked safe.
54+
>>> example = 'Example of link: <a href="http://www.example.com/">example</a>'
55+
>>> print ValidationError(example).messages
56+
<ul class="errorlist"><li>Example of link: &lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;</li></ul>
57+
>>> print ValidationError(mark_safe(example)).messages
58+
<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>
5259
"""

0 commit comments

Comments
 (0)