Improve normalization and sanitization of memcached keys
[lhc/web/wiklou.git] / includes / objectcache / MemcachedBagOStuff.php
index 95f5c8d..12ba83e 100644 (file)
@@ -104,6 +104,46 @@ class MemcachedBagOStuff extends BagOStuff {
                return $this->client;
        }
 
+       /**
+        * Construct a cache key.
+        *
+        * @since 1.27
+        * @param string $keyspace
+        * @param array $args
+        * @return string
+        */
+       public function makeKeyInternal( $keyspace, $args ) {
+               // Memcached keys have a maximum length of 255 characters. From that,
+               // subtract the number of characters we need for the keyspace and for
+               // the separator character needed for each argument.
+               $charsLeft = 255 - strlen( $keyspace ) - count( $args );
+
+               $that = $this;
+               $args = array_map(
+                       function ( $arg ) use ( $that, &$charsLeft ) {
+                               // Because MemcachedBagOStuff::encodeKey() will be called again
+                               // with this input once the key is actually used, we have to
+                               // encode pound signs here rather than in encodeKey().
+                               $arg = $that->encodeKey( str_replace( '#', '%23', $arg ) );
+
+                               // 33 = 32 characters for the MD5 + 1 for the '#' prefix.
+                               if ( $charsLeft > 33 && strlen( $arg ) > $charsLeft ) {
+                                       $arg = '#' . md5( $arg );
+                               }
+
+                               $charsLeft -= strlen( $arg );
+                               return $arg;
+                       },
+                       $args
+               );
+
+               if ( $charsLeft < 0 ) {
+                       $args = array( '##' . md5( implode( ':', $args ) ) );
+               }
+
+               return parent::makeKeyInternal( $keyspace, $args );
+       }
+
        /**
         * Encode a key for use on the wire inside the memcached protocol.
         *
@@ -115,7 +155,7 @@ class MemcachedBagOStuff extends BagOStuff {
         * @return string
         */
        public function encodeKey( $key ) {
-               return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/',
+               return preg_replace_callback( '/[^\x21-\x7e]+/',
                        array( $this, 'encodeKeyCallback' ), $key );
        }