Skip to content

Commit 08577ab

Browse files
committed
[1.0.X] Fixed #10034: the formtools security hash function is now friendlier to browsers that submit leading/trailing whitespace in form fields. Backport of [10752] from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10754 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 5e20f14 commit 08577ab

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

django/contrib/formtools/tests.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import unittest
12
from django import forms
2-
from django.contrib.formtools import preview, wizard
3+
from django.contrib.formtools import preview, wizard, utils
34
from django import http
45
from django.test import TestCase
56

@@ -101,6 +102,24 @@ def test_bool_submit(self):
101102
response = self.client.post('/test1/', self.test_data)
102103
self.assertEqual(response.content, success_string)
103104

105+
class SecurityHashTests(unittest.TestCase):
106+
107+
def test_textfield_hash(self):
108+
"""
109+
Regression test for #10034: the hash generation function should ignore
110+
leading/trailing whitespace so as to be friendly to broken browsers that
111+
submit it (usually in textareas).
112+
"""
113+
class TestForm(forms.Form):
114+
name = forms.CharField()
115+
bio = forms.CharField()
116+
117+
f1 = TestForm({'name': 'joe', 'bio': 'Nothing notable.'})
118+
f2 = TestForm({'name': ' joe', 'bio': 'Nothing notable. '})
119+
hash1 = utils.security_hash(None, f1)
120+
hash2 = utils.security_hash(None, f2)
121+
self.assertEqual(hash1, hash2)
122+
104123
#
105124
# FormWizard tests
106125
#

django/contrib/formtools/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ def security_hash(request, form, *args):
1616
hash of that.
1717
"""
1818

19-
data = [(bf.name, bf.field.clean(bf.data) or '') for bf in form]
19+
data = []
20+
for bf in form:
21+
value = bf.field.clean(bf.data) or ''
22+
if isinstance(value, basestring):
23+
value = value.strip()
24+
data.append((bf.name, value))
2025
data.extend(args)
2126
data.append(settings.SECRET_KEY)
2227

0 commit comments

Comments
 (0)