Ticket #2249: svn.diff
File svn.diff, 4.1 KB (added by , 18 years ago) |
---|
-
conf/global_settings.py
221 221 # Hint: you really don't! 222 222 TRANSACTIONS_MANAGED = False 223 223 224 # Which hashing algorithm do you prefer? (see django.util.hashes for available algorithms) 225 FAVORITE_HASH_ALGO = "md5" 226 224 227 ############## 225 228 # MIDDLEWARE # 226 229 ############## -
contrib/sessions/models.py
1 import base64, md5,random, sys1 import base64, random, sys 2 2 import cPickle as pickle 3 3 from django.db import models 4 from django.utils.hashes import hash 4 5 from django.utils.translation import gettext_lazy as _ 5 6 from django.conf import settings 6 7 … … 8 9 def encode(self, session_dict): 9 10 "Returns the given session dictionary pickled and encoded as a string." 10 11 pickled = pickle.dumps(session_dict) 11 pickled_ md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()12 return base64.encodestring(pickled + pickled_ md5)12 pickled_hash = hash.new(pickled + settings.SECRET_KEY).hexdigest() 13 return base64.encodestring(pickled + pickled_hash) 13 14 14 15 def get_new_session_key(self): 15 16 "Returns session key that isn't being used." 16 17 # The random module is seeded when this Apache child is created. 17 18 # Use person_id and SECRET_KEY as added salt. 18 19 while 1: 19 session_key = md5.new(str(random.randint(0, sys.maxint - 1)) + str(random.randint(0, sys.maxint - 1)) + settings.SECRET_KEY).hexdigest()20 session_key = hash.new(str(random.randint(0, sys.maxint - 1)) + str(random.randint(0, sys.maxint - 1)) + settings.SECRET_KEY).hexdigest() 20 21 try: 21 22 self.get(session_key=session_key) 22 23 except self.model.DoesNotExist: … … 49 50 50 51 def get_decoded(self): 51 52 encoded_data = base64.decodestring(self.session_data) 52 pickled, tamper_check = encoded_data[: -32], encoded_data[-32:]53 if md5.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check:53 pickled, tamper_check = encoded_data[:hash.digest_size*-2], encoded_data[hash.digest_size*-2:] 54 if hash.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check: 54 55 from django.core.exceptions import SuspiciousOperation 55 56 raise SuspiciousOperation, "User tampered with session cookie." 56 57 try: -
contrib/admin/views/decorators.py
2 2 from django.conf import settings 3 3 from django.contrib.auth.models import User, SESSION_KEY 4 4 from django.shortcuts import render_to_response 5 from django.utils.hashes import hash 5 6 from django.utils.translation import gettext_lazy 6 import base64, datetime , md57 import base64, datetime 7 8 import cPickle as pickle 8 9 9 10 ERROR_MESSAGE = gettext_lazy("Please enter a correct username and password. Note that both fields are case-sensitive.") … … 28 29 29 30 def _encode_post_data(post_data): 30 31 pickled = pickle.dumps(post_data) 31 pickled_ md5 = md5.new(pickled + settings.SECRET_KEY).hexdigest()32 return base64.encodestring(pickled + pickled_ md5)32 pickled_hash = hash.new(pickled + settings.SECRET_KEY).hexdigest() 33 return base64.encodestring(pickled + pickled_hash) 33 34 34 35 def _decode_post_data(encoded_data): 35 36 encoded_data = base64.decodestring(encoded_data) 36 pickled, tamper_check = encoded_data[: -32], encoded_data[-32:]37 if md5.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check:37 pickled, tamper_check = encoded_data[:hash.digest_size*-2], encoded_data[hash.digest_size*-2:] 38 if hash.new(pickled + settings.SECRET_KEY).hexdigest() != tamper_check: 38 39 from django.core.exceptions import SuspiciousOperation 39 40 raise SuspiciousOperation, "User may have tampered with session cookie." 40 41 return pickle.loads(pickled)