Skip to content

Commit

Permalink
Merge pull request #1 from dennisinteractive/27724_lock_sanitisation
Browse files Browse the repository at this point in the history
Case 27724; Updated MemcacheBacked lock sanitisation.
  • Loading branch information
cristinallamas authored Apr 10, 2017
2 parents 86475db + a8e8824 commit 4908f1c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
22 changes: 3 additions & 19 deletions src/DrupalMemcacheBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
abstract class DrupalMemcacheBase implements DrupalMemcacheInterface {

use MemcacheCacheNormalizerTrait;

/**
* The memcache config object.
*
Expand All @@ -29,13 +31,6 @@ abstract class DrupalMemcacheBase implements DrupalMemcacheInterface {
*/
protected $memcache;

/**
* The hash algorithm to pass to hash(). Defaults to 'sha1'
*
* @var string
*/
protected $hashAlgorithm;

/**
* The prefix memcache key for all keys.
*
Expand All @@ -52,7 +47,6 @@ abstract class DrupalMemcacheBase implements DrupalMemcacheInterface {
public function __construct(DrupalMemcacheConfig $settings) {
$this->settings = $settings;

$this->hashAlgorithm = $this->settings->get('key_hash_algorithm', 'sha1');
$this->prefix = $this->settings->get('key_prefix', '');
}

Expand All @@ -79,17 +73,7 @@ public function get($key) {
* {@inheritdoc}
*/
public function key($key) {
$full_key = urlencode($this->prefix . '-' . $key);

// Memcache only supports key lengths up to 250 bytes. If we have generated
// a longer key, we shrink it to an acceptable length with a configurable
// hashing algorithm. Sha1 was selected as the default as it performs
// quickly with minimal collisions.
if (strlen($full_key) > 250) {
$full_key = urlencode(hash($this->hashAlgorithm, $this->prefix . '-' . $key));
}

return $full_key;
return $this->normalizeKey($this->prefix . '-' . $key);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/MemcacheBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
class MemcacheBackend implements CacheBackendInterface {

use MemcacheCacheNormalizerTrait;

/**
* The cache bin to use.
*
Expand Down Expand Up @@ -128,7 +130,7 @@ public function getMultiple(&$cids, $allow_invalid = FALSE) {
* @return bool
*/
protected function valid($cid, \stdClass $cache) {
$lock_key = "memcache_$cid:$this->bin";
$lock_key = $this->normalizeKey("memcache_$cid:$this->bin");
$cache->valid = FALSE;

if ($cache) {
Expand Down Expand Up @@ -316,7 +318,7 @@ public function isEmpty() {
* @return string
*/
protected function key($cid) {
return $this->bin . '-' . $cid;
return $this->normalizeKey($this->bin . '-' . $cid);
}

}
41 changes: 41 additions & 0 deletions src/MemcacheCacheNormalizerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\memcache;

/**
* Class MemcacheCacheNormalizer.
*/
trait MemcacheCacheNormalizerTrait {

/**
* Normalizes a cache ID in order to comply with key length limitations.
*
* @param string $key
* The passed in cache ID.
*
* @return string
* An ASCII-encoded cache ID that is at most 250 characters long.
*/
protected function normalizeKey($key) {
$key = urlencode($key);
// Nothing to do if the ID is a US ASCII string of 250 characters or less.
$key_is_ascii = mb_check_encoding($key, 'ASCII');
if (strlen($key) <= 250 && $key_is_ascii) {
return $key;
}
// Memcache only supports key lengths up to 250 bytes. If we have generated
// a longer key, we shrink it to an acceptable length with a configurable
// hashing algorithm. Sha1 was selected as the default as it performs
// quickly with minimal collisions.
// Return a string that uses as much as possible of the original cache ID
// with the hash appended.
/** @var DrupalMemcacheConfig $this->settings */
$hash_algorithm = $this->settings->get('key_hash_algorithm', 'sha1');
$hash = hash($hash_algorithm, $key);
if (!$key_is_ascii) {
return $hash;
}
return substr($key, 0, 250 - strlen($hash)) . $hash;
}

}

0 comments on commit 4908f1c

Please sign in to comment.