Always give hashlib.sha1 and friends bytes.

This fixes an experienced bug where you couldn't subscribe to a stream
with non-ASCII characters (failing with a UnicodeEncodeError), as well
as many other potential bugs.

(imported from commit f084a4b4b597b85935655097a7b5a163811c4d71)
This commit is contained in:
Jessica McKellar
2013-03-20 10:31:27 -04:00
parent 55240e3de2
commit 0c3382fabb
4 changed files with 27 additions and 7 deletions

View File

@@ -1,9 +1,10 @@
from functools import wraps
import hashlib
from django.core.cache import cache as djcache
from django.core.cache import get_cache
from utils import make_safe_digest
def cache_with_key(keyfunc, cache_name=None, timeout=None):
"""Decorator which applies Django caching to a function.
@@ -56,7 +57,10 @@ def message_cache_key(message_id):
return "message:%d" % (message_id,)
def user_profile_by_email_cache_key(email):
return 'user_profile_by_email:%s' % (hashlib.sha1(email).hexdigest(),)
# See the comment in zephyr/lib/avatar.py:gravatar_hash for why we
# are proactively encoding email addresses even though they will
# with high likelihood be ASCII-only for the foreseeable future.
return 'user_profile_by_email:%s' % (make_safe_digest(email),)
def user_profile_by_user_cache_key(user_id):
return 'user_profile_by_user_id:%d' % (user_id,)