From e96bb10af4df8c44819c9edfa296d913aa2983d7 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 10 Mar 2016 09:59:31 +0100 Subject: [PATCH] [Cache] Hash using B64+MD5 in FilesystemAdapter --- .../Component/Cache/Adapter/FilesystemAdapter.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php b/src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php index db6d9ce4cf..136e811754 100644 --- a/src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php @@ -34,7 +34,7 @@ class FilesystemAdapter extends AbstractAdapter throw new InvalidArgumentException(sprintf('Cache directory is not writable (%s)', $directory)); } // On Windows the whole path is limited to 258 chars - if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 190) { + if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 234) { throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory)); } @@ -62,10 +62,13 @@ class FilesystemAdapter extends AbstractAdapter @unlink($file); } } else { + $i = rawurldecode(rtrim(fgets($h))); $value = stream_get_contents($h); flock($h, LOCK_UN); fclose($h); - $values[$id] = unserialize($value); + if ($i === $id) { + $values[$id] = unserialize($value); + } } } @@ -125,7 +128,7 @@ class FilesystemAdapter extends AbstractAdapter if (!file_exists($dir)) { @mkdir($dir, 0777, true); } - $value = $expiresAt."\n".serialize($value); + $value = $expiresAt."\n".rawurlencode($id)."\n".serialize($value); if (false !== @file_put_contents($file, $value, LOCK_EX)) { @touch($file, $expiresAt); } else { @@ -138,8 +141,8 @@ class FilesystemAdapter extends AbstractAdapter private function getFile($id) { - $hash = hash('sha256', $id); + $hash = str_replace('/', '-', base64_encode(md5($id, true))); - return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.$hash; + return $this->directory.$hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR.substr($hash, 2, -2); } }