Skip to content

Commit cc96ed9

Browse files
committed
[1.0.X] Fixed #10643: fixed the formtools security hash to handle allowed empty forms or forms without changed data. Backport of [10753] from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10755 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 08577ab commit cc96ed9

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

django/contrib/formtools/tests.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,31 @@ def test_textfield_hash(self):
110110
leading/trailing whitespace so as to be friendly to broken browsers that
111111
submit it (usually in textareas).
112112
"""
113-
class TestForm(forms.Form):
114-
name = forms.CharField()
115-
bio = forms.CharField()
113+
f1 = HashTestForm({'name': 'joe', 'bio': 'Nothing notable.'})
114+
f2 = HashTestForm({'name': ' joe', 'bio': 'Nothing notable. '})
115+
hash1 = utils.security_hash(None, f1)
116+
hash2 = utils.security_hash(None, f2)
117+
self.assertEqual(hash1, hash2)
116118

117-
f1 = TestForm({'name': 'joe', 'bio': 'Nothing notable.'})
118-
f2 = TestForm({'name': ' joe', 'bio': 'Nothing notable. '})
119+
def test_empty_permitted(self):
120+
"""
121+
Regression test for #10643: the security hash should allow forms with
122+
empty_permitted = True, or forms where data has not changed.
123+
"""
124+
f1 = HashTestBlankForm({})
125+
f2 = HashTestForm({}, empty_permitted=True)
119126
hash1 = utils.security_hash(None, f1)
120127
hash2 = utils.security_hash(None, f2)
121128
self.assertEqual(hash1, hash2)
122129

130+
class HashTestForm(forms.Form):
131+
name = forms.CharField()
132+
bio = forms.CharField()
133+
134+
class HashTestBlankForm(forms.Form):
135+
name = forms.CharField(required=False)
136+
bio = forms.CharField(required=False)
137+
123138
#
124139
# FormWizard tests
125140
#

django/contrib/formtools/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ def security_hash(request, form, *args):
1818

1919
data = []
2020
for bf in form:
21-
value = bf.field.clean(bf.data) or ''
21+
# Get the value from the form data. If the form allows empty or hasn't
22+
# changed then don't call clean() to avoid trigger validation errors.
23+
if form.empty_permitted and not form.has_changed():
24+
value = bf.data or ''
25+
else:
26+
value = bf.field.clean(bf.data) or ''
2227
if isinstance(value, basestring):
2328
value = value.strip()
2429
data.append((bf.name, value))
30+
2531
data.extend(args)
2632
data.append(settings.SECRET_KEY)
2733

0 commit comments

Comments
 (0)