Ticket #17613: cache_key_validation.patch

File cache_key_validation.patch, 2.6 KB (added by Vlastimil Zíma, 13 years ago)
  • django/core/cache/backends/base.py

    diff --git a/django/core/cache/backends/base.py b/django/core/cache/backends/base.py
    index f7573b2..1d767cc 100644
    a b class InvalidCacheBackendError(ImproperlyConfigured):  
    1212class CacheKeyWarning(DjangoRuntimeWarning):
    1313    pass
    1414
     15class CacheKeyError(ValueError):
     16    pass
     17
     18
    1519# Memcached does not accept keys longer than this.
    1620MEMCACHE_MAX_KEY_LENGTH = 250
    1721
    class BaseCache(object):  
    8084        new_key = self.key_func(key, self.key_prefix, version)
    8185        return new_key
    8286
     87    def _check_key(self, key):
     88        """
     89        Checks if key can be used by any cache backend. Raises CacheKeyError
     90        if not.
     91
     92        This is inner check method, it checks given key as is.
     93        """
     94        if len(key) > MEMCACHE_MAX_KEY_LENGTH:
     95            raise CacheKeyError('Cache key will cause errors if used with '
     96                                'memcached: %s (longer than %s)' % (key,
     97                                MEMCACHE_MAX_KEY_LENGTH))
     98        for char in key:
     99            if ord(char) < 33 or ord(char) == 127:
     100                raise CacheKeyError('Cache key contains characters that will '
     101                                    'cause errors if used with memcached: %r' %
     102                                    key)
     103
     104    def check_key(self, key):
     105        """
     106        Returns whether key can be used by any cache backend.
     107        """
     108        try:
     109            self._check_key(self.make_key(key))
     110        except CacheKeyError:
     111            return False
     112        return True
     113
    83114    def add(self, key, value, timeout=None, version=None):
    84115        """
    85116        Set a value in the cache if the key does not already exist. If
    class BaseCache(object):  
    191222        cache code.
    192223
    193224        """
    194         if len(key) > MEMCACHE_MAX_KEY_LENGTH:
    195             warnings.warn('Cache key will cause errors if used with memcached: '
    196                     '%s (longer than %s)' % (key, MEMCACHE_MAX_KEY_LENGTH),
    197                     CacheKeyWarning)
    198         for char in key:
    199             if ord(char) < 33 or ord(char) == 127:
    200                 warnings.warn('Cache key contains characters that will cause '
    201                         'errors if used with memcached: %r' % key,
    202                               CacheKeyWarning)
     225        try:
     226            self._check_key(key)
     227        except CacheKeyError, error:
     228            warnings.warn(str(error), CacheKeyWarning)
    203229
    204230    def incr_version(self, key, delta=1, version=None):
    205231        """Adds delta to the cache version for the supplied key. Returns the
Back to Top