fixed security check in filesystem loader
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 14 Dec 2010 13:36:45 +0000 (14:36 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 14 Dec 2010 13:36:45 +0000 (14:36 +0100)
lib/Twig/Loader/Filesystem.php
test/Twig/Tests/Loader/FilesystemTest.php [moved from test/Twig/Tests/Loader/Filesystem.php with 95% similarity]

index e733da4..7fb2b7d 100644 (file)
@@ -104,12 +104,18 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
         // normalize name
         $name = str_replace('\\', '/', $name);
 
-        // remove ./
-        $name = preg_replace('#(^|/)\./(\./)*#', '$1', $name);
+        $parts = explode('/', $name);
+        $level = 0;
+        foreach ($parts as $part) {
+            if ('..' === $part) {
+                --$level;
+            } elseif ('.' !== $part) {
+                ++$level;
+            }
 
-        // security check (a name cannot start with ../)
-        if ('..' === substr($name, 0, 2)) {
-            throw new Twig_Error_Loader('Looks like you try to load a template outside configured directories.');
+            if ($level < 0) {
+                throw new Twig_Error_Loader('Looks like you try to load a template outside configured directories.');
+            }
         }
 
         if (isset($this->cache[$name])) {
similarity index 95%
rename from test/Twig/Tests/Loader/Filesystem.php
rename to test/Twig/Tests/Loader/FilesystemTest.php
index caf0d68..a539686 100644 (file)
@@ -34,6 +34,7 @@ class Twig_Tests_Loader_FilesystemTest extends PHPUnit_Framework_TestCase
             array('foo\\..\\..\\AutoloaderTest.php'),
             array('foo/../bar/../../AutoloaderTest.php'),
             array('foo/bar/../../../AutoloaderTest.php'),
+            array('filters/../../AutoloaderTest.php'),
         );
     }
 }