2008-08-26 06:14:22 +08:00
|
|
|
import time
|
2010-12-04 15:28:12 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.comments.forms import CommentForm
|
2010-12-04 15:28:12 +08:00
|
|
|
from django.contrib.comments.models import Comment
|
Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-15 04:54:30 +08:00
|
|
|
from django.utils.hashcompat import sha_constructor
|
2010-12-04 15:28:12 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
from regressiontests.comment_tests.models import Article
|
|
|
|
from regressiontests.comment_tests.tests import CommentTestCase
|
|
|
|
|
|
|
|
|
2010-12-04 15:28:12 +08:00
|
|
|
class CommentFormTests(CommentTestCase):
|
2008-08-26 06:14:22 +08:00
|
|
|
def testInit(self):
|
|
|
|
f = CommentForm(Article.objects.get(pk=1))
|
|
|
|
self.assertEqual(f.initial['content_type'], str(Article._meta))
|
|
|
|
self.assertEqual(f.initial['object_pk'], "1")
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertNotEqual(f.initial['security_hash'], None)
|
|
|
|
self.assertNotEqual(f.initial['timestamp'], None)
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
def testValidPost(self):
|
|
|
|
a = Article.objects.get(pk=1)
|
|
|
|
f = CommentForm(a, data=self.getValidData(a))
|
|
|
|
self.assert_(f.is_valid(), f.errors)
|
|
|
|
return f
|
|
|
|
|
|
|
|
def tamperWithForm(self, **kwargs):
|
|
|
|
a = Article.objects.get(pk=1)
|
|
|
|
d = self.getValidData(a)
|
|
|
|
d.update(kwargs)
|
|
|
|
f = CommentForm(Article.objects.get(pk=1), data=d)
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(f.is_valid())
|
2008-08-26 06:14:22 +08:00
|
|
|
return f
|
|
|
|
|
|
|
|
def testHoneypotTampering(self):
|
|
|
|
self.tamperWithForm(honeypot="I am a robot")
|
|
|
|
|
|
|
|
def testTimestampTampering(self):
|
|
|
|
self.tamperWithForm(timestamp=str(time.time() - 28800))
|
|
|
|
|
|
|
|
def testSecurityHashTampering(self):
|
|
|
|
self.tamperWithForm(security_hash="Nobody expects the Spanish Inquisition!")
|
|
|
|
|
|
|
|
def testContentTypeTampering(self):
|
|
|
|
self.tamperWithForm(content_type="auth.user")
|
|
|
|
|
|
|
|
def testObjectPKTampering(self):
|
|
|
|
self.tamperWithForm(object_pk="3")
|
|
|
|
|
Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-15 04:54:30 +08:00
|
|
|
def testDjango12Hash(self):
|
|
|
|
# Ensure we can use the hashes generated by Django 1.2
|
|
|
|
a = Article.objects.get(pk=1)
|
|
|
|
d = self.getValidData(a)
|
|
|
|
|
|
|
|
content_type = d['content_type']
|
|
|
|
object_pk = d['object_pk']
|
|
|
|
timestamp = d['timestamp']
|
|
|
|
|
|
|
|
# The Django 1.2 method hard-coded here:
|
|
|
|
info = (content_type, object_pk, timestamp, settings.SECRET_KEY)
|
|
|
|
security_hash = sha_constructor("".join(info)).hexdigest()
|
|
|
|
|
|
|
|
d['security_hash'] = security_hash
|
|
|
|
f = CommentForm(a, data=d)
|
|
|
|
self.assertTrue(f.is_valid(), f.errors)
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
def testSecurityErrors(self):
|
|
|
|
f = self.tamperWithForm(honeypot="I am a robot")
|
|
|
|
self.assert_("honeypot" in f.security_errors())
|
|
|
|
|
|
|
|
def testGetCommentObject(self):
|
|
|
|
f = self.testValidPost()
|
|
|
|
c = f.get_comment_object()
|
|
|
|
self.assert_(isinstance(c, Comment))
|
|
|
|
self.assertEqual(c.content_object, Article.objects.get(pk=1))
|
|
|
|
self.assertEqual(c.comment, "This is my comment")
|
|
|
|
c.save()
|
|
|
|
self.assertEqual(Comment.objects.count(), 1)
|
|
|
|
|
|
|
|
def testProfanities(self):
|
|
|
|
"""Test COMMENTS_ALLOW_PROFANITIES and PROFANITIES_LIST settings"""
|
|
|
|
a = Article.objects.get(pk=1)
|
|
|
|
d = self.getValidData(a)
|
|
|
|
|
|
|
|
# Save settings in case other tests need 'em
|
|
|
|
saved = settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES
|
|
|
|
|
|
|
|
# Don't wanna swear in the unit tests if we don't have to...
|
|
|
|
settings.PROFANITIES_LIST = ["rooster"]
|
|
|
|
|
|
|
|
# Try with COMMENTS_ALLOW_PROFANITIES off
|
|
|
|
settings.COMMENTS_ALLOW_PROFANITIES = False
|
|
|
|
f = CommentForm(a, data=dict(d, comment="What a rooster!"))
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertFalse(f.is_valid())
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
# Now with COMMENTS_ALLOW_PROFANITIES on
|
|
|
|
settings.COMMENTS_ALLOW_PROFANITIES = True
|
|
|
|
f = CommentForm(a, data=dict(d, comment="What a rooster!"))
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertTrue(f.is_valid())
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
# Restore settings
|
|
|
|
settings.PROFANITIES_LIST, settings.COMMENTS_ALLOW_PROFANITIES = saved
|